From 382cf5d7e3a534fe7f23192dfef17cc19c956ebf Mon Sep 17 00:00:00 2001 From: lijianshe02 <48898730+lijianshe02@users.noreply.github.com> Date: Thu, 21 Nov 2019 09:51:05 +0800 Subject: [PATCH] add input type and input data type check for Print_op test=develop (#21250) * add input type and input data type check for Print_op test=develop --- python/paddle/fluid/layers/control_flow.py | 6 +++++- .../paddle/fluid/tests/unittests/test_print_op.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index 25c552c9a88..92d05065363 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 0fc11ef8d92..49b55e376b4 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): -- GitLab