未验证 提交 a686b3cf 编写于 作者: W Wen Sun 提交者: GitHub

[Fluid API] Remove `shrink_memory`, `create_array` & `array_length` (#48287)

* refactor: rm shrink_memory

* refactor: rm create_array

* refactor: rm array_length
上级 3d0c8b12
......@@ -56,10 +56,8 @@ __all__ = [
'Switch',
'increment',
'array_write',
'create_array',
'less_than',
'array_read',
'array_length',
'cond',
'IfElse',
'StaticRNN',
......@@ -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"
......@@ -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):
"""
......@@ -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
......
......@@ -18,7 +18,7 @@ import inspect
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_ = [
......@@ -246,6 +246,8 @@ def monkey_patch_variable():
self.type
)
)
from paddle.tensor.array import array_length
array_write(x=var, i=array_length(self), array=self)
@static_only
......
......@@ -1638,7 +1638,7 @@ def _dynamic_decode_declarative(
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
......
......@@ -49,7 +49,7 @@ class TestCollectiveSendRecv(TestCollectiveRunnerBase):
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)
......
......@@ -127,12 +127,12 @@ class TestHybridParallelInferenceHelperClass(unittest.TestCase):
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,
......
......@@ -220,8 +220,8 @@ class BaseModel(fluid.dygraph.Layer):
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(
......@@ -322,8 +322,8 @@ class BaseModel(fluid.dygraph.Layer):
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(
......@@ -735,8 +735,8 @@ class AttentionModel(fluid.dygraph.Layer):
)
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(
......
......@@ -170,7 +170,7 @@ class TestConcatAPIWithLoDTensorArray(unittest.TestCase):
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"
)
......@@ -183,7 +183,7 @@ class TestConcatAPIWithLoDTensorArray(unittest.TestCase):
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")
......
......@@ -138,7 +138,7 @@ class TestStackAPIWithLoDTensorArray(unittest.TestCase):
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):
......@@ -176,7 +176,7 @@ class TestTensorStackAPIWithLoDTensorArray(unittest.TestCase):
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):
......
......@@ -16,6 +16,7 @@ import unittest
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
......@@ -124,7 +125,7 @@ class TestBeamSearchDecodeOpError(unittest.TestCase):
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
)
......@@ -133,7 +134,7 @@ class TestBeamSearchDecodeOpError(unittest.TestCase):
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
......@@ -143,8 +144,8 @@ class TestBeamSearchDecodeOpError(unittest.TestCase):
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
)
......@@ -153,8 +154,8 @@ class TestBeamSearchDecodeOpError(unittest.TestCase):
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
)
......
......@@ -414,7 +414,7 @@ class TestConcatAPIWithLoDTensorArray(unittest.TestCase):
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"
)
......@@ -427,7 +427,7 @@ class TestConcatAPIWithLoDTensorArray(unittest.TestCase):
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")
......
......@@ -17,7 +17,6 @@ import unittest
import numpy
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.fluid.layers as layers
from paddle.fluid import Program, program_guard
......@@ -29,7 +28,7 @@ class TestLoDArrayLength(unittest.TestCase):
tmp = layers.zeros(shape=[10], dtype='int32')
i = layers.fill_constant(shape=[1], dtype='int64', value=10)
arr = layers.array_write(tmp, i=i)
arr_len = layers.array_length(arr)
arr_len = paddle.tensor.array_length(arr)
cpu = core.CPUPlace()
exe = Executor(cpu)
result = exe.run(fetch_list=[arr_len])[0]
......@@ -42,7 +41,7 @@ class TestLoDArrayLengthOpError(unittest.TestCase):
# for ci coverage
x1 = numpy.random.randn(2, 4).astype('int32')
self.assertRaises(TypeError, fluid.layers.array_length, array=x1)
self.assertRaises(TypeError, paddle.tensor.array_length, array=x1)
class TestArrayLengthApi(unittest.TestCase):
......
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import numpy as np
import paddle
import paddle.fluid.core as core
import paddle.fluid.layers as layers
from paddle.fluid.backward import append_backward
from paddle.fluid.executor import Executor
from paddle.fluid.framework import Program, program_guard, switch_main_program
from paddle.fluid.layers.control_flow import lod_rank_table, shrink_memory
class TestShrinkRNNMemoryBase(unittest.TestCase):
def setUp(self):
self.main_program = Program()
switch_main_program(self.main_program)
x = layers.data('x', shape=[100], dtype='float32')
x.stop_gradient = False
rank_table_tensor = layers.data(
'rank_table_tensor', shape=[1], dtype='float32', lod_level=1
)
table = lod_rank_table(x=rank_table_tensor)
i = layers.zeros(dtype='int64', shape=[1])
self.mem1 = shrink_memory(x=x, i=i, table=table)
i = layers.increment(x=i)
i.stop_gradient = True
self.mem2 = shrink_memory(x=self.mem1, i=i, table=table)
i = layers.increment(x=i)
i.stop_gradient = True
self.mem3 = shrink_memory(x=self.mem2, i=i, table=table)
mem3_mean = paddle.mean(self.mem3)
append_backward(loss=mem3_mean)
self.x_grad = self.main_program.global_block().var('x@GRAD')
def sum_lodtensor(self, tensor):
sum_res = 0.0
for i in range(np.product(tensor.shape())):
sum_res += tensor._get_float_element(i)
return sum_res
class TestShrinkRNNMemoryReferLoD(TestShrinkRNNMemoryBase):
def test_refer_lod(self):
cpu = core.CPUPlace()
x_tensor = core.LoDTensor()
x_tensor.set_recursive_sequence_lengths([[2, 3, 1]])
tensor_np = np.random.random(size=(6, 100)).astype('float32')
x_tensor.set(tensor_np, cpu)
rank_table_tensor = core.LoDTensor()
rank_table_tensor.set_recursive_sequence_lengths([[1, 2, 3]])
rank_table_tensor.set(
np.random.random(size=(6, 1)).astype('float32'), cpu
)
exe = Executor(cpu)
outs = exe.run(
feed={'x': x_tensor, 'rank_table_tensor': rank_table_tensor},
fetch_list=[self.mem1, self.mem2, self.mem3, self.x_grad],
return_numpy=False,
)
np.testing.assert_allclose(tensor_np[0:6], outs[0], rtol=1e-05)
np.testing.assert_allclose(tensor_np[0:5], outs[1], rtol=1e-05)
np.testing.assert_allclose(tensor_np[0:2], outs[2], rtol=1e-05)
self.assertAlmostEqual(1.0, self.sum_lodtensor(outs[3]), delta=0.01)
class TestShrinkRNNMemoryNoLoD(TestShrinkRNNMemoryBase):
def test_no_lod(self):
cpu = core.CPUPlace()
x_tensor = core.LoDTensor()
tensor_np = np.random.random(size=(3, 100)).astype('float32')
x_tensor.set(tensor_np, cpu)
rank_table_tensor = core.LoDTensor()
rank_table_tensor.set_recursive_sequence_lengths([[1, 2, 3]])
rank_table_tensor.set(
np.random.random(size=(6, 1)).astype('float32'), cpu
)
exe = Executor(cpu)
outs = exe.run(
feed={'x': x_tensor, 'rank_table_tensor': rank_table_tensor},
fetch_list=[self.mem1, self.mem2, self.mem3, self.x_grad],
return_numpy=False,
)
np.testing.assert_allclose(tensor_np[0:3], outs[0], rtol=1e-05)
np.testing.assert_allclose(tensor_np[0:2], outs[1], rtol=1e-05)
np.testing.assert_allclose(tensor_np[0:1], outs[2], rtol=1e-05)
self.assertAlmostEqual(1.0, self.sum_lodtensor(outs[3]), delta=0.01)
class TestShrinkRNNMemoryOpError(unittest.TestCase):
def test_erroes(self):
with program_guard(Program(), Program()):
x = layers.zeros(dtype='int64', shape=[3, 100])
i = layers.zeros(dtype='int64', shape=[1])
rank_table_tensor = core.LoDTensor()
rank_table_tensor.set_recursive_sequence_lengths([[1, 2, 3]])
rank_table_tensor.set(
np.random.random(size=(6, 1)).astype('float32'), core.CPUPlace()
)
rank_table = np.random.random(size=(6, 1)).astype('float32')
# The type of x in shrink_rnn_memory must be Variable.
def test_x_type():
out = shrink_memory(x=1, i=i, table=rank_table_tensor)
self.assertRaises(TypeError, test_x_type)
# The type of i in shrink_rnn_memory must be Variable.
def test_i_type():
out = shrink_memory(x=x, i=0, table=rank_table_tensor)
self.assertRaises(TypeError, test_i_type)
# The type of table in shrink_rnn_memory must be Variable.
def test_table_type():
out = shrink_memory(x=x, i=i, table=rank_table)
self.assertRaises(TypeError, test_table_type)
if __name__ == '__main__':
unittest.main()
......@@ -692,9 +692,9 @@ class TestSliceApiWithLoDTensorArray(unittest.TestCase):
for each_x in x:
each_x.stop_gradient = False
arr = layers.create_array(dtype="float32")
arr = paddle.tensor.create_array(dtype="float32")
for i in range(3):
idx = layers.array_length(arr)
idx = paddle.tensor.array_length(arr)
arr = layers.array_write(x=x[i], i=idx, array=arr)
if case_num == 1:
......@@ -702,7 +702,7 @@ class TestSliceApiWithLoDTensorArray(unittest.TestCase):
elif case_num == 2:
end = (
fluid.layers.array_length(arr) - 1
paddle.tensor.array_length(arr) - 1
) # dtype of end is int64
self.sliced_arr = slice_arr = arr[self.start : end]
output, _ = fluid.layers.tensor_array_to_tensor(
......
......@@ -168,7 +168,7 @@ class TestStackAPIWithLoDTensorArray(unittest.TestCase):
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):
......@@ -206,7 +206,7 @@ class TestTensorStackAPIWithLoDTensorArray(unittest.TestCase):
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):
......
......@@ -191,7 +191,7 @@ class TestLoDTensorArrayStack(unittest.TestCase):
def set_program(self):
self.program = fluid.Program()
with fluid.program_guard(self.program):
self.array = array = fluid.layers.create_array(dtype='float32')
self.array = array = paddle.tensor.create_array(dtype='float32')
idx = fluid.layers.fill_constant(shape=[1], dtype="int64", value=0)
for i, x in enumerate(self.inputs):
x = fluid.layers.assign(x)
......@@ -236,7 +236,7 @@ class TestTensorArrayToTensorAPI(unittest.TestCase):
x1 = fluid.layers.assign(inp2)
x1.stop_gradient = False
i = fluid.layers.fill_constant(shape=[1], dtype="int64", value=0)
array = fluid.layers.create_array(dtype='float32')
array = paddle.tensor.create_array(dtype='float32')
fluid.layers.array_write(x0, i, array)
fluid.layers.array_write(x1, i + 1, array)
output_stack, output_index_stack = fluid.layers.tensor_array_to_tensor(
......@@ -275,7 +275,7 @@ class TestTensorArrayToTensorAPI(unittest.TestCase):
zero = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=1)
ten = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
array = fluid.layers.create_array(dtype='float32')
array = paddle.tensor.create_array(dtype='float32')
inp0 = np.random.rand(2, 3, 4).astype("float32")
x0 = fluid.layers.assign(inp0)
fluid.layers.array_write(x0, zero, array)
......@@ -290,7 +290,7 @@ class TestTensorArrayToTensorAPI(unittest.TestCase):
_, _, array = fluid.layers.while_loop(cond, body, [i, ten, array])
self.assertTrue(fluid.layers.array_length(array), 10)
self.assertTrue(paddle.tensor.array_length(array), 10)
last = fluid.layers.fill_constant(shape=[1], dtype='int64', value=9)
np.testing.assert_array_equal(
fluid.layers.array_read(array, last).numpy(), inp0
......
......@@ -21,10 +21,8 @@ from paddle.jit.dy2static.variable_trans_func import (
from paddle.fluid.framework import core, Variable
from paddle.fluid.layers import Assert, Print
from paddle.fluid.layers import (
array_length,
array_read,
array_write,
create_array,
)
from paddle.fluid.layers import (
assign,
......@@ -136,7 +134,7 @@ def _convert_tensor_arrray_if_necessary(setterhelper, push_pop_names):
def maybe_to_tensor_array(v):
if isinstance(v, list):
return create_array("float32", initialized_list=v)
return paddle.tensor.create_array("float32", initialized_list=v)
else:
return v
......@@ -531,7 +529,7 @@ def convert_len(var):
return var.shape[0]
return nn.shape(var)[0]
elif var.type == core.VarDesc.VarType.LOD_TENSOR_ARRAY:
return control_flow.array_length(var)
return paddle.tensor.array_length(var)
else:
raise TypeError(
'len(var) only supports LoDTensor/LoDTensorArray/SelectedRows, but received %s.'
......@@ -790,11 +788,11 @@ def _run_paddle_pop(array, *args):
def body(i, new_array):
item = array_read(array=array, i=i)
array_write(item, array_length(new_array), new_array)
array_write(item, paddle.tensor.array_length(new_array), new_array)
i = increment(i)
return i, new_array
arr_len = array_length(array)
arr_len = paddle.tensor.array_length(array)
if idx < 0:
idx = idx + arr_len
else:
......@@ -814,7 +812,7 @@ def _run_paddle_pop(array, *args):
# Maybe support start == end for slice op.
def _slice_tensor_array(array, start, end):
def true_fn():
null_array = create_array("float32")
null_array = paddle.tensor.create_array("float32")
return null_array
def false_fn(array, start, end):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册