From 5e65c753ea837f07dfaafe74b869cea8bb34747a Mon Sep 17 00:00:00 2001 From: zhupengyang Date: Mon, 14 Oct 2019 11:53:50 +0800 Subject: [PATCH] enhance input type chec for concat (#20584) test=develop --- python/paddle/fluid/layers/tensor.py | 7 ++++++- .../fluid/tests/unittests/test_concat_op.py | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index b42dddce55..17175a1c3f 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -249,10 +249,15 @@ def concat(input, axis=0, name=None): # [14 15 16]] """ helper = LayerHelper('concat', **locals()) + if not isinstance(input, list): + warnings.warn( + "The type of input in concat should be list, but received %s." % + (type(input))) + input = [input] for x in input: if not isinstance(x, Variable): raise TypeError( - "The type of x in 'input' in concat must be Variable, but received %s" + "The type of x in 'input' in concat must be Variable, but received %s." % (type(x))) if convert_dtype(x.dtype) in ['float16']: warnings.warn( diff --git a/python/paddle/fluid/tests/unittests/test_concat_op.py b/python/paddle/fluid/tests/unittests/test_concat_op.py index 7055e4e251..4e06b40dc9 100644 --- a/python/paddle/fluid/tests/unittests/test_concat_op.py +++ b/python/paddle/fluid/tests/unittests/test_concat_op.py @@ -118,13 +118,22 @@ create_test_fp16(TestConcatOp5) class TestConcatOpError(OpTest): def test_errors(self): with program_guard(Program(), Program()): - # The input type of concat_op must be Variable. - x1 = fluid.create_lod_tensor( + # The input type of concat_op should be list. + x1 = fluid.layers.data(shape=[4], dtype='int32', name='x1') + fluid.layers.concat(x1) + # The item in input must be Variable. + x2 = fluid.create_lod_tensor( np.array([[-1]]), [[1]], fluid.CPUPlace()) - self.assertRaises(TypeError, fluid.layers.concat, x1) + x3 = fluid.create_lod_tensor( + np.array([[-1]]), [[1]], fluid.CPUPlace()) + self.assertRaises(TypeError, fluid.layers.concat, [x2]) # The input dtype of concat_op must be float16(only support on GPU), float32, float64, int32, int64. - x2 = fluid.layers.data(name='x2', shape=[4], dtype='uint8') - self.assertRaises(TypeError, fluid.layers.concat, x2) + x4 = fluid.layers.data(shape=[4], dtype='uint8', name='x4') + x5 = fluid.layers.data(shape=[4], dtype='uint8', name='x5') + self.assertRaises(TypeError, fluid.layers.concat, [x4, x5]) + x6 = fluid.layers.data(shape=[4], dtype='float16', name='x6') + x7 = fluid.layers.data(shape=[4], dtype='float16', name='x7') + fluid.layers.concat([x6, x7]) if __name__ == '__main__':