diff --git a/python/paddle/fluid/data_feeder.py b/python/paddle/fluid/data_feeder.py index 496f7a8ada563af96a02c1e7f5df7725e15faace..fa1d7f8cddb899859a13a8068544b60ffb7598f4 100644 --- a/python/paddle/fluid/data_feeder.py +++ b/python/paddle/fluid/data_feeder.py @@ -29,31 +29,32 @@ __all__ = ['DataFeeder'] def convert_dtype(dtype): if isinstance(dtype, str): if dtype in [ - 'float32', 'int64', 'float64', 'float16', 'int32', 'uint8', - 'bool' + 'bool', 'float16', 'float32', 'float64', 'int8', 'int16', + 'int32', 'int64', 'uint8' ]: return dtype - else: - raise ValueError( - "dtype must be any of [bool, int32, float32, int64, " - "float64, uint8]") - elif dtype == core.VarDesc.VarType.BOOL: - return 'bool' - elif dtype == core.VarDesc.VarType.FP32: - return 'float32' - elif dtype == core.VarDesc.VarType.INT64: - return 'int64' - elif dtype == core.VarDesc.VarType.FP64: - return 'float64' - elif dtype == core.VarDesc.VarType.FP16: - return 'float16' - elif dtype == core.VarDesc.VarType.INT32: - return 'int32' - elif dtype == core.VarDesc.VarType.UINT8: - return 'uint8' else: - raise ValueError("dtype must be any of [bool,int32, float32, int64, " - "float64, uint8]") + if dtype == core.VarDesc.VarType.BOOL: + return 'bool' + elif dtype == core.VarDesc.VarType.FP16: + return 'float16' + elif dtype == core.VarDesc.VarType.FP32: + return 'float32' + elif dtype == core.VarDesc.VarType.FP64: + return 'float64' + elif dtype == core.VarDesc.VarType.INT8: + return 'int8' + elif dtype == core.VarDesc.VarType.INT16: + return 'int16' + elif dtype == core.VarDesc.VarType.INT32: + return 'int32' + elif dtype == core.VarDesc.VarType.INT64: + return 'int64' + elif dtype == core.VarDesc.VarType.UINT8: + return 'uint8' + raise ValueError( + "dtype must be any of [bool, float16, float32, float64, int8, int16, " + "int32, int64, uint8]") class DataToLoDTensorConverter(object): diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index 17175a1c3fccdebe5d2cf4684f656165522eaa37..0641739243a74de14487f5d712d2ea2ea9c0f3eb 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -192,6 +192,17 @@ def cast(x, dtype): # [ 0 4]] int32 """ helper = LayerHelper('cast', **locals()) + if not isinstance(x, Variable): + raise TypeError( + "The type of 'x' in cast must be Variable, but received %s" % + (type(x))) + if convert_dtype(x.dtype) not in [ + 'bool', 'float16', 'float32', 'float64', 'int32', 'int64', 'uint8' + ]: + raise TypeError( + "The data type of 'x' in cast must be one of [bool, float16, float32, float64, int32, int64, uint8], but received %s." + % (convert_dtype(x.dtype))) + out = helper.create_variable_for_type_inference(dtype=dtype) helper.append_op( type='cast', diff --git a/python/paddle/fluid/tests/unittests/test_cast_op.py b/python/paddle/fluid/tests/unittests/test_cast_op.py index 71a2ccb6da47588d84c263105560626435ac461a..53f7df60b8a541fe18946479a84571fbef5d63f1 100644 --- a/python/paddle/fluid/tests/unittests/test_cast_op.py +++ b/python/paddle/fluid/tests/unittests/test_cast_op.py @@ -18,6 +18,8 @@ import op_test import unittest import numpy as np import paddle.fluid.core as core +import paddle.fluid as fluid +from paddle.fluid import compiler, Program, program_guard class TestCastOp1(op_test.OpTest): @@ -69,5 +71,19 @@ class TestCastOp3(op_test.OpTest): self.check_output(atol=1e-3) +class TestCastOpError(op_test.OpTest): + def test_errors(self): + with program_guard(Program(), Program()): + # The input type of cast_op must be Variable. + x1 = fluid.create_lod_tensor( + np.array([[-1]]), [[1]], fluid.CPUPlace()) + self.assertRaises(TypeError, fluid.layers.cast, x1, 'int32') + # The input dtype of cast_op must be bool, float16, float32, float64, int32, int64, uint8. + x2 = fluid.layers.data(name='x2', shape=[4], dtype='int8') + self.assertRaises(TypeError, fluid.layers.cast, x2, 'int32') + x3 = fluid.layers.data(name='x3', shape=[4], dtype='int16') + self.assertRaises(TypeError, fluid.layers.cast, x3, 'int32') + + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py index 3d2340fddce3e822d614566809866890ddb84fa4..f6e5c2166bc25b559600250b6a0f3435d6df80d9 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py @@ -230,7 +230,7 @@ class TestFillConstantOpError(OpTest): value=5, dtype='uint4') self.assertRaises( - ValueError, + TypeError, fluid.layers.fill_constant, shape=[1], value=5,