-
Notifications
You must be signed in to change notification settings - Fork 222
add qwen3-moe optimization #1441
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
shiyuan680
wants to merge
2
commits into
vllm-project:main
Choose a base branch
from
shiyuan680:qwen3
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.
+110
−8
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 |
---|---|---|
|
@@ -16,7 +16,22 @@ | |
# Adapted from vllm/model_executor/models/qwen3_moe.py | ||
# This file is a part of the vllm-ascend project. | ||
|
||
from typing import Optional | ||
|
||
import torch | ||
import vllm | ||
from torch import nn | ||
from transformers import PretrainedConfig | ||
from vllm.attention import AttentionMetadata | ||
from vllm.distributed import get_tensor_model_parallel_world_size, get_tp_group | ||
from vllm.distributed.parallel_state import get_dp_group | ||
from vllm.forward_context import get_forward_context | ||
from vllm.model_executor.layers.linear import ReplicatedLinear | ||
from vllm.model_executor.layers.quantization import QuantizationConfig | ||
from vllm.model_executor.models.qwen3_moe import Qwen3MoeForCausalLM | ||
from vllm_ascend.ascend_config import get_ascend_config | ||
from vllm_ascend.distributed.parallel_state import get_ep_group | ||
from vllm_ascend.ops.fused_moe import AscendFusedMoE | ||
|
||
|
||
class CustomQwen3MoeForCausalLM(Qwen3MoeForCausalLM): | ||
|
@@ -31,5 +46,89 @@ class CustomQwen3MoeForCausalLM(Qwen3MoeForCausalLM): | |
"up_proj", | ||
], | ||
"experts": | ||
["experts.0.gate_proj", "experts.0.up_proj", "experts.0.down_proj"], | ||
["experts.0.gate_proj", "experts.0.up_proj", "experts.0.down_proj"], | ||
} | ||
|
||
|
||
class AscendQwen3MoeSparseMoeBlock(nn.Module): | ||
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. Please add ut for this: https://vllm-ascend.readthedocs.io/en/latest/developer_guide/contribution/testing.html |
||
top_k: int | ||
|
||
def __init__( | ||
self, | ||
config: PretrainedConfig, | ||
quant_config: Optional[QuantizationConfig] = None, | ||
prefix: str = "", | ||
): | ||
super().__init__() | ||
self.tp_size = get_tensor_model_parallel_world_size() | ||
if self.tp_size > config.num_experts: | ||
raise ValueError( | ||
f"Tensor parallel size {self.tp_size} is greater than " | ||
f"the number of experts {config.num_experts}.") | ||
|
||
ascend_config = get_ascend_config() | ||
self.torchair_graph_enabled = ascend_config.torchair_graph_config.enabled | ||
self.enable_multistream_moe = \ | ||
ascend_config.torchair_graph_config.enable_multistream_moe | ||
|
||
self.gate = ReplicatedLinear(config.hidden_size, | ||
config.num_experts, | ||
bias=False, | ||
quant_config=None, | ||
prefix=f"{prefix}.gate") | ||
|
||
self.experts = AscendFusedMoE( | ||
num_experts=config.num_experts, | ||
top_k=config.num_experts_per_tok, | ||
hidden_size=config.hidden_size, | ||
intermediate_size=config.moe_intermediate_size, | ||
reduce_results=False, | ||
renormalize=config.norm_topk_prob, | ||
quant_config=quant_config, | ||
prefix=f"{prefix}.experts") | ||
|
||
self.top_k = config.num_experts_per_tok | ||
|
||
self.dp_size = get_dp_group().world_size | ||
|
||
self.tp_group = get_tp_group().device_group | ||
self.tp_rank = get_tp_group().rank_in_group | ||
self.ep_group = get_ep_group() | ||
|
||
self.params_dtype = torch.get_default_dtype() | ||
|
||
def forward( | ||
self, | ||
hidden_states: torch.Tensor, | ||
attn_metadata: Optional[AttentionMetadata] = None) -> torch.Tensor: | ||
if attn_metadata is None: | ||
attn_metadata = get_forward_context().attn_metadata | ||
# when profile runs, force experts to load balanced tokens | ||
# to avoid high memory consumption on a single rank. | ||
# TODO: need a better flag to indicate whether in profile run or not. | ||
if attn_metadata is None: | ||
# for profile run | ||
is_prefill = True | ||
enable_force_load_balance = True | ||
else: | ||
# is_prefill = attn_metadata.num_prefills > 0 | ||
enable_force_load_balance = False | ||
if hasattr(attn_metadata, 'with_prefill_across_dp'): | ||
is_prefill = attn_metadata.with_prefill_across_dp | ||
|
||
# router_logits: (num_tokens, n_experts) | ||
router_logits, _ = self.gate(hidden_states) | ||
|
||
hidden_states = self.experts( | ||
hidden_states=hidden_states, | ||
router_logits=router_logits, | ||
is_prefill=is_prefill, | ||
top_k=self.top_k, | ||
enable_force_load_balance=enable_force_load_balance, | ||
shared_experts=None, | ||
) | ||
|
||
return hidden_states | ||
|
||
|
||
vllm.model_executor.models.qwen3_moe.Qwen3MoeSparseMoeBlock = AscendQwen3MoeSparseMoeBlock |
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.
This file is too huge and will cost lots of time to run ci, please try to used reduce layer model: https://vllm-ascend.readthedocs.io/en/latest/developer_guide/contribution/testing.html#e2e-test-example