From 89cf4f90ecb4ab819723bc36378bb684ec8673b4 Mon Sep 17 00:00:00 2001 From: yongqiangma Date: Tue, 21 Apr 2020 02:11:29 -0500 Subject: [PATCH] lod_rank_table error message enhance (#23613) * lod_rank_table error message enhance. test=develop --- python/paddle/fluid/layers/control_flow.py | 6 ++ .../tests/unittests/test_lod_rank_table.py | 22 ++++++- .../unittests/test_lod_tensor_array_ops.py | 65 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 4c002373750..c8b2cef7353 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 865ca118d55..d8b5c2eef37 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 6a78ef5078a..0148e15b079 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() -- GitLab