diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 4c002373750888ecb004c923c4dde91a2de34203..c8b2cef73530a835731b629fa8cd7867ddeaf504 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -1140,6 +1140,12 @@ def lod_rank_table(x, level=0): dtype='float32', lod_level=1) out = layers.lod_rank_table(x=x, level=0) """ + check_type(x, 'x', (Variable, list), 'lod_rank_table') + if isinstance(x, (list)): + for i, input_x in enumerate(x): + check_type(input_x, 'input[' + str(i) + ']', Variable, + 'lod_rank_table') + helper = LayerHelper("lod_rank_table", **locals()) table = helper.create_variable( type=core.VarDesc.VarType.LOD_RANK_TABLE, diff --git a/python/paddle/fluid/tests/unittests/test_lod_rank_table.py b/python/paddle/fluid/tests/unittests/test_lod_rank_table.py index 865ca118d55f82c66d44f4e3d553baafa0c14c3a..d8b5c2eef37b9354a40614a0ef9dc5e080f36b76 100644 --- a/python/paddle/fluid/tests/unittests/test_lod_rank_table.py +++ b/python/paddle/fluid/tests/unittests/test_lod_rank_table.py @@ -17,7 +17,7 @@ from __future__ import print_function from paddle.fluid.layers import data from paddle.fluid.layers.control_flow import lod_rank_table from paddle.fluid.executor import Executor -import paddle.fluid.core as core +from paddle.fluid import Program, program_guard, core import numpy import unittest @@ -41,5 +41,25 @@ class TestLoDRankTable(unittest.TestCase): self.assertEqual([(0, 5), (1, 1), (2, 1)], list(table.items())) +class TestLoDRankTableError(unittest.TestCase): + def test_errors(self): + with program_guard(Program(), Program()): + x = numpy.random.random((2, 4)).astype("float32") + + def test_Variable(): + rank_table = lod_rank_table(x=x, level=1) + + self.assertRaises(TypeError, test_Variable) + + def test_list_Variable(): + rank_table = lod_rank_table(x=[x], level=1) + + self.assertRaises(TypeError, test_list_Variable) + + x = data(name='x', shape=[10], dtype='float32', lod_level=1) + out = lod_rank_table(x=x, level=0) + out = lod_rank_table(x=[x], level=0) + + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_lod_tensor_array_ops.py b/python/paddle/fluid/tests/unittests/test_lod_tensor_array_ops.py index 6a78ef5078a738efa2ae39ea23645fedaecce63b..0148e15b0799045dd8c9839f2035e89ffef5a80c 100644 --- a/python/paddle/fluid/tests/unittests/test_lod_tensor_array_ops.py +++ b/python/paddle/fluid/tests/unittests/test_lod_tensor_array_ops.py @@ -219,5 +219,70 @@ class TestCPULoDTensorArrayOpGrad(unittest.TestCase): self.assertAlmostEqual(1.0, g_out_sum, delta=0.1) +class TestLoDTensorArrayError(unittest.TestCase): + def test_errors(self): + with program_guard(Program(), Program()): + x = numpy.random.random((10)).astype("float32") + x2 = layers.data(name='x', shape=[10]) + table = lod_rank_table(x2, level=0) + + def test_x_Variable(): + rank_table = lod_tensor_to_array(x=x, table=table) + + self.assertRaises(TypeError, test_x_Variable) + + table2 = numpy.random.random((2)).astype("int64") + + def test_table_Variable(): + rank_table = lod_tensor_to_array(x=x2, table=table2) + + self.assertRaises(TypeError, test_table_Variable) + + def test_x_list_Variable(): + rank_table = lod_tensor_to_array(x=[x], table=table) + + self.assertRaises(TypeError, test_x_list_Variable) + + def test_table_list_Variable(): + rank_table = lod_tensor_to_array(x=x2, table=[table2]) + + self.assertRaises(TypeError, test_table_list_Variable) + + array = lod_tensor_to_array(x2, table) + + +class TestArrayLoDTensorError(unittest.TestCase): + def test_errors(self): + with program_guard(Program(), Program()): + x = numpy.random.random((10)).astype("float32") + x2 = layers.data(name='x', shape=[10]) + table = lod_rank_table(x2, level=0) + array = lod_tensor_to_array(x2, table) + + def test_x_Variable(): + rank_table = array_to_lod_tensor(x=x, table=table) + + self.assertRaises(TypeError, test_x_Variable) + + table2 = numpy.random.random((2)).astype("int64") + + def test_table_Variable(): + rank_table = array_to_lod_tensor(x=array, table=table2) + + self.assertRaises(TypeError, test_table_Variable) + + def test_x_list_Variable(): + rank_table = array_to_lod_tensor(x=[x], table=table) + + self.assertRaises(TypeError, test_x_list_Variable) + + def test_table_list_Variable(): + rank_table = array_to_lod_tensor(x=x2, table=[table2]) + + self.assertRaises(TypeError, test_table_list_Variable) + + array = array_to_lod_tensor(x2, table) + + if __name__ == '__main__': unittest.main()