-
Notifications
You must be signed in to change notification settings - Fork 4k
optimized qmoe code path for 1 token #27383
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
7 commits
Select commit
Hold shift + click to select a range
acabc51
add weight_index_indirect to nbitmm
guschmue 41701c8
optimized qmoe code pass for 1 token
guschmue 7e4b875
missing file
guschmue aa177a1
add missing file
guschmue 327763e
Merge branch 'main' into gs/wgpu-qmoe-opt
guschmue 89c648a
Update onnxruntime/contrib_ops/webgpu/moe/gate_1token.wgsl.template
guschmue 957e65f
Update onnxruntime/contrib_ops/webgpu/moe/final_mix.wgsl.template
guschmue 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
19 changes: 19 additions & 0 deletions
19
onnxruntime/contrib_ops/webgpu/moe/final_mix_1token.wgsl.template
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,19 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // in: fc2_outputs [used_by, inter_size] | ||
| // in: router_values [num_tokens, num_experts] | ||
| // in: indirect_experts | ||
| // out: output | ||
| // uniform: hidden_size, expert_idx | ||
|
|
||
| $MAIN { | ||
| let expert_idx = indirect_experts[uniforms.expert_idx]; | ||
| let steps = uniforms.hidden_size / workgroup_size_x; | ||
| let router_value = router_values[expert_idx]; | ||
| let offset = local_idx * steps; | ||
| for (var i = 0u; i < steps; i++) { | ||
| let weight = fc2_outputs[offset + i]; | ||
| output[offset + i] += router_value * weight; | ||
| } | ||
| } | ||
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
74 changes: 74 additions & 0 deletions
74
onnxruntime/contrib_ops/webgpu/moe/gate_1token.wgsl.template
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,74 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // | ||
| // MOE 1 token gate shader | ||
| // | ||
| // called with expert as local_idx | ||
| // input: router_logits | ||
| // output: topk_values | ||
| // output: indirect_experts | ||
|
|
||
| #param is_fp16 | ||
| #param k | ||
|
|
||
| const K: u32 = k; | ||
| #if is_fp16 | ||
| const MAX_FLOAT: f16 = 65504.0; | ||
| #else | ||
| const MAX_FLOAT: f32 = 3.4028234663852886e+38; | ||
| #endif | ||
|
|
||
| var<workgroup> shared_vals: array<router_logits_element_t, workgroup_size_x>; | ||
| var<workgroup> shared_idxs: array<u32, workgroup_size_x>; | ||
|
|
||
| $MAIN { | ||
| let row = workgroup_idx; | ||
| if (row >= uniforms.rows) { | ||
| return; | ||
| } | ||
| let cols = uniforms.cols; | ||
| let output_base = row * cols; | ||
|
|
||
| var max_val: router_logits_element_t = -MAX_FLOAT; | ||
| var max_idx: u32 = 0u; | ||
|
|
||
| if (local_idx < cols) { | ||
| max_val = router_logits[row * cols + local_idx]; | ||
| max_idx = local_idx; | ||
| } | ||
| shared_vals[local_idx] = max_val; | ||
| shared_idxs[local_idx] = max_idx; | ||
| topk_values[output_base + local_idx] = topk_values_value_t(0); | ||
| workgroupBarrier(); | ||
|
|
||
| // K is small, use a simple bubble sort | ||
| for (var i = 0u; i < workgroup_size_x - 1u; i++) { | ||
| for (var j = 0u; j < workgroup_size_x - 1u - i; j++) { | ||
| if (local_idx == j && local_idx < cols && (local_idx + 1u) < cols) { | ||
| // Compare adjacent elements and swap if needed (descending order) | ||
| if (shared_vals[local_idx] < shared_vals[local_idx + 1u]) { | ||
| let temp_val = shared_vals[local_idx]; | ||
| let temp_idx = shared_idxs[local_idx]; | ||
| shared_vals[local_idx] = shared_vals[local_idx + 1u]; | ||
| shared_idxs[local_idx] = shared_idxs[local_idx + 1u]; | ||
| shared_vals[local_idx + 1u] = temp_val; | ||
| shared_idxs[local_idx + 1u] = temp_idx; | ||
| } | ||
| } | ||
| workgroupBarrier(); | ||
| } | ||
| } | ||
| if (local_idx == 0u) { | ||
| // softmax | ||
| var sum : f32 = 0.0; | ||
| for (var i = 0u; i < K; i++) { | ||
| sum += exp(f32(shared_vals[i])); | ||
| } | ||
| for (var i = 0u; i < K; i++) { | ||
| let expert_idx = shared_idxs[i]; | ||
| topk_values[output_base + expert_idx] = topk_values_value_t(exp(f32(shared_vals[i])) / sum); | ||
| indirect_experts[i] = expert_idx; | ||
| } | ||
| } | ||
| } // MAIN |
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.
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.