[API Compatibility] add aminmax op-part#78441
[API Compatibility] add aminmax op-part#78441Manfredss wants to merge 17 commits intoPaddlePaddle:developfrom
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
|
/re-run all-failed |
|
/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. |
| amin_grad : GetReduceGradExpectedKernelType | ||
| manual_signature : [amin] | ||
|
|
||
| - op : aminmax |
There was a problem hiding this comment.
这个应该是一些不兼容历史问题的适配,这个需要改吗,不报错可以不改
|
|
||
| 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 |
| 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`. | ||
|
|
| class TestAminmaxAPI(unittest.TestCase): | ||
| def setUp(self): | ||
| self.init_case() | ||
| self.place = paddle.CPUPlace() |
There was a problem hiding this comment.
无需设置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() |
| import paddle | ||
|
|
||
|
|
||
| class TestAminmaxAPI(unittest.TestCase): |
There was a problem hiding this comment.
新增OP可以测全一些,可以继承OPTest基类来测下。
| """, | ||
| ) | ||
|
|
||
| add_doc_and_signature( |
There was a problem hiding this comment.
文档统一按这个格式规范来吧:
|
/re-run all-failed |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
/re-run all-failed |
| amin_grad : GetReduceGradExpectedKernelType | ||
| manual_signature : [amin] | ||
|
|
||
| - op : aminmax |
There was a problem hiding this comment.
这里的定义用于映射旧框架大写命名(X、Min、Max)到新 phi 内核小写命名(x、min、max)。OpTest 类依赖此映射来构建算子,移除后 check_output 和 check_grad 会报 Missing x as input 或 out_dtype not found 错误,已通过验证不能去掉
| # 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) |
| x: Tensor, | ||
| axis: int | Sequence[int] | None = None, | ||
| keepdim: bool = False, | ||
| name: str | None = None, |
There was a problem hiding this comment.
默认是支持out为关键字参数的,所有op会默认解析kwargs: out
|
/re-run all-failed |
|
/re-run all-failed |
| outputs : | ||
| {min : Min, max : Max} | ||
| extra : | ||
| attrs : [bool use_mkldnn = false, bool use_onednn = false] |
| x: Tensor, | ||
| axis: int | Sequence[int] | None = None, | ||
| keepdim: bool = False, | ||
| name: str | None = None, |
| def init_shape(self): | ||
| self.shape = [10, 10] | ||
|
|
||
| def test_check_grad(self): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
| np.testing.assert_allclose(fetches[i + 1], ref_max) | ||
|
|
||
|
|
||
| if __name__ == '__main__': |
There was a problem hiding this comment.
还需要加一下 paddle.jit.to_static(backend='CINN') 的测试,覆盖AminmaxOpInferSymbolicShape的测试
…e extra in op_compat.yaml
|
/re-run all-failed |
zhwesky2010
left a comment
There was a problem hiding this comment.
test_api_compatibility_part3.py的TestConv2dTransposeAPI有点问题,导致现在CI全部无法通过了,这个PR注释下case或者修复
| self.init_shape = [None, None, 6] | ||
| self.x = np.random.random(self.shape).astype(self.dtype) | ||
| self.net = aminmax_net | ||
| self.enable_cinn = False |
| return paddle.aminmax(x, axis=1) | ||
|
|
||
|
|
||
| def apply_to_static(net, use_cinn, input_spec=None): |
|
|
||
| def base_net(self, flag=None): | ||
| x = paddle.to_tensor(self.x) | ||
| if flag == "static": |
There was a problem hiding this comment.
if "static"
elif "dygraph"
|
/re-run all-failed |
1 similar comment
|
/re-run all-failed |
|
/re-run all-failed |
@zhwesky2010 加了打印信息在跑 pyhton test/legacy_test/test_aminmax_op.py 的时候会有打印,这是覆盖到了吧? |
|
@Manfredss 看起来是已经正常覆盖了 |
|
@Manfredss 示例代码还需要先关一下 |
|
/re-run all-failed |
1 similar comment
|
/re-run all-failed |
| 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) |
There was a problem hiding this comment.
这些示例代码跳过失效了,这个可能只是跳过对输出结果的检查。不跳过示例代码本身的执行?
|
/re-run all-failed |
1 similar comment
|
/re-run all-failed |
|
/re-run all-failed |
1 similar comment
|
/re-run all-failed |



PR Category
User Experience
PR Types
New features
Description
paddle.aminmaxAPI that returns both minimum and maximum of tensor elements over the given axis, aligned withtorch.aminmaxaxis,keepdimparameters and PyTorch-compatible aliases (inputforx,dimforaxis)amin/amax)Changes
New files
paddle/phi/kernels/aminmax_kernel.h/.cc— forward kernel (reusesAMinRawKernel+AMaxRawKernel)paddle/phi/kernels/aminmax_grad_kernel.h/.cc— backward kernel (amax_grad + amin_grad)test/legacy_test/test_aminmax_op.py— unit testsModified files
paddle/phi/ops/yaml/ops.yaml/backward.yaml— op and grad definitionspaddle/phi/ops/yaml/python_api_info.yaml/op_compat.yaml— API info and compatpaddle/phi/infermeta/unary.h/.cc—AMinMaxInferMeta(two-output reduce shape).../infer_symbolic_shape/unary_infer_sym.h/.cc—AminmaxOpInferSymbolicShapepython/paddle/__init__.py,python/paddle/tensor/__init__.py— exportspython/paddle/_paddle_docs.py— docstring and signaturetest/legacy_test/test_api_compatibility.py— API compatibility testAPI Signature
Test plan
是否引起精度变化
否