Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 1 addition & 169 deletions python/paddle/fluid/layers/control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@
'Switch',
'increment',
'array_write',
'create_array',
'less_than',
'array_read',
'array_length',
'cond',
'IfElse',
'StaticRNN',
Expand Down Expand Up @@ -1712,7 +1710,7 @@ def array_write(x, i, array=None):
], "The shape of index 'i' should be [1] in dygraph mode"
i = i.numpy().item(0)
if array is None:
array = create_array(x.dtype)
array = paddle.tensor.create_array(x.dtype)
assert isinstance(
array, list
), "The 'array' in array_write must be a list in dygraph mode"
Expand Down Expand Up @@ -1750,64 +1748,6 @@ def array_write(x, i, array=None):
return array


def create_array(dtype, initialized_list=None):
"""
This OP creates an LOD_TENSOR_ARRAY. It is used as
the input of :ref:`api_fluid_layers_array_read` and
:ref:`api_fluid_layers_array_write`. Also it can be used
with :ref:`api_fluid_layers_While` to create RNN network.

Args:
dtype (str): The data type of the elements in the lod_tensor_array.
Support data type: float32, float64, int32, int64.
initialized_list(list): Used to initialize as default value for created array.
All values in initialized list should be a Tensor.

Returns:
Variable: The empty lod_tensor_array. The data type of elements in Tensor is ``dtype``.

Examples:
.. code-block:: python

import paddle.fluid as fluid
data = fluid.layers.create_array(dtype='float32') # Create a float32 LoDTensorArray.

"""
array = []
if initialized_list is not None:
if not isinstance(initialized_list, (list, tuple)):
raise TypeError(
"Require type(initialized_list) should be list/tuple, but received {}".format(
type(initialized_list)
)
)
array = list(initialized_list)

# NOTE: Only support plain list like [x, y,...], not support nested list in static mode.
for val in array:
if not isinstance(val, Variable):
raise TypeError(
"All values in `initialized_list` should be Variable, but recevied {}.".format(
type(val)
)
)

if _non_static_mode():
return array

helper = LayerHelper("array", **locals())
tensor_array = helper.create_variable(
name="{0}.out".format(helper.name),
type=core.VarDesc.VarType.LOD_TENSOR_ARRAY,
dtype=dtype,
)

for val in array:
array_write(x=val, i=array_length(tensor_array), array=tensor_array)

return tensor_array


@templatedoc()
def less_than(x, y, force_cpu=None, cond=None, name=None):
"""
Expand Down Expand Up @@ -1956,114 +1896,6 @@ def array_read(array, i):
return out


def shrink_memory(x, i, table):
"""
This function creates an operator to shrink rnn memory using the RankTable
as mentioned in the input parameter.

NOTE: This API is very low-level API. It is used by DynamicRNN only.

Since the Dynamic RNN uses no-padding way to implement RNN. The sequence
will be sorted by order, and the length of valid memory will be shrink after
each time step.

Args:
x(Variable): The memory object in the previous time step.
i(Variable): The step count variable. A int scalar as LoDTensor.
table(Variable): The RNNRankTable object.

Returns:
the memory variable after shrink.

Examples:

Since this API is very low level API. The example is not provided.
Please reference the implementation of class DynamicRNN for detail
usage.
"""
helper = LayerHelper('shrink_memory', **locals())
check_type(x, 'x', Variable, 'shrink_memory')
check_type(i, 'i', Variable, 'shrink_memory')
check_type(table, 'table', Variable, 'shrink_memory')
out = helper.create_variable_for_type_inference(dtype=x.dtype)
helper.append_op(
type='shrink_rnn_memory',
inputs={'X': [x], 'I': [i], 'RankTable': [table]},
outputs={'Out': [out]},
attrs={},
)
return out


def array_length(array):
"""
This OP is used to get the length of the input array :ref:`api_fluid_LoDTensorArray` .
It can be used together with :ref:`api_fluid_layers_array_read` , :ref:`api_fluid_layers_array_write` ,
:ref:`api_fluid_layers_While` OP to traverse, read and write LoDTensorArray.

Args:
array (LoDTensorArray): The input array that will be used to compute the length.

Returns:
Variable: 1-D Tensor with shape [1], which is the length of array. Datatype: int64.

Examples:
.. code-block:: python

import paddle.fluid as fluid
tmp = fluid.layers.zeros(shape=[10], dtype='int32')
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
# tmp is 1-D Tensor with shape [10]. We write tmp into arr on subscript 10,
# then the length of arr becomes 11.
arr = fluid.layers.array_write(tmp, i=i)
# return the length of arr
arr_len = fluid.layers.array_length(arr)

# You can use executor to print out the length of LoDTensorArray.
input = fluid.layers.Print(arr_len, message="The length of LoDTensorArray:")
main_program = fluid.default_main_program()
exe = fluid.Executor(fluid.CPUPlace())
exe.run(main_program)

# The printed result is:

# 1569576542 The length of LoDTensorArray: The place is:CPUPlace
# Tensor[array_length_0.tmp_0]
# shape: [1,]
# dtype: l
# data: 11,

# 1-D Tensor with shape [1], whose value is 11. It means that the length of LoDTensorArray
# is 11.
# dtype is the corresponding C++ data type, which may vary in different environments.
# Eg: if the data type of tensor is int64, then the corresponding C++ data type is int64_t,
# so the dtype value is typeid(int64_t).Name(), which is 'x' on MacOS, 'l' on Linux,
# and '__int64' on Windows. They both represent 64-bit integer variables.
"""

if _non_static_mode():
assert isinstance(
array, list
), "The 'array' in array_write must be a list in dygraph mode"
return len(array)

if (
not isinstance(array, Variable)
or array.type != core.VarDesc.VarType.LOD_TENSOR_ARRAY
):
raise TypeError(
"array should be tensor array vairable in array_length Op"
)

helper = LayerHelper('array_length', **locals())
tmp = helper.create_variable_for_type_inference(dtype='int64')
tmp.stop_gradient = True
helper.append_op(
type='lod_array_length', inputs={'X': [array]}, outputs={'Out': [tmp]}
)
return tmp


class ConditionalBlockGuard(BlockGuard):
"""
ConditionalBlockGuard is derived from BlockGuard. It is dedicated for
Expand Down
4 changes: 3 additions & 1 deletion python/paddle/fluid/layers/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .. import core
from ..framework import Variable, unique_name, static_only
from .layer_function_generator import OpProtoHolder
from .control_flow import array_write, array_length
from .control_flow import array_write
from paddle.fluid.dygraph.base import in_declarative_mode

_supported_int_dtype_ = [
Expand Down Expand Up @@ -246,6 +246,8 @@ def append(self, var):
self.type
)
)
from paddle.tensor.array import array_length

array_write(x=var, i=array_length(self), array=self)

@static_only
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/layers/rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ def _create_array_out_of_while(dtype):
default_main_program().current_block_idx = (
default_main_program().current_block().parent_idx
)
tensor_array = control_flow.create_array(dtype)
tensor_array = paddle.tensor.create_array(dtype)
default_main_program().current_block_idx = current_block_idx
return tensor_array

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_model(self, main_prog, startup_program):
data2 = fluid.layers.assign(
np.array([[0, 1, 2]], dtype='float32')
)
tensor_array = fluid.layers.create_array(dtype='float32')
tensor_array = paddle.tensor.create_array(dtype='float32')
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
fluid.layers.array_write(data1, i, tensor_array)
fluid.layers.array_write(data2, i + 1, tensor_array)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ def test_hybrid_parallel_inference_helper_mp1pp2(self):
layers.assign(layers.cast(cond_int, dtype='bool'), cond)

with paddle.fluid.device_guard(f'{device}:all'):
out = layers.create_array(data.dtype)
out = paddle.tensor.create_array(data.dtype)
layers.assign(data, out)

with paddle.fluid.device_guard(f'{device}:all'):
# use a empty lod_tensor_array to clear lod_tensor_array
layers.assign(layers.create_array(data.dtype), data)
layers.assign(paddle.tensor.create_array(data.dtype), data)

helper = HybridParallelInferenceHelper(
startup_program,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ def forward(self, inputs):
np.zeros((self.batch_size, self.hidden_size), dtype='float32')
)
zero = fluid.layers.zeros(shape=[1], dtype="int64")
enc_hidden = fluid.layers.create_array(dtype="float32")
enc_cell = fluid.layers.create_array(dtype="float32")
enc_hidden = paddle.tensor.create_array(dtype="float32")
enc_cell = paddle.tensor.create_array(dtype="float32")
for i in range(self.num_layers):
index = zero + i
enc_hidden = fluid.layers.array_write(
Expand Down Expand Up @@ -322,8 +322,8 @@ def beam_search(self, inputs):
np.zeros((self.batch_size, self.hidden_size), dtype='float32')
)
zero = fluid.layers.zeros(shape=[1], dtype="int64")
enc_hidden = fluid.layers.create_array(dtype="float32")
enc_cell = fluid.layers.create_array(dtype="float32")
enc_hidden = paddle.tensor.create_array(dtype="float32")
enc_cell = paddle.tensor.create_array(dtype="float32")
for j in range(self.num_layers):
index = zero + j
enc_hidden = fluid.layers.array_write(
Expand Down Expand Up @@ -735,8 +735,8 @@ def forward(self, inputs):
)
enc_hidden_0.stop_gradient = True
zero = fluid.layers.zeros(shape=[1], dtype="int64")
enc_hidden = fluid.layers.create_array(dtype="float32")
enc_cell = fluid.layers.create_array(dtype="float32")
enc_hidden = paddle.tensor.create_array(dtype="float32")
enc_cell = paddle.tensor.create_array(dtype="float32")
for i in range(self.num_layers):
index = zero + i
enc_hidden = fluid.layers.array_write(
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/npu/test_concat_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def set_program(self, use_fluid_api):
self.program = fluid.Program()
with fluid.program_guard(self.program):
input = fluid.layers.assign(self.x)
tensor_array = fluid.layers.create_array(dtype='float32')
tensor_array = paddle.tensor.create_array(dtype='float32')
zero = fluid.layers.fill_constant(
shape=[1], value=0, dtype="int64"
)
Expand All @@ -183,7 +183,7 @@ def set_program(self, use_fluid_api):
self.program = paddle.static.Program()
with paddle.static.program_guard(self.program):
input = paddle.assign(self.x)
tensor_array = fluid.layers.create_array(
tensor_array = paddle.tensor.create_array(
dtype='float32'
) # Api create_array is not supported in paddle 2.0 yet.
zero = paddle.zeros(shape=[1], dtype="int64")
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/npu/test_stack_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def set_program(self):
self.program = fluid.Program()
with fluid.program_guard(self.program):
input = fluid.layers.assign(self.x)
tensor_array = fluid.layers.create_array(dtype='float32')
tensor_array = paddle.tensor.create_array(dtype='float32')
zero = fluid.layers.fill_constant(shape=[1], value=0, dtype="int64")

for i in range(self.iter_num):
Expand Down Expand Up @@ -176,7 +176,7 @@ def set_program(self):
self.program = fluid.Program()
with fluid.program_guard(self.program):
input = fluid.layers.assign(self.x)
tensor_array = fluid.layers.create_array(dtype='float32')
tensor_array = paddle.tensor.create_array(dtype='float32')
zero = fluid.layers.fill_constant(shape=[1], value=0, dtype="int64")

for i in range(self.iter_num):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import numpy as np

import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.framework import Program, program_guard
Expand Down Expand Up @@ -124,7 +125,7 @@ def test_errors(self):
def test_id_Variable():
# the input pre_ids must be Variable
test_ids = np.random.randint(1, 5, [5, 1]).astype("int64")
scores = fluid.layers.create_array(dtype='float32')
scores = paddle.tensor.create_array(dtype='float32')
fluid.layers.beam_search_decode(
test_ids, scores, beam_size=5, end_id=0
)
Expand All @@ -133,7 +134,7 @@ def test_id_Variable():

def test_score_Variable():
# the input pre_scores must be Variable
ids = fluid.layers.create_array(dtype='int64')
ids = paddle.tensor.create_array(dtype='int64')
test_scores = np.random.uniform(1, 5, [5, 1]).astype("float32")
fluid.layers.beam_search_decode(
ids, test_scores, beam_size=5, end_id=0
Expand All @@ -143,8 +144,8 @@ def test_score_Variable():

def test_id_dtype():
# the dtype of input pre_ids must be int64
type_ids = fluid.layers.create_array(dtype='float32')
scores = fluid.layers.create_array(dtype='float32')
type_ids = paddle.tensor.create_array(dtype='float32')
scores = paddle.tensor.create_array(dtype='float32')
fluid.layers.beam_search_decode(
type_ids, scores, beam_size=5, end_id=0
)
Expand All @@ -153,8 +154,8 @@ def test_id_dtype():

def test_score_dtype():
# the dtype of input pre_scores must be float32
ids = fluid.layers.create_array(dtype='int64')
type_scores = fluid.layers.create_array(dtype='int64')
ids = paddle.tensor.create_array(dtype='int64')
type_scores = paddle.tensor.create_array(dtype='int64')
fluid.layers.beam_search_decode(
ids, type_scores, beam_size=5, end_id=0
)
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_concat_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def set_program(self, use_fluid_api):
self.program = fluid.Program()
with fluid.program_guard(self.program):
input = fluid.layers.assign(self.x)
tensor_array = fluid.layers.create_array(dtype='float32')
tensor_array = paddle.tensor.create_array(dtype='float32')
zero = fluid.layers.fill_constant(
shape=[1], value=0, dtype="int64"
)
Expand All @@ -427,7 +427,7 @@ def set_program(self, use_fluid_api):
self.program = paddle.static.Program()
with paddle.static.program_guard(self.program):
input = paddle.assign(self.x)
tensor_array = fluid.layers.create_array(
tensor_array = paddle.tensor.create_array(
dtype='float32'
) # Api create_array is not supported in paddle 2.0 yet.
zero = paddle.zeros(shape=[1], dtype="int64")
Expand Down
Loading