-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Update MoE and qMoE spec #25619
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
Update MoE and qMoE spec #25619
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e7cb844
update moe spec
tianleiwu bd36de4
update doc
tianleiwu 1d70f69
format
tianleiwu 451814f
add swiglu limit
tianleiwu fa0224c
Merge branch 'main' into tlwu/moe_spec
tianleiwu 4a0d84f
CPU change from apsonawane
tianleiwu 03a6146
use moe_helper in CPU
tianleiwu 6a5871e
remove MoEQuantType
tianleiwu c4eb332
Fix build
tianleiwu 08c3114
Add swiglu parameters
tianleiwu b7de4a7
Merge branch 'main' into tlwu/moe_spec
tianleiwu edd065c
update doc
tianleiwu 1b72088
improve backward compatible
tianleiwu b1562dd
Revert "emsdk" change
tianleiwu 37abf5d
refacotring
tianleiwu 0635f11
Disable cpu qmoe test
tianleiwu 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "core/common/common.h" | ||
| #include "core/providers/common.h" | ||
| #include "core/framework/tensor_shape.h" | ||
| #include "core/util/shape_checker.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace contrib { | ||
|
|
||
| enum class MoEParallelType { | ||
| None = 0, | ||
| EP = 1, | ||
| TP = 2, | ||
| EPAndTP = 3, | ||
| }; | ||
|
|
||
| struct MoEParameters { | ||
| MoEParameters() {} | ||
| explicit MoEParameters(int64_t tensor_shards) : tensor_shards(tensor_shards) {} | ||
| int64_t num_rows; | ||
| int64_t num_experts; | ||
| int64_t local_num_experts; | ||
| int64_t hidden_size; | ||
| int64_t inter_size; | ||
|
|
||
| MoEParallelType parallel_type; | ||
| int64_t tensor_shards{1}; | ||
| }; | ||
|
|
||
| namespace moe_helper { | ||
|
|
||
| template <typename Tensor> | ||
| Status CheckInputs(MoEParameters& parameters, | ||
| const Tensor* input, // required | ||
| const Tensor* router_probs, // required | ||
| const Tensor* fc1_experts_weights, // required | ||
| const Tensor* fc1_experts_bias, // optional | ||
| const Tensor* fc1_experts_scales, // required for qMoE; NULL for MOE | ||
| const Tensor* fc2_experts_weights, // required | ||
| const Tensor* fc2_experts_bias, // optional | ||
| const Tensor* fc2_experts_scales, // required for qMoE; NULL for MOE | ||
| const Tensor* fc3_experts_weights, // optional | ||
| const Tensor* fc3_experts_bias, // optional | ||
| const Tensor* fc3_experts_scales, // required for qMoE; NULL for MOE | ||
| const int pack_size, // number of weights packed together (like 2 for uint4 packed to uint8) | ||
| const bool is_fused_swiglu) { | ||
| ASSERT_TENSOR_2D_OR_3D(input); | ||
| ASSERT_TENSOR_3D(fc1_experts_weights); | ||
| ASSERT_TENSOR_3D(fc2_experts_weights); | ||
| ASSERT_TENSOR_2D(router_probs); | ||
| ASSERT_TENSOR_2D(router_probs); | ||
|
|
||
| const auto& input_dims = input->Shape().GetDims(); | ||
| const auto& router_probs_dims = router_probs->Shape().GetDims(); | ||
| const auto& fc1_experts_weights_dims = fc1_experts_weights->Shape().GetDims(); | ||
| const auto& fc2_experts_weights_dims = fc2_experts_weights->Shape().GetDims(); | ||
|
|
||
| int64_t num_rows = input_dims.size() == 2 ? input_dims[0] : input_dims[0] * input_dims[1]; | ||
| int64_t hidden_size = input_dims[input_dims.size() - 1]; | ||
| int64_t local_num_experts = fc1_experts_weights_dims[0]; | ||
| int64_t num_experts = router_probs_dims[1]; | ||
| int64_t inter_size = (fc2_experts_weights_dims[1] * fc2_experts_weights_dims[2] * pack_size) / hidden_size; | ||
| const bool legacy_shape = hidden_size != inter_size && fc2_experts_weights_dims[1] == inter_size; | ||
|
|
||
| // Fused swiglu doubles the output dimension of FC1 since it fused two GEMMs into one. | ||
| const int fc1_inter_size = is_fused_swiglu ? 2 * inter_size : inter_size; | ||
|
|
||
| if (legacy_shape) { | ||
| // legacy shape does not match the memory layout. This is for backward compatible | ||
| CHECK_TENSOR_SHAPE(fc1_experts_weights, num_experts, hidden_size, fc1_inter_size / pack_size); | ||
| CHECK_TENSOR_SHAPE(fc2_experts_weights, num_experts, inter_size, hidden_size / pack_size); | ||
| CHECK_TENSOR_SHAPE(fc3_experts_weights, num_experts, hidden_size, inter_size / pack_size); | ||
| } else { | ||
| CHECK_TENSOR_SHAPE(fc1_experts_weights, num_experts, fc1_inter_size, hidden_size / pack_size); | ||
| CHECK_TENSOR_SHAPE(fc2_experts_weights, num_experts, hidden_size, inter_size / pack_size); | ||
| CHECK_TENSOR_SHAPE(fc3_experts_weights, num_experts, inter_size, hidden_size / pack_size); | ||
| } | ||
|
|
||
| CHECK_TENSOR_SHAPE(router_probs, num_rows, num_experts); | ||
|
|
||
| CHECK_TENSOR_SHAPE(fc1_experts_bias, num_experts, fc1_inter_size); | ||
| CHECK_TENSOR_SHAPE(fc2_experts_bias, num_experts, hidden_size); | ||
| CHECK_TENSOR_SHAPE(fc3_experts_bias, num_experts, inter_size); | ||
|
|
||
| CHECK_TENSOR_SHAPE(fc1_experts_scales, num_experts, fc1_inter_size); | ||
| CHECK_TENSOR_SHAPE(fc2_experts_scales, num_experts, hidden_size); | ||
| CHECK_TENSOR_SHAPE(fc3_experts_scales, num_experts, inter_size); | ||
|
|
||
| if (fc3_experts_weights == nullptr) { | ||
| ORT_ENFORCE(fc3_experts_bias == nullptr && fc3_experts_scales == nullptr); | ||
| } else { // fc3 exists | ||
| ORT_ENFORCE(fc1_experts_scales == nullptr || fc3_experts_scales != nullptr); // MOE no scale, or qMOE need scales | ||
tianleiwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| parameters.num_rows = num_rows; | ||
| parameters.num_experts = num_experts; | ||
| parameters.local_num_experts = local_num_experts; | ||
| parameters.hidden_size = hidden_size; | ||
| parameters.inter_size = inter_size; | ||
| if (num_experts == local_num_experts) { | ||
| if (parameters.tensor_shards == 1) { | ||
| parameters.parallel_type = MoEParallelType::None; | ||
| } else { | ||
| parameters.parallel_type = MoEParallelType::TP; | ||
| } | ||
| } else if (num_experts > local_num_experts) { | ||
| if (parameters.tensor_shards == 1) { | ||
| parameters.parallel_type = MoEParallelType::EP; | ||
| } else { | ||
| parameters.parallel_type = MoEParallelType::EPAndTP; | ||
| } | ||
| } else { | ||
| return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, | ||
| "num_experts must be greater than or equal to local_num_experts, got ", num_experts, | ||
| " and ", local_num_experts); | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| } // namespace moe_helper | ||
| } // namespace contrib | ||
| } // 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.
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.