Skip to content

Commit 57b062e

Browse files
[Cherry-Pick] [2.0-beta] error enhancement (#24169)
1 parent 514dd09 commit 57b062e

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

python/paddle/fluid/layers/nn.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12553,6 +12553,11 @@ def get_tensor_from_selected_rows(x, name=None):
1255312553
out = fluid.layers.get_tensor_from_selected_rows(input)
1255412554
"""
1255512555

12556+
check_type(x, 'x', Variable, 'get_tensor_from_selected_rows')
12557+
if x.type != core.VarDesc.VarType.SELECTED_ROWS:
12558+
raise TypeError(
12559+
"The type of 'x' in get_tensor_from_selected_rows must be SELECTED_ROWS."
12560+
)
1255612561
helper = LayerHelper('get_tensor_from_selected_rows', **locals())
1255712562
out = helper.create_variable_for_type_inference(dtype=x.dtype)
1255812563
helper.append_op(

python/paddle/fluid/layers/tensor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,11 @@ def tensor_array_to_tensor(input, axis=1, name=None, use_stack=False):
461461
numpy.array(list(map(lambda x: int(x.shape[axis]), input))))
462462
return res, sizes
463463

464+
check_type(input, 'input', (list, Variable), 'tensor_array_to_tensor')
465+
if isinstance(input, list):
466+
for i, input_x in enumerate(input):
467+
check_type(input_x, 'input[' + str(i) + ']', Variable,
468+
'tensor_array_to_tensor')
464469
helper = LayerHelper('tensor_array_to_tensor', **locals())
465470
out = helper.create_variable_for_type_inference(dtype=helper.input_dtype())
466471
out_index = helper.create_variable_for_type_inference(dtype="int32")

python/paddle/fluid/tests/unittests/test_get_tensor_from_selected_rows_op.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,28 @@
1717
import unittest
1818
import paddle.fluid.core as core
1919
import numpy as np
20+
import paddle.fluid as fluid
2021
from paddle.fluid.op import Operator
22+
from paddle.fluid import Program, program_guard
23+
24+
25+
class TestGetTensorFromSelectedRowsError(unittest.TestCase):
26+
"""get_tensor_from_selected_rows error message enhance"""
27+
28+
def test_errors(self):
29+
with program_guard(Program()):
30+
x_var = fluid.data('X', [2, 3])
31+
x_data = np.random.random((2, 4)).astype("float32")
32+
33+
def test_Variable():
34+
fluid.layers.get_tensor_from_selected_rows(x=x_data)
35+
36+
self.assertRaises(TypeError, test_Variable)
37+
38+
def test_SELECTED_ROWS():
39+
fluid.layers.get_tensor_from_selected_rows(x=x_var)
40+
41+
self.assertRaises(TypeError, test_SELECTED_ROWS)
2142

2243

2344
class TestGetTensorFromSelectedRows(unittest.TestCase):

python/paddle/fluid/tests/unittests/test_tensor_array_to_tensor.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@
2020
import paddle.fluid.core as core
2121
from paddle.fluid.op import Operator
2222
from paddle.fluid.executor import Executor
23+
from paddle.fluid import Program, program_guard
24+
25+
26+
class TestTensorArrayToTensorError(unittest.TestCase):
27+
"""Tensor_array_to_tensor error message enhance"""
28+
29+
def test_errors(self):
30+
with program_guard(Program()):
31+
input_data = numpy.random.random((2, 4)).astype("float32")
32+
33+
def test_Variable():
34+
fluid.layers.tensor_array_to_tensor(input=input_data)
35+
36+
self.assertRaises(TypeError, test_Variable)
37+
38+
def test_list_Variable():
39+
fluid.layers.tensor_array_to_tensor(input=[input_data])
40+
41+
self.assertRaises(TypeError, test_list_Variable)
2342

2443

2544
class TestLoDTensorArrayConcat(unittest.TestCase):

0 commit comments

Comments
 (0)