-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[API-Compat] Add paddle.compat.min/max and new PHI kernel (min/max_with_index) #74547
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
Merged
zhangbo9674
merged 24 commits into
PaddlePaddle:develop
from
Enigmatisms:min_max_api_new
Aug 25, 2025
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
35707c5
[API-Compat] paddle.compat.split is added and tested
Enigmatisms 23c422d
[API-Compat] paddle.compat.split is rigorously tested
Enigmatisms 309b44a
[API-Compat] Make the forbid_keywords decorator transparent
Enigmatisms 2a33744
[API-Compat] Fixed decorator str input
Enigmatisms 11d9640
[API-Compat] More unittest & static graph check & updated decorator
Enigmatisms 6a58470
[API-Compat] Add paddle.compat.min/max and new PHI kernel (min/max_wi…
Enigmatisms 6255ed9
[API-Compat] Add compat.min/max EN doc
Enigmatisms 6fa8807
[WIP][API-Compat] Add dyna-graph unittests for min/max
Enigmatisms adb4c25
[WIP][API-Compat] Fixed CPU failure
Enigmatisms fd6adf0
[API-Compat] Correct min/max_with index gradient behavior
Enigmatisms 3081556
[API-Compat] XPU fix (attempt)
Enigmatisms cd8d6ae
[API-Compat] Updated ForbidKeywordsDecorator
Enigmatisms 085801e
some create api support more usage (#74494)
zhwesky2010 2864eb0
[API-Compat] Static Graph and CPU end debug
Enigmatisms 693ff52
[API-Compat] Resolved conflicts in decorator_utils.py
Enigmatisms f3d7353
[API-Compat] Added static graph min/max_with_index op check, simplifi…
Enigmatisms bfd5134
[API-Compat] min/max static graph op test and out tensor support
Enigmatisms fb8bba0
[API-Compat] Resolved merge conflicts.
Enigmatisms 47a08dc
[API-Compat] Fixed CPU static graph bugs
Enigmatisms 9300d17
[API-Compat] Resolved merged conflicts, add symbolic shape test.
Enigmatisms 17d848c
[API-Compat] Updated unittests
Enigmatisms 822e8d7
[API-Compat] Update version year
Enigmatisms 17f080e
[API-Compat] Fixed min/max out mechanism
Enigmatisms 0fbbb99
[API-Compat] Try adding even more unittests.
Enigmatisms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
paddle/phi/kernels/gpu/min_max_with_index_grad_kernel.cu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // 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/backends/gpu/gpu_context.h" | ||
| #include "paddle/phi/common/place.h" | ||
| #include "paddle/phi/core/kernel_registry.h" | ||
| #include "paddle/phi/core/utils/data_type.h" | ||
| #include "paddle/phi/kernels/funcs/gather_scatter_functor.h" | ||
| #include "paddle/phi/kernels/funcs/math_function.h" | ||
|
|
||
| namespace phi { | ||
|
|
||
| template <typename T> | ||
| using EnableIfInteger = | ||
| typename std::enable_if<std::is_integral<T>::value, int>::type; | ||
|
|
||
| template <typename T> | ||
| using EnableIfNonInteger = | ||
| typename std::enable_if<!std::is_integral<T>::value, int>::type; | ||
|
|
||
| // Here if keepdim=True, this will fallback to a simplified version of | ||
| // take_along_axis. However, if keepdim=False (by default), indices will | ||
| // not have equal rank will the input values (and values_grad), therefore | ||
| // needs an unsqueeze operation by shallow copying indices and Resize | ||
| #define DEFINE_WITH_INDEX_GRAD_KERNEL(OpType) \ | ||
| template <typename T, typename Context, EnableIfNonInteger<T> = 0> \ | ||
| void OpType##WithIndexGradKernel(const Context& dev_ctx, \ | ||
| const DenseTensor& x, \ | ||
| const DenseTensor& values, \ | ||
| const DenseTensor& indices, \ | ||
| const DenseTensor& values_grad, \ | ||
| const Scalar& dim, \ | ||
| bool keepdim, \ | ||
| DenseTensor* x_grad) { \ | ||
| x_grad->Resize(x.dims()); \ | ||
| dev_ctx.template Alloc<T>(x_grad); \ | ||
| if (x_grad->numel() == 0) { \ | ||
| return; \ | ||
| } \ | ||
| int64_t dim_val = dim.to<int64_t>(); \ | ||
| if (dim_val < 0) { \ | ||
| dim_val += x.dims().size(); \ | ||
| } \ | ||
| DenseTensor shallow_copied_inds(indices); \ | ||
| if (!keepdim) { \ | ||
| auto indices_dim = x.dims(); \ | ||
| indices_dim[dim_val] = 1; \ | ||
| shallow_copied_inds.Resize(indices_dim); \ | ||
| } \ | ||
| phi::funcs::SetConstant<Context, T> functor; \ | ||
| functor(dev_ctx, x_grad, static_cast<T>(0)); \ | ||
| phi::funcs::gpu_scatter_add_kernel<T, int64_t>( \ | ||
| *x_grad, dim_val, shallow_copied_inds, values_grad, true, dev_ctx); \ | ||
| } \ | ||
| template <typename T, typename Context, EnableIfInteger<T> = 0> \ | ||
| void OpType##WithIndexGradKernel(const Context& dev_ctx, \ | ||
| const DenseTensor& x, \ | ||
| const DenseTensor& values, \ | ||
| const DenseTensor& indices, \ | ||
| const DenseTensor& values_grad, \ | ||
| const Scalar& dim, \ | ||
| bool keepdim, \ | ||
| DenseTensor* x_grad) { \ | ||
| std::string dtype_name = phi::DataTypeToString(values.dtype()); \ | ||
| PADDLE_ENFORCE_EQ( \ | ||
| 0, \ | ||
| 1, \ | ||
| phi::errors::InvalidArgument( \ | ||
| "Integer type '%s' is not allowed to have stop_gradient=False.", \ | ||
| dtype_name.c_str())); \ | ||
| } | ||
|
|
||
| DEFINE_WITH_INDEX_GRAD_KERNEL(Max) | ||
| DEFINE_WITH_INDEX_GRAD_KERNEL(Min) | ||
|
|
||
| #undef DEFINE_WITH_INDEX_GRAD_KERNEL | ||
|
|
||
| } // namespace phi | ||
|
|
||
| PD_REGISTER_KERNEL(max_with_index_grad, | ||
| GPU, | ||
| ALL_LAYOUT, | ||
| phi::MaxWithIndexGradKernel, | ||
| float, | ||
| double, | ||
| uint8_t, | ||
| int, | ||
| int16_t, | ||
| int64_t, | ||
| phi::dtype::float16, | ||
| phi::dtype::bfloat16) {} | ||
|
|
||
| PD_REGISTER_KERNEL(min_with_index_grad, | ||
| GPU, | ||
| ALL_LAYOUT, | ||
| phi::MinWithIndexGradKernel, | ||
| float, | ||
| double, | ||
| uint8_t, | ||
| int, | ||
| int16_t, | ||
| int64_t, | ||
| phi::dtype::float16, | ||
| phi::dtype::bfloat16) {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.