-
Notifications
You must be signed in to change notification settings - Fork 579
fix(v2): build complete MoE inference metadata for weight transfer #1595
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,6 +127,21 @@ def parallelism_strategy(self) -> dict: | |
| "num_engines": 1, | ||
| } | ||
|
|
||
| def _expert_id_offset(self, num_local_experts: int) -> int: | ||
| """Global id of this rank's first routed expert. | ||
|
|
||
| Under expert parallelism SGLang holds only ``num_experts // ep_size`` | ||
| experts per rank, so the fused tensor's leading index is local. The | ||
| training side publishes global HuggingFace names and the transfer plan is | ||
| indexed by name, so without the offset every rank claims the same | ||
| low-numbered experts and the rest are never advertised at all. | ||
| """ | ||
| rank_info = self._rank_info or self._build_rank_info() | ||
| ep_size = getattr(rank_info, "ep_size", 1) or 1 | ||
| if ep_size <= 1: | ||
| return 0 | ||
| return getattr(rank_info, "ep_rank", 0) * num_local_experts | ||
|
|
||
| def _unfuse_params( | ||
| self, name: str, tensor: torch.Tensor | ||
| ) -> list[tuple[str, torch.Tensor]]: | ||
|
|
@@ -185,13 +200,15 @@ def _unfuse_params( | |
| prefix = name.replace(".w13_weight", "") | ||
| result = [] | ||
| ffn_hidden = tensor.shape[1] // 2 | ||
| id_offset = self._expert_id_offset(tensor.shape[0]) | ||
|
Collaborator
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. SGLang uses fused moe. It is recommended to confirm whether the fused tensor can still meet this retrieval method.
Collaborator
Author
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. I checked this against SGLang v0.5.10.post1 (commit The fused MoE implementation preserves the local-expert index as tensor dimension 0. The unquantized implementation allocates both tensors as: w13_weight = torch.nn.Parameter(
torch.empty(num_experts, w13_weight_n, w13_weight_k, dtype=params_dtype),
requires_grad=False,
)
...
w2_weight = torch.nn.Parameter(
torch.empty(num_experts, w2_weight_n, w2_weight_k, dtype=params_dtype),
requires_grad=False,
)
SGLang maps the global routed-expert ID to that local dimension using the same EP-rank offset: start_idx = self.moe_ep_rank * num_local_routed_experts
...
return expert_id - start_idxTherefore, for the Qwen3-MoE path covered by this PR, dimension 0 remains the local-expert dimension and the |
||
| for i in range(tensor.shape[0]): | ||
| expert_tensor = tensor[i] | ||
| if i < num_routed: | ||
| expert_prefix = f"{prefix}.{i}" | ||
| expert_id = i + id_offset | ||
| if expert_id < num_routed: | ||
| expert_prefix = f"{prefix}.{expert_id}" | ||
| else: | ||
| shared_idx = i - num_routed | ||
| num_shared = tensor.shape[0] - num_routed | ||
| shared_idx = expert_id - num_routed | ||
| num_shared = tensor.shape[0] + id_offset - num_routed | ||
| if num_shared > 1: | ||
| expert_prefix = prefix.replace( | ||
| "experts", f"shared_experts.{shared_idx}" | ||
|
|
@@ -211,12 +228,14 @@ def _unfuse_params( | |
| num_routed = getattr(cfg, "num_experts", None) or cfg.n_routed_experts | ||
| prefix = name.replace(".w2_weight", "") | ||
| result = [] | ||
| id_offset = self._expert_id_offset(tensor.shape[0]) | ||
| for i in range(tensor.shape[0]): | ||
| if i < num_routed: | ||
| expert_prefix = f"{prefix}.{i}" | ||
| expert_id = i + id_offset | ||
| if expert_id < num_routed: | ||
| expert_prefix = f"{prefix}.{expert_id}" | ||
| else: | ||
| shared_idx = i - num_routed | ||
| num_shared = tensor.shape[0] - num_routed | ||
| shared_idx = expert_id - num_routed | ||
| num_shared = tensor.shape[0] + id_offset - num_routed | ||
| if num_shared > 1: | ||
| expert_prefix = prefix.replace( | ||
| "experts", f"shared_experts.{shared_idx}" | ||
|
|
@@ -330,6 +349,10 @@ def get_local_shard_parameters( | |
| ) -> dict[str, torch.Tensor]: | ||
| required = set(required_names) if required_names else None | ||
| local_params: dict[str, torch.Tensor] = {} | ||
| # Expert ids below come from this rank's expert-parallel position, and | ||
| # the payload has to use the same names the metadata advertised. | ||
| if self._rank_info is None: | ||
| self._rank_info = self._build_rank_info() | ||
|
|
||
| for name, param in self._get_model().named_parameters(): | ||
| for hf_name, hf_tensor in self._unfuse_params(name, param.data): | ||
|
|
||
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.
If there is a shared expert here, it will be recorded in the current num_local_experts, causing the overall routed ID to shift.