diff --git a/python/paddle/fluid/layers/math_op_patch.py b/python/paddle/fluid/layers/math_op_patch.py index 01426a0c7928282a91626734dacc031b4906c811..41299a624c12b27a1b36e1e7330d5a554bb784f4 100644 --- a/python/paddle/fluid/layers/math_op_patch.py +++ b/python/paddle/fluid/layers/math_op_patch.py @@ -409,11 +409,19 @@ def monkey_patch_variable(): self = other_var other_var = tmp + if ( + op_type == "divide" or op_type == "elementwise_div" + ) and self.dtype in _supported_int_dtype_: + self = astype(self, 'float32') + other_var = astype(other_var, 'float32') + # NOTE(zhiqiu): the output of compare operator should be bool. if method_name in compare_ops: out = create_new_tmp_var(current_block(self), dtype="bool") else: - out = create_new_tmp_var(current_block(self), dtype=lhs_dtype) + out = create_new_tmp_var( + current_block(self), dtype=safe_get_dtype(self) + ) axis = -1 if other_var.ndim > 0 and other_var.shape[0] == -1: diff --git a/test/dygraph_to_static/test_tensor_methods.py b/test/dygraph_to_static/test_tensor_methods.py index b1a512c08fddd1a991d1766011e1796769c249d6..fff5476167d03432df1b8aa23569604162f2a2f1 100644 --- a/test/dygraph_to_static/test_tensor_methods.py +++ b/test/dygraph_to_static/test_tensor_methods.py @@ -101,5 +101,24 @@ class TestTensorSize(unittest.TestCase): np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-5) +@paddle.jit.to_static +def true_div(x, y): + z = x / y + return z + + +class TestTrueDiv(unittest.TestCase): + def _run(self, to_static): + paddle.jit.enable_to_static(to_static) + x = paddle.to_tensor([3], dtype='int64') + y = paddle.to_tensor([4], dtype='int64') + return true_div(x, y).numpy() + + def test_ture_div(self): + dygraph_res = self._run(to_static=False) + static_res = self._run(to_static=True) + np.testing.assert_allclose(dygraph_res, static_res, rtol=1e-5) + + if __name__ == '__main__': unittest.main()