-
Notifications
You must be signed in to change notification settings - Fork 750
Add DeepseekV3HybridMoeModuleArchitecture #661
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
zhoutong-hai
wants to merge
2
commits into
arcee-ai:main
Choose a base branch
from
zhoutong-hai:ds-v3
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.
+119
−1
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 |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # Copyright (C) 2025 Arcee AI | ||
| # SPDX-License-Identifier: LGPL-3.0-only | ||
|
|
||
| from typing import ClassVar, List, Optional | ||
|
|
||
| from pydantic import BaseModel | ||
| from transformers import PretrainedConfig | ||
|
|
||
| from mergekit.architecture.base import ModuleArchitecture, WeightInfo | ||
|
|
||
|
|
||
| class DeepseekV3HybridMoeModuleArchitecture(ModuleArchitecture, BaseModel): | ||
| """ | ||
| DeepSeek V3 uses a hybrid dense + MoE MLP: | ||
| - the first `first_k_dense_replace` layers are dense MLP weights: | ||
| mlp.{gate_proj,up_proj,down_proj}.weight | ||
| - the remaining layers are typically MoE (controlled by moe_layer_freq): | ||
| mlp.gate.weight (+ optional e_score_correction_bias), | ||
| mlp.shared_experts.{gate_proj,up_proj,down_proj}.weight, | ||
| mlp.experts.{i}.{gate_proj,up_proj,down_proj}.weight | ||
| """ | ||
|
|
||
| ARCHITECTURE_NAME: ClassVar[str] = "DeepseekV3ForCausalLM" | ||
|
|
||
| first_k_dense_replace: int = 0 | ||
| moe_layer_freq: int = 1 | ||
| n_routed_experts: int = 0 | ||
| n_shared_experts: int = 0 | ||
|
|
||
| def name(self) -> str: | ||
| return "deepseek_v3" | ||
|
|
||
| @classmethod | ||
| def from_config(cls, config: PretrainedConfig) -> "DeepseekV3HybridMoeModuleArchitecture": | ||
| return cls( | ||
| first_k_dense_replace=int(getattr(config, "first_k_dense_replace", 0) or 0), | ||
| moe_layer_freq=max(1, int(getattr(config, "moe_layer_freq", 1) or 1)), | ||
| n_routed_experts=int(getattr(config, "n_routed_experts", 0) or 0), | ||
| n_shared_experts=int(getattr(config, "n_shared_experts", 0) or 0), | ||
| ) | ||
|
|
||
| def pre_weights(self, config: PretrainedConfig) -> List[WeightInfo]: | ||
| # Observed in DeepSeek V3 safetensors indices | ||
| return [WeightInfo(name="model.embed_tokens.weight", is_embed=True)] | ||
|
|
||
| def post_weights(self, config: PretrainedConfig) -> List[WeightInfo]: | ||
| # Observed in DeepSeek V3 safetensors indices | ||
| return [ | ||
| WeightInfo(name="model.norm.weight"), | ||
| WeightInfo(name="lm_head.weight", is_embed=True), | ||
| ] | ||
|
|
||
| def _is_moe_layer(self, index: int) -> bool: | ||
| """ | ||
| DeepSeek V3 is dense for first_k_dense_replace layers, then MoE with frequency. | ||
| """ | ||
| if index < self.first_k_dense_replace: | ||
| return False | ||
| # after dense block, apply MoE every `moe_layer_freq` layers | ||
| return ((index - self.first_k_dense_replace) % self.moe_layer_freq) == 0 | ||
|
|
||
| def layer_weights( | ||
| self, index: int, config: PretrainedConfig | ||
| ) -> Optional[List[WeightInfo]]: | ||
| prefix = f"model.layers.{index}" | ||
| res: List[WeightInfo] = [] | ||
|
|
||
| # Attention weights (based on observed tensor names in DeepSeek V3 checkpoints) | ||
| for suffix in ( | ||
| ".self_attn.o_proj.weight", | ||
| ".self_attn.q_a_proj.weight", | ||
| ".self_attn.q_b_proj.weight", | ||
| ".self_attn.kv_a_proj_with_mqa.weight", | ||
| ".self_attn.kv_b_proj.weight", | ||
| ".self_attn.q_a_layernorm.weight", | ||
| ".self_attn.kv_a_layernorm.weight", | ||
| ".input_layernorm.weight", | ||
| ".post_attention_layernorm.weight", | ||
| ): | ||
| res.append(WeightInfo(name=prefix + suffix)) | ||
|
|
||
| # Dense vs MoE MLP blocks | ||
| if not self._is_moe_layer(index): | ||
| for p in ("gate_proj", "up_proj", "down_proj"): | ||
| res.append(WeightInfo(name=prefix + f".mlp.{p}.weight")) | ||
| return res | ||
|
|
||
| # MoE MLP weights | ||
| res.append(WeightInfo(name=prefix + ".mlp.gate.weight")) | ||
| # Not always present across variants; treat as optional for robustness. | ||
| res.append( | ||
| WeightInfo( | ||
| name=prefix + ".mlp.gate.e_score_correction_bias", | ||
| optional=True, | ||
| ) | ||
| ) | ||
|
|
||
| if self.n_shared_experts and self.n_shared_experts > 0: | ||
| for p in ("gate_proj", "up_proj", "down_proj"): | ||
| res.append(WeightInfo(name=prefix + f".mlp.shared_experts.{p}.weight")) | ||
|
|
||
| for expert_idx in range(self.n_routed_experts): | ||
| for p in ("gate_proj", "up_proj", "down_proj"): | ||
| res.append( | ||
| WeightInfo(name=prefix + f".mlp.experts.{expert_idx}.{p}.weight") | ||
| ) | ||
|
|
||
| return res | ||
|
|
||
|
|
||
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.
Missing
optionalandtied_namesforlm_head.weightHigh Severity
The
lm_head.weightinpost_weightsis missingoptional=Trueandtied_names=("model.embed_tokens.weight",). Many transformer models (including DeepSeek V3 variants) tie the input embeddings with the output LM head, storing only one copy asmodel.embed_tokens.weight. Withoutoptionalandtied_names, the weight loading will fail whenlm_head.weightdoesn't exist separately in the checkpoint, as the code has no fallback to look for the weight under its tied name. Other architecture definitions (LLaMA, Mistral) correctly handle this pattern.