Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions paddle/fluid/pybind/eager_math_op_patch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2190,8 +2190,16 @@ static PyObject* tensor__ne__method(TensorObject* self,
other_tensor = paddle::empty({}, phi::DataType::FLOAT32, place);
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
} else {
paddle::experimental::Scalar value =
CastPyArg2Scalar(other_obj, "__ne__", 0);
paddle::experimental::Scalar value;

// return True if other_obj is unsupported type
try {
value = CastPyArg2Scalar(other_obj, "__ne__", 0);
} catch (const ::common::enforce::EnforceNotMet& e) {
Py_INCREF(Py_True);
return Py_True;
}

if (PyComplex_Check(other_obj)) {
eager_gil_scoped_release guard;
other_tensor =
Expand Down Expand Up @@ -2283,8 +2291,16 @@ static PyObject* tensor__eq__method(TensorObject* self,
other_tensor = paddle::empty({}, phi::DataType::FLOAT32, place);
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
} else {
paddle::experimental::Scalar value =
CastPyArg2Scalar(other_obj, "__eq__", 0);
paddle::experimental::Scalar value;

// return False if other_obj is unsupported type
try {
value = CastPyArg2Scalar(other_obj, "__eq__", 0);
} catch (const ::common::enforce::EnforceNotMet& e) {
Py_INCREF(Py_False);
return Py_False;
}

if (PyComplex_Check(other_obj)) {
eager_gil_scoped_release guard;
other_tensor =
Expand Down
7 changes: 7 additions & 0 deletions python/paddle/pir/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ def __impl__(self, other_var):
# but only +, -, *, / can use this method
if scalar_method is not None:
return scalar_method(self, other_var)
elif other_var is None:
if method_name == "__eq__":
return False
elif method_name == "__ne__":
return True
else:
pass
else:
# do nothing
pass
Expand Down
15 changes: 14 additions & 1 deletion test/legacy_test/test_eager_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ def test_tensor_pin_memory_and_device(self):

with self.assertRaises(RuntimeError) as context:
paddle.tensor(
self.array, device="cpu", pin_memory=True # no support
self.array,
device="cpu",
pin_memory=True, # no support
)
self.assertIn(
"Pinning memory is not supported",
Expand Down Expand Up @@ -1529,6 +1531,17 @@ def test_tensor__format__(self):
paddle_scalar = paddle.uniform([], min=-100, max=100)
self.assertRaises(ValueError, paddle_scalar.__format__, "3d")

def test_tensor_eq_unsupported_type(self):
a = paddle.empty([2])

# Compare with None
self.assertFalse(a == None) # noqa: E711
self.assertTrue(a != None) # noqa: E711

# Compare with other obj
self.assertFalse(a == object())
self.assertTrue(a != object())


class TestEagerTensorSetitem(unittest.TestCase):
def func_setUp(self):
Expand Down
2 changes: 2 additions & 0 deletions test/legacy_test/test_math_op_patch_pir.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ def _test(x_np, y_np, input_dtype):
e = x != y
f = x.not_equal(y)
g = x.__ne__(y)
self.assertFalse(x == None) # noqa: E711
self.assertTrue(x != None) # noqa: E711
(e_np, f_np, g_np) = exe.run(
main_program,
feed={"x": x_np, "y": y_np},
Expand Down
Loading