-
Notifications
You must be signed in to change notification settings - Fork 6k
[API Compatibility] add aminmax op-part #78441
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
Changes from 14 commits
3fc934e
bf2fd62
e62826d
e735b93
d780164
96b10be
f2ae4ab
04e7223
f52a13f
3e603a0
5e2ff5d
e2e3e03
9883a5c
7a184e2
f28ebcd
2d9488d
9a11de4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "paddle/phi/kernels/aminmax_grad_kernel.h" | ||
|
|
||
| #include "paddle/phi/backends/all_context.h" | ||
| #include "paddle/phi/core/kernel_registry.h" | ||
| #include "paddle/phi/kernels/elementwise_add_kernel.h" | ||
| #include "paddle/phi/kernels/reduce_amax_grad_kernel.h" | ||
| #include "paddle/phi/kernels/reduce_amin_grad_kernel.h" | ||
|
|
||
| namespace phi { | ||
|
|
||
| template <typename T, typename Context> | ||
| void AMinMaxGradKernel(const Context& dev_ctx, | ||
| const DenseTensor& x, | ||
| const DenseTensor& min, | ||
| const DenseTensor& max, | ||
| const DenseTensor& min_grad, | ||
| const DenseTensor& max_grad, | ||
| const std::vector<int64_t>& dims, | ||
| bool keep_dim, | ||
| bool reduce_all, | ||
| DenseTensor* x_grad) { | ||
| if (x_grad && x_grad->numel() == 0) { | ||
| dev_ctx.template Alloc<T>(x_grad); | ||
| return; | ||
| } | ||
| reduce_all = recompute_reduce_all(x, dims, reduce_all); | ||
|
|
||
| // Compute amax grad contribution into x_grad | ||
| ReduceAMaxGradKernel<T, Context>( | ||
| dev_ctx, x, max, max_grad, dims, keep_dim, reduce_all, x_grad); | ||
|
|
||
| // Compute amin grad contribution into a temporary tensor | ||
| DenseTensor amin_x_grad; | ||
| amin_x_grad.Resize(x_grad->dims()); | ||
| dev_ctx.template Alloc<T>(&amin_x_grad); | ||
| ReduceAMinGradKernel<T, Context>( | ||
| dev_ctx, x, min, min_grad, dims, keep_dim, reduce_all, &amin_x_grad); | ||
|
|
||
| // x_grad = amax_grad_result + amin_grad_result | ||
| Add<T, Context>(dev_ctx, *x_grad, amin_x_grad, x_grad); | ||
| } | ||
|
|
||
| } // namespace phi | ||
|
|
||
| PD_REGISTER_KERNEL(aminmax_grad, | ||
| CPU, | ||
| ALL_LAYOUT, | ||
| phi::AMinMaxGradKernel, | ||
| float, | ||
| double, | ||
| int, | ||
| int64_t) {} | ||
|
|
||
| #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
| PD_REGISTER_KERNEL(aminmax_grad, | ||
| GPU, | ||
| ALL_LAYOUT, | ||
| phi::AMinMaxGradKernel, | ||
| float, | ||
| double, | ||
| int, | ||
| int64_t) {} | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "paddle/phi/core/dense_tensor.h" | ||
|
|
||
| namespace phi { | ||
| template <typename T, typename Context> | ||
| void AMinMaxGradKernel(const Context& dev_ctx, | ||
| const DenseTensor& x, | ||
| const DenseTensor& min, | ||
| const DenseTensor& max, | ||
| const DenseTensor& min_grad, | ||
| const DenseTensor& max_grad, | ||
| const std::vector<int64_t>& axis, | ||
| bool keep_dim, | ||
| bool reduce_all, | ||
| DenseTensor* x_grad); | ||
| } // namespace phi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "paddle/phi/kernels/aminmax_kernel.h" | ||
|
|
||
| #include "paddle/phi/backends/all_context.h" | ||
| #include "paddle/phi/core/kernel_registry.h" | ||
| #include "paddle/phi/kernels/reduce_amax_kernel.h" | ||
| #include "paddle/phi/kernels/reduce_amin_kernel.h" | ||
|
|
||
| namespace phi { | ||
|
|
||
| template <typename T, typename Context> | ||
| void AMinMaxKernel(const Context& dev_ctx, | ||
| const DenseTensor& x, | ||
| const std::vector<int64_t>& dims, | ||
| bool keep_dim, | ||
| DenseTensor* min, | ||
| DenseTensor* max) { | ||
| bool reduce_all = recompute_reduce_all(x, dims); | ||
| AMinRawKernel<T>(dev_ctx, x, dims, keep_dim, reduce_all, min); | ||
| AMaxRawKernel<T>(dev_ctx, x, dims, keep_dim, reduce_all, max); | ||
| } | ||
|
|
||
| } // namespace phi | ||
|
|
||
| PD_REGISTER_KERNEL( | ||
| aminmax, CPU, ALL_LAYOUT, phi::AMinMaxKernel, float, double, int, int64_t) { | ||
| } | ||
|
|
||
| #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
| PD_REGISTER_KERNEL( | ||
| aminmax, GPU, ALL_LAYOUT, phi::AMinMaxKernel, float, double, int, int64_t) { | ||
| } | ||
| #endif | ||
|
|
||
| #if defined(PADDLE_WITH_XPU_KP) | ||
| PD_REGISTER_KERNEL(aminmax, KPS, ALL_LAYOUT, phi::AMinMaxKernel, float) {} | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "paddle/phi/core/dense_tensor.h" | ||
|
|
||
| namespace phi { | ||
|
|
||
| template <typename T, typename Context> | ||
| void AMinMaxKernel(const Context& dev_ctx, | ||
| const DenseTensor& x, | ||
| const std::vector<int64_t>& dims, | ||
| bool keep_dim, | ||
| DenseTensor* min, | ||
| DenseTensor* max); | ||
|
|
||
| } // namespace phi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,14 @@ | |
| amin_grad : GetReduceGradExpectedKernelType | ||
| manual_signature : [amin] | ||
|
|
||
| - op : aminmax | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个应该是一些不兼容历史问题的适配,这个需要改吗,不报错可以不改
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是否可以不改?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 错误,已通过验证不能去掉 |
||
| backward : aminmax_grad | ||
| inputs : | ||
| x : X | ||
| outputs : | ||
| {min : Min, max : Max} | ||
| manual_signature : [aminmax] | ||
|
|
||
| - op : anchor_generator | ||
| inputs: | ||
| input : Input | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1124,6 +1124,7 @@ def __dir__(self): | |
| 'min', | ||
| 'narrow', | ||
| 'amin', | ||
| 'aminmax', | ||
| 'any', | ||
| 'slice', | ||
| 'slice_scatter', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个打印个信息调试下吧,显示没覆盖到