| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | # Copyright 2024 The Qwen team. |
| 5 | # Copyright 2023 The vLLM team. |
| 6 | # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. |
| 7 | # |
| 8 | # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX |
| 9 | # and OPT implementations in this library. It has been modified from its |
| 10 | # original forms to accommodate minor architectural differences compared |
| 11 | # to GPT-NeoX and OPT used by the Meta AI team that trained the model. |
| 12 | # |
| 13 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | # you may not use this file except in compliance with the License. |
| 15 | # You may obtain a copy of the License at |
| 16 | # |
| 17 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | # |
| 19 | # Unless required by applicable law or agreed to in writing, software |
| 20 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | # See the License for the specific language governing permissions and |
| 23 | # limitations under the License. |
| 24 | """Inference-only Qwen3MoE model compatible with HuggingFace weights.""" |
| 25 | |
| 26 | from collections.abc import Iterable |
| 27 | from itertools import islice |
| 28 | from typing import Any |
| 29 | |
| 30 | import torch |
| 31 | import torch.nn.functional as F |
| 32 | from torch import nn |
| 33 | |
| 34 | from vllm.compilation.decorators import support_torch_compile |
| 35 | from vllm.config import CacheConfig, VllmConfig, get_current_vllm_config |
| 36 | from vllm.distributed import ( |
| 37 | get_ep_group, |
| 38 | get_pp_group, |
| 39 | get_tensor_model_parallel_world_size, |
| 40 | tensor_model_parallel_all_gather, |
| 41 | ) |
| 42 | from vllm.logger import init_logger |
| 43 | from vllm.model_executor.layers.activation import SiluAndMul |
| 44 | from vllm.model_executor.layers.attention import Attention |
| 45 | from vllm.model_executor.layers.fused_moe import ( |
| 46 | FusedMoEFactory, |
| 47 | GateLinear, |
| 48 | ) |
| 49 | from vllm.model_executor.layers.layernorm import RMSNorm |
| 50 | from vllm.model_executor.layers.linear import ( |
| 51 | MergedColumnParallelLinear, |
| 52 | QKVParallelLinear, |
| 53 | ReplicatedLinear, |
| 54 | RowParallelLinear, |
| 55 | ) |
| 56 | from vllm.model_executor.layers.logits_processor import LogitsProcessor |
| 57 | from vllm.model_executor.layers.quantization import QuantizationConfig |
| 58 | from vllm.model_executor.layers.rotary_embedding import get_rope |
| 59 | from vllm.model_executor.layers.vocab_parallel_embedding import ( |
| 60 | ParallelLMHead, |
| 61 | VocabParallelEmbedding, |
| 62 | ) |
| 63 | from vllm.model_executor.models.utils import sequence_parallel_chunk |
| 64 | from vllm.sequence import IntermediateTensors |
| 65 | |
| 66 | from .interfaces import ( |
| 67 | EagleModelMixin, |
| 68 | MixtureOfExperts, |
| 69 | SupportsEagle, |
| 70 | SupportsEagle3, |
| 71 | SupportsLoRA, |
| 72 | SupportsPP, |
| 73 | SupportsQuant, |
| 74 | ) |
| 75 | from .utils import ( |
| 76 | AutoWeightsLoader, |
| 77 | PPMissingLayer, |
| 78 | WeightsMapper, |
| 79 | extract_layer_index, |
| 80 | make_empty_intermediate_tensors_factory, |
| 81 | make_layers, |
| 82 | maybe_prefix, |
| 83 | ) |
| 84 | |
| 85 | logger = init_logger(__name__) |
| 86 | |
| 87 | |
| 88 | class Qwen3MoeMLP(nn.Module): |
| 89 | def __init__( |
| 90 | self, |
| 91 | hidden_size: int, |
| 92 | intermediate_size: int, |
| 93 | hidden_act: str, |
| 94 | quant_config: QuantizationConfig | None = None, |
| 95 | reduce_results: bool = True, |
| 96 | expert_gate: torch.nn.Linear | None = None, |
| 97 | prefix: str = "", |
| 98 | ) -> None: |
| 99 | super().__init__() |
| 100 | self.gate_up_proj = MergedColumnParallelLinear( |
| 101 | hidden_size, |
| 102 | [intermediate_size] * 2, |
| 103 | bias=False, |
| 104 | quant_config=quant_config, |
| 105 | prefix=f"{prefix}.gate_up_proj", |
| 106 | ) |
| 107 | self.down_proj = RowParallelLinear( |
| 108 | intermediate_size, |
| 109 | hidden_size, |
| 110 | bias=False, |
| 111 | quant_config=quant_config, |
| 112 | reduce_results=reduce_results, |
| 113 | prefix=f"{prefix}.down_proj", |
| 114 | ) |
| 115 | if hidden_act != "silu": |
| 116 | raise ValueError( |
| 117 | f"Unsupported activation: {hidden_act}. Only silu is supported for now." |
| 118 | ) |
| 119 | self.act_fn = SiluAndMul() |
| 120 | self.expert_gate = expert_gate |
| 121 | |
| 122 | def forward(self, x): |
| 123 | gate_up, _ = self.gate_up_proj(x) |
| 124 | out = self.act_fn(gate_up) |
| 125 | out, _ = self.down_proj(out) |
| 126 | |
| 127 | if self.expert_gate is not None: |
| 128 | out = F.sigmoid(self.expert_gate(x)[0]) * out |
| 129 | |
| 130 | return out |
| 131 | |
| 132 | |
| 133 | class Qwen3MoeSparseMoeBlock(nn.Module): |
| 134 | def __init__( |
| 135 | self, |
| 136 | vllm_config: VllmConfig, |
| 137 | prefix: str = "", |
| 138 | is_fused_checkpoint_transposed: bool = False, |
| 139 | ): |
| 140 | super().__init__() |
| 141 | |
| 142 | config = vllm_config.model_config.hf_text_config |
| 143 | parallel_config = vllm_config.parallel_config |
| 144 | quant_config = vllm_config.quant_config |
| 145 | |
| 146 | self.tp_size = get_tensor_model_parallel_world_size() |
| 147 | |
| 148 | self.ep_group = get_ep_group().device_group |
| 149 | self.ep_size = self.ep_group.size() |
| 150 | self.n_routed_experts = config.num_experts |
| 151 | |
| 152 | self.is_sequence_parallel = parallel_config.use_sequence_parallel_moe |
| 153 | |
| 154 | if self.tp_size > config.num_experts: |
| 155 | raise ValueError( |
| 156 | f"Tensor parallel size {self.tp_size} is greater than " |
| 157 | f"the number of experts {config.num_experts}." |
| 158 | ) |
| 159 | |
| 160 | # Load balancing settings. |
| 161 | vllm_config = get_current_vllm_config() |
| 162 | eplb_config = vllm_config.parallel_config.eplb_config |
| 163 | self.enable_eplb = parallel_config.enable_eplb |
| 164 | |
| 165 | self.n_logical_experts = self.n_routed_experts |
| 166 | self.n_redundant_experts = eplb_config.num_redundant_experts |
| 167 | self.n_physical_experts = self.n_logical_experts + self.n_redundant_experts |
| 168 | self.n_local_physical_experts = self.n_physical_experts // self.ep_size |
| 169 | |
| 170 | self.gate = GateLinear( |
| 171 | config.hidden_size, |
| 172 | config.num_experts, |
| 173 | quant_config=quant_config, |
| 174 | prefix=f"{prefix}.gate", |
| 175 | ) |
| 176 | |
| 177 | shared_expert_intermediate_size = getattr( |
| 178 | config, "shared_expert_intermediate_size", 0 |
| 179 | ) |
| 180 | self.shared_expert_gate: ReplicatedLinear | None |
| 181 | self.shared_expert: Qwen3MoeMLP | None |
| 182 | if shared_expert_intermediate_size > 0: |
| 183 | self.shared_expert_gate = ReplicatedLinear( |
| 184 | config.hidden_size, |
| 185 | 1, |
| 186 | bias=False, |
| 187 | quant_config=None, |
| 188 | prefix=f"{prefix}.shared_expert_gate", |
| 189 | ) |
| 190 | self.shared_expert = Qwen3MoeMLP( |
| 191 | hidden_size=config.hidden_size, |
| 192 | intermediate_size=shared_expert_intermediate_size, |
| 193 | hidden_act=config.hidden_act, |
| 194 | quant_config=quant_config, |
| 195 | reduce_results=False, |
| 196 | expert_gate=self.shared_expert_gate, |
| 197 | prefix=f"{prefix}.shared_expert", |
| 198 | ) |
| 199 | else: |
| 200 | self.shared_expert_gate = None |
| 201 | self.shared_expert = None |
| 202 | |
| 203 | self.experts = FusedMoEFactory( |
| 204 | shared_experts=self.shared_expert, |
| 205 | gate=self.gate, |
| 206 | num_experts=self.n_routed_experts, |
| 207 | top_k=config.num_experts_per_tok, |
| 208 | hidden_size=config.hidden_size, |
| 209 | intermediate_size=config.moe_intermediate_size, |
| 210 | renormalize=config.norm_topk_prob, |
| 211 | quant_config=quant_config, |
| 212 | prefix=f"{prefix}.experts", |
| 213 | enable_eplb=self.enable_eplb, |
| 214 | num_redundant_experts=self.n_redundant_experts, |
| 215 | is_sequence_parallel=self.is_sequence_parallel, |
| 216 | is_fused_checkpoint_transposed=is_fused_checkpoint_transposed, |
| 217 | ) |
| 218 | |
| 219 | def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: |
| 220 | assert hidden_states.dim() <= 2, ( |
| 221 | "Qwen3MoeSparseMoeBlock only supports 1D or 2D inputs" |
| 222 | ) |
| 223 | is_input_1d = hidden_states.dim() == 1 |
| 224 | num_tokens, hidden_dim = hidden_states.shape |
| 225 | hidden_states = hidden_states.view(-1, hidden_dim) |
| 226 | |
| 227 | if self.is_sequence_parallel: |
| 228 | hidden_states = sequence_parallel_chunk(hidden_states) |
| 229 | |
| 230 | final_hidden_states = self.experts( |
| 231 | hidden_states=hidden_states, router_logits=hidden_states |
| 232 | ) |
| 233 | |
| 234 | if self.is_sequence_parallel: |
| 235 | final_hidden_states = tensor_model_parallel_all_gather( |
| 236 | final_hidden_states, 0 |
| 237 | ) |
| 238 | final_hidden_states = final_hidden_states[:num_tokens] |
| 239 | |
| 240 | # return to 1d if input is 1d |
| 241 | return final_hidden_states.squeeze(0) if is_input_1d else final_hidden_states |
| 242 | |
| 243 | |
| 244 | class Qwen3MoeAttention(nn.Module): |
| 245 | def __init__( |
| 246 | self, |
| 247 | hidden_size: int, |
| 248 | num_heads: int, |
| 249 | num_kv_heads: int, |
| 250 | rope_parameters: dict[str, Any], |
| 251 | max_position_embeddings: int = 8192, |
| 252 | head_dim: int | None = None, |
| 253 | rms_norm_eps: float = 1e-06, |
| 254 | qkv_bias: bool = False, |
| 255 | cache_config: CacheConfig | None = None, |
| 256 | quant_config: QuantizationConfig | None = None, |
| 257 | prefix: str = "", |
| 258 | dual_chunk_attention_config: dict[str, Any] | None = None, |
| 259 | ) -> None: |
| 260 | super().__init__() |
| 261 | self.hidden_size = hidden_size |
| 262 | tp_size = get_tensor_model_parallel_world_size() |
| 263 | self.total_num_heads = num_heads |
| 264 | assert self.total_num_heads % tp_size == 0 |
| 265 | self.num_heads = self.total_num_heads // tp_size |
| 266 | self.total_num_kv_heads = num_kv_heads |
| 267 | if self.total_num_kv_heads >= tp_size: |
| 268 | # Number of KV heads is greater than TP size, so we partition |
| 269 | # the KV heads across multiple tensor parallel GPUs. |
| 270 | assert self.total_num_kv_heads % tp_size == 0 |
| 271 | else: |
| 272 | # Number of KV heads is less than TP size, so we replicate |
| 273 | # the KV heads across multiple tensor parallel GPUs. |
| 274 | assert tp_size % self.total_num_kv_heads == 0 |
| 275 | self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size) |
| 276 | self.head_dim = head_dim or (hidden_size // self.total_num_heads) |
| 277 | self.q_size = self.num_heads * self.head_dim |
| 278 | self.kv_size = self.num_kv_heads * self.head_dim |
| 279 | self.scaling = self.head_dim**-0.5 |
| 280 | self.max_position_embeddings = max_position_embeddings |
| 281 | self.dual_chunk_attention_config = dual_chunk_attention_config |
| 282 | |
| 283 | self.qkv_proj = QKVParallelLinear( |
| 284 | hidden_size, |
| 285 | self.head_dim, |
| 286 | self.total_num_heads, |
| 287 | self.total_num_kv_heads, |
| 288 | bias=qkv_bias, |
| 289 | quant_config=quant_config, |
| 290 | prefix=f"{prefix}.qkv_proj", |
| 291 | ) |
| 292 | |
| 293 | self.o_proj = RowParallelLinear( |
| 294 | self.total_num_heads * self.head_dim, |
| 295 | hidden_size, |
| 296 | bias=False, |
| 297 | quant_config=quant_config, |
| 298 | prefix=f"{prefix}.o_proj", |
| 299 | ) |
| 300 | |
| 301 | self.rotary_emb = get_rope( |
| 302 | self.head_dim, |
| 303 | max_position=max_position_embeddings, |
| 304 | rope_parameters=rope_parameters, |
| 305 | dual_chunk_attention_config=dual_chunk_attention_config, |
| 306 | ) |
| 307 | attention_kwargs: dict[str, Any] = {} |
| 308 | if dual_chunk_attention_config: |
| 309 | attention_kwargs = { |
| 310 | "layer_idx": extract_layer_index(prefix), |
| 311 | "dual_chunk_attention_config": dual_chunk_attention_config, |
| 312 | } |
| 313 | self.attn = Attention( |
| 314 | self.num_heads, |
| 315 | self.head_dim, |
| 316 | self.scaling, |
| 317 | num_kv_heads=self.num_kv_heads, |
| 318 | cache_config=cache_config, |
| 319 | quant_config=quant_config, |
| 320 | prefix=f"{prefix}.attn", |
| 321 | **attention_kwargs, |
| 322 | ) |
| 323 | |
| 324 | self.q_norm = RMSNorm(self.head_dim, eps=rms_norm_eps) |
| 325 | self.k_norm = RMSNorm(self.head_dim, eps=rms_norm_eps) |
| 326 | |
| 327 | def forward( |
| 328 | self, |
| 329 | positions: torch.Tensor, |
| 330 | hidden_states: torch.Tensor, |
| 331 | ) -> torch.Tensor: |
| 332 | qkv, _ = self.qkv_proj(hidden_states) |
| 333 | q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1) |
| 334 | # Add qk-norm |
| 335 | q_by_head = q.view(*q.shape[:-1], q.shape[-1] // self.head_dim, self.head_dim) |
| 336 | q_by_head = self.q_norm(q_by_head) |
| 337 | q = q_by_head.view(q.shape) |
| 338 | |
| 339 | k_by_head = k.view(*k.shape[:-1], k.shape[-1] // self.head_dim, self.head_dim) |
| 340 | k_by_head = self.k_norm(k_by_head) |
| 341 | k = k_by_head.view(k.shape) |
| 342 | q, k = self.rotary_emb(positions, q, k) |
| 343 | attn_output = self.attn(q, k, v) |
| 344 | output, _ = self.o_proj(attn_output) |
| 345 | return output |
| 346 | |
| 347 | |
| 348 | class Qwen3MoeDecoderLayer(nn.Module): |
| 349 | def __init__( |
| 350 | self, |
| 351 | vllm_config: VllmConfig, |
| 352 | prefix: str = "", |
| 353 | is_fused_checkpoint_transposed: bool = False, |
| 354 | ) -> None: |
| 355 | super().__init__() |
| 356 | |
| 357 | config = vllm_config.model_config.hf_text_config |
| 358 | cache_config = vllm_config.cache_config |
| 359 | quant_config = vllm_config.quant_config |
| 360 | |
| 361 | self.hidden_size = config.hidden_size |
| 362 | max_position_embeddings = getattr(config, "max_position_embeddings", 8192) |
| 363 | dual_chunk_attention_config = getattr( |
| 364 | config, "dual_chunk_attention_config", None |
| 365 | ) |
| 366 | self.self_attn = Qwen3MoeAttention( |
| 367 | hidden_size=self.hidden_size, |
| 368 | num_heads=config.num_attention_heads, |
| 369 | num_kv_heads=config.num_key_value_heads, |
| 370 | rope_parameters=config.rope_parameters, |
| 371 | max_position_embeddings=max_position_embeddings, |
| 372 | rms_norm_eps=config.rms_norm_eps, |
| 373 | qkv_bias=getattr(config, "attention_bias", False), |
| 374 | head_dim=getattr(config, "head_dim", None), |
| 375 | cache_config=cache_config, |
| 376 | quant_config=quant_config, |
| 377 | prefix=f"{prefix}.self_attn", |
| 378 | dual_chunk_attention_config=dual_chunk_attention_config, |
| 379 | ) |
| 380 | |
| 381 | # `mlp_only_layers` in the config. |
| 382 | layer_idx = extract_layer_index(prefix) |
| 383 | mlp_only_layers = ( |
| 384 | [] if not hasattr(config, "mlp_only_layers") else config.mlp_only_layers |
| 385 | ) |
| 386 | if (layer_idx not in mlp_only_layers) and ( |
| 387 | config.num_experts > 0 and (layer_idx + 1) % config.decoder_sparse_step == 0 |
| 388 | ): |
| 389 | self.mlp = Qwen3MoeSparseMoeBlock( |
| 390 | vllm_config=vllm_config, |
| 391 | prefix=f"{prefix}.mlp", |
| 392 | is_fused_checkpoint_transposed=is_fused_checkpoint_transposed, |
| 393 | ) |
| 394 | else: |
| 395 | self.mlp = Qwen3MoeMLP( |
| 396 | hidden_size=config.hidden_size, |
| 397 | intermediate_size=config.intermediate_size, |
| 398 | hidden_act=config.hidden_act, |
| 399 | quant_config=quant_config, |
| 400 | prefix=f"{prefix}.mlp", |
| 401 | ) |
| 402 | self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) |
| 403 | self.post_attention_layernorm = RMSNorm( |
| 404 | config.hidden_size, eps=config.rms_norm_eps |
| 405 | ) |
| 406 | |
| 407 | def forward( |
| 408 | self, |
| 409 | positions: torch.Tensor, |
| 410 | hidden_states: torch.Tensor, |
| 411 | residual: torch.Tensor | None, |
| 412 | ) -> tuple[torch.Tensor, torch.Tensor]: |
| 413 | # Self Attention |
| 414 | if residual is None: |
| 415 | residual = hidden_states |
| 416 | hidden_states = self.input_layernorm(hidden_states) |
| 417 | else: |
| 418 | hidden_states, residual = self.input_layernorm(hidden_states, residual) |
| 419 | hidden_states = self.self_attn( |
| 420 | positions=positions, |
| 421 | hidden_states=hidden_states, |
| 422 | ) |
| 423 | |
| 424 | # Fully Connected |
| 425 | hidden_states, residual = self.post_attention_layernorm(hidden_states, residual) |
| 426 | hidden_states = self.mlp(hidden_states) |
| 427 | return hidden_states, residual |
| 428 | |
| 429 | |
| 430 | @support_torch_compile |
| 431 | class Qwen3MoeModel(nn.Module, EagleModelMixin): |
| 432 | hf_to_vllm_mapper = WeightsMapper( |
| 433 | orig_to_new_stacked={ |
| 434 | # weight_name: (param_name, shard_id) |
| 435 | ".q_proj": (".qkv_proj", "q"), |
| 436 | ".k_proj": (".qkv_proj", "k"), |
| 437 | ".v_proj": (".qkv_proj", "v"), |
| 438 | # .experts.gate_up_proj must be handled by MoERunner.load_weights for EP |
| 439 | ".mlp.gate_proj": (".mlp.gate_up_proj", 0), |
| 440 | ".mlp.up_proj": (".mlp.gate_up_proj", 1), |
| 441 | ".shared_expert.gate_proj": (".shared_expert.gate_up_proj", 0), |
| 442 | ".shared_expert.up_proj": (".shared_expert.gate_up_proj", 1), |
| 443 | } |
| 444 | ) |
| 445 | |
| 446 | def __init__( |
| 447 | self, |
| 448 | *, |
| 449 | vllm_config: VllmConfig, |
| 450 | prefix: str = "", |
| 451 | decoder_layer_type: type[torch.nn.Module] = Qwen3MoeDecoderLayer, |
| 452 | ): |
| 453 | super().__init__() |
| 454 | |
| 455 | config = vllm_config.model_config.hf_text_config |
| 456 | quant_config = vllm_config.quant_config |
| 457 | parallel_config = vllm_config.parallel_config |
| 458 | eplb_config = parallel_config.eplb_config |
| 459 | self.num_redundant_experts = eplb_config.num_redundant_experts |
| 460 | |
| 461 | self.vocab_size = config.vocab_size |
| 462 | self.config = config |
| 463 | self.quant_config = quant_config |
| 464 | self.embed_tokens = VocabParallelEmbedding( |
| 465 | config.vocab_size, |
| 466 | config.hidden_size, |
| 467 | quant_config=quant_config, |
| 468 | prefix=f"{prefix}.embed_tokens", |
| 469 | ) |
| 470 | self.start_layer, self.end_layer, self.layers = make_layers( |
| 471 | config.num_hidden_layers, |
| 472 | lambda prefix: decoder_layer_type(vllm_config=vllm_config, prefix=prefix), |
| 473 | prefix=f"{prefix}.layers", |
| 474 | ) |
| 475 | self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) |
| 476 | self.make_empty_intermediate_tensors = make_empty_intermediate_tensors_factory( |
| 477 | ["hidden_states", "residual"], config.hidden_size |
| 478 | ) |
| 479 | |
| 480 | def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: |
| 481 | return self.embed_tokens(input_ids) |
| 482 | |
| 483 | def forward( |
| 484 | self, |
| 485 | input_ids: torch.Tensor | None, |
| 486 | positions: torch.Tensor, |
| 487 | intermediate_tensors: IntermediateTensors | None = None, |
| 488 | inputs_embeds: torch.Tensor | None = None, |
| 489 | ) -> torch.Tensor | IntermediateTensors | tuple[torch.Tensor, list[torch.Tensor]]: |
| 490 | if get_pp_group().is_first_rank: |
| 491 | if inputs_embeds is not None: |
| 492 | hidden_states = inputs_embeds |
| 493 | else: |
| 494 | hidden_states = self.embed_input_ids(input_ids) |
| 495 | residual = None |
| 496 | else: |
| 497 | assert intermediate_tensors is not None |
| 498 | hidden_states = intermediate_tensors["hidden_states"] |
| 499 | residual = intermediate_tensors["residual"] |
| 500 | |
| 501 | aux_hidden_states = self._maybe_add_hidden_state( |
| 502 | [], self.start_layer, hidden_states, residual |
| 503 | ) |
| 504 | for layer_idx, layer in enumerate( |
| 505 | islice(self.layers, self.start_layer, self.end_layer), |
| 506 | start=self.start_layer, |
| 507 | ): |
| 508 | hidden_states, residual = layer(positions, hidden_states, residual) |
| 509 | self._maybe_add_hidden_state( |
| 510 | aux_hidden_states, layer_idx + 1, hidden_states, residual |
| 511 | ) |
| 512 | |
| 513 | if not get_pp_group().is_last_rank: |
| 514 | return IntermediateTensors( |
| 515 | {"hidden_states": hidden_states, "residual": residual} |
| 516 | ) |
| 517 | hidden_states, _ = self.norm(hidden_states, residual) |
| 518 | |
| 519 | # Return auxiliary hidden states if collected |
| 520 | if len(aux_hidden_states) > 0: |
| 521 | return hidden_states, aux_hidden_states |
| 522 | return hidden_states |
| 523 | |
| 524 | def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: |
| 525 | loader = AutoWeightsLoader( |
| 526 | self, |
| 527 | ignore_unexpected_suffixes=[ |
| 528 | ".bias", |
| 529 | "_bias", |
| 530 | ".weight_scale", |
| 531 | "_weight_scale", |
| 532 | ".input_scale", |
| 533 | "_input_scale", |
| 534 | ], |
| 535 | ) |
| 536 | return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper) |
| 537 | |
| 538 | |
| 539 | class Qwen3MoeForCausalLM( |
| 540 | nn.Module, |
| 541 | SupportsPP, |
| 542 | SupportsLoRA, |
| 543 | SupportsEagle, |
| 544 | SupportsEagle3, |
| 545 | MixtureOfExperts, |
| 546 | SupportsQuant, |
| 547 | ): |
| 548 | hf_to_vllm_mapper = Qwen3MoeModel.hf_to_vllm_mapper |
| 549 | packed_modules_mapping = { |
| 550 | "qkv_proj": [ |
| 551 | "q_proj", |
| 552 | "k_proj", |
| 553 | "v_proj", |
| 554 | ] |
| 555 | } |
| 556 | |
| 557 | embedding_modules = { |
| 558 | "embed_tokens": "input_embeddings", |
| 559 | "lm_head": "output_embeddings", |
| 560 | } |
| 561 | |
| 562 | fall_back_to_pt_during_load = False |
| 563 | |
| 564 | def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): |
| 565 | super().__init__() |
| 566 | config = vllm_config.model_config.hf_text_config |
| 567 | quant_config = vllm_config.quant_config |
| 568 | self.config = config |
| 569 | self.quant_config = quant_config |
| 570 | # Only perform the following mapping when Qwen3MoeMLP exists |
| 571 | if getattr(config, "mlp_only_layers", []): |
| 572 | self.packed_modules_mapping["gate_up_proj"] = ["gate_proj", "up_proj"] |
| 573 | self.model = Qwen3MoeModel( |
| 574 | vllm_config=vllm_config, prefix=maybe_prefix(prefix, "model") |
| 575 | ) |
| 576 | self.lm_head = ParallelLMHead( |
| 577 | config.vocab_size, |
| 578 | config.hidden_size, |
| 579 | quant_config=quant_config, |
| 580 | prefix=maybe_prefix(prefix, "lm_head"), |
| 581 | ) |
| 582 | if self.config.tie_word_embeddings: |
| 583 | self.lm_head = self.lm_head.tie_weights(self.model.embed_tokens) |
| 584 | self.logits_processor = LogitsProcessor(config.vocab_size) |
| 585 | self.make_empty_intermediate_tensors = ( |
| 586 | self.model.make_empty_intermediate_tensors |
| 587 | ) |
| 588 | |
| 589 | # Set MoE hyperparameters |
| 590 | |
| 591 | self.moe_layers = [] |
| 592 | example_layer = None |
| 593 | for layer in self.model.layers: |
| 594 | if isinstance(layer, PPMissingLayer): |
| 595 | continue |
| 596 | |
| 597 | assert isinstance(layer, Qwen3MoeDecoderLayer) |
| 598 | if isinstance(layer.mlp, Qwen3MoeSparseMoeBlock): |
| 599 | example_layer = layer.mlp |
| 600 | self.moe_layers.append(layer.mlp.experts) |
| 601 | |
| 602 | if example_layer is None: |
| 603 | raise RuntimeError("No Qwen3MoE layer found in the model.layers.") |
| 604 | |
| 605 | self.num_moe_layers = len(self.moe_layers) |
| 606 | self.num_expert_groups = 1 |
| 607 | self.num_shared_experts = 0 |
| 608 | self.num_logical_experts = example_layer.n_logical_experts |
| 609 | self.num_physical_experts = example_layer.n_physical_experts |
| 610 | self.num_local_physical_experts = example_layer.n_local_physical_experts |
| 611 | self.num_routed_experts = example_layer.n_routed_experts |
| 612 | self.num_redundant_experts = example_layer.n_redundant_experts |
| 613 | |
| 614 | def update_physical_experts_metadata( |
| 615 | self, |
| 616 | num_physical_experts: int, |
| 617 | num_local_physical_experts: int, |
| 618 | ) -> None: |
| 619 | assert self.num_local_physical_experts == num_local_physical_experts |
| 620 | self.num_physical_experts = num_physical_experts |
| 621 | self.num_local_physical_experts = num_local_physical_experts |
| 622 | self.num_redundant_experts = num_physical_experts - self.num_logical_experts |
| 623 | for layer in self.model.layers: |
| 624 | if isinstance(layer.mlp, Qwen3MoeSparseMoeBlock): |
| 625 | moe = layer.mlp |
| 626 | moe.n_local_physical_experts = num_local_physical_experts |
| 627 | moe.n_physical_experts = num_physical_experts |
| 628 | moe.n_redundant_experts = self.num_redundant_experts |
| 629 | moe.experts.update_expert_map() |
| 630 | |
| 631 | def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: |
| 632 | return self.model.embed_input_ids(input_ids) |
| 633 | |
| 634 | def forward( |
| 635 | self, |
| 636 | input_ids: torch.Tensor | None, |
| 637 | positions: torch.Tensor, |
| 638 | intermediate_tensors: IntermediateTensors | None = None, |
| 639 | inputs_embeds: torch.Tensor | None = None, |
| 640 | ) -> torch.Tensor | IntermediateTensors: |
| 641 | hidden_states = self.model( |
| 642 | input_ids, positions, intermediate_tensors, inputs_embeds |
| 643 | ) |
| 644 | return hidden_states |
| 645 | |
| 646 | def compute_logits( |
| 647 | self, |
| 648 | hidden_states: torch.Tensor, |
| 649 | ) -> torch.Tensor | None: |
| 650 | logits = self.logits_processor(self.lm_head, hidden_states) |
| 651 | return logits |
| 652 | |
| 653 | def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: |
| 654 | loader = AutoWeightsLoader(self) |
| 655 | return loader.load_weights(weights) |