diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 371ba65a46d156df44c02d5df393416435d81d34..b3ed1df95709f9a8b86ad0a6f4d056a9fe4e0742 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -1343,6 +1343,9 @@ paddle::experimental::Scalar CastNumpy2Scalar(PyObject* obj, } else if (type_name == "numpy.float32") { float value = CastPyArg2Float(obj, op_type, arg_pos); return paddle::experimental::Scalar(value); + } else if (type_name == "numpy.float16") { + float16 value = CastPyArg2Float16(obj, op_type, arg_pos); + return paddle::experimental::Scalar(value); } else if (type_name == "numpy.int64") { int64_t value = CastPyArg2Long(obj, op_type, arg_pos); return paddle::experimental::Scalar(value); @@ -1352,7 +1355,7 @@ paddle::experimental::Scalar CastNumpy2Scalar(PyObject* obj, } else { PADDLE_THROW(platform::errors::InvalidArgument( "%s(): argument (position %d) must be " - "numpy.float32/float64, numpy.int32/int64, but got %s", + "numpy.float16/float32/float64, numpy.int32/int64, but got %s", op_type, arg_pos + 1, type_name)); // NOLINT diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 5cdd9a0fa06688bec5bdede6cb4b7f01df7f1262..edab97c8b5e696fa72e9b89d732cb7cc121abc61 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -184,6 +184,12 @@ void CastPyArg2AttrLong(PyObject* obj, attrs[key] = CastPyArg2Long(obj, op_type, arg_pos); } +float16 CastPyArg2Float16(PyObject* obj, + const std::string& op_type, + ssize_t arg_pos) { + return static_cast(CastPyArg2Double(obj, op_type, arg_pos)); +} + float CastPyArg2Float(PyObject* obj, const std::string& op_type, ssize_t arg_pos) { diff --git a/paddle/fluid/pybind/op_function_common.h b/paddle/fluid/pybind/op_function_common.h index 686694631cc662e0c7ec42522ddec25835e8845d..57423bb3b74a31e3f0c9f8456fe56f740bbe2aa0 100644 --- a/paddle/fluid/pybind/op_function_common.h +++ b/paddle/fluid/pybind/op_function_common.h @@ -55,6 +55,9 @@ int CastPyArg2Int(PyObject* obj, const std::string& op_type, ssize_t arg_pos); int64_t CastPyArg2Long(PyObject* obj, const std::string& op_type, ssize_t arg_pos); +float16 CastPyArg2Float16(PyObject* obj, + const std::string& op_type, + ssize_t arg_pos); float CastPyArg2Float(PyObject* obj, const std::string& op_type, ssize_t arg_pos); diff --git a/python/paddle/fluid/tests/unittests/test_elementwise_add_op.py b/python/paddle/fluid/tests/unittests/test_elementwise_add_op.py index 1cb57e6d72fd574892e4a2601ca7c1a1ece7fbf4..3bf2b7cdcd703583e2cf0e00a9542b6130acef22 100644 --- a/python/paddle/fluid/tests/unittests/test_elementwise_add_op.py +++ b/python/paddle/fluid/tests/unittests/test_elementwise_add_op.py @@ -737,6 +737,24 @@ class TestElementwiseAddop1(unittest.TestCase): paddle.enable_static() +class TestTensorAddNumpyScalar(unittest.TestCase): + def test_float32_add(self): + paddle.disable_static() + a = paddle.full([4, 5, 6], 1.5, dtype='float32') + b = np.array([1.5], dtype='float32')[0] + c = a + b + self.assertTrue(c.dtype == core.VarDesc.VarType.FP32) + + def test_float16_add(self): + if not core.is_compiled_with_cuda(): + return + paddle.disable_static() + a = paddle.full([4, 5, 6], 1.5, dtype='float16') + b = np.array([1.5], dtype='float16')[0] + c = a + b + self.assertTrue(c.dtype == core.VarDesc.VarType.FP16) + + if __name__ == '__main__': paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/transpiler/collective.py b/python/paddle/fluid/transpiler/collective.py index 870efa0968d72b04e87cb594a58936854b30de67..04bd68d2571635c19e55802305bc37149e6e7f08 100644 --- a/python/paddle/fluid/transpiler/collective.py +++ b/python/paddle/fluid/transpiler/collective.py @@ -516,12 +516,12 @@ class SingleProcessMultiThread(GradAllReduce): def _transpile_main_program(self): # not need loss scale and no dense param param_cnt = self._get_update_param_count() - if self.loss_scale is 0 and param_cnt is 0: + if self.loss_scale == 0 and param_cnt == 0: return # scale loss self._insert_scale_loss_grad_ops() # no param - if param_cnt is 0: + if param_cnt == 0: return # fuse allreduce if self.fuse_allreduce > 0: