Skip to content

Commit 2816f59

Browse files
authored
[cherri-pick] Fix bug: delete wrong check_type of paddle.concat and support LoDTensorArray (#29306) (#29368)
1 parent fbb6cd7 commit 2816f59

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

python/paddle/fluid/layers/tensor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ def concat(input, axis=0, name=None):
321321
out = helper.create_variable_for_type_inference(dtype=helper.input_dtype())
322322

323323
if input[0].desc.type() == core.VarDesc.VarType.LOD_TENSOR_ARRAY:
324+
# NOTE(liym27): Don't remove this if branch!
325+
# This feature is supported for Dynamic-to-Static, because after transformed, the type of inputs[0]
326+
# is LOD_TENSOR_ARRAY in some scenarios. And this feature can be used in static mode.
327+
324328
assert len(input) == 1, "If the elements of 'input' in concat are Variable(LoDTensorArray), " \
325329
"number of the elements must be 1, but received %s." % len(input)
326330
out_index = helper.create_variable_for_type_inference(dtype="int32")

python/paddle/fluid/tests/unittests/test_concat_op.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def test_input_same_dtype():
228228

229229
class TestConcatAPI(unittest.TestCase):
230230
def test_fluid_api(self):
231+
paddle.enable_static()
231232
x_1 = fluid.data(shape=[None, 1, 4, 5], dtype='int32', name='x_1')
232233
fluid.layers.concat([x_1, x_1], 0)
233234

@@ -253,6 +254,7 @@ def test_fluid_api(self):
253254
assert np.array_equal(res_3, np.concatenate((input_2, input_3), axis=1))
254255

255256
def test_api(self):
257+
paddle.enable_static()
256258
x_1 = paddle.fluid.data(
257259
shape=[None, 1, 4, 5], dtype='int32', name='x_1')
258260
paddle.concat([x_1, x_1], 0)
@@ -338,21 +340,44 @@ def setUp(self):
338340
self.x = np.random.random(self.input_shape).astype("float32")
339341
self.place = fluid.CUDAPlace(0) \
340342
if fluid.is_compiled_with_cuda() else fluid.CPUPlace()
341-
self.set_program()
342343

343-
def set_program(self):
344-
self.program = fluid.Program()
345-
with fluid.program_guard(self.program):
346-
input = fluid.layers.assign(self.x)
347-
tensor_array = fluid.layers.create_array(dtype='float32')
348-
zero = fluid.layers.fill_constant(shape=[1], value=0, dtype="int64")
344+
def set_program(self, use_fluid_api):
345+
paddle.enable_static()
346+
if use_fluid_api:
347+
self.program = fluid.Program()
348+
with fluid.program_guard(self.program):
349+
input = fluid.layers.assign(self.x)
350+
tensor_array = fluid.layers.create_array(dtype='float32')
351+
zero = fluid.layers.fill_constant(
352+
shape=[1], value=0, dtype="int64")
353+
354+
for i in range(self.iter_num):
355+
fluid.layers.array_write(input, zero + i, tensor_array)
356+
357+
self.out_var = fluid.layers.concat(tensor_array, axis=self.axis)
358+
else:
359+
self.program = paddle.static.Program()
360+
with paddle.static.program_guard(self.program):
361+
input = paddle.assign(self.x)
362+
tensor_array = fluid.layers.create_array(
363+
dtype='float32'
364+
) # Api create_array is not supported in paddle 2.0 yet.
365+
zero = paddle.zeros(shape=[1], dtype="int64")
349366

350-
for i in range(self.iter_num):
351-
fluid.layers.array_write(input, zero + i, tensor_array)
367+
for i in range(self.iter_num):
368+
# Api array_write is not supported in paddle 2.0 yet.
369+
fluid.layers.array_write(input, zero + i, tensor_array)
370+
371+
self.out_var = paddle.concat(tensor_array, axis=self.axis)
372+
373+
def test_fluid_api(self):
374+
self._run_static_mode(use_fluid_api=True)
352375

353-
self.out_var = fluid.layers.concat(tensor_array, axis=self.axis)
376+
def test_paddle_api(self):
377+
self._run_static_mode(use_fluid_api=False)
354378

355-
def test_case(self):
379+
def _run_static_mode(self, use_fluid_api):
380+
self.set_program(use_fluid_api)
356381
self.assertTrue(self.out_var.shape[self.axis] == -1)
357382
exe = fluid.Executor(self.place)
358383
res = exe.run(self.program, fetch_list=self.out_var)

python/paddle/tensor/manipulation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def concat(x, axis=0, name=None):
7171
This OP concatenates the input along the axis.
7272
7373
Args:
74-
x(list|tuple): ``x`` is a Tensor list or Tensor tuple which is with data type bool, float16,
74+
x(list|tuple): ``x`` is a Tensor list or Tensor tuple which is with data type bool, float16,
7575
float32, float64, int32, int64. All the Tensors in ``x`` must have same data type.
7676
axis(int|Tensor, optional): Specify the axis to operate on the input Tensors.
7777
It's a scalar with data type int or a Tensor with shape [1] and data type int32
@@ -110,7 +110,6 @@ def concat(x, axis=0, name=None):
110110
# [11 12 13]
111111
# [14 15 16]]
112112
"""
113-
check_type(x, 'x', (list, tuple), 'concat')
114113
return paddle.fluid.layers.concat(input=x, axis=axis, name=name)
115114

116115

0 commit comments

Comments
 (0)