Skip to content

[Bug Fix] Support mixed int/float dtypes in comparison ops (#78468) - #80039

Open
jycschool wants to merge 1 commit into
PaddlePaddle:developfrom
jycschool:fix/compare-mixed-dtype
Open

jycschool wants to merge 1 commit into
PaddlePaddle:developfrom
jycschool:fix/compare-mixed-dtype

Conversation

@jycschool

Copy link
Copy Markdown

PR Category

PR Types

Description

是否引起精度变化

@CLAassistant

CLAassistant commented Nov 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Paddle-Review-Bot

Paddle-Review-Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Paddle-Bot Review Board (review完成)

序号 位置 优先级 规则来源 状态
1 比较算子 dtype 提升对复数场景的处理 P1 默认规则 🚧
2 新增混合 dtype 测试的执行模式 P1 默认规则 🚧
3 greater_than 缺少混合 dtype 提升 P2 默认规则 🚧

可维护性建议(总分 86.2 / 100,只给建议,不影响评审结论)

序号 位置 维度 状态
1 dtype 提升逻辑与 logaddexp 重复 跨模块架构 💡
2 不可达的 hasattr 守卫 逻辑清晰度 💡
3 测试只覆盖单向操作数顺序 测试策略 💡
各维度得分(仅评本 PR 改动,点击展开)
78 契约一致性
80 跨模块架构
1
82 测试策略
3
82 API 表面一致性
84 抽象适配度
85 约定偏离
85 设计一致性
85 错误处理一致性
85 高层优雅度
85 未完成迁移
87 中层优雅度
87 命名质量
88 逻辑清晰度
2
88 底层优雅度
90 AI 生成债务
90 类型安全
95 初始化耦合
95 包组织结构
— 依赖健康度
— 鉴权一致性

Powered by Nyanpasu claude with DeepSeek-V4.1-Flash 默认推理级别, please check the suggestions carefully.

@jycschool
jycschool force-pushed the fix/compare-mixed-dtype branch from 2e64688 to df1e3c9 Compare November 30, 2026 09:19
Comment on lines +72 to +75
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 当一侧为 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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 本类未像同文件其它 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 混合 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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 可维护性建议(跨模块架构,不阻塞合入):_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")):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 可维护性建议(逻辑清晰度,不阻塞合入):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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 可维护性建议(测试策略,不阻塞合入):_check 始终以 x=int、y=float 调用,只覆盖 _promote_compare_dtypes 中“y 为浮点”的分支;x 为浮点、y 为整型的对称分支(本函数唯一对 x 做 astype 的路径)零覆盖。建议补充反向操作数断言(如 api(paddle.to_tensor(y_float), paddle.to_tensor(x_int))),与现有正向用例形成对称覆盖。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants