Skip to content

[API Compatibility] add aminmax op-part#78441

Open
Manfredss wants to merge 17 commits intoPaddlePaddle:developfrom
Manfredss:ApiEnhance_aminmax
Open

[API Compatibility] add aminmax op-part#78441
Manfredss wants to merge 17 commits intoPaddlePaddle:developfrom
Manfredss:ApiEnhance_aminmax

Conversation

@Manfredss
Copy link
Copy Markdown
Contributor

PR Category

User Experience

PR Types

New features

Description

  • Add paddle.aminmax API that returns both minimum and maximum of tensor elements over the given axis, aligned with
    torch.aminmax
  • Supports axis, keepdim parameters and PyTorch-compatible aliases (input for x, dim for axis)
  • Backward gradient correctly distributes evenly among duplicate min/max elements (same as amin/amax)

Changes

New files

  • paddle/phi/kernels/aminmax_kernel.h/.cc — forward kernel (reuses AMinRawKernel + AMaxRawKernel)
  • paddle/phi/kernels/aminmax_grad_kernel.h/.cc — backward kernel (amax_grad + amin_grad)
  • test/legacy_test/test_aminmax_op.py — unit tests

Modified files

  • paddle/phi/ops/yaml/ops.yaml / backward.yaml — op and grad definitions
  • paddle/phi/ops/yaml/python_api_info.yaml / op_compat.yaml — API info and compat
  • paddle/phi/infermeta/unary.h/.ccAMinMaxInferMeta (two-output reduce shape)
  • .../infer_symbolic_shape/unary_infer_sym.h/.ccAminmaxOpInferSymbolicShape
  • python/paddle/__init__.py, python/paddle/tensor/__init__.py — exports
  • python/paddle/_paddle_docs.py — docstring and signature
  • test/legacy_test/test_api_compatibility.py — API compatibility test

API Signature

paddle.aminmax(x, axis=None, keepdim=False, name=None) -> tuple[Tensor, Tensor]

Test plan

  • test_aminmax_op.py — 27 tests (axis variants, keepdim, dtypes, zero-dim, gradient)
  • test_api_compatibility.py::TestAminmaxAPI — dygraph + static, paddle/torch keyword aliases
  • Passed all 15 tests on enhanced PaConvert tests

是否引起精度变化

@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Mar 23, 2026

你的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.

@paddle-bot paddle-bot Bot added the contributor External developers label Mar 23, 2026
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@zhwesky2010 zhwesky2010 changed the title [API Compatibility] develop paddle.aminmax to align paddle with torch.Tensor.aminmax -part [API Compatibility] add aminmax to align with torch -part Mar 24, 2026
@zhwesky2010 zhwesky2010 changed the title [API Compatibility] add aminmax to align with torch -part [API Compatibility] add aminmax -part Mar 24, 2026
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

aminmax evenly distributes gradient between these equal values.

Args:
x (Tensor): A tensor, the data type is float32, float64, int32, int64.
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.

Alias写下

amin_grad : GetReduceGradExpectedKernelType
manual_signature : [amin]

- op : aminmax
Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 Mar 24, 2026

Choose a reason for hiding this comment

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

这个应该是一些不兼容历史问题的适配,这个需要改吗,不报错可以不改


Args:
x (Tensor): A tensor, the data type is float32, float64, int32, int64.
axis (int|list|tuple|None, optional): The axis along which the minimum and maximum
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.

Alias

than the `x` unless :attr:`keepdim` is true, default
value is False.
name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

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.

关键字参数文档

Comment thread test/legacy_test/test_aminmax_op.py Outdated
class TestAminmaxAPI(unittest.TestCase):
def setUp(self):
self.init_case()
self.place = paddle.CPUPlace()
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.

无需设置place,新增OP还是在每个设备上都测下较好

paddle.disable_static()
x = paddle.to_tensor(self.x_np, dtype=self.dtype, stop_gradient=False)
min_val, max_val = paddle.aminmax(x, self.axis, self.keepdim)
loss = min_val.sum() + max_val.sum()
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.

反向测下值吧

Comment thread test/legacy_test/test_aminmax_op.py Outdated
import paddle


class TestAminmaxAPI(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.

新增OP可以测全一些,可以继承OPTest基类来测下。

""",
)

add_doc_and_signature(
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.

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@4174bb8). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #78441   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         5           
  Lines              ?        50           
  Branches           ?         0           
===========================================
  Hits               ?        50           
  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.

@zhwesky2010
Copy link
Copy Markdown
Contributor

zhwesky2010 commented Mar 25, 2026

覆盖率没过
infoflow 2026-03-25 11-21-27

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

amin_grad : GetReduceGradExpectedKernelType
manual_signature : [amin]

- op : aminmax
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.

这个是否可以不改?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

这里的定义用于映射旧框架大写命名(X、Min、Max)到新 phi 内核小写命名(x、min、max)。OpTest 类依赖此映射来构建算子,移除后 check_output 和 check_grad 会报 Missing x as input 或 out_dtype not found 错误,已通过验证不能去掉

Comment thread test/legacy_test/test_aminmax_op.py Outdated
# 4. Mixed arguments (positional x + keyword axis)
min4, max4 = paddle.aminmax(x, axis=0)
# 5. PyTorch keyword arguments (dim alias)
min5, max5 = paddle.aminmax(x, dim=0)
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.

测一下out参数

Comment thread python/paddle/_paddle_docs.py Outdated
x: Tensor,
axis: int | Sequence[int] | None = None,
keepdim: bool = False,
name: str | None = None,
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.

默认是支持out为关键字参数的,所有op会默认解析kwargs: out

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss Manfredss closed this Mar 29, 2026
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss Manfredss reopened this Mar 29, 2026
Comment thread paddle/phi/ops/yaml/op_compat.yaml Outdated
outputs :
{min : Min, max : Max}
extra :
attrs : [bool use_mkldnn = false, bool use_onednn = false]
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.

这些extra应该是没有的

Comment thread python/paddle/_paddle_docs.py Outdated
x: Tensor,
axis: int | Sequence[int] | None = None,
keepdim: bool = False,
name: str | None = None,
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.

新增API统一不支持name

def init_shape(self):
self.shape = [10, 10]

def test_check_grad(self):
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.

这个是什么问题

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

float32 精度下(约7位有效数字),0.005 的扰动会改变 min/max 落在哪个元素上,导致数值梯度和解析梯度完全不一致,最大误差达到 1.0。float64 精度足够,元素间的数值差距远大于扰动量,不会出现这个问题

[[0.2, 0.3, 0.5, 0.9], [0.1, 0.2, 0.6, 0.7]], dtype='float64'
)

def test_dygraph_Compatibility(self):
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.

np.testing.assert_allclose(fetches[i + 1], ref_max)


if __name__ == '__main__':
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.

还需要加一下 paddle.jit.to_static(backend='CINN') 的测试,覆盖AminmaxOpInferSymbolicShape的测试

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@zhwesky2010 zhwesky2010 changed the title [API Compatibility] add aminmax -part [API Compatibility] add aminmax op-part Apr 1, 2026
Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

test_api_compatibility_part3.py的TestConv2dTransposeAPI有点问题,导致现在CI全部无法通过了,这个PR注释下case或者修复

Comment thread test/legacy_test/test_aminmax_op.py Outdated
self.init_shape = [None, None, 6]
self.x = np.random.random(self.shape).astype(self.dtype)
self.net = aminmax_net
self.enable_cinn = False
Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 Apr 1, 2026

Choose a reason for hiding this comment

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

你这没开cinn,导致还是没覆盖到

Comment thread test/legacy_test/test_aminmax_op.py Outdated
return paddle.aminmax(x, axis=1)


def apply_to_static(net, use_cinn, input_spec=None):
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.

这一层建议不包了,包了太多层了


def base_net(self, flag=None):
x = paddle.to_tensor(self.x)
if flag == "static":
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.

if "static"

elif "dygraph"

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@Manfredss
Copy link
Copy Markdown
Contributor Author

Manfredss commented Apr 10, 2026

屏幕截图 2026-04-12 081133

@zhwesky2010 加了打印信息在跑 pyhton test/legacy_test/test_aminmax_op.py 的时候会有打印,这是覆盖到了吧?

@zhwesky2010
Copy link
Copy Markdown
Contributor

@Manfredss 看起来是已经正常覆盖了

zhwesky2010
zhwesky2010 previously approved these changes Apr 17, 2026
Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhwesky2010
Copy link
Copy Markdown
Contributor

@Manfredss 示例代码还需要先关一下

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

Comment thread python/paddle/_paddle_docs.py Outdated
Tensor(shape=[4], dtype=float64, place=Place(cpu), stop_gradient=False,
[0.90000000, 0.90000000, 0.90000000, 0.90000000])

>>> min_val, max_val = paddle.aminmax(x, axis=1, keepdim=True)
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.

这些示例代码跳过失效了,这个可能只是跳过对输出结果的检查。不跳过示例代码本身的执行?

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@zhwesky2010
Copy link
Copy Markdown
Contributor

zhwesky2010 commented Apr 23, 2026

需要看一下CI结果,这个不是随机挂,rerun只针对随机挂才有用
infoflow 2026-04-23 15-15-36

@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@Manfredss
Copy link
Copy Markdown
Contributor Author

/re-run all-failed

Copy link
Copy Markdown
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

@zhwesky2010 zhwesky2010 requested review from SigureMo and removed request for SigureMo April 24, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants