| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | from collections.abc import Iterable |
| 4 | |
| 5 | import regex |
| 6 | import torch |
| 7 | |
| 8 | from vllm.model_executor.models.deepseek_v2 import DeepseekV3ForCausalLM |
| 9 | from vllm.model_executor.models.utils import AutoWeightsLoader, WeightsMapper |
| 10 | |
| 11 | |
| 12 | class MistralLarge3ForCausalLM(DeepseekV3ForCausalLM): |
| 13 | # WeightsMapper applies all matching patterns sequentially (no break on first |
| 14 | # match). This is safe here because every pattern is anchored at both ends |
| 15 | # (\A...\Z) and after substitution the resulting key always starts with |
| 16 | # "model." or "lm_head.", so no later pattern can accidentally match again. |
| 17 | hf_to_vllm_mapper = WeightsMapper( |
| 18 | orig_to_new_regex={ |
| 19 | regex.compile( |
| 20 | r"\Alayers\.(\d+)\.attention_norm\.weight\Z" |
| 21 | ): r"model.layers.\1.input_layernorm.weight", |
| 22 | regex.compile( |
| 23 | r"\Alayers\.(\d+)\.attention\.wq_a\.(\w+)\Z" |
| 24 | ): r"model.layers.\1.self_attn.q_a_proj.\2", |
| 25 | regex.compile( |
| 26 | r"\Alayers\.(\d+)\.attention\.q_a_norm\.weight\Z" |
| 27 | ): r"model.layers.\1.self_attn.q_a_layernorm.weight", |
| 28 | regex.compile( |
| 29 | r"\Alayers\.(\d+)\.attention\.wq_b\.(\w+)\Z" |
| 30 | ): r"model.layers.\1.self_attn.q_b_proj.\2", |
| 31 | regex.compile( |
| 32 | r"\Alayers\.(\d+)\.attention\.wkv_a_with_mqa\.(\w+)\Z" |
| 33 | ): r"model.layers.\1.self_attn.kv_a_proj_with_mqa.\2", |
| 34 | regex.compile( |
| 35 | r"\Alayers\.(\d+)\.attention\.kv_a_norm\.weight\Z" |
| 36 | ): r"model.layers.\1.self_attn.kv_a_layernorm.weight", |
| 37 | regex.compile( |
| 38 | r"\Alayers\.(\d+)\.attention\.wkv_b\.(\w+)\Z" |
| 39 | ): r"model.layers.\1.self_attn.kv_b_proj.\2", |
| 40 | regex.compile( |
| 41 | r"\Alayers\.(\d+)\.attention\.wo\.(\w+)\Z" |
| 42 | ): r"model.layers.\1.self_attn.o_proj.\2", |
| 43 | regex.compile( |
| 44 | r"\Alayers\.(\d+)\.ffn_norm\.weight\Z" |
| 45 | ): r"model.layers.\1.post_attention_layernorm.weight", |
| 46 | regex.compile( |
| 47 | r"\Alayers\.(\d+)\.feed_forward\.w1\.(\w+)\Z" |
| 48 | ): r"model.layers.\1.mlp.gate_proj.\2", |
| 49 | regex.compile( |
| 50 | r"\Alayers\.(\d+)\.feed_forward\.w2\.(\w+)\Z" |
| 51 | ): r"model.layers.\1.mlp.down_proj.\2", |
| 52 | regex.compile( |
| 53 | r"\Alayers\.(\d+)\.feed_forward\.w3\.(\w+)\Z" |
| 54 | ): r"model.layers.\1.mlp.up_proj.\2", |
| 55 | regex.compile( |
| 56 | r"\Alayers\.(\d+)\.gate\.weight\Z" |
| 57 | ): r"model.layers.\1.mlp.gate.weight", |
| 58 | regex.compile( |
| 59 | r"\Alayers\.(\d+)\.shared_experts\.w1\.(\w+)\Z" |
| 60 | ): r"model.layers.\1.mlp.shared_experts.gate_proj.\2", |
| 61 | regex.compile( |
| 62 | r"\Alayers\.(\d+)\.shared_experts\.w2\.(\w+)\Z" |
| 63 | ): r"model.layers.\1.mlp.shared_experts.down_proj.\2", |
| 64 | regex.compile( |
| 65 | r"\Alayers\.(\d+)\.shared_experts\.w3\.(\w+)\Z" |
| 66 | ): r"model.layers.\1.mlp.shared_experts.up_proj.\2", |
| 67 | regex.compile( |
| 68 | r"\Alayers\.(\d+)\.experts\.(\d+)\.w1\.(\w+)\Z" |
| 69 | ): r"model.layers.\1.mlp.experts.\2.gate_proj.\3", |
| 70 | regex.compile( |
| 71 | r"\Alayers\.(\d+)\.experts\.(\d+)\.w2\.(\w+)\Z" |
| 72 | ): r"model.layers.\1.mlp.experts.\2.down_proj.\3", |
| 73 | regex.compile( |
| 74 | r"\Alayers\.(\d+)\.experts\.(\d+)\.w3\.(\w+)\Z" |
| 75 | ): r"model.layers.\1.mlp.experts.\2.up_proj.\3", |
| 76 | regex.compile(r"\Anorm\.weight\Z"): "model.norm.weight", |
| 77 | regex.compile(r"\Atok_embeddings\.weight\Z"): "model.embed_tokens.weight", |
| 78 | regex.compile(r"\Aoutput\.weight\Z"): "lm_head.weight", |
| 79 | }, |
| 80 | orig_to_new_suffix={ |
| 81 | ".qscale_act": ".input_scale", |
| 82 | ".qscale_weight": ".weight_scale", |
| 83 | }, |
| 84 | ) |
| 85 | |
| 86 | # Bypass super().load_weights() and construct AutoWeightsLoader(self) |
| 87 | # directly (same pattern as Qwen2ForCausalLM). Any logic in the parent |
| 88 | # class's load_weights is a thin wrapper around AutoWeightsLoader, and |
| 89 | # we must apply hf_to_vllm_mapper before the loader walks the tree. |
| 90 | def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: |
| 91 | loader = AutoWeightsLoader(self) |
| 92 | return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper) |