-
Notifications
You must be signed in to change notification settings - Fork 268
Add super kernel in moe #1877
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
NNUCJ
wants to merge
1
commit into
vllm-project:v0.9.1-dev
Choose a base branch
from
NNUCJ:sk_model_091
base: v0.9.1-dev
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.
+111
−74
Open
Add super kernel in moe #1877
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -30,7 +30,8 @@ | |
from vllm_ascend.ops.fused_moe import select_experts | ||
from vllm_ascend.utils import (ACL_FORMAT_FRACTAL_NZ, AscendSocVersion, | ||
dispose_tensor, get_ascend_soc_version, | ||
npu_stream_switch, npu_wait_tensor) | ||
npu_stream_switch, npu_wait_tensor, | ||
super_kernel) | ||
|
||
CHUNK_SIZE: int = ascend_envs.VLLM_ASCEND_FUSED_MOE_MC2_CHUNK_SIZE | ||
|
||
|
@@ -853,77 +854,84 @@ def apply( | |
shared_experts: Optional[Any] = None, | ||
quantized_x_for_share: Optional[Any] = None, | ||
dynamic_scale_for_share: Optional[Any] = None, | ||
prefix: str = "", | ||
enable_super_kernel: bool = False, | ||
**kwargs, | ||
) -> torch.Tensor: | ||
assert router_logits.shape[ | ||
1] == global_num_experts, "Number of global experts mismatch" | ||
|
||
# NOTE: now npu_moe_gating_top_k can only support `group_count=256` pattern | ||
if global_num_experts == 256: | ||
topk_weights, topk_ids, _ = torch_npu.npu_moe_gating_top_k( | ||
router_logits, | ||
k=top_k, # topk当前写8 | ||
bias=e_score_correction_bias, | ||
k_group=topk_group, # fix: 4 | ||
group_count=num_expert_group, # fix 8 | ||
group_select_mode=1, # 0: group中的最大; 1: topk2.sum(fix) | ||
renorm=0, # 0: softmax->topk(fix); 1: topk->softmax | ||
norm_type=1, # 0: softmax; 1: sigmoid(fix) | ||
# out_flag=False, # todo new api; 第三个输出是否输出 | ||
# y2_flag=False, # old api; 第三个输出是否输出 | ||
routed_scaling_factor=1, | ||
eps=float(1e-20)) | ||
else: | ||
topk_weights, topk_ids = select_experts( | ||
hidden_states=x, | ||
router_logits=router_logits, | ||
top_k=top_k, | ||
use_grouped_topk=use_grouped_topk, | ||
renormalize=renormalize, | ||
topk_group=topk_group, | ||
num_expert_group=num_expert_group, | ||
custom_routing_function=custom_routing_function, | ||
scoring_func=scoring_func, | ||
e_score_correction_bias=e_score_correction_bias, | ||
) | ||
|
||
fused_moe_state = get_forward_context().fused_moe_state | ||
shared_gate_up, shared_dequant_scale = None, None | ||
if shared_experts is not None and fused_moe_state == FusedMoEState.MC2: | ||
with npu_stream_switch("moe_secondary", 0): | ||
npu_wait_tensor(quantized_x_for_share, router_logits) | ||
share_up_out, _ = shared_experts.gate_up_proj( | ||
(quantized_x_for_share, dynamic_scale_for_share)) | ||
shared_gate_up, shared_dequant_scale = share_up_out[ | ||
0], share_up_out[1] | ||
|
||
# this is a naive implementation for experts load balance so as | ||
# to avoid accumulating too much tokens on a single rank. | ||
# currently it is only activated when doing profile runs. | ||
if enable_force_load_balance: | ||
topk_ids = torch.randint_like(topk_ids, 0, global_num_experts) | ||
|
||
topk_weights = topk_weights.to(x.dtype) | ||
with super_kernel(prefix, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding with super_kernel in this location would require modifying too many lines of code. Why not add it in fused_moe.py instead? |
||
"stream-fusion=1", | ||
enabled=enable_super_kernel): | ||
# NOTE: now npu_moe_gating_top_k can only support `group_count=256` pattern | ||
if global_num_experts == 256: | ||
topk_weights, topk_ids, _ = torch_npu.npu_moe_gating_top_k( | ||
router_logits.float(), | ||
k=top_k, # topk当前写8 | ||
bias=e_score_correction_bias, | ||
k_group=topk_group, # fix: 4 | ||
group_count=num_expert_group, # fix 8 | ||
group_select_mode=1, # 0: group中的最大; 1: topk2.sum(fix) | ||
renorm=0, # 0: softmax->topk(fix); 1: topk->softmax | ||
norm_type=1, # 0: softmax; 1: sigmoid(fix) | ||
# out_flag=False, # todo new api; 第三个输出是否输出 | ||
# y2_flag=False, # old api; 第三个输出是否输出 | ||
routed_scaling_factor=1, | ||
eps=float(1e-20)) | ||
else: | ||
topk_weights, topk_ids = select_experts( | ||
hidden_states=x, | ||
router_logits=router_logits, | ||
top_k=top_k, | ||
use_grouped_topk=use_grouped_topk, | ||
renormalize=renormalize, | ||
topk_group=topk_group, | ||
num_expert_group=num_expert_group, | ||
custom_routing_function=custom_routing_function, | ||
scoring_func=scoring_func, | ||
e_score_correction_bias=e_score_correction_bias, | ||
) | ||
|
||
fused_moe_state = get_forward_context().fused_moe_state | ||
shared_gate_up, shared_dequant_scale = None, None | ||
if shared_experts is not None and fused_moe_state == FusedMoEState.MC2: | ||
with npu_stream_switch("moe_secondary", 0): | ||
npu_wait_tensor(quantized_x_for_share, router_logits) | ||
share_up_out, _ = shared_experts.gate_up_proj( | ||
(quantized_x_for_share, dynamic_scale_for_share)) | ||
shared_gate_up, shared_dequant_scale = share_up_out[ | ||
0], share_up_out[1] | ||
|
||
# this is a naive implementation for experts load balance so as | ||
# to avoid accumulating too much tokens on a single rank. | ||
# currently it is only activated when doing profile runs. | ||
if enable_force_load_balance: | ||
topk_ids = torch.randint_like(topk_ids, 0, global_num_experts) | ||
|
||
topk_weights = topk_weights.to(x.dtype) | ||
|
||
if fused_moe_state == FusedMoEState.MC2: | ||
return fused_experts_with_mc2( | ||
hidden_states=x, | ||
w1=layer.w13_weight, | ||
w2=layer.w2_weight, | ||
w1_scale=layer.w13_weight_scale_fp32, | ||
w2_scale=layer.w2_weight_scale, | ||
topk_weights=topk_weights, | ||
topk_ids=topk_ids, | ||
top_k=top_k, | ||
expert_map=expert_map, | ||
moe_all_to_all_group_name=self.moe_all_to_all_group_name, | ||
log2phy=log2phy, | ||
global_redundant_expert_num=global_redundant_expert_num, | ||
shared_experts=shared_experts, | ||
is_torchair=self.torchair_graph_enabled, | ||
quantized_x_for_share=shared_gate_up, | ||
dynamic_scale_for_share=shared_dequant_scale, | ||
mc2_mask=kwargs.get("mc2_mask", None)) | ||
with super_kernel(prefix, | ||
"stream-fusion=1", | ||
enabled=enable_super_kernel): | ||
return fused_experts_with_mc2( | ||
hidden_states=x, | ||
w1=layer.w13_weight, | ||
w2=layer.w2_weight, | ||
w1_scale=layer.w13_weight_scale_fp32, | ||
w2_scale=layer.w2_weight_scale, | ||
topk_weights=topk_weights, | ||
topk_ids=topk_ids, | ||
top_k=top_k, | ||
expert_map=expert_map, | ||
moe_all_to_all_group_name=self.moe_all_to_all_group_name, | ||
log2phy=log2phy, | ||
global_redundant_expert_num=global_redundant_expert_num, | ||
shared_experts=shared_experts, | ||
is_torchair=self.torchair_graph_enabled, | ||
quantized_x_for_share=shared_gate_up, | ||
dynamic_scale_for_share=shared_dequant_scale, | ||
mc2_mask=kwargs.get("mc2_mask", None)) | ||
elif fused_moe_state == FusedMoEState.MC2_PREFILL: | ||
return fused_prefill_experts_with_mc2( | ||
hidden_states=x, | ||
|
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.
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.
Please add the document for the new filed in
docs/source/user_guide/configuration/additional_config.md