-
Notifications
You must be signed in to change notification settings - Fork 13
Restructure MoE and Add MoE prepare input kernels #29
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
kareemshaik80
wants to merge
9
commits into
sgl-project:main
Choose a base branch
from
kareemshaik80:main
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.
+631
−6
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bab489e
Restructure MoE and add prepare inputs/meta kernel
kareemshaik80 d5e78ac
fix minor issues
kareemshaik80 651c0f6
Add tests
kareemshaik80 328d63a
Add shuffle_rows Kernel
kareemshaik80 efb105f
register shuffle_rows
kareemshaik80 d849b61
Enable Build and Add apply_shuffle_mul_sum kernel
kareemshaik80 f2f1577
functional
kareemshaik80 33fe2ed
cleanup
kareemshaik80 8944fbc
cleanup1
kareemshaik80 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,216 @@ | ||
| #include <ATen/ATen.h> | ||
| #include <ATen/OpMathType.h> | ||
| #include <ATen/Parallel.h> | ||
| #include <c10/xpu/XPUStream.h> | ||
| #include <torch/all.h> | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <iostream> | ||
| #include <sycl/sycl.hpp> | ||
| #include <vector> | ||
|
|
||
| #include "MemoryAccess.h" | ||
| #include "SYCLHelpers.h" | ||
| #include "Utils.h" | ||
|
|
||
| constexpr uint64_t THREADS_PER_EXPERT = 512; | ||
| constexpr int block_size = 128 | ||
|
|
||
| void compute_problem_sizes_sycl( | ||
| sycl::queue& q, | ||
| const int* topk_ids, | ||
| int32_t* problem_sizes1, | ||
| int32_t* problem_sizes2, | ||
| int32_t* atomic_buffer, | ||
| const int64_t num_experts, | ||
| const int64_t topk_length, | ||
| const int64_t n, | ||
| const int64_t k) { | ||
|
|
||
| sycl::range<1> global_range{ num_experts * topk_length }; | ||
| sycl::range<1> local_range{ topk_length }; | ||
|
|
||
| // Launch kernel | ||
| q.submit([&](sycl::handler& cgh) { | ||
| cgh.parallel_for( | ||
| sycl::nd_range<1>(global_range, local_range), | ||
| [=](sycl::nd_item<1> item) { | ||
| int expert_id = item.get_group(0); | ||
| int occurrences = 0; | ||
| size_t local_id = item.get_local_id(0); | ||
| for (int i = local_id; i < topk_length; i += THREADS_PER_EXPERT) { | ||
| occurrences += (topk_ids[i] == expert_id); | ||
| } | ||
|
|
||
| atomic_ref< | ||
| int32_t, | ||
| sycl::memory_order::relaxed, | ||
| sycl::memory_scope::work_group, | ||
| sycl::access::address_space::generic_space | ||
| > atomic_counter(atomic_buffer[expert_id]); | ||
|
|
||
| atomic_counter.fetch_add(occurrences); | ||
|
|
||
| if (local_id == 0) { | ||
| int final_occurrences = atomic_buffer[expert_id]; | ||
| problem_sizes1[expert_id * 3] = final_occurrences; | ||
| problem_sizes1[expert_id * 3 + 1] = static_cast<int32_t>(2 * n); | ||
| problem_sizes1[expert_id * 3 + 2] = static_cast<int32_t>(k); | ||
| problem_sizes2[expert_id * 3] = final_occurrences; | ||
| problem_sizes2[expert_id * 3 + 1] = static_cast<int32_t>(k); | ||
| problem_sizes2[expert_id * 3 + 2] = static_cast<int32_t>(n); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| void compute_expert_offsets_sycl( | ||
| sycl::queue& q, | ||
| const int32_t* problem_sizes1, | ||
| int32_t* expert_offsets, | ||
| int32_t* atomic_buffer, | ||
| const int64_t num_experts) { | ||
|
|
||
| // Launch kernel | ||
| q.submit([&](sycl::handler& cgh) { | ||
| cgh.parallel_for( | ||
| sycl::nd_range<1>(1, 1), | ||
| [=](sycl::nd_item<1> item) { | ||
| int32_t tot_offset = 0; | ||
| expert_offsets[0] = 0; | ||
| for (int i = 0; i < num_experts; ++i) { | ||
| atomic_buffer[i] = tot_offset; | ||
| tot_offset += problem_sizes1[i * 3]; | ||
| expert_offsets[i + 1] = tot_offset; | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| void compute_expert_blockscale_offsets_sycl( | ||
| sycl::queue& q, | ||
| const int32_t* problem_sizes1, | ||
| int32_t* expert_offsets, | ||
| int32_t* blockscale_offsets, | ||
| int32_t* atomic_buffer, | ||
| const int64_t num_experts) { | ||
|
|
||
| // Launch kernel | ||
| q.submit([&](sycl::handler& cgh) { | ||
| cgh.parallel_for( | ||
| sycl::nd_range<1>(1, 1), | ||
| [=](sycl::nd_item<1> item) { | ||
| int32_t tot_offset = 0; | ||
| int32_t tot_rounded_offset = 0; | ||
| expert_offsets[0] = 0; | ||
| blockscale_offsets[0] = 0; | ||
| for (int i = 0; i < num_experts; ++i) { | ||
| atomic_buffer[i] = tot_offset; | ||
| int num_tokens = problem_sizes1[i * 3]; | ||
| int rounded_num_tokens = (num_tokens + (block_size - 1)) / block_size * block_size; | ||
| tot_offset += num_tokens; | ||
| tot_rounded_offset += rounded_num_tokens; | ||
| expert_offsets[i + 1] = tot_offset; | ||
| blockscale_offsets[i + 1] = tot_rounded_offset; | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| void compute_arg_sorts_sycl( | ||
| sycl::queue& q, | ||
| const int32_t* topk_ids, | ||
| int32_t* input_permutation, | ||
| int32_t* output_permutation, | ||
| int32_t* atomic_buffer, | ||
| const int64_t topk_length, | ||
| const int64_t topk) { | ||
|
|
||
| sycl::range<1> global_range{ num_experts * topk_length }; | ||
| sycl::range<1> local_range{ topk_length }; | ||
|
|
||
| // Launch kernel | ||
| q.submit([&](sycl::handler& cgh) { | ||
| cgh.parallel_for( | ||
| sycl::nd_range<1>(global_range, local_range), | ||
| [=](sycl::nd_item<1> item) { | ||
| int expert_id = item.get_group(0); | ||
|
|
||
| atomic_ref< | ||
| int32_t, | ||
| sycl::memory_order::relaxed, | ||
| sycl::memory_scope::work_group, | ||
| sycl::access::address_space::generic_space | ||
| > atomic_counter(atomic_buffer[expert_id]); | ||
|
|
||
| for (int i = threadIdx.x; i < topk_length; i += THREADS_PER_EXPERT) { | ||
| if (topk_ids[i] == expert_id) { | ||
| int start = atomic_counter.fetch_add(occurrences); | ||
| input_permutation[start] = i / topk; | ||
| output_permutation[i] = start; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| void prepare_moe_input( | ||
| const torch::Tensor& topk_ids, | ||
| torch::Tensor& expert_offsets, | ||
| const std::optional<torch::Tensor>& blockscale_offsets, | ||
| torch::Tensor& problem_sizes1, | ||
| torch::Tensor& problem_sizes2, | ||
| torch::Tensor& input_permutation, | ||
| torch::Tensor& output_permutation, | ||
| const int64_t num_experts, | ||
| const int64_t n, | ||
| const int64_t k) { | ||
| TORCH_CHECK(topk_ids.dtype() == torch::kInt32); | ||
| auto stream = at::xpu::getCurrentXPUStream(); | ||
| auto queue = stream.queue(); | ||
|
|
||
| auto options_int32 = torch::TensorOptions().dtype(torch::kInt32).device(topk_ids.device()); | ||
| torch::Tensor atomic_buffer = torch::zeros(num_experts, options_int32); | ||
|
|
||
| uint32_t num_threads = static_cast<uint32_t>(min(THREADS_PER_EXPERT, topk_ids.numel())); | ||
| uint32_t num_blocks = static_cast<uint32_t>(num_experts); | ||
|
|
||
| compute_problem_sizes_sycl( | ||
| queue, | ||
| static_cast<const int32_t*>(topk_ids.data_ptr()), | ||
| static_cast<int32_t*>(problem_sizes1.data_ptr()), | ||
| static_cast<int32_t*>(problem_sizes2.data_ptr()), | ||
| static_cast<int32_t*>(atomic_buffer.data_ptr()), | ||
| num_experts, | ||
| topk_ids.numel(), | ||
| n, | ||
| k); | ||
|
|
||
| if (blockscale_offsets.has_value()) { | ||
| compute_expert_blockscale_offsets_sycl( | ||
| static_cast<const int32_t*>(problem_sizes1.data_ptr()), | ||
| static_cast<int32_t*>(expert_offsets.data_ptr()), | ||
| static_cast<int32_t*>(blockscale_offsets.value().data_ptr()), | ||
| static_cast<int32_t*>(atomic_buffer.data_ptr()), | ||
| num_experts); | ||
| } else { | ||
| compute_expert_offsets_sycl( | ||
| queue, | ||
| static_cast<int32_t*>(problem_sizes1), | ||
| static_cast<int32_t*>(expert_offsets), | ||
| static_cast<int32_t*>(atomic_buffer), | ||
| num_experts); | ||
| } | ||
|
|
||
| compute_arg_sorts( | ||
| queue, | ||
| static_cast<const int32_t*>(topk_ids.data_ptr()), | ||
| static_cast<int32_t*>(input_permutation.data_ptr()), | ||
| static_cast<int32_t*>(output_permutation.data_ptr()), | ||
| static_cast<int32_t*>(atomic_buffer.data_ptr()), | ||
| topk_ids.numel(), | ||
| topk_ids.size(1)); | ||
|
|
||
| return; | ||
| } |
File renamed without changes.
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.
activations not only serve for MoE, better leave it unchanged.
Beside I'd prefer only put customized cutlass code under
src/sycl/kernels/, and leave pure SYCL code outside