-
Notifications
You must be signed in to change notification settings - Fork 82
issue/563 Add metax support for topkrouter #678
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
Open
Zhao-Shi-jie
wants to merge
2
commits into
InfiniTensor:main
Choose a base branch
from
Zhao-Shi-jie:dev_topkrouter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #ifndef __TOPKROUTER_METAX_H__ | ||
| #define __TOPKROUTER_METAX_H__ | ||
|
|
||
| #include "../topkrouter.h" | ||
|
|
||
| DESCRIPTOR(metax) | ||
|
|
||
| #endif |
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,93 @@ | ||
| #include "../../../devices/metax/metax_common.h" | ||
| #include "../../../devices/metax/metax_kernel_common.h" | ||
| #include "../cuda/kernel.cuh" | ||
| #include "topkrouter_metax.h" | ||
| #include <cub/block/block_reduce.cuh> | ||
|
|
||
| namespace op::topkrouter::metax { | ||
|
|
||
| struct Descriptor::Opaque { | ||
| std::shared_ptr<device::metax::Handle::Internal> internal; | ||
| }; | ||
|
|
||
| Descriptor::~Descriptor() { | ||
| delete _opaque; | ||
| } | ||
|
|
||
| infiniStatus_t Descriptor::create( | ||
| infiniopHandle_t handle, | ||
| Descriptor **desc_ptr, | ||
| infiniopTensorDescriptor_t x_desc, | ||
| infiniopTensorDescriptor_t correction_bias_desc) { | ||
| auto result = TopkrouterInfo::create(x_desc); | ||
| CHECK_RESULT(result); | ||
| auto info = result.take(); | ||
|
|
||
| if (info.x_strides[1] != 1) { | ||
| return INFINI_STATUS_BAD_TENSOR_STRIDES; | ||
| } | ||
|
|
||
| *desc_ptr = new Descriptor( | ||
| new Opaque{reinterpret_cast<device::metax::Handle *>(handle)->internal()}, | ||
| std::move(info), | ||
| 0, | ||
| handle->device, handle->device_id); | ||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| namespace { | ||
|
|
||
| template <int BLOCK_SIZE = 128> | ||
| infiniStatus_t launch_topkrouter(float *d_values_out, int *d_indices_out, const void *d_input, const float *d_correction_bias, | ||
| const float routed_scaling_factor, const size_t N, const size_t width, const size_t topk, infiniDtype_t xtype, | ||
| hcStream_t stream) { | ||
| const int block_threads = BLOCK_SIZE; | ||
| dim3 blocks(N); | ||
| dim3 threads(block_threads); | ||
|
|
||
| if (xtype == INFINI_DTYPE_F32) { | ||
| topkrouter_kernel<float, BLOCK_SIZE><<<blocks, threads, 0, stream>>>(d_values_out, d_indices_out, (float *)d_input, d_correction_bias, routed_scaling_factor, N, width, topk); | ||
| } else if (xtype == INFINI_DTYPE_F16) { | ||
| topkrouter_kernel<half, BLOCK_SIZE><<<blocks, threads, 0, stream>>>(d_values_out, d_indices_out, (half *)d_input, d_correction_bias, routed_scaling_factor, N, width, topk); | ||
| } else if (xtype == INFINI_DTYPE_BF16) { | ||
| topkrouter_kernel<cuda_bfloat16, BLOCK_SIZE><<<blocks, threads, 0, stream>>>(d_values_out, d_indices_out, (cuda_bfloat16 *)d_input, d_correction_bias, routed_scaling_factor, N, width, topk); | ||
| } else { | ||
| return INFINI_STATUS_BAD_TENSOR_DTYPE; | ||
| } | ||
|
|
||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| }; // namespace | ||
|
|
||
| infiniStatus_t Descriptor::calculate( | ||
| void *workspace, | ||
| size_t workspace_size, | ||
| float *values, | ||
| int *indices, | ||
| const void *x, | ||
| const float *correction_bias, | ||
| const float routed_scaling_factor, | ||
| const size_t topk, | ||
| void *stream) const { | ||
| if (workspace_size < _workspace_size) { | ||
| return INFINI_STATUS_INSUFFICIENT_WORKSPACE; | ||
| } | ||
|
|
||
| size_t N = _info.N; | ||
| size_t width = _info.width; // 256 | ||
|
|
||
| // size_t n_routed_experts = 256; | ||
| // size_t n_group = 8; | ||
| // size_t topk_group = 4; | ||
| auto cuda_stream = reinterpret_cast<hcStream_t>(stream); | ||
|
|
||
| if (256 == width) { | ||
| launch_topkrouter<256>(values, indices, x, correction_bias, routed_scaling_factor, N, width, topk, _info.xtype, cuda_stream); | ||
| } else { | ||
| return INFINI_STATUS_BAD_PARAM; | ||
| } | ||
|
|
||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
| } // namespace op::topkrouter::metax |
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
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.
请参考infiniop/ops/causal_softmax/cuda/kernel.cuh
通用cuda kernel头文件不能带include。在使用通用kernel时先include所需头文件(如cub),再include kernel.cuh
修改后请提供英伟达和沐曦两个平台正确编译运行截图,谢谢