未验证 提交 89cf4f90 编写于 作者: myq406450149's avatar myq406450149 提交者: GitHub

lod_rank_table error message enhance (#23613)

* lod_rank_table error message enhance. test=develop
上级 5c669ad1
......@@ -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,
......
......@@ -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()
......@@ -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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册