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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 2 |
+
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
| 3 |
+
|
| 4 |
+
# Adapted from
|
| 5 |
+
# https://github.com/huggingface/transformers/blob/v4.28.0/src/transformers/models/llama/modeling_llama.py
|
| 6 |
+
# Copyright 2023 The vLLM team.
|
| 7 |
+
# Copyright 2023 DeepSeek-AI and the HuggingFace Inc. team. All rights reserved.
|
| 8 |
+
#
|
| 9 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
| 10 |
+
# and OPT implementations in this library. It has been modified from its
|
| 11 |
+
# original forms to accommodate minor architectural differences compared
|
| 12 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
| 13 |
+
#
|
| 14 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 15 |
+
# you may not use this file except in compliance with the License.
|
| 16 |
+
# You may obtain a copy of the License at
|
| 17 |
+
#
|
| 18 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 19 |
+
#
|
| 20 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 21 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 22 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 23 |
+
# See the License for the specific language governing permissions and
|
| 24 |
+
# limitations under the License.
|
| 25 |
+
"""Inference-only DeepseekV2/DeepseekV3 model."""
|
| 26 |
+
import typing
|
| 27 |
+
from collections.abc import Callable, Iterable
|
| 28 |
+
from typing import Any, Optional, Union
|
| 29 |
+
|
| 30 |
+
import torch
|
| 31 |
+
from torch import nn
|
| 32 |
+
from transformers import PretrainedConfig
|
| 33 |
+
|
| 34 |
+
from vllm.attention import Attention
|
| 35 |
+
from vllm.compilation.decorators import support_torch_compile
|
| 36 |
+
from vllm.config import (CacheConfig, ModelConfig, VllmConfig,
|
| 37 |
+
get_current_vllm_config)
|
| 38 |
+
from vllm.distributed import (get_ep_group, get_pp_group,
|
| 39 |
+
get_tensor_model_parallel_world_size)
|
| 40 |
+
from vllm.model_executor.layers.activation import SiluAndMul
|
| 41 |
+
from vllm.model_executor.layers.fused_moe import FusedMoE
|
| 42 |
+
from vllm.model_executor.layers.layernorm import RMSNorm
|
| 43 |
+
from vllm.model_executor.layers.linear import (ColumnParallelLinear,
|
| 44 |
+
MergedColumnParallelLinear,
|
| 45 |
+
ReplicatedLinear,
|
| 46 |
+
RowParallelLinear)
|
| 47 |
+
from vllm.model_executor.layers.logits_processor import LogitsProcessor
|
| 48 |
+
from vllm.model_executor.layers.quantization import QuantizationConfig
|
| 49 |
+
from vllm.model_executor.layers.rotary_embedding import get_rope
|
| 50 |
+
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
| 51 |
+
ParallelLMHead, VocabParallelEmbedding)
|
| 52 |
+
from vllm.model_executor.model_loader.weight_utils import (
|
| 53 |
+
default_weight_loader, maybe_remap_kv_scale_name)
|
| 54 |
+
from vllm.model_executor.sampling_metadata import SamplingMetadata
|
| 55 |
+
from vllm.sequence import IntermediateTensors
|
| 56 |
+
|
| 57 |
+
from .interfaces import MixtureOfExperts, SupportsPP
|
| 58 |
+
from .utils import (PPMissingLayer, is_pp_missing_parameter,
|
| 59 |
+
make_empty_intermediate_tensors_factory, make_layers,
|
| 60 |
+
maybe_prefix)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class DeepseekV2MLP(nn.Module):
|
| 64 |
+
|
| 65 |
+
def __init__(
|
| 66 |
+
self,
|
| 67 |
+
hidden_size: int,
|
| 68 |
+
intermediate_size: int,
|
| 69 |
+
hidden_act: str,
|
| 70 |
+
quant_config: Optional[QuantizationConfig] = None,
|
| 71 |
+
reduce_results: bool = True,
|
| 72 |
+
prefix: str = "",
|
| 73 |
+
) -> None:
|
| 74 |
+
super().__init__()
|
| 75 |
+
self.gate_up_proj = MergedColumnParallelLinear(
|
| 76 |
+
hidden_size, [intermediate_size] * 2,
|
| 77 |
+
bias=False,
|
| 78 |
+
quant_config=quant_config,
|
| 79 |
+
prefix=f"{prefix}.gate_up_proj")
|
| 80 |
+
self.down_proj = RowParallelLinear(intermediate_size,
|
| 81 |
+
hidden_size,
|
| 82 |
+
bias=False,
|
| 83 |
+
quant_config=quant_config,
|
| 84 |
+
reduce_results=reduce_results,
|
| 85 |
+
prefix=f"{prefix}.down_proj")
|
| 86 |
+
if hidden_act != "silu":
|
| 87 |
+
raise ValueError(f"Unsupported activation: {hidden_act}. "
|
| 88 |
+
"Only silu is supported for now.")
|
| 89 |
+
self.act_fn = SiluAndMul()
|
| 90 |
+
|
| 91 |
+
def forward(self, x):
|
| 92 |
+
gate_up, _ = self.gate_up_proj(x)
|
| 93 |
+
x = self.act_fn(gate_up)
|
| 94 |
+
x, _ = self.down_proj(x)
|
| 95 |
+
return x
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class DeepseekV2MoE(nn.Module):
|
| 99 |
+
|
| 100 |
+
def __init__(
|
| 101 |
+
self,
|
| 102 |
+
config: PretrainedConfig,
|
| 103 |
+
quant_config: Optional[QuantizationConfig] = None,
|
| 104 |
+
prefix: str = "",
|
| 105 |
+
enable_eplb: bool = False,
|
| 106 |
+
):
|
| 107 |
+
super().__init__()
|
| 108 |
+
self.tp_size = get_tensor_model_parallel_world_size()
|
| 109 |
+
self.routed_scaling_factor = config.routed_scaling_factor
|
| 110 |
+
|
| 111 |
+
self.ep_group = get_ep_group().device_group
|
| 112 |
+
self.ep_rank = self.ep_group.rank()
|
| 113 |
+
self.ep_size = self.ep_group.size()
|
| 114 |
+
self.n_routed_experts: int = config.n_routed_experts
|
| 115 |
+
self.n_shared_experts: int = config.n_shared_experts
|
| 116 |
+
|
| 117 |
+
if config.hidden_act != "silu":
|
| 118 |
+
raise ValueError(f"Unsupported activation: {config.hidden_act}. "
|
| 119 |
+
"Only silu is supported for now.")
|
| 120 |
+
|
| 121 |
+
self.gate = ReplicatedLinear(config.hidden_size,
|
| 122 |
+
config.n_routed_experts,
|
| 123 |
+
bias=False,
|
| 124 |
+
quant_config=None,
|
| 125 |
+
prefix=f"{prefix}.gate")
|
| 126 |
+
if config.topk_method == "noaux_tc":
|
| 127 |
+
self.gate.e_score_correction_bias = nn.Parameter(
|
| 128 |
+
torch.empty(config.n_routed_experts, dtype=torch.float32))
|
| 129 |
+
else:
|
| 130 |
+
self.gate.e_score_correction_bias = None
|
| 131 |
+
|
| 132 |
+
# Load balancing settings.
|
| 133 |
+
vllm_config = get_current_vllm_config()
|
| 134 |
+
parallel_config = vllm_config.parallel_config
|
| 135 |
+
self.enable_eplb = enable_eplb
|
| 136 |
+
|
| 137 |
+
self.n_redundant_experts = parallel_config.num_redundant_experts
|
| 138 |
+
self.n_logical_experts = self.n_routed_experts
|
| 139 |
+
self.n_physical_experts = (self.n_logical_experts +
|
| 140 |
+
self.n_redundant_experts)
|
| 141 |
+
self.n_local_physical_experts = self.n_physical_experts // self.ep_size
|
| 142 |
+
|
| 143 |
+
self.physical_expert_start = (self.ep_rank *
|
| 144 |
+
self.n_local_physical_experts)
|
| 145 |
+
self.physical_expert_end = (self.physical_expert_start +
|
| 146 |
+
self.n_local_physical_experts)
|
| 147 |
+
|
| 148 |
+
self.experts = FusedMoE(
|
| 149 |
+
num_experts=config.n_routed_experts,
|
| 150 |
+
top_k=config.num_experts_per_tok,
|
| 151 |
+
hidden_size=config.hidden_size,
|
| 152 |
+
intermediate_size=config.moe_intermediate_size,
|
| 153 |
+
reduce_results=False,
|
| 154 |
+
renormalize=config.norm_topk_prob,
|
| 155 |
+
quant_config=quant_config,
|
| 156 |
+
use_grouped_topk=True,
|
| 157 |
+
num_expert_group=config.n_group,
|
| 158 |
+
topk_group=config.topk_group,
|
| 159 |
+
prefix=f"{prefix}.experts",
|
| 160 |
+
scoring_func=config.scoring_func,
|
| 161 |
+
e_score_correction_bias=self.gate.e_score_correction_bias,
|
| 162 |
+
enable_eplb=self.enable_eplb,
|
| 163 |
+
num_redundant_experts=self.n_redundant_experts)
|
| 164 |
+
|
| 165 |
+
if config.n_shared_experts is not None:
|
| 166 |
+
intermediate_size = (config.moe_intermediate_size *
|
| 167 |
+
config.n_shared_experts)
|
| 168 |
+
self.shared_experts = DeepseekV2MLP(
|
| 169 |
+
hidden_size=config.hidden_size,
|
| 170 |
+
intermediate_size=intermediate_size,
|
| 171 |
+
hidden_act=config.hidden_act,
|
| 172 |
+
quant_config=quant_config,
|
| 173 |
+
reduce_results=self.experts.must_reduce_shared_expert_outputs(
|
| 174 |
+
),
|
| 175 |
+
prefix=f"{prefix}.shared_experts",
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 179 |
+
num_tokens, hidden_dim = hidden_states.shape
|
| 180 |
+
hidden_states = hidden_states.view(-1, hidden_dim)
|
| 181 |
+
if self.n_shared_experts is not None:
|
| 182 |
+
shared_output = self.shared_experts(hidden_states)
|
| 183 |
+
# router_logits: (num_tokens, n_experts)
|
| 184 |
+
router_logits, _ = self.gate(hidden_states)
|
| 185 |
+
|
| 186 |
+
if hidden_states.dtype != torch.float16:
|
| 187 |
+
final_hidden_states = self.experts(
|
| 188 |
+
hidden_states=hidden_states,
|
| 189 |
+
router_logits=router_logits) * self.routed_scaling_factor
|
| 190 |
+
else:
|
| 191 |
+
# Fix FP16 overflow
|
| 192 |
+
# See DeepseekV2DecoderLayer for more details.
|
| 193 |
+
final_hidden_states = self.experts(hidden_states=hidden_states,
|
| 194 |
+
router_logits=router_logits)
|
| 195 |
+
if shared_output is not None:
|
| 196 |
+
if hidden_states.dtype != torch.float16:
|
| 197 |
+
final_hidden_states = final_hidden_states + shared_output
|
| 198 |
+
else:
|
| 199 |
+
# Fix FP16 overflow
|
| 200 |
+
# See DeepseekV2DecoderLayer for more details.
|
| 201 |
+
final_hidden_states = final_hidden_states + shared_output \
|
| 202 |
+
* (1. / self.routed_scaling_factor)
|
| 203 |
+
|
| 204 |
+
if self.tp_size > 1:
|
| 205 |
+
final_hidden_states = (
|
| 206 |
+
self.experts.maybe_all_reduce_tensor_model_parallel(
|
| 207 |
+
final_hidden_states))
|
| 208 |
+
|
| 209 |
+
return final_hidden_states.view(num_tokens, hidden_dim)
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def yarn_get_mscale(scale: float = 1, mscale: float = 1) -> float:
|
| 213 |
+
import math
|
| 214 |
+
if scale <= 1:
|
| 215 |
+
return 1.0
|
| 216 |
+
return 0.1 * mscale * math.log(scale) + 1.0
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
class DeepseekV2Attention(nn.Module):
|
| 220 |
+
|
| 221 |
+
def __init__(
|
| 222 |
+
self,
|
| 223 |
+
config: PretrainedConfig,
|
| 224 |
+
hidden_size: int,
|
| 225 |
+
num_heads: int,
|
| 226 |
+
qk_nope_head_dim: int,
|
| 227 |
+
qk_rope_head_dim: int,
|
| 228 |
+
v_head_dim: int,
|
| 229 |
+
q_lora_rank: int,
|
| 230 |
+
kv_lora_rank: int,
|
| 231 |
+
rope_theta: float = 10000,
|
| 232 |
+
rope_scaling: Optional[dict[str, Any]] = None,
|
| 233 |
+
max_position_embeddings: int = 8192,
|
| 234 |
+
cache_config: Optional[CacheConfig] = None,
|
| 235 |
+
quant_config: Optional[QuantizationConfig] = None,
|
| 236 |
+
prefix: str = "",
|
| 237 |
+
) -> None:
|
| 238 |
+
super().__init__()
|
| 239 |
+
self.hidden_size = hidden_size
|
| 240 |
+
self.qk_nope_head_dim = qk_nope_head_dim
|
| 241 |
+
self.qk_rope_head_dim = qk_rope_head_dim
|
| 242 |
+
self.qk_head_dim = qk_nope_head_dim + qk_rope_head_dim
|
| 243 |
+
self.v_head_dim = v_head_dim
|
| 244 |
+
self.q_lora_rank = q_lora_rank
|
| 245 |
+
self.kv_lora_rank = kv_lora_rank
|
| 246 |
+
self.num_heads = num_heads
|
| 247 |
+
tp_size = get_tensor_model_parallel_world_size()
|
| 248 |
+
assert num_heads % tp_size == 0
|
| 249 |
+
self.num_local_heads = num_heads // tp_size
|
| 250 |
+
self.scaling = self.qk_head_dim ** -0.5
|
| 251 |
+
self.rope_theta = rope_theta
|
| 252 |
+
self.max_position_embeddings = max_position_embeddings
|
| 253 |
+
|
| 254 |
+
if self.q_lora_rank is not None:
|
| 255 |
+
self.q_a_proj = ReplicatedLinear(self.hidden_size,
|
| 256 |
+
self.q_lora_rank,
|
| 257 |
+
bias=False,
|
| 258 |
+
quant_config=quant_config,
|
| 259 |
+
prefix=f"{prefix}.q_a_proj")
|
| 260 |
+
self.q_a_layernorm = RMSNorm(self.q_lora_rank,
|
| 261 |
+
eps=config.rms_norm_eps)
|
| 262 |
+
self.q_b_proj = ColumnParallelLinear(q_lora_rank,
|
| 263 |
+
self.num_heads *
|
| 264 |
+
self.qk_head_dim,
|
| 265 |
+
bias=False,
|
| 266 |
+
quant_config=quant_config,
|
| 267 |
+
prefix=f"{prefix}.q_b_proj")
|
| 268 |
+
else:
|
| 269 |
+
self.q_proj = ColumnParallelLinear(self.hidden_size,
|
| 270 |
+
self.num_heads *
|
| 271 |
+
self.qk_head_dim,
|
| 272 |
+
bias=False,
|
| 273 |
+
quant_config=quant_config,
|
| 274 |
+
prefix=f"{prefix}.q_proj")
|
| 275 |
+
|
| 276 |
+
self.kv_a_proj_with_mqa = ReplicatedLinear(
|
| 277 |
+
self.hidden_size,
|
| 278 |
+
self.kv_lora_rank + self.qk_rope_head_dim,
|
| 279 |
+
bias=False,
|
| 280 |
+
quant_config=quant_config,
|
| 281 |
+
prefix=f"{prefix}.kv_a_proj_with_mqa")
|
| 282 |
+
self.kv_a_layernorm = RMSNorm(self.kv_lora_rank,
|
| 283 |
+
eps=config.rms_norm_eps)
|
| 284 |
+
self.kv_b_proj = ColumnParallelLinear(
|
| 285 |
+
self.kv_lora_rank,
|
| 286 |
+
self.num_heads * (self.qk_nope_head_dim + self.v_head_dim),
|
| 287 |
+
bias=False,
|
| 288 |
+
quant_config=quant_config,
|
| 289 |
+
prefix=f"{prefix}.kv_b_proj")
|
| 290 |
+
# O projection.
|
| 291 |
+
self.o_proj = RowParallelLinear(self.num_heads * self.v_head_dim,
|
| 292 |
+
self.hidden_size,
|
| 293 |
+
bias=False,
|
| 294 |
+
quant_config=quant_config,
|
| 295 |
+
prefix=f"{prefix}.o_proj")
|
| 296 |
+
if rope_scaling:
|
| 297 |
+
rope_scaling["rope_type"] = 'deepseek_yarn'
|
| 298 |
+
|
| 299 |
+
self.rotary_emb = get_rope(qk_rope_head_dim,
|
| 300 |
+
rotary_dim=qk_rope_head_dim,
|
| 301 |
+
max_position=max_position_embeddings,
|
| 302 |
+
base=rope_theta,
|
| 303 |
+
rope_scaling=rope_scaling,
|
| 304 |
+
is_neox_style=False)
|
| 305 |
+
|
| 306 |
+
if rope_scaling:
|
| 307 |
+
mscale_all_dim = rope_scaling.get("mscale_all_dim", False)
|
| 308 |
+
scaling_factor = rope_scaling["factor"]
|
| 309 |
+
mscale = yarn_get_mscale(scaling_factor, float(mscale_all_dim))
|
| 310 |
+
self.scaling = self.scaling * mscale * mscale
|
| 311 |
+
|
| 312 |
+
self.attn = Attention(self.num_local_heads,
|
| 313 |
+
self.qk_head_dim,
|
| 314 |
+
self.scaling,
|
| 315 |
+
num_kv_heads=self.num_local_heads,
|
| 316 |
+
cache_config=cache_config,
|
| 317 |
+
quant_config=quant_config,
|
| 318 |
+
prefix=f"{prefix}.attn")
|
| 319 |
+
|
| 320 |
+
def forward(
|
| 321 |
+
self,
|
| 322 |
+
positions: torch.Tensor,
|
| 323 |
+
hidden_states: torch.Tensor,
|
| 324 |
+
) -> torch.Tensor:
|
| 325 |
+
if self.q_lora_rank is not None:
|
| 326 |
+
q = self.q_a_proj(hidden_states)[0]
|
| 327 |
+
q = self.q_a_layernorm(q)
|
| 328 |
+
q = self.q_b_proj(q)[0].view(-1, self.num_local_heads,
|
| 329 |
+
self.qk_head_dim)
|
| 330 |
+
else:
|
| 331 |
+
q = self.q_proj(hidden_states)[0].view(-1, self.num_local_heads,
|
| 332 |
+
self.qk_head_dim)
|
| 333 |
+
q_nope, q_pe = q.split([self.qk_nope_head_dim, self.qk_rope_head_dim],
|
| 334 |
+
dim=-1)
|
| 335 |
+
latent_cache = self.kv_a_proj_with_mqa(hidden_states)[0]
|
| 336 |
+
kv_a, _ = latent_cache.split(
|
| 337 |
+
[self.kv_lora_rank, self.qk_rope_head_dim], dim=-1)
|
| 338 |
+
latent_cache = latent_cache.unsqueeze(1)
|
| 339 |
+
kv_a = self.kv_a_layernorm(kv_a.contiguous())
|
| 340 |
+
kv = self.kv_b_proj(kv_a)[0]
|
| 341 |
+
kv = kv.view(-1, self.num_local_heads,
|
| 342 |
+
self.qk_nope_head_dim + self.v_head_dim)
|
| 343 |
+
k_nope, v = kv.split([self.qk_nope_head_dim, self.v_head_dim], dim=-1)
|
| 344 |
+
k_pe = latent_cache[:, :, self.kv_lora_rank:]
|
| 345 |
+
|
| 346 |
+
q_pe, k_pe = self.rotary_emb(positions, q_pe, k_pe)
|
| 347 |
+
|
| 348 |
+
q[..., self.qk_nope_head_dim:] = q_pe
|
| 349 |
+
k = torch.empty_like(q)
|
| 350 |
+
k[..., :self.qk_nope_head_dim] = k_nope
|
| 351 |
+
k[..., self.qk_nope_head_dim:] = k_pe
|
| 352 |
+
# padding value to qk_head_dim for alignment
|
| 353 |
+
v = torch.nn.functional.pad(
|
| 354 |
+
v, [0, self.qk_head_dim - self.v_head_dim],
|
| 355 |
+
value=0).view(-1, self.num_local_heads * self.qk_head_dim)
|
| 356 |
+
attn_output = self.attn(q, k, v)
|
| 357 |
+
attn_output = attn_output.view(
|
| 358 |
+
-1, self.num_local_heads,
|
| 359 |
+
self.qk_head_dim)[..., :self.v_head_dim].reshape(
|
| 360 |
+
-1, self.num_local_heads * self.v_head_dim)
|
| 361 |
+
output, _ = self.o_proj(attn_output)
|
| 362 |
+
return output
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
class DeepseekV2MLAAttention(nn.Module):
|
| 366 |
+
"""
|
| 367 |
+
Main reference: DeepseekV2 paper, and FlashInfer Implementation
|
| 368 |
+
(https://arxiv.org/abs/2405.04434 and https://github.com/flashinfer-ai/flashinfer/pull/551).
|
| 369 |
+
|
| 370 |
+
For more info see MLACommonImpl in: vllm/attention/backends/mla/utils.py
|
| 371 |
+
"""
|
| 372 |
+
|
| 373 |
+
def __init__(
|
| 374 |
+
self,
|
| 375 |
+
config: PretrainedConfig,
|
| 376 |
+
hidden_size: int,
|
| 377 |
+
num_heads: int,
|
| 378 |
+
qk_nope_head_dim: int,
|
| 379 |
+
qk_rope_head_dim: int,
|
| 380 |
+
v_head_dim: int,
|
| 381 |
+
q_lora_rank: Optional[int],
|
| 382 |
+
kv_lora_rank: int,
|
| 383 |
+
rope_theta: float = 10000,
|
| 384 |
+
rope_scaling: Optional[dict[str, Any]] = None,
|
| 385 |
+
max_position_embeddings: int = 8192,
|
| 386 |
+
cache_config: Optional[CacheConfig] = None,
|
| 387 |
+
quant_config: Optional[QuantizationConfig] = None,
|
| 388 |
+
prefix: str = "",
|
| 389 |
+
) -> None:
|
| 390 |
+
super().__init__()
|
| 391 |
+
self.hidden_size = hidden_size
|
| 392 |
+
self.qk_nope_head_dim = qk_nope_head_dim
|
| 393 |
+
self.qk_rope_head_dim = qk_rope_head_dim
|
| 394 |
+
self.qk_head_dim = qk_nope_head_dim + qk_rope_head_dim
|
| 395 |
+
self.v_head_dim = v_head_dim
|
| 396 |
+
|
| 397 |
+
self.q_lora_rank = q_lora_rank
|
| 398 |
+
self.kv_lora_rank = kv_lora_rank
|
| 399 |
+
|
| 400 |
+
self.num_heads = num_heads
|
| 401 |
+
tp_size = get_tensor_model_parallel_world_size()
|
| 402 |
+
assert num_heads % tp_size == 0
|
| 403 |
+
self.num_local_heads = num_heads // tp_size
|
| 404 |
+
|
| 405 |
+
self.scaling = self.qk_head_dim ** -0.5
|
| 406 |
+
self.rope_theta = rope_theta
|
| 407 |
+
self.max_position_embeddings = max_position_embeddings
|
| 408 |
+
|
| 409 |
+
if self.q_lora_rank is not None:
|
| 410 |
+
self.q_a_proj = ReplicatedLinear(self.hidden_size,
|
| 411 |
+
self.q_lora_rank,
|
| 412 |
+
bias=False,
|
| 413 |
+
quant_config=quant_config,
|
| 414 |
+
prefix=f"{prefix}.q_a_proj")
|
| 415 |
+
self.q_a_layernorm = RMSNorm(self.q_lora_rank,
|
| 416 |
+
eps=config.rms_norm_eps)
|
| 417 |
+
self.q_b_proj = ColumnParallelLinear(q_lora_rank,
|
| 418 |
+
self.num_heads *
|
| 419 |
+
self.qk_head_dim,
|
| 420 |
+
bias=False,
|
| 421 |
+
quant_config=quant_config,
|
| 422 |
+
prefix=f"{prefix}.q_b_proj")
|
| 423 |
+
else:
|
| 424 |
+
self.q_proj = ColumnParallelLinear(self.hidden_size,
|
| 425 |
+
self.num_heads *
|
| 426 |
+
self.qk_head_dim,
|
| 427 |
+
bias=False,
|
| 428 |
+
quant_config=quant_config,
|
| 429 |
+
prefix=f"{prefix}.q_proj")
|
| 430 |
+
|
| 431 |
+
self.kv_a_proj_with_mqa = ReplicatedLinear(
|
| 432 |
+
self.hidden_size,
|
| 433 |
+
self.kv_lora_rank + self.qk_rope_head_dim,
|
| 434 |
+
bias=False,
|
| 435 |
+
quant_config=quant_config,
|
| 436 |
+
prefix=f"{prefix}.kv_a_proj_with_mqa")
|
| 437 |
+
self.kv_a_layernorm = RMSNorm(self.kv_lora_rank,
|
| 438 |
+
eps=config.rms_norm_eps)
|
| 439 |
+
self.kv_b_proj = ColumnParallelLinear(
|
| 440 |
+
self.kv_lora_rank,
|
| 441 |
+
self.num_heads * (self.qk_nope_head_dim + self.v_head_dim),
|
| 442 |
+
bias=False,
|
| 443 |
+
quant_config=quant_config,
|
| 444 |
+
prefix=f"{prefix}.kv_b_proj")
|
| 445 |
+
self.o_proj = RowParallelLinear(self.num_heads * self.v_head_dim,
|
| 446 |
+
self.hidden_size,
|
| 447 |
+
bias=False,
|
| 448 |
+
quant_config=quant_config,
|
| 449 |
+
prefix=f"{prefix}.o_proj")
|
| 450 |
+
|
| 451 |
+
if rope_scaling:
|
| 452 |
+
rope_scaling["rope_type"] = 'deepseek_yarn'
|
| 453 |
+
self.rotary_emb = get_rope(qk_rope_head_dim,
|
| 454 |
+
rotary_dim=qk_rope_head_dim,
|
| 455 |
+
max_position=max_position_embeddings,
|
| 456 |
+
base=rope_theta,
|
| 457 |
+
rope_scaling=rope_scaling,
|
| 458 |
+
is_neox_style=False)
|
| 459 |
+
if rope_scaling:
|
| 460 |
+
mscale_all_dim = rope_scaling.get("mscale_all_dim", False)
|
| 461 |
+
scaling_factor = rope_scaling["factor"]
|
| 462 |
+
mscale = yarn_get_mscale(scaling_factor, float(mscale_all_dim))
|
| 463 |
+
self.scaling = self.scaling * mscale * mscale
|
| 464 |
+
|
| 465 |
+
# In the MLA backend, kv_cache includes both k_c and
|
| 466 |
+
# pe (i.e. decoupled position embeddings). In particular,
|
| 467 |
+
# the concat_and_cache_mla op requires
|
| 468 |
+
# k_c.size(1) + k_pe.size(1) == kv_cache.size(2)
|
| 469 |
+
# i.e.
|
| 470 |
+
# kv_lora_rank + qk_rope_head_dim == head_size
|
| 471 |
+
self.mla_attn = Attention(
|
| 472 |
+
num_heads=self.num_local_heads,
|
| 473 |
+
head_size=self.kv_lora_rank + self.qk_rope_head_dim,
|
| 474 |
+
scale=self.scaling,
|
| 475 |
+
num_kv_heads=1,
|
| 476 |
+
cache_config=cache_config,
|
| 477 |
+
quant_config=quant_config,
|
| 478 |
+
prefix=f"{prefix}.attn",
|
| 479 |
+
use_mla=True,
|
| 480 |
+
# MLA Args
|
| 481 |
+
q_lora_rank=self.q_lora_rank,
|
| 482 |
+
kv_lora_rank=self.kv_lora_rank,
|
| 483 |
+
qk_nope_head_dim=self.qk_nope_head_dim,
|
| 484 |
+
qk_rope_head_dim=self.qk_rope_head_dim,
|
| 485 |
+
qk_head_dim=self.qk_head_dim,
|
| 486 |
+
v_head_dim=self.v_head_dim,
|
| 487 |
+
kv_b_proj=self.kv_b_proj,
|
| 488 |
+
)
|
| 489 |
+
|
| 490 |
+
self.prefix = prefix
|
| 491 |
+
self.debug_layer_idx = int(self.prefix.split(".")[-2])
|
| 492 |
+
|
| 493 |
+
def forward(
|
| 494 |
+
self,
|
| 495 |
+
positions: torch.Tensor,
|
| 496 |
+
hidden_states: torch.Tensor,
|
| 497 |
+
) -> torch.Tensor:
|
| 498 |
+
if self.q_lora_rank is not None:
|
| 499 |
+
q_c = self.q_a_proj(hidden_states)[0]
|
| 500 |
+
q_c = self.q_a_layernorm(q_c)
|
| 501 |
+
q = self.q_b_proj(q_c)[0]
|
| 502 |
+
else:
|
| 503 |
+
q = self.q_proj(hidden_states)[0]
|
| 504 |
+
kv_c, k_pe = self.kv_a_proj_with_mqa(hidden_states)[0].split(
|
| 505 |
+
[self.kv_lora_rank, self.qk_rope_head_dim], dim=-1)
|
| 506 |
+
kv_c_normed = self.kv_a_layernorm(kv_c.contiguous())
|
| 507 |
+
|
| 508 |
+
q = q.view(-1, self.num_local_heads, self.qk_head_dim)
|
| 509 |
+
# Add head dim of 1 to k_pe
|
| 510 |
+
k_pe = k_pe.unsqueeze(1)
|
| 511 |
+
|
| 512 |
+
q[..., self.qk_nope_head_dim:], k_pe = self.rotary_emb(
|
| 513 |
+
positions, q[..., self.qk_nope_head_dim:], k_pe)
|
| 514 |
+
|
| 515 |
+
attn_out = self.mla_attn(
|
| 516 |
+
q,
|
| 517 |
+
kv_c_normed,
|
| 518 |
+
k_pe,
|
| 519 |
+
output_shape=(hidden_states.shape[0],
|
| 520 |
+
self.num_local_heads * self.v_head_dim))
|
| 521 |
+
return self.o_proj(attn_out)[0]
|
| 522 |
+
|
| 523 |
+
|
| 524 |
+
class DeepseekV2DecoderLayer(nn.Module):
|
| 525 |
+
|
| 526 |
+
def __init__(
|
| 527 |
+
self,
|
| 528 |
+
config: PretrainedConfig,
|
| 529 |
+
prefix: str,
|
| 530 |
+
model_config: ModelConfig,
|
| 531 |
+
cache_config: Optional[CacheConfig] = None,
|
| 532 |
+
quant_config: Optional[QuantizationConfig] = None,
|
| 533 |
+
enable_eplb: bool = False,
|
| 534 |
+
) -> None:
|
| 535 |
+
super().__init__()
|
| 536 |
+
self.hidden_size = config.hidden_size
|
| 537 |
+
rope_theta = getattr(config, "rope_theta", 10000)
|
| 538 |
+
rope_scaling = getattr(config, "rope_scaling", None)
|
| 539 |
+
max_position_embeddings = getattr(config, "max_position_embeddings",
|
| 540 |
+
8192)
|
| 541 |
+
# DecoderLayers are created with `make_layers` which passes the prefix
|
| 542 |
+
# with the layer's index.
|
| 543 |
+
layer_idx = int(prefix.split(sep='.')[-1])
|
| 544 |
+
self.layer_idx = layer_idx
|
| 545 |
+
if model_config.use_mla:
|
| 546 |
+
attn_cls = DeepseekV2MLAAttention
|
| 547 |
+
else:
|
| 548 |
+
attn_cls = DeepseekV2Attention
|
| 549 |
+
self.self_attn = attn_cls(
|
| 550 |
+
config=config,
|
| 551 |
+
hidden_size=self.hidden_size,
|
| 552 |
+
num_heads=config.num_attention_heads,
|
| 553 |
+
qk_nope_head_dim=config.qk_nope_head_dim,
|
| 554 |
+
qk_rope_head_dim=config.qk_rope_head_dim,
|
| 555 |
+
v_head_dim=config.v_head_dim,
|
| 556 |
+
q_lora_rank=config.q_lora_rank
|
| 557 |
+
if hasattr(config, "q_lora_rank") else None,
|
| 558 |
+
kv_lora_rank=config.kv_lora_rank,
|
| 559 |
+
rope_theta=rope_theta,
|
| 560 |
+
rope_scaling=rope_scaling,
|
| 561 |
+
max_position_embeddings=max_position_embeddings,
|
| 562 |
+
cache_config=cache_config,
|
| 563 |
+
quant_config=quant_config,
|
| 564 |
+
prefix=f"{prefix}.self_attn",
|
| 565 |
+
)
|
| 566 |
+
|
| 567 |
+
if (config.n_routed_experts is not None
|
| 568 |
+
and layer_idx >= config.first_k_dense_replace
|
| 569 |
+
and layer_idx % config.moe_layer_freq == 0):
|
| 570 |
+
self.mlp = DeepseekV2MoE(
|
| 571 |
+
config=config,
|
| 572 |
+
quant_config=quant_config,
|
| 573 |
+
prefix=f"{prefix}.mlp",
|
| 574 |
+
enable_eplb=enable_eplb,
|
| 575 |
+
)
|
| 576 |
+
else:
|
| 577 |
+
self.mlp = DeepseekV2MLP(
|
| 578 |
+
hidden_size=config.hidden_size,
|
| 579 |
+
intermediate_size=config.intermediate_size,
|
| 580 |
+
hidden_act=config.hidden_act,
|
| 581 |
+
quant_config=quant_config,
|
| 582 |
+
prefix=f"{prefix}.mlp",
|
| 583 |
+
)
|
| 584 |
+
self.input_layernorm = RMSNorm(config.hidden_size,
|
| 585 |
+
eps=config.rms_norm_eps)
|
| 586 |
+
self.post_attention_layernorm = RMSNorm(config.hidden_size,
|
| 587 |
+
eps=config.rms_norm_eps)
|
| 588 |
+
self.routed_scaling_factor = config.routed_scaling_factor
|
| 589 |
+
|
| 590 |
+
def forward(
|
| 591 |
+
self,
|
| 592 |
+
positions: torch.Tensor,
|
| 593 |
+
hidden_states: torch.Tensor,
|
| 594 |
+
residual: Optional[torch.Tensor],
|
| 595 |
+
) -> torch.Tensor:
|
| 596 |
+
# Self Attention
|
| 597 |
+
if residual is None:
|
| 598 |
+
residual = hidden_states
|
| 599 |
+
hidden_states = self.input_layernorm(hidden_states)
|
| 600 |
+
else:
|
| 601 |
+
hidden_states, residual = self.input_layernorm(
|
| 602 |
+
hidden_states, residual)
|
| 603 |
+
hidden_states = self.self_attn(
|
| 604 |
+
positions=positions,
|
| 605 |
+
hidden_states=hidden_states,
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
if hidden_states.dtype == torch.float16:
|
| 609 |
+
# Fix FP16 overflow
|
| 610 |
+
# We scale both hidden_states and residual before
|
| 611 |
+
# rmsnorm, and rmsnorm result would not affect by scale.
|
| 612 |
+
hidden_states *= 1. / self.routed_scaling_factor
|
| 613 |
+
if self.layer_idx == 0:
|
| 614 |
+
# The residual is shared by all layers, we only scale it on
|
| 615 |
+
# first layer.
|
| 616 |
+
residual *= 1. / self.routed_scaling_factor
|
| 617 |
+
|
| 618 |
+
# Fully Connected
|
| 619 |
+
hidden_states, residual = self.post_attention_layernorm(
|
| 620 |
+
hidden_states, residual)
|
| 621 |
+
hidden_states = self.mlp(hidden_states)
|
| 622 |
+
|
| 623 |
+
if isinstance(self.mlp,
|
| 624 |
+
DeepseekV2MLP) and hidden_states.dtype == torch.float16:
|
| 625 |
+
# Fix FP16 overflow
|
| 626 |
+
# Scaling the DeepseekV2MLP output, it is the input of
|
| 627 |
+
# input_layernorm of next decoder layer.
|
| 628 |
+
# The scaling of DeepseekV2MOE output would be done in the forward
|
| 629 |
+
# of DeepseekV2MOE
|
| 630 |
+
hidden_states *= 1. / self.routed_scaling_factor
|
| 631 |
+
|
| 632 |
+
return hidden_states, residual
|
| 633 |
+
|
| 634 |
+
|
| 635 |
+
@support_torch_compile
|
| 636 |
+
class DeepseekV2Model(nn.Module):
|
| 637 |
+
fall_back_to_pt_during_load = False
|
| 638 |
+
|
| 639 |
+
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
|
| 640 |
+
super().__init__()
|
| 641 |
+
|
| 642 |
+
config = vllm_config.model_config.hf_config
|
| 643 |
+
model_config = vllm_config.model_config
|
| 644 |
+
cache_config = vllm_config.cache_config
|
| 645 |
+
quant_config = vllm_config.quant_config
|
| 646 |
+
enable_eplb = vllm_config.parallel_config.enable_eplb
|
| 647 |
+
self.config = config
|
| 648 |
+
|
| 649 |
+
self.vocab_size = config.vocab_size
|
| 650 |
+
|
| 651 |
+
if get_pp_group().is_first_rank:
|
| 652 |
+
self.embed_tokens = VocabParallelEmbedding(
|
| 653 |
+
config.vocab_size,
|
| 654 |
+
config.hidden_size,
|
| 655 |
+
quant_config=quant_config,
|
| 656 |
+
prefix=f"{prefix}.embed_tokens")
|
| 657 |
+
else:
|
| 658 |
+
self.embed_tokens = PPMissingLayer()
|
| 659 |
+
|
| 660 |
+
self.start_layer, self.end_layer, self.layers = make_layers(
|
| 661 |
+
config.num_hidden_layers,
|
| 662 |
+
lambda prefix: DeepseekV2DecoderLayer(
|
| 663 |
+
config,
|
| 664 |
+
prefix,
|
| 665 |
+
model_config=model_config,
|
| 666 |
+
cache_config=cache_config,
|
| 667 |
+
quant_config=quant_config,
|
| 668 |
+
enable_eplb=enable_eplb,
|
| 669 |
+
),
|
| 670 |
+
prefix=f"{prefix}.layers")
|
| 671 |
+
|
| 672 |
+
if get_pp_group().is_last_rank:
|
| 673 |
+
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 674 |
+
else:
|
| 675 |
+
self.norm = PPMissingLayer()
|
| 676 |
+
self.make_empty_intermediate_tensors = (
|
| 677 |
+
make_empty_intermediate_tensors_factory(
|
| 678 |
+
["hidden_states", "residual"], config.hidden_size))
|
| 679 |
+
|
| 680 |
+
def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
|
| 681 |
+
return self.embed_tokens(input_ids)
|
| 682 |
+
|
| 683 |
+
def forward(
|
| 684 |
+
self,
|
| 685 |
+
input_ids: torch.Tensor,
|
| 686 |
+
positions: torch.Tensor,
|
| 687 |
+
intermediate_tensors: Optional[IntermediateTensors],
|
| 688 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 689 |
+
) -> Union[torch.Tensor, IntermediateTensors]:
|
| 690 |
+
if get_pp_group().is_first_rank:
|
| 691 |
+
if inputs_embeds is not None:
|
| 692 |
+
hidden_states = inputs_embeds
|
| 693 |
+
else:
|
| 694 |
+
hidden_states = self.get_input_embeddings(input_ids)
|
| 695 |
+
residual = None
|
| 696 |
+
else:
|
| 697 |
+
assert intermediate_tensors is not None
|
| 698 |
+
hidden_states = intermediate_tensors["hidden_states"]
|
| 699 |
+
residual = intermediate_tensors["residual"]
|
| 700 |
+
|
| 701 |
+
for layer in self.layers[self.start_layer:self.end_layer]:
|
| 702 |
+
hidden_states, residual = layer(positions, hidden_states, residual)
|
| 703 |
+
|
| 704 |
+
if not get_pp_group().is_last_rank:
|
| 705 |
+
return IntermediateTensors({
|
| 706 |
+
"hidden_states": hidden_states,
|
| 707 |
+
"residual": residual
|
| 708 |
+
})
|
| 709 |
+
|
| 710 |
+
hidden_states, _ = self.norm(hidden_states, residual)
|
| 711 |
+
return hidden_states
|
| 712 |
+
|
| 713 |
+
|
| 714 |
+
class DeepseekV2ForCausalLM(nn.Module, SupportsPP, MixtureOfExperts):
|
| 715 |
+
|
| 716 |
+
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
|
| 717 |
+
super().__init__()
|
| 718 |
+
config = vllm_config.model_config.hf_config
|
| 719 |
+
quant_config = vllm_config.quant_config
|
| 720 |
+
self.config = config
|
| 721 |
+
self.quant_config = quant_config
|
| 722 |
+
self.model = DeepseekV2Model(vllm_config=vllm_config,
|
| 723 |
+
prefix=maybe_prefix(prefix, "model"))
|
| 724 |
+
if get_pp_group().is_last_rank:
|
| 725 |
+
self.lm_head = ParallelLMHead(config.vocab_size,
|
| 726 |
+
config.hidden_size,
|
| 727 |
+
quant_config=quant_config)
|
| 728 |
+
else:
|
| 729 |
+
self.lm_head = PPMissingLayer()
|
| 730 |
+
self.logits_processor = LogitsProcessor(config.vocab_size)
|
| 731 |
+
self.make_empty_intermediate_tensors = (
|
| 732 |
+
self.model.make_empty_intermediate_tensors)
|
| 733 |
+
self.expert_weights = []
|
| 734 |
+
|
| 735 |
+
# Set MoE hyperparameters
|
| 736 |
+
self.num_moe_layers = (config.num_hidden_layers -
|
| 737 |
+
config.first_k_dense_replace)
|
| 738 |
+
self.num_expert_groups = config.n_group
|
| 739 |
+
|
| 740 |
+
self.moe_layers: list[FusedMoE] = []
|
| 741 |
+
for layer in self.model.layers:
|
| 742 |
+
assert isinstance(layer, DeepseekV2DecoderLayer)
|
| 743 |
+
if isinstance(layer.mlp, DeepseekV2MoE):
|
| 744 |
+
self.moe_layers.append(layer.mlp.experts)
|
| 745 |
+
|
| 746 |
+
# Pick last one layer since the first ones may be dense layers.
|
| 747 |
+
example_moe = typing.cast(
|
| 748 |
+
DeepseekV2MoE, self.model.layers[config.num_hidden_layers - 1].mlp)
|
| 749 |
+
self.num_logical_experts = example_moe.n_logical_experts
|
| 750 |
+
self.num_physical_experts = example_moe.n_physical_experts
|
| 751 |
+
self.num_local_physical_experts = example_moe.n_local_physical_experts
|
| 752 |
+
self.num_routed_experts = example_moe.n_routed_experts
|
| 753 |
+
self.num_shared_experts = example_moe.n_shared_experts
|
| 754 |
+
self.num_redundant_experts = example_moe.n_redundant_experts
|
| 755 |
+
|
| 756 |
+
def set_eplb_state(
|
| 757 |
+
self,
|
| 758 |
+
expert_load_view: torch.Tensor,
|
| 759 |
+
logical_to_physical_map: torch.Tensor,
|
| 760 |
+
logical_replica_count: torch.Tensor,
|
| 761 |
+
) -> None:
|
| 762 |
+
for layer_idx, layer in enumerate(self.moe_layers):
|
| 763 |
+
# Register the expert weights.
|
| 764 |
+
self.expert_weights.append(layer.get_expert_weights())
|
| 765 |
+
layer.set_eplb_state(
|
| 766 |
+
moe_layer_idx=layer_idx,
|
| 767 |
+
expert_load_view=expert_load_view,
|
| 768 |
+
logical_to_physical_map=logical_to_physical_map,
|
| 769 |
+
logical_replica_count=logical_replica_count,
|
| 770 |
+
)
|
| 771 |
+
|
| 772 |
+
def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
|
| 773 |
+
return self.model.get_input_embeddings(input_ids)
|
| 774 |
+
|
| 775 |
+
def forward(
|
| 776 |
+
self,
|
| 777 |
+
input_ids: torch.Tensor,
|
| 778 |
+
positions: torch.Tensor,
|
| 779 |
+
intermediate_tensors: Optional[IntermediateTensors] = None,
|
| 780 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 781 |
+
) -> Union[torch.Tensor, IntermediateTensors]:
|
| 782 |
+
hidden_states = self.model(input_ids, positions, intermediate_tensors,
|
| 783 |
+
inputs_embeds)
|
| 784 |
+
return hidden_states
|
| 785 |
+
|
| 786 |
+
def compute_logits(
|
| 787 |
+
self,
|
| 788 |
+
hidden_states: torch.Tensor,
|
| 789 |
+
sampling_metadata: SamplingMetadata,
|
| 790 |
+
) -> Optional[torch.Tensor]:
|
| 791 |
+
logits = self.logits_processor(self.lm_head, hidden_states,
|
| 792 |
+
sampling_metadata)
|
| 793 |
+
return logits
|
| 794 |
+
|
| 795 |
+
def make_empty_intermediate_tensors(
|
| 796 |
+
self, batch_size: int, dtype: torch.dtype,
|
| 797 |
+
device: torch.device) -> IntermediateTensors:
|
| 798 |
+
return IntermediateTensors({
|
| 799 |
+
"hidden_states":
|
| 800 |
+
torch.zeros((batch_size, self.config.hidden_size),
|
| 801 |
+
dtype=dtype,
|
| 802 |
+
device=device),
|
| 803 |
+
"residual":
|
| 804 |
+
torch.zeros((batch_size, self.config.hidden_size),
|
| 805 |
+
dtype=dtype,
|
| 806 |
+
device=device),
|
| 807 |
+
})
|
| 808 |
+
|
| 809 |
+
def load_weights(self, weights: Iterable[tuple[str,
|
| 810 |
+
torch.Tensor]]) -> set[str]:
|
| 811 |
+
stacked_params_mapping = [
|
| 812 |
+
# (param_name, shard_name, shard_id)
|
| 813 |
+
("gate_up_proj", "gate_proj", 0),
|
| 814 |
+
("gate_up_proj", "up_proj", 1),
|
| 815 |
+
]
|
| 816 |
+
|
| 817 |
+
# Params for weights, fp8 weight scales, fp8 activation scales
|
| 818 |
+
# (param_name, weight_name, expert_id, shard_id)
|
| 819 |
+
expert_params_mapping = FusedMoE.make_expert_params_mapping(
|
| 820 |
+
ckpt_gate_proj_name="gate_proj",
|
| 821 |
+
ckpt_down_proj_name="down_proj",
|
| 822 |
+
ckpt_up_proj_name="up_proj",
|
| 823 |
+
num_experts=self.config.n_routed_experts,
|
| 824 |
+
num_redundant_experts=self.num_redundant_experts)
|
| 825 |
+
|
| 826 |
+
params_dict = dict(self.named_parameters())
|
| 827 |
+
loaded_params: set[str] = set()
|
| 828 |
+
for name, loaded_weight in weights:
|
| 829 |
+
if "rotary_emb.inv_freq" in name:
|
| 830 |
+
continue
|
| 831 |
+
|
| 832 |
+
spec_layer = get_spec_layer_idx_from_weight_name(self.config, name)
|
| 833 |
+
if spec_layer is not None:
|
| 834 |
+
continue # skip spec decode layers for main model
|
| 835 |
+
|
| 836 |
+
for (param_name, weight_name, shard_id) in stacked_params_mapping:
|
| 837 |
+
# Skip non-stacked layers and experts (experts handled below).
|
| 838 |
+
if weight_name not in name:
|
| 839 |
+
continue
|
| 840 |
+
# We have mlp.experts[0].gate_proj in the checkpoint.
|
| 841 |
+
# Since we handle the experts below in expert_params_mapping,
|
| 842 |
+
# we need to skip here BEFORE we update the name, otherwise
|
| 843 |
+
# name will be updated to mlp.experts[0].gate_up_proj, which
|
| 844 |
+
# will then be updated below in expert_params_mapping
|
| 845 |
+
# for mlp.experts[0].gate_gate_up_proj, which breaks load.
|
| 846 |
+
if (("mlp.experts." in name) and name not in params_dict):
|
| 847 |
+
continue
|
| 848 |
+
name = name.replace(weight_name, param_name)
|
| 849 |
+
# Skip loading extra bias for GPTQ models.
|
| 850 |
+
if name.endswith(".bias") and name not in params_dict:
|
| 851 |
+
continue
|
| 852 |
+
|
| 853 |
+
if is_pp_missing_parameter(name, self):
|
| 854 |
+
continue
|
| 855 |
+
|
| 856 |
+
param = params_dict[name]
|
| 857 |
+
weight_loader = param.weight_loader
|
| 858 |
+
weight_loader(param, loaded_weight, shard_id)
|
| 859 |
+
break
|
| 860 |
+
else:
|
| 861 |
+
is_expert_weight = False
|
| 862 |
+
for mapping in expert_params_mapping:
|
| 863 |
+
param_name, weight_name, expert_id, shard_id = mapping
|
| 864 |
+
if weight_name not in name:
|
| 865 |
+
continue
|
| 866 |
+
|
| 867 |
+
# Anyway, this is an expert weight and should not be
|
| 868 |
+
# attempted to load as other weights later
|
| 869 |
+
is_expert_weight = True
|
| 870 |
+
|
| 871 |
+
# Do not modify `name` since the loop may continue here
|
| 872 |
+
# Instead, create a new variable
|
| 873 |
+
name_mapped = name.replace(weight_name, param_name)
|
| 874 |
+
|
| 875 |
+
if is_pp_missing_parameter(name_mapped, self):
|
| 876 |
+
continue
|
| 877 |
+
|
| 878 |
+
param = params_dict[name_mapped]
|
| 879 |
+
# We should ask the weight loader to return success or not
|
| 880 |
+
# here since otherwise we may skip experts with other
|
| 881 |
+
# available replicas.
|
| 882 |
+
weight_loader = typing.cast(Callable[..., bool],
|
| 883 |
+
param.weight_loader)
|
| 884 |
+
success = weight_loader(param,
|
| 885 |
+
loaded_weight,
|
| 886 |
+
name_mapped,
|
| 887 |
+
shard_id=shard_id,
|
| 888 |
+
expert_id=expert_id,
|
| 889 |
+
return_success=True)
|
| 890 |
+
if success:
|
| 891 |
+
name = name_mapped
|
| 892 |
+
break
|
| 893 |
+
else:
|
| 894 |
+
if is_expert_weight:
|
| 895 |
+
# We've checked that this is an expert weight
|
| 896 |
+
# However it's not mapped locally to this rank
|
| 897 |
+
# So we simply skip it
|
| 898 |
+
continue
|
| 899 |
+
|
| 900 |
+
# Skip loading extra bias for GPTQ models.
|
| 901 |
+
if name.endswith(".bias") and name not in params_dict:
|
| 902 |
+
continue
|
| 903 |
+
|
| 904 |
+
# Remapping the name of FP8 kv-scale.
|
| 905 |
+
name = maybe_remap_kv_scale_name(name, params_dict)
|
| 906 |
+
if name is None:
|
| 907 |
+
continue
|
| 908 |
+
|
| 909 |
+
if is_pp_missing_parameter(name, self):
|
| 910 |
+
continue
|
| 911 |
+
|
| 912 |
+
param = params_dict[name]
|
| 913 |
+
weight_loader = getattr(param, "weight_loader",
|
| 914 |
+
default_weight_loader)
|
| 915 |
+
weight_loader(param, loaded_weight)
|
| 916 |
+
loaded_params.add(name)
|
| 917 |
+
|
| 918 |
+
return loaded_params
|
| 919 |
+
|
| 920 |
+
|
| 921 |
+
class DeepseekV3ForCausalLM(DeepseekV2ForCausalLM):
|
| 922 |
+
pass
|
| 923 |
+
|
| 924 |
+
|
| 925 |
+
def get_spec_layer_idx_from_weight_name(config: PretrainedConfig,
|
| 926 |
+
weight_name: str) -> Optional[int]:
|
| 927 |
+
if hasattr(config,
|
| 928 |
+
"num_nextn_predict_layers") and (config.num_nextn_predict_layers
|
| 929 |
+
> 0):
|
| 930 |
+
layer_idx = config.num_hidden_layers
|
| 931 |
+
for i in range(config.num_nextn_predict_layers):
|
| 932 |
+
if weight_name.startswith(f"model.layers.{layer_idx + i}."):
|
| 933 |
+
return layer_idx + i
|
| 934 |
+
return None
|