未验证 提交 1b7de894 编写于 作者: Z Zeng Jinle 提交者: GitHub

fix math_op_path.py when integers, test=develop (#20008)

上级 bb271b6d
...@@ -143,7 +143,11 @@ def monkey_patch_variable(): ...@@ -143,7 +143,11 @@ def monkey_patch_variable():
reverse=False, reverse=False,
scalar_method=None): scalar_method=None):
def __impl__(self, other_var): def __impl__(self, other_var):
if scalar_method is not None: # FIXME(zjl): elementwise_div between integers cannot be converted to scale,
# which may lose accuracy. This is a hot fix for release 1.6.
if scalar_method is not None and not (
op_type == 'elementwise_div' and
self.dtype in _supported_int_dtype_):
if isinstance(other_var, float): if isinstance(other_var, float):
if self.dtype in _supported_int_dtype_: if self.dtype in _supported_int_dtype_:
assert other_var == int(other_var), \ assert other_var == int(other_var), \
......
...@@ -186,6 +186,20 @@ class TestMathOpPatches(unittest.TestCase): ...@@ -186,6 +186,20 @@ class TestMathOpPatches(unittest.TestCase):
fetch_list=[c]) fetch_list=[c])
self.assertTrue(numpy.allclose(a_np - b_np, c_np)) self.assertTrue(numpy.allclose(a_np - b_np, c_np))
@prog_scope()
def test_integer_div(self):
a = fluid.layers.data(name="a", shape=[1], dtype='int64')
b = a / 7
place = fluid.CPUPlace()
exe = fluid.Executor(place)
a_np = numpy.array([3, 4, 10, 14, 9, 18]).astype('int64')
b_np, = exe.run(fluid.default_main_program(),
feed={"a": a_np},
fetch_list=[b])
b_np_actual = (a_np / 7).astype('int64')
self.assertTrue(numpy.array_equal(b_np, b_np_actual))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册