-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Implement the Concat CUDA kernel #1333
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 all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "core/providers/cuda/cu_inc/common.cuh" | ||
| #include "core/providers/cuda/cuda_common.h" | ||
| #include "concat_impl.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace cuda { | ||
|
|
||
| template <typename T> | ||
| __global__ void _ConcatKernel(const fast_divmod block_size_including_axis_dim_div, | ||
| const fast_divmod block_size_inside_axis_dim_div, | ||
| const int64_t* concat_sizes, | ||
| const int64_t* concat_sizes_range, | ||
| const int64_t* axis_dimension_input_output_mapping, | ||
| const int num_inputs, | ||
| T* output_data, | ||
| const void** input_ptr, | ||
| const CUDA_LONG N) { | ||
| CALCULATE_ELEMENTWISE_INDEX_OR_EXIT(id, N); | ||
| CUDA_LONG input_pos = 0; | ||
|
|
||
| int outter_block_index = 0; | ||
| int block_index = 0; | ||
| int offset = 0; | ||
|
|
||
| block_size_including_axis_dim_div.divmod(id, outter_block_index, offset); | ||
| block_size_inside_axis_dim_div.divmod(offset, block_index, offset); | ||
|
|
||
| int input_index = axis_dimension_input_output_mapping[block_index]; | ||
| int64_t range_left = (input_index == 0) ? 0 : concat_sizes_range[input_index - 1]; | ||
| int block_offset = block_index - range_left; | ||
|
|
||
| input_pos = (outter_block_index * concat_sizes[input_index] + block_offset) * | ||
| block_size_inside_axis_dim_div.d_ + | ||
| offset; | ||
|
|
||
| output_data[id] = reinterpret_cast<const T*>(input_ptr[input_index])[input_pos]; | ||
| } | ||
|
|
||
| Status ConcatImpl(const size_t element_bytes, | ||
| const int block_size_including_axis_dim, | ||
| const int block_size_inside_axis_dim, | ||
| const int64_t* concat_sizes, | ||
| const int64_t* concat_sizes_range, | ||
| const int64_t* axis_dimension_input_output_mapping, | ||
| const int num_inputs, | ||
| void* output_data, | ||
| const void** input_ptr, | ||
| const size_t N) { | ||
| int blocksPerGrid = (int)(ceil(static_cast<float>(N) / GridDim::maxThreadsPerBlock)); | ||
|
|
||
| fast_divmod block_size_including_axis_dim_div = fast_divmod(block_size_including_axis_dim); | ||
| fast_divmod block_size_inside_axis_dim_div = fast_divmod(block_size_inside_axis_dim); | ||
|
|
||
| switch (element_bytes) { | ||
| case sizeof(int8_t): | ||
| _ConcatKernel<<<blocksPerGrid, GridDim::maxThreadsPerBlock, 0>>>( | ||
| block_size_including_axis_dim_div, block_size_inside_axis_dim_div, | ||
| concat_sizes, concat_sizes_range, axis_dimension_input_output_mapping, | ||
| num_inputs, | ||
| reinterpret_cast<int8_t*>(output_data), | ||
| input_ptr, | ||
| (CUDA_LONG)N); | ||
| break; | ||
| case sizeof(int16_t): | ||
| _ConcatKernel<<<blocksPerGrid, GridDim::maxThreadsPerBlock, 0>>>( | ||
| block_size_including_axis_dim_div, block_size_inside_axis_dim_div, | ||
| concat_sizes, concat_sizes_range, axis_dimension_input_output_mapping, | ||
| num_inputs, | ||
| reinterpret_cast<int16_t*>(output_data), | ||
| input_ptr, | ||
| (CUDA_LONG)N); | ||
| break; | ||
| case sizeof(int32_t): | ||
| _ConcatKernel<<<blocksPerGrid, GridDim::maxThreadsPerBlock, 0>>>( | ||
| block_size_including_axis_dim_div, block_size_inside_axis_dim_div, | ||
| concat_sizes, concat_sizes_range, axis_dimension_input_output_mapping, | ||
| num_inputs, | ||
| reinterpret_cast<int32_t*>(output_data), | ||
| input_ptr, | ||
| (CUDA_LONG)N); | ||
| break; | ||
| case sizeof(int64_t): | ||
| _ConcatKernel<<<blocksPerGrid, GridDim::maxThreadsPerBlock, 0>>>( | ||
| block_size_including_axis_dim_div, block_size_inside_axis_dim_div, | ||
| concat_sizes, concat_sizes_range, axis_dimension_input_output_mapping, | ||
| num_inputs, | ||
| reinterpret_cast<int64_t*>(output_data), | ||
| input_ptr, | ||
| (CUDA_LONG)N); | ||
| break; | ||
| default: | ||
| return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Type not supported for Concat operator"); | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| } // namespace cuda | ||
| } // namespace onnxruntime | ||
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,24 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
| #include <stdint.h> | ||
| #include "core/providers/cuda/shared_inc/cuda_utils.h" | ||
| #include "core/common/common.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace cuda { | ||
|
|
||
| Status ConcatImpl(const size_t element_bytes, | ||
| const int block_size_including_axis_dim, | ||
| const int block_size_inside_axis_dim, | ||
| const int64_t* concat_sizes, | ||
| const int64_t* concat_sizes_range, | ||
| const int64_t* axis_dimension_input_output_mapping, | ||
| const int num_inputs, | ||
| void* output_data, | ||
| const void** input_ptr, | ||
| const size_t N); | ||
|
|
||
| } // namespace cuda | ||
| } // namespace onnxruntime |
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
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.
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.
It seem like every thread writes into output_data[id] only 1 time -- so 1 thread implies 1 output index is populated. If the output tensor is large (i.e. larger than number of threads in the grid), how are the remaining indexes being populated? #Resolved
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.
there's also a blocksPerGrid, check line 52
In reply to: 299716784 [](ancestors = 299716784)