Skip to content

Commit

Permalink
Clean and migrate fluid APIs of paddle.fluid.layers.control_flow (#48233
Browse files Browse the repository at this point in the history
)

* Merge branch 'reduce_sum' of https://github.com/GhostScreaming/Paddle into mine_fluid_clean_common.

* Fix some bugs.

* Clean APIs in python/paddle/fluid/layers/control_flow.py

* Polish code style.

* Change API.

* Fix some bugs.

* Fix some bugs.
  • Loading branch information
GhostScreaming authored Dec 8, 2022
1 parent 3a387df commit 6b0d959
Show file tree
Hide file tree
Showing 37 changed files with 1,008 additions and 2,620 deletions.
753 changes: 8 additions & 745 deletions python/paddle/fluid/layers/control_flow.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion python/paddle/fluid/layers/rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ def _dynamic_decode_declarative(
max_step_num = tensor.fill_constant(
shape=[1], dtype="int64", value=max_step_num
)
while_op = control_flow.While(cond, is_test=is_test)
while_op = paddle.static.nn.control_flow.While(cond, is_test=is_test)

sequence_lengths = tensor.cast(paddle.zeros_like(initial_finished), "int64")
sequence_lengths.stop_gradient = True
Expand Down
255 changes: 0 additions & 255 deletions python/paddle/fluid/tests/test_if_else_op.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def get_program():
cond = paddle.less_than(x=i, y=loop_len)
auto.shard_tensor(cond, _g_process_mesh, [None])

while_op = fluid.layers.While(cond=cond)
while_op = paddle.static.nn.control_flow.While(cond=cond)
with while_op.block():

pre_input = fluid.layers.array_read(array=input_array, i=i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def test_hybrid_parallel_inference_helper_mp1pp2(self):
)
print(cond_int.shape)
cond = paddle.less_than(x=step_idx, y=max_len)
while_op = layers.While(cond, is_test=True)
while_op = paddle.static.nn.control_flow.While(
cond, is_test=True
)

with while_op.block():
with paddle.fluid.device_guard(f'{device}:all'):
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/dist_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ def beam_search():
shape=[1], dtype=start_tokens.dtype, value=0
)
cond = paddle.less_than(x=step_idx, y=max_len)
while_op = layers.While(cond)
while_op = paddle.static.nn.control_flow.While(cond)
# array states will be stored for each step.
ids = layers.array_write(
paddle.reshape(start_tokens, (-1, 1)), step_idx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def body(i, ten, y):

i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
ten = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
i, ten, y = fluid.layers.while_loop(cond, body, [i, ten, y])
i, ten, y = paddle.static.nn.while_loop(cond, body, [i, ten, y])
return y[0]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def _get_answer(self):
# return i, x
#
# i = paddle.zeros(shape=(1, ), dtype='int32')
# i, x = paddle.fluid.layers.while_loop(cond, body, [i, x])
# i, x = paddle.static.nn.while_loop(cond, body, [i, x])
#
# def _get_answer(self):
# self.data[0] = self.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def _get_answer(self):
# return i, x

# i = paddle.zeros(shape=(1, ), dtype='int32')
# i, x = paddle.fluid.layers.while_loop(cond, body, [i, x])
# i, x = paddle.static.nn.while_loop(cond, body, [i, x])

# def _get_answer(self):
# self.data[0] = self.value
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/npu/test_while_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def simple_net(self):
cond2 = paddle.logical_or(x=j, y=array_len2)
cond2 = paddle.ones(shape=[1], dtype='int32')
cond2 = layers.cast(cond2, 'bool')
while_op = layers.While(cond=cond)
while_op2 = layers.While(cond=cond2)
while_op = paddle.static.nn.control_flow.While(cond=cond)
while_op2 = paddle.static.nn.control_flow.While(cond=cond2)
with while_op.block():
d = layers.array_read(array=data_array, i=i)
prev = layers.array_read(array=mem_array, i=i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@

import numpy as np

import paddle

sys.path.append("../")
from op_test import OpTest, skip_check_grad_ci
from test_reorder_lod_tensor import convert_to_offset

paddle.enable_static()


def convert_to_offset(lod):
offset = [[0] for i in lod]
for i, level in enumerate(lod):
for seq_len in level:
offset[i].append(offset[i][-1] + seq_len)
return offset


def compute_seqpool_sum(x, offset, out, pad_value=0.0):
Expand Down
Loading

0 comments on commit 6b0d959

Please sign in to comment.