- Notifications
You must be signed in to change notification settings - Fork 5.9k
[slice]add IndexPutWithSortKernel in index_elementwise_get_grad slice… #74344
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
Merged
Changes from 1 commit
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // 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/funcs/radix_sort.h" | ||
| #include "paddle/phi/common/memory_utils.h" | ||
| | ||
| namespace phi { | ||
| namespace funcs { | ||
| | ||
| namespace { | ||
| | ||
| template <typename T> | ||
| struct CudaType { | ||
| using type = T; | ||
| }; | ||
| | ||
| template <> | ||
| struct CudaType<int64_t> { | ||
| using type = long long; // NOLINT | ||
| }; | ||
| | ||
| #define PADDLE_CUB_WRAPPER(func, ...) \ | ||
| do { \ | ||
| size_t temp_storage_bytes = 0; \ | ||
| func(nullptr, temp_storage_bytes, __VA_ARGS__); \ | ||
| auto temp_storage = \ | ||
| phi::memory_utils::Alloc(dev_ctx.GetPlace(), temp_storage_bytes); \ | ||
| func(temp_storage->ptr(), temp_storage_bytes, __VA_ARGS__); \ | ||
| } while (0) | ||
| | ||
| } // namespace | ||
| | ||
| template <typename key_t, int value_size> | ||
| void RadixSortPairsImpl(const phi::GPUContext& dev_ctx, | ||
| const key_t* keys_in, | ||
| key_t* keys_out, | ||
| const OpaqueTypeRadix<value_size>* values_in, | ||
| OpaqueTypeRadix<value_size>* values_out, | ||
| int64_t n, | ||
| bool descending, | ||
| int64_t begin_bit, | ||
| int64_t end_bit) { | ||
| PADDLE_ENFORCE_LE( | ||
| n, | ||
| std::numeric_limits<int>::max(), | ||
| phi::errors::InvalidArgument( | ||
| "CUB sort does not support sorting more than INT_MAX elements")); | ||
| | ||
| using key_t_ = typename CudaType<key_t>::type; | ||
| | ||
| phi::Allocator::AllocationPtr keys_out_owner; | ||
| if (keys_out == nullptr) { | ||
| keys_out_owner = | ||
| phi::memory_utils::Alloc(dev_ctx.GetPlace(), n * sizeof(key_t)); | ||
| keys_out = reinterpret_cast<key_t*>(keys_out_owner->ptr()); | ||
| } | ||
| | ||
| const key_t_* keys_in_ = reinterpret_cast<const key_t_*>(keys_in); | ||
| key_t_* keys_out_ = reinterpret_cast<key_t_*>(keys_out); | ||
| | ||
| if (descending) { | ||
| PADDLE_CUB_WRAPPER(cub::DeviceRadixSort::SortPairsDescending, | ||
| keys_in_, | ||
| keys_out_, | ||
| values_in, | ||
| values_out, | ||
| static_cast<int>(n), | ||
| begin_bit, | ||
| end_bit, | ||
| dev_ctx.stream()); | ||
| } else { | ||
| PADDLE_CUB_WRAPPER(cub::DeviceRadixSort::SortPairs, | ||
| keys_in_, | ||
| keys_out_, | ||
| values_in, | ||
| values_out, | ||
| static_cast<int>(n), | ||
| begin_bit, | ||
| end_bit, | ||
| dev_ctx.stream()); | ||
| } | ||
| } | ||
| | ||
| #define INSTANTIATE_SORT_PAIRS(key_t, value_size) \ | ||
| template void RadixSortPairsImpl<key_t, value_size>( \ | ||
| const phi::GPUContext&, \ | ||
| const key_t*, \ | ||
| key_t*, \ | ||
| const OpaqueTypeRadix<value_size>*, \ | ||
| OpaqueTypeRadix<value_size>*, \ | ||
| int64_t, \ | ||
| bool, \ | ||
| int64_t, \ | ||
| int64_t); | ||
| | ||
| INSTANTIATE_SORT_PAIRS(int32_t, 1) | ||
| INSTANTIATE_SORT_PAIRS(int32_t, 2) | ||
| INSTANTIATE_SORT_PAIRS(int32_t, 4) | ||
| INSTANTIATE_SORT_PAIRS(int64_t, 1) | ||
| INSTANTIATE_SORT_PAIRS(int64_t, 2) | ||
| INSTANTIATE_SORT_PAIRS(int64_t, 4) | ||
| INSTANTIATE_SORT_PAIRS(int32_t, 8) | ||
| INSTANTIATE_SORT_PAIRS(int64_t, 8) | ||
| | ||
| } // namespace funcs | ||
| } // namespace phi |
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,80 @@ | ||
| // 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 <cub/cub.cuh> | ||
| #include "paddle/phi/backends/gpu/gpu_context.h" | ||
| #include "paddle/phi/core/dense_tensor.h" | ||
| | ||
| namespace phi { | ||
| namespace funcs { | ||
| | ||
| template <int kValueSize> | ||
| struct OpaqueTypeRadix { | ||
| uint8_t data[kValueSize]; | ||
| __device__ __host__ OpaqueTypeRadix() = default; | ||
| }; | ||
| | ||
| template <typename key_t, int kValueSize> | ||
| void RadixSortPairsImpl(const phi::GPUContext& dev_ctx, | ||
| const key_t* keys_in, | ||
| key_t* keys_out, | ||
| const OpaqueTypeRadix<kValueSize>* values_in, | ||
| OpaqueTypeRadix<kValueSize>* values_out, | ||
| int64_t n, | ||
| bool descending = false, | ||
| int64_t begin_bit = 0, | ||
| int64_t end_bit = sizeof(key_t) * 8); | ||
| | ||
| template <typename key_t, typename value_t> | ||
| void RadixSortPairs(const phi::GPUContext& dev_ctx, | ||
| const key_t* keys_in, | ||
| key_t* keys_out, | ||
| const value_t* values_in, | ||
| value_t* values_out, | ||
| int64_t n, | ||
| bool descending = false, | ||
| int64_t begin_bit = 0, | ||
| int64_t end_bit = sizeof(key_t) * 8) { | ||
| PADDLE_ENFORCE_EQ( | ||
| std::is_trivially_copyable<value_t>::value, | ||
| true, | ||
| phi::errors::InvalidArgument( | ||
| "RadixSortPairs value type must be trivially copyable")); | ||
| | ||
| using opaque_t = OpaqueTypeRadix<sizeof(value_t)>; | ||
| PADDLE_ENFORCE_EQ( | ||
| sizeof(value_t) <= 8 && (sizeof(value_t) & (sizeof(value_t) - 1)) == 0, | ||
| true, | ||
| phi::errors::InvalidArgument( | ||
| "Unsupported value_t size (must be 1, 2, 4, or 8 bytes)")); | ||
| PADDLE_ENFORCE_EQ( | ||
| sizeof(value_t), | ||
| alignof(value_t), | ||
| phi::errors::InvalidArgument("Expected value_t to be size-aligned")); | ||
| | ||
| RadixSortPairsImpl<key_t, sizeof(value_t)>( | ||
| dev_ctx, | ||
| keys_in, | ||
| keys_out, | ||
| reinterpret_cast<const opaque_t*>(values_in), | ||
| reinterpret_cast<opaque_t*>(values_out), | ||
| n, | ||
| descending, | ||
| begin_bit, | ||
| end_bit); | ||
| } | ||
| | ||
| } // namespace funcs | ||
| } // namespace phi |
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.
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.
is_combined表示什么含义?加些注释说明
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.
is_combined用来区分是普通索引还是组合索引,如果仅有一个普通索引反向时会采用性能更好的IndexPutWithSortKernel。新增了注释。