Repository navigation
Conversation
|
|
Paddle-Bot Review Board (review完成)
可维护性建议(总分 86.2 / 100,只给建议,不影响评审结论)
各维度得分(仅评本 PR 改动,点击展开)
Powered by Nyanpasu claude with DeepSeek-V4.1-Flash 默认推理级别, please check the suggestions carefully. |
2e64688 to
df1e3c9
Compare
| if paddle.is_floating_point(x) and not paddle.is_floating_point(y): | ||
| return x, y.astype(x.dtype) | ||
| if paddle.is_floating_point(y) and not paddle.is_floating_point(x): | ||
| return x.astype(y.dtype), y |
There was a problem hiding this comment.
当一侧为 float、另一侧为 complex 时,本函数会把复数操作数
astype 成浮点 dtype 并丢弃虚部:paddle.is_floating_point 对 complex64/complex128 返回 False,因此会命中 is_floating_point(x) and not is_floating_point(y) 分支。例如 paddle.equal(paddle.to_tensor([1.0]), paddle.to_tensor([1+2j])) 由“提升为 complex 后比较、结果 False”变成“仅比较实部、结果 True”,属于静默错误结果,也与 docstring 中 “Same-dtype and complex pairs are returned unchanged” 矛盾。建议在提升前显式短路复数(如 if paddle.is_complex(x) or paddle.is_complex(y): return x, y),或改为仅在另一操作数为整型/布尔时才转换(参考 logaddexp 的 integral 判断),并补充 float+complex 用例。
| ) | ||
|
|
||
|
|
||
| class TestCompareMixedDtypePromotion(unittest.TestCase): |
There was a problem hiding this comment.
本类未像同文件其它 dygraph 用例那样调用
paddle.disable_static()。CI 通过 tools/test_runner.py 以 unittest.TestLoader.loadTestsFromModule 按类名字母序执行,API_TestElementwise_Equal / API_TestElementwise_Greater_Than 会先调用 paddle.enable_static() 且无 teardown 恢复,因此本类会在静态/PIR 模式下运行:out.numpy() 会命中 fake_interface_only(见 python/paddle/base/framework.py 与 python/paddle/pir/math_op_patch.py)直接抛 AssertionError,或根本走不到 in_dynamic_or_pir_mode() 的提升分支,新增用例无法验证本 PR 的修复。建议在 setUp 或 _check 开头调用 paddle.disable_static(),显式固定在动态图模式。
| [True , False, True ]) | ||
| """ | ||
| if in_dynamic_or_pir_mode(): | ||
| x, y = _promote_compare_dtypes(x, y) |
There was a problem hiding this comment.
混合 dtype 提升只接入了 equal/not_equal/greater_equal/less_equal/less_than 五个包装,而同模块公开导出的
paddle.greater_than 在文件顶部直接从 _C_ops 重导出(无 Python 包装),因此 paddle.greater_than(int32_tensor, float32_tensor) 仍会因 dtype 不一致报错,而对称的 paddle.less_than 已可比较;Tensor.__gt__ 也走这条未提升路径。建议为 greater_than 增加与 less_than 对称的包装并调用 _promote_compare_dtypes,同时在 test_compare_op.py 的 OPS 列表补上 paddle.greater_than,覆盖该对称路径。
| __all__ = [] | ||
|
|
||
|
|
||
| def _promote_compare_dtypes(x, y): |
There was a problem hiding this comment.
💡 可维护性建议(跨模块架构,不阻塞合入):_promote_compare_dtypes 与 python/paddle/tensor/math.py 中 logaddexp 已有的“整型操作数转为浮点方 dtype”逻辑几乎重复(后者用 integral 集合判断,不会误伤复数)。建议把这段 PyTorch 风格提升抽成共享 helper(放在 paddle/base/data_feeder.py 或 tensor 内部)供两者复用,或基于已有的 promote_types 统一实现,避免两份规则后续漂移。
| PyTorch/NumPy promotion: the integer operand is cast to the floating dtype. | ||
| Same-dtype and complex pairs are returned unchanged. | ||
| """ | ||
| if not (hasattr(x, "dtype") and hasattr(y, "dtype")): |
There was a problem hiding this comment.
💡 可维护性建议(逻辑清晰度,不阻塞合入):hasattr(x, "dtype") and hasattr(y, "dtype") 守卫在五个调用点均不可达(参数在调用前已被归一化为 Tensor/pir.Value),且紧接着的 paddle.is_floating_point 对非 Tensor 会直接抛 TypeError,守卫并不能提供所暗示的兼容性。建议删除该守卫,直接从 dtype 判断开始,或在 docstring 中声明入参必须是已归一化的 Tensor/Value。
| y = numpy.array([[1.0, 2.5], [3.0, 4.5]], dtype=float_dtype) | ||
| expected_x = x.astype(float_dtype) | ||
| for api, np_api in self.OPS: | ||
| out = api(paddle.to_tensor(x), paddle.to_tensor(y)) |
There was a problem hiding this comment.
💡 可维护性建议(测试策略,不阻塞合入):_check 始终以 x=int、y=float 调用,只覆盖 _promote_compare_dtypes 中“y 为浮点”的分支;x 为浮点、y 为整型的对称分支(本函数唯一对 x 做 astype 的路径)零覆盖。建议补充反向操作数断言(如 api(paddle.to_tensor(y_float), paddle.to_tensor(x_int))),与现有正向用例形成对称覆盖。
PR Category
PR Types
Description
是否引起精度变化