Skip to content

Conversation

@HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Jul 4, 2025

PR Category

User Experience

PR Types

Bug fixes

Description

Pcard-75624

  1. [Feat] Support custom __radd__ and __rmul__ #73119 中为了支持触发 __rxx__ 方法,将原来抛出的所有异常都改为了 NotImplement,导致正常的异常信息无法正确显示,在 Python 侧会全部显示 NotImplement。因此将触发 __rxx__ 的条件放到了相关运算的 pybind 代码中,只有当所有逻辑失败时,才会运行到 catch分支,触发 __rxx__。这样不会影响原来的正常异常抛出;单测添加了不同异常触发的判断测试,确保不影响正常的其它异常信息捕获
  2. 额外支持了动态图下已有的__rxx__系列运算触发机制

cc @BeingGod

@paddle-bot
Copy link

paddle-bot bot commented Jul 4, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@HydrogenSulfate HydrogenSulfate force-pushed the fix_eager_add_and_mul_exception branch 2 times, most recently from 823a637 to 88b49ab Compare July 4, 2025 08:51
@HydrogenSulfate HydrogenSulfate force-pushed the fix_eager_add_and_mul_exception branch from 88b49ab to dddfc7b Compare July 4, 2025 08:53
@HydrogenSulfate HydrogenSulfate requested a review from Copilot July 5, 2025 10:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR restores proper exception reporting for tensor reflected methods by moving the NotImplemented fallback into individual PyBind operator implementations and updating Python-side tests to validate multiple exception scenarios and dygraph behavior.

  • Relocated reflected-method trigger logic from a global macro into each __op__ method so original exceptions surface correctly before falling back to NotImplemented
  • Updated tests to use a dygraph_guard context and added new reflected-operation tests for custom tensors
  • Removed the now-unused global EAGER_CATCH_AND_THROW_RETURN_NOT_IMPLEMENTED macro

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
test/legacy_test/test_math_op_patch.py Swapped to dygraph_guard in tests, added reflected‐op tests, and updated CustomTensor methods
paddle/fluid/pybind/exception.h Removed obsolete EAGER_CATCH_AND_THROW_RETURN_NOT_IMPLEMENTED macro
paddle/fluid/pybind/eager_math_op_patch.cc Inlined per-operator fallback logic and removed macro usage
Comments suppressed due to low confidence (1)

test/legacy_test/test_math_op_patch.py:669

  • Re-adding the @unittest.skipIf(paddle.device.is_compiled_with_xpu(), ...) decorator will prevent failures on XPU, where custom tensor operations aren't supported.
 # @unittest.skipIf( 
value = CastPyArg2Scalar(other_obj, "__add__", 0);
} catch (...) {
PyObject* radd = PyObject_GetAttrString(other_obj, "__radd__");
if (radd) {
Copy link

Copilot AI Jul 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If PyObject_GetAttrString fails, it leaves a pending Python error. Call PyErr_Clear() when radd is nullptr to avoid a stale AttributeError in the interpreter state.

Suggested change
if (radd) {
if (!radd) {
PyErr_Clear(); // Clear the Python error state if PyObject_GetAttrString fails
} else {
Copilot uses AI. Check for mistakes.
Comment on lines +310 to +323
try {
value = CastPyArg2Scalar(other_obj, "__add__", 0);
} catch (...) {
PyObject* radd = PyObject_GetAttrString(other_obj, "__radd__");
if (radd) {
bool has_callable_radd = PyCallable_Check(radd);
Py_DECREF(radd);
if (has_callable_radd) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
}
throw;
}
Copy link

Copilot AI Jul 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The reflected-method fallback pattern is repeated across operators; consider extracting this into a helper function to reduce duplication and improve readability.

Suggested change
try {
value = CastPyArg2Scalar(other_obj, "__add__", 0);
} catch (...) {
PyObject* radd = PyObject_GetAttrString(other_obj, "__radd__");
if (radd) {
bool has_callable_radd = PyCallable_Check(radd);
Py_DECREF(radd);
if (has_callable_radd) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
}
throw;
}
value = HandleReflectedMethodFallback(other_obj, "__radd__", [&]() {
return CastPyArg2Scalar(other_obj, "__add__", 0);
});
Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@HydrogenSulfate HydrogenSulfate merged commit c4b30c1 into PaddlePaddle:develop Jul 7, 2025
68 of 73 checks passed
@HydrogenSulfate HydrogenSulfate deleted the fix_eager_add_and_mul_exception branch July 7, 2025 02:39
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (develop@39a0d0a). Learn more about missing BASE report.

Additional details and impacted files
@@ Coverage Diff @@ ## develop #73833 +/- ## ========================================== Coverage ? 0 ========================================== Files ? 0 Lines ? 0 Branches ? 0 ========================================== Hits ? 0 Misses ? 0 Partials ? 0 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants