Skip to content
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

Added support for DeepseekV2 model #382

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions mergekit/_data/architectures/deepseekv2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"model_type": "deepseekv2",
"architectures": [
"DeepseekV2ForCausalLM"
],
"pre_weights": [
{
"name": "model.embed_tokens.weight",
"is_embed": true
}
],
"post_weights": [
{
"name": "model.norm.weight"
},
{
"name": "lm_head.weight",
"is_embed": true,
"aliases": [
"model.embed_tokens.weight"
]
}
],
"num_layers_config_key": "num_hidden_layers",
"layer_templates": {
"weights": [
{
"name" : "model.layers.${layer_index}.self_attn.q_proj.weight"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm mistaken I don't think this weight exists in the model. Please see here: https://huggingface.co/deepseek-ai/DeepSeek-V2/raw/main/model.safetensors.index.json

    "model.layers.0.self_attn.q_a_proj.weight": "model-00001-of-000055.safetensors",
    "model.layers.0.self_attn.q_a_layernorm.weight": "model-00001-of-000055.safetensors",
    "model.layers.0.self_attn.q_b_proj.weight": "model-00001-of-000055.safetensors",

},
{
"name" : "model.layers.${layer_index}.self_attn.kv_a_proj_with_mqa.weight"
},
{
"name" : "model.layers.${layer_index}.self_attn.kv_a_layernorm.weight"
},
{
"name" : "model.layers.${layer_index}.self_attn.kv_b_proj.weight"
},
{
"name" : "model.layers.${layer_index}.self_attn.o_proj.weight"
},
{
"name": "model.layers.${layer_index}.input_layernorm.weight"
},
{
"name": "model.layers.${layer_index}.post_attention_layernorm.weight"
}
]
}
}
52 changes: 52 additions & 0 deletions mergekit/architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,57 @@ def sliceable(self) -> bool:
def has_defined_spaces(self) -> bool:
return False

class DeepseekV2TensorNames(ArchitectureInfo, BaseModel):
ARCHITECTURE_NAME: ClassVar[str] = "DeepseekV2ForCausalLM"
num_local_experts: int

def name(self) -> str:
return "DeepseekV2"

@classmethod
def from_config(cls, config: PretrainedConfig):
return DeepseekV2TensorNames(num_local_experts=config.n_routed_experts)

def pre_weights(sef, config: PretrainedConfig) -> List[WeightInfo]:
return DEEPSEEKV2_INFO.pre_weights(config)

def post_weights(self, config: PretrainedConfig) -> List[WeightInfo]:
return DEEPSEEKV2_INFO.post_weights(config)

def num_layers_config_key(self) -> str:
return DEEPSEEKV2_INFO.num_layers_config_key()

def layer_weights(
self, index: int, config: PretrainedConfig
) -> Optional[List[WeightInfo]]:
num_experts = self.num_local_experts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Any reason for local aliasing? I see it is also present in the Mixtral code, but any reason you left it in?

prefix = f"model.layers.{index}"
tensor_names = []

if index > 0:
for expert_idx in range(num_experts):
for param in ("gate_proj", "up_proj", "down_proj"):
tensor_names.append(
prefix + f".mlp.experts.{expert_idx}.{param}.weight"
aditya-29 marked this conversation as resolved.
Show resolved Hide resolved
)
tensor_names.append(prefix + ".mlp.gate.weight")

tensor_names.append(prefix + ".mlp.shared_experts.gate_proj.weight")
tensor_names.append(prefix + ".mlp.shared_experts.up_proj.weight")
tensor_names.append(prefix + ".mlp.shared_experts.down_proj.weight")

res = [WeightInfo(name=name) for name in tensor_names]

for weight_info in DEEPSEEKV2_INFO.layer_weights(index, config):
res.append(weight_info)

return res

def sliceable(self) -> bool:
return True

def has_defined_spaces(self) -> bool:
return False

def _load_json_arch(name: str) -> JsonArchitectureInfo:
text = importlib.resources.read_text(mergekit._data.architectures, name)
Expand Down Expand Up @@ -353,6 +404,7 @@ def _load_all_architectures() -> (
JSON_ARCHITECTURES, NAME_TO_ARCH = _load_all_architectures()
MISTRAL_INFO = _load_json_arch("mistral.json")
QWEN2_INFO = _load_json_arch("qwen2.json")
DEEPSEEKV2_INFO = _load_json_arch("deepseekv2.json")


def get_architecture_info(config: PretrainedConfig) -> ArchitectureInfo:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I am mistaken which I could be, there needs to be a clause within the get_architecture_info function similar to Mixtral's otherwise the code https://github.com/arcee-ai/mergekit/blob/main/mergekit/merge.py#L51 will just pull in just the info associated with the template

Expand Down
Loading