diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 25c552c9a889702bb48d8f10fa79d56d5a0c43d5..92d0506536357fb7868599b0b68e67dee376ae7c 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -27,7 +27,7 @@ import numpy import warnings import six from functools import reduce, partial -from ..data_feeder import convert_dtype +from ..data_feeder import convert_dtype, check_type_and_dtype __all__ = [ 'While', 'Switch', 'increment', 'array_write', 'create_array', 'less_than', @@ -253,6 +253,10 @@ def Print(input, data: 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, ''' + check_type_and_dtype(input, 'input', Variable, + ['float32', 'float64', 'int32_t', 'int64_t', 'bool'], + 'fluid.layers.Print') + helper = LayerHelper('print' + "_" + input.name, **locals()) output = helper.create_variable_for_type_inference(input.dtype) helper.append_op( diff --git a/python/paddle/fluid/tests/unittests/test_print_op.py b/python/paddle/fluid/tests/unittests/test_print_op.py index 0fc11ef8d9220dcc6875b6df2a3e527244872e11..49b55e376b41811be37c7f149565480a45b556da 100644 --- a/python/paddle/fluid/tests/unittests/test_print_op.py +++ b/python/paddle/fluid/tests/unittests/test_print_op.py @@ -24,6 +24,8 @@ from paddle.fluid.framework import switch_main_program from paddle.fluid.framework import Program import numpy as np from simple_nets import simple_fc_net, init_data +from paddle.fluid import compiler, Program, program_guard +from op_test import OpTest class TestPrintOpCPU(unittest.TestCase): @@ -80,6 +82,18 @@ class TestPrintOpCPU(unittest.TestCase): return_numpy=False) +class TestPrintOpError(OpTest): + def test_errors(self): + with program_guard(Program(), Program()): + # The input type of Print_op must be Variable. + x1 = fluid.create_lod_tensor( + np.array([[-1]]), [[1]], fluid.CPUPlace()) + self.assertRaises(TypeError, fluid.layers.Print, x1) + # The input dtype of Print_op must be float32, float64, int32_t, int64_t or bool. + x2 = fluid.layers.data(name='x2', shape=[4], dtype="float16") + self.assertRaises(TypeError, fluid.layers.Print, x2) + + @unittest.skipIf(not core.is_compiled_with_cuda(), "core is not compiled with CUDA") class TestPrintOpGPU(TestPrintOpCPU):