Skip to content

[WIP][API-Compat] Add paddle.compat.min/max and new PHI kernel (min/max_with_index) #74512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from

Conversation

Enigmatisms
Copy link
Contributor

@Enigmatisms Enigmatisms commented Aug 9, 2025

PR Category

Operator Mechanism

PR Types

New features

Description

本 PR 为 #74495 的 reopen 版本,rebase 到了一个更新的版本,并且解决了与 #74506 的冲突。#74495 当时只用于CI发现问题,本 PR 尝试对其中“共用amin/amax backward op 但amin/amax 不支持某些整数类型“的问题进行了修复,基于SFINAE与python端检查。目前本 PR 在 #74506 未合入前会显得改动过多,实际上是包含了部分前序 PR 的改动,前序 PR merge 后应该可以自动 resolve。

本 PR 尚未完成:缺少对应的单测(进行了测试,见最后的TODO),并且依赖一个前置 PR(前置PR目前没有合入,合入后本PR信息将会修改): #74446,需要其中的 ForbidKeywordsDecorator 装饰器。

本 PR 新增的 feature:

  • 两个新的 PHI kernel:(min/max)_with_index,底层基于 cub 的 Argmin/Argmax 操作,同时 reduce key/value。此 PHI kernel 仅在 GPU 端进行了支持。评估:
    • CPU 端的 argmin/argmax 操作基于 Eigen::TensorMap 提供的 argmin/argmax 操作,无法对 value 同时进行 reduce。
    • 其他后端暂不考虑支持。
  • 新的 PHI kernel 的反向传播算子:(min/max)_with_index_grad,与 torch 行为一致,基于 amin/amax 的梯度操作修改而来。
  • 其他与新算子相关的支持(如InferMeta/InferMetaSymbolic)
  • 两个新增的操作:paddle.compat.min, paddle.compat.max,与 torch 的行为进行对齐。

torch.min/torch.max 输入输出关系很复杂(一个API包含了太多功能):

  • 单输入时是全 reduce:仅输出 tensor
  • 输入 dim/keepdim 时:输出 values/indices
  • 输入 other 时:行为与 minimum/maximum 一致

除上述【情况2】在 CUDA GPU 后端下会调用 (min/max)_with_index,其余情况都是由 python 调用 _C_ops.xxx 获得结果的。其中情况1/2/3 在CUDA GPU后端下应该都具有较好的性能(没有进行组合,调用单算子完成),而情况2在其他后端下使用 argmin/max 与 take_along_axis 组合(并且需要配合 squeeze_ 操作),不是最优性能方案,但应当具有较高的开发性价比。

TODO
  • 本地 25个简单单测(单算子不同的输入组合)
  • PaConvert 库测试(10/14),4个失败的测试由于 out 机制没有进行开发导致。此问题暂时不由本人解决。
  • PHI kernel 相关的单测(比如静态图 OpPass 单测等等)
  • test_compat_minmax.py: 能达到单测覆盖率要求的算子单测。
  • 算子python函数内英文文档

Pcard-89620

Copy link

paddle-bot bot commented Aug 9, 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.

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.

1 participant