diff --git a/python/paddle/fluid/dygraph/math_op_patch.py b/python/paddle/fluid/dygraph/math_op_patch.py index 8e4752999b63292d2b82affa64dbf6d26a8b453e..b15e6388884c7b194654f3c3b71b8c111f76e565 100644 --- a/python/paddle/fluid/dygraph/math_op_patch.py +++ b/python/paddle/fluid/dygraph/math_op_patch.py @@ -249,3 +249,13 @@ def monkey_patch_math_varbase(): core.VarBase.__len__ = _len_ core.VarBase.__index__ = _index_ core.VarBase.astype = astype + """ + When code is written like this + y = np.pi * var + ndarray.__mul__(self, var) is called, var will be traced as an array(by using __len__, __getitem__), which is not right. + when var.__array_ufunc__ is set to None, var.__rmul__(self, np) will be called. + + The details can be seen bellow: + https://docs.scipy.org/doc/numpy-1.13.0/neps/ufunc-overrides.html#behavior-in-combination-with-python-s-binary-operations + """ + core.VarBase.__array_ufunc__ = None diff --git a/python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py b/python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py index 4a967d979646d23be4ccd7b75fc09adda76b80a5..34f14b759526026f66930a4ba5322b5363fbb50f 100644 --- a/python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py @@ -237,6 +237,20 @@ class TestMathOpPatchesVarBase(unittest.TestCase): str1 = "just test" self.assertTrue(str1[var1] == 's') + def test_np_left_mul(self): + with fluid.dygraph.guard(): + t = np.sqrt(2.0 * np.pi) + x = fluid.layers.ones((2, 2), dtype="float32") + y = t * x + + self.assertTrue( + np.allclose( + y.numpy(), + t * np.ones( + (2, 2), dtype="float32"), + rtol=1e-05, + atol=0.0)) + if __name__ == '__main__': unittest.main()