未验证 提交 517929f1 编写于 作者: G GaoWei8 提交者: GitHub

Op (reorder_lod_tensor_by_rank) error message enhancement (#23552)

上级 28f04c6a
......@@ -188,7 +188,9 @@ class ReorderLoDTensorByRankTableOp : public ReorderLoDTensorByRankTableBase {
size_t out_offset = 0;
out->mutable_lod()->clear();
for (auto &item : rank_table.items()) {
PADDLE_ENFORCE_LT(item.index, absolute_table.size());
PADDLE_ENFORCE_LT(item.index, absolute_table.size(),
platform::errors::OutOfRange(
"The value of rank_table is out of range."));
out_offset = CopyTensorAndLod(place, absolute_table[item.index], x, out,
out_offset);
}
......
......@@ -3476,9 +3476,14 @@ def reorder_lod_tensor_by_rank(x, rank_table):
x=data, rank_table=table)
"""
check_type(x, 'x', (Variable), 'reorder_lod_tensor_by_rank')
check_type(rank_table, 'rank_table', (Variable),
'reorder_lod_tensor_by_rank')
if rank_table.type != core.VarDesc.VarType.LOD_RANK_TABLE:
raise TypeError("The type of rank_table should be LOD_RANK_TABLE.")
helper = LayerHelper('reorder_lod_tensor_by_rank', **locals())
helper.is_instance('x', Variable)
helper.is_instance('rank_table', Variable)
out = helper.create_variable_for_type_inference(dtype=x.dtype)
helper.append_op(
......
......@@ -18,6 +18,7 @@ import unittest
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.layers.control_flow import lod_rank_table
from paddle.fluid import Program, program_guard
import numpy
import functools
......@@ -209,5 +210,29 @@ class TestReorderLoDTensor(unittest.TestCase):
numpy.array(actual_output), expect_output, atol=0.001))
class TestReorderLoDTensorError(unittest.TestCase):
def test_errors(self):
with program_guard(Program()):
def test_Variable():
# The input must be Variable.
x1 = numpy.array([0.9383, 0.1983, 3.2, 1.2]).astype("float64")
table1 = numpy.array(
[0.9383, 0.1983, 3.2, 1.2]).astype("float64")
new_dat = fluid.layers.reorder_lod_tensor_by_rank(
x=x1, rank_table=table1)
self.assertRaises(TypeError, test_Variable)
def test_type():
x2 = fluid.layers.data(name='x1', shape=[4], dtype='float32')
table2 = fluid.layers.data(
name='table2', shape=[4], dtype='int32')
new_dat2 = fluid.layers.reorder_lod_tensor_by_rank(
x=x2, rank_table=table2)
self.assertRaises(TypeError, test_type)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册