94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
595 lines
21 KiB
Python
595 lines
21 KiB
Python
# coding=utf-8
|
|
# Copyright 2026 The HunYuan team.
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from typing import Iterable, Optional, Tuple
|
|
|
|
import torch
|
|
from torch import nn
|
|
from transformers import PretrainedConfig
|
|
|
|
from sglang.srt.distributed import (
|
|
moe_expert_parallel_all_reduce,
|
|
moe_tensor_model_parallel_all_reduce,
|
|
)
|
|
from sglang.srt.layers.activation import SiluAndMul
|
|
from sglang.srt.layers.layernorm import RMSNorm
|
|
from sglang.srt.layers.linear import (
|
|
MergedColumnParallelLinear,
|
|
QKVParallelLinear,
|
|
ReplicatedLinear,
|
|
RowParallelLinear,
|
|
)
|
|
from sglang.srt.layers.logits_processor import LogitsProcessor
|
|
from sglang.srt.layers.moe import should_skip_post_experts_all_reduce
|
|
from sglang.srt.layers.moe.fused_moe_triton.layer import FusedMoE
|
|
from sglang.srt.layers.moe.topk import TopK
|
|
from sglang.srt.layers.quantization.base_config import QuantizationConfig
|
|
from sglang.srt.layers.radix_attention import RadixAttention
|
|
from sglang.srt.layers.rotary_embedding import get_rope
|
|
from sglang.srt.layers.vocab_parallel_embedding import (
|
|
ParallelLMHead,
|
|
VocabParallelEmbedding,
|
|
)
|
|
from sglang.srt.managers.schedule_batch import ForwardBatch
|
|
from sglang.srt.model_executor.runner import get_is_capture_mode
|
|
from sglang.srt.model_loader.weight_utils import default_weight_loader
|
|
from sglang.srt.runtime_context import get_parallel, get_stream
|
|
from sglang.srt.utils import is_cuda
|
|
from sglang.srt.utils.hf_transformers_utils import get_rope_config
|
|
|
|
|
|
class HYV3FeedForward(nn.Module):
|
|
def __init__(
|
|
self,
|
|
hidden_size: int,
|
|
intermediate_size: int,
|
|
hidden_act: str,
|
|
quant_config: Optional[QuantizationConfig] = None,
|
|
reduce_results: bool = True,
|
|
prefix: str = "",
|
|
) -> None:
|
|
super().__init__()
|
|
self.gate_up_proj = MergedColumnParallelLinear(
|
|
hidden_size,
|
|
[intermediate_size] * 2,
|
|
bias=False,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.gate_up_proj",
|
|
)
|
|
self.down_proj = RowParallelLinear(
|
|
intermediate_size,
|
|
hidden_size,
|
|
bias=False,
|
|
quant_config=quant_config,
|
|
reduce_results=reduce_results,
|
|
prefix=f"{prefix}.down_proj",
|
|
)
|
|
if hidden_act != "silu":
|
|
raise ValueError(
|
|
f"Unsupported activation: {hidden_act}. Only silu is supported for now."
|
|
)
|
|
self.act_fn = SiluAndMul()
|
|
|
|
def forward(self, x):
|
|
gate_up, _ = self.gate_up_proj(x)
|
|
out = self.act_fn(gate_up)
|
|
out, _ = self.down_proj(out)
|
|
return out
|
|
|
|
|
|
class HYV3MoEFused(nn.Module):
|
|
def __init__(
|
|
self,
|
|
config: PretrainedConfig,
|
|
layer_id: int,
|
|
quant_config: Optional[QuantizationConfig] = None,
|
|
prefix: str = "",
|
|
alt_stream: Optional[torch.cuda.Stream] = None,
|
|
):
|
|
super().__init__()
|
|
self.tp_size = get_parallel().moe_tp_size
|
|
self.ep_size = get_parallel().moe_ep_size
|
|
self.layer_id = layer_id
|
|
self.alt_stream = alt_stream
|
|
self.n_routed_experts = config.num_experts
|
|
top_k = config.num_experts_per_tok
|
|
intermediate_size = config.moe_intermediate_size
|
|
|
|
self.expert_bias = nn.Parameter(
|
|
torch.empty(config.num_experts, dtype=torch.float32)
|
|
)
|
|
self.expert_bias.weight_loader = HYV3MoEFused.ebias_weight_loader
|
|
scoring_func = "sigmoid"
|
|
self.e_score_correction_bias = self.expert_bias
|
|
self.router_scaling_factor = getattr(config, "router_scaling_factor", 1.0)
|
|
self.gate = ReplicatedLinear(
|
|
config.hidden_size,
|
|
config.num_experts,
|
|
bias=False,
|
|
quant_config=None,
|
|
params_dtype=torch.float32,
|
|
prefix=f"{prefix}.gate",
|
|
)
|
|
self.topk = TopK(
|
|
top_k=config.num_experts_per_tok,
|
|
use_grouped_topk=True,
|
|
num_expert_group=1,
|
|
topk_group=1,
|
|
renormalize=config.route_norm,
|
|
scoring_func=scoring_func,
|
|
correction_bias=self.e_score_correction_bias,
|
|
routed_scaling_factor=self.router_scaling_factor,
|
|
apply_routed_scaling_factor_on_output=True,
|
|
)
|
|
|
|
if getattr(config, "num_shared_experts", 0) > 0:
|
|
self.shared_mlp = HYV3FeedForward(
|
|
hidden_size=config.hidden_size,
|
|
intermediate_size=config.moe_intermediate_size
|
|
* config.num_shared_experts,
|
|
hidden_act=config.hidden_act,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.shared_mlp",
|
|
reduce_results=False,
|
|
)
|
|
else:
|
|
self.shared_mlp = None
|
|
|
|
self.experts = FusedMoE(
|
|
num_experts=self.n_routed_experts,
|
|
top_k=top_k,
|
|
hidden_size=config.hidden_size,
|
|
intermediate_size=intermediate_size,
|
|
reduce_results=False,
|
|
layer_id=layer_id,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.experts",
|
|
)
|
|
|
|
@staticmethod
|
|
def ebias_weight_loader(param: nn.Parameter, loaded_weight: torch.Tensor) -> None:
|
|
assert param.size() == loaded_weight.size()
|
|
param.data.copy_(loaded_weight.to(torch.float32))
|
|
|
|
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
|
if (
|
|
self.alt_stream is not None
|
|
and self.shared_mlp is not None
|
|
and hidden_states.shape[0] > 0
|
|
and get_is_capture_mode()
|
|
):
|
|
return self._forward_dual_stream(hidden_states)
|
|
return self._forward_single_stream(hidden_states)
|
|
|
|
def _forward_single_stream(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
|
orig_shape = hidden_states.shape
|
|
hidden_dim = hidden_states.shape[-1]
|
|
hidden_states = hidden_states.view(-1, hidden_dim)
|
|
|
|
router_logits, _ = self.gate(hidden_states.to(dtype=torch.float32))
|
|
topk_output = self.topk(hidden_states, router_logits)
|
|
if self.shared_mlp is not None:
|
|
shared_output = self.shared_mlp(hidden_states)
|
|
final_hidden_states = self.experts(
|
|
hidden_states=hidden_states, topk_output=topk_output
|
|
)
|
|
final_hidden_states = final_hidden_states + shared_output
|
|
else:
|
|
final_hidden_states = self.experts(
|
|
hidden_states=hidden_states, topk_output=topk_output
|
|
)
|
|
|
|
if self.ep_size > 1 and not should_skip_post_experts_all_reduce(
|
|
is_tp_path=False,
|
|
):
|
|
final_hidden_states = moe_expert_parallel_all_reduce(final_hidden_states)
|
|
|
|
if self.tp_size > 1 and not should_skip_post_experts_all_reduce(
|
|
is_tp_path=True,
|
|
):
|
|
final_hidden_states = moe_tensor_model_parallel_all_reduce(
|
|
final_hidden_states
|
|
)
|
|
|
|
return final_hidden_states.view(orig_shape)
|
|
|
|
def _forward_dual_stream(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
|
"""Shared experts on main stream, routed experts on alt stream."""
|
|
orig_shape = hidden_states.shape
|
|
hidden_dim = hidden_states.shape[-1]
|
|
hidden_states = hidden_states.view(-1, hidden_dim)
|
|
|
|
current_stream = torch.cuda.current_stream()
|
|
self.alt_stream.wait_stream(current_stream)
|
|
|
|
shared_output = self.shared_mlp(hidden_states)
|
|
|
|
with torch.cuda.stream(self.alt_stream):
|
|
router_logits, _ = self.gate(hidden_states.to(dtype=torch.float32))
|
|
topk_output = self.topk(hidden_states, router_logits)
|
|
final_hidden_states = self.experts(
|
|
hidden_states=hidden_states, topk_output=topk_output
|
|
)
|
|
|
|
current_stream.wait_stream(self.alt_stream)
|
|
final_hidden_states = final_hidden_states + shared_output
|
|
|
|
if self.ep_size > 1 and not should_skip_post_experts_all_reduce(
|
|
is_tp_path=False,
|
|
):
|
|
final_hidden_states = moe_expert_parallel_all_reduce(final_hidden_states)
|
|
|
|
if self.tp_size > 1 and not should_skip_post_experts_all_reduce(
|
|
is_tp_path=True,
|
|
):
|
|
final_hidden_states = moe_tensor_model_parallel_all_reduce(
|
|
final_hidden_states
|
|
)
|
|
|
|
return final_hidden_states.view(orig_shape)
|
|
|
|
|
|
class HYV3Attention(nn.Module):
|
|
def __init__(
|
|
self,
|
|
config: PretrainedConfig,
|
|
hidden_size: int,
|
|
num_heads: int,
|
|
num_kv_heads: int,
|
|
layer_id: int = 0,
|
|
rope_theta: float = 10000,
|
|
rope_scaling: Optional[dict] = None,
|
|
max_position_embeddings: int = 8192,
|
|
quant_config: Optional[QuantizationConfig] = None,
|
|
prefix: str = "",
|
|
) -> None:
|
|
super().__init__()
|
|
self.hidden_size = hidden_size
|
|
tp_size = get_parallel().tp_size
|
|
self.total_num_heads = num_heads
|
|
assert self.total_num_heads % tp_size == 0
|
|
self.num_heads = self.total_num_heads // tp_size
|
|
self.total_num_kv_heads = num_kv_heads
|
|
if self.total_num_kv_heads >= tp_size:
|
|
assert self.total_num_kv_heads % tp_size == 0
|
|
else:
|
|
assert tp_size % self.total_num_kv_heads == 0
|
|
self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size)
|
|
|
|
self.head_dim = getattr(config, "head_dim", hidden_size // self.total_num_heads)
|
|
self.q_size = self.num_heads * self.head_dim
|
|
self.kv_size = self.num_kv_heads * self.head_dim
|
|
self.scaling = self.head_dim**-0.5
|
|
self.use_qk_norm = getattr(
|
|
config, "use_qk_norm", getattr(config, "qk_norm", False)
|
|
)
|
|
|
|
self.qkv_proj = QKVParallelLinear(
|
|
hidden_size,
|
|
self.head_dim,
|
|
self.total_num_heads,
|
|
self.total_num_kv_heads,
|
|
bias=False,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.qkv_proj",
|
|
)
|
|
self.o_proj = RowParallelLinear(
|
|
self.total_num_heads * self.head_dim,
|
|
hidden_size,
|
|
bias=False,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.o_proj",
|
|
)
|
|
|
|
self.rotary_emb = get_rope(
|
|
self.head_dim,
|
|
rotary_dim=self.head_dim,
|
|
max_position=max_position_embeddings,
|
|
base=rope_theta,
|
|
rope_scaling=rope_scaling,
|
|
is_neox_style=True,
|
|
)
|
|
self.attn = RadixAttention(
|
|
self.num_heads,
|
|
self.head_dim,
|
|
self.scaling,
|
|
num_kv_heads=self.num_kv_heads,
|
|
layer_id=layer_id,
|
|
prefix=f"{prefix}.attn",
|
|
)
|
|
if self.use_qk_norm:
|
|
rms_norm_eps = getattr(config, "rms_norm_eps", 1e-5)
|
|
self.q_norm = RMSNorm(self.head_dim, rms_norm_eps)
|
|
self.k_norm = RMSNorm(self.head_dim, rms_norm_eps)
|
|
|
|
def forward(
|
|
self,
|
|
positions: torch.Tensor,
|
|
hidden_states: torch.Tensor,
|
|
forward_batch: ForwardBatch,
|
|
) -> torch.Tensor:
|
|
qkv, _ = self.qkv_proj(hidden_states)
|
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
|
|
|
if self.use_qk_norm:
|
|
q = self.q_norm(q.reshape(-1, self.head_dim))
|
|
q = q.view(-1, self.q_size)
|
|
k = self.k_norm(k.reshape(-1, self.head_dim))
|
|
k = k.view(-1, self.kv_size)
|
|
|
|
q, k = self.rotary_emb(positions, q, k)
|
|
attn_output = self.attn(q, k, v, forward_batch)
|
|
output, _ = self.o_proj(attn_output)
|
|
return output
|
|
|
|
|
|
class HYV3DecoderLayer(nn.Module):
|
|
def __init__(
|
|
self,
|
|
config: PretrainedConfig,
|
|
layer_id: int,
|
|
quant_config: Optional[QuantizationConfig] = None,
|
|
prefix: str = "",
|
|
alt_stream: Optional[torch.cuda.Stream] = None,
|
|
) -> None:
|
|
super().__init__()
|
|
self.layer_id = layer_id
|
|
self.hidden_size = config.hidden_size
|
|
max_position_embeddings = getattr(config, "max_position_embeddings", 8192)
|
|
rope_theta, _ = get_rope_config(config)
|
|
self.self_attn = HYV3Attention(
|
|
config=config,
|
|
hidden_size=self.hidden_size,
|
|
num_heads=config.num_attention_heads,
|
|
num_kv_heads=config.num_key_value_heads,
|
|
layer_id=layer_id,
|
|
rope_theta=rope_theta,
|
|
max_position_embeddings=max_position_embeddings,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.self_attn",
|
|
)
|
|
self.input_layernorm = RMSNorm(config.hidden_size, config.rms_norm_eps)
|
|
self.post_attention_layernorm = RMSNorm(config.hidden_size, config.rms_norm_eps)
|
|
|
|
first_k_dense_replace = getattr(config, "first_k_dense_replace", 0)
|
|
if layer_id < first_k_dense_replace:
|
|
self.mlp = HYV3FeedForward(
|
|
hidden_size=config.hidden_size,
|
|
intermediate_size=config.intermediate_size,
|
|
hidden_act=config.hidden_act,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.mlp",
|
|
)
|
|
self.block_type = "feedforward"
|
|
else:
|
|
self.mlp = HYV3MoEFused(
|
|
config=config,
|
|
layer_id=layer_id,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.mlp",
|
|
alt_stream=alt_stream,
|
|
)
|
|
self.block_type = "moe"
|
|
|
|
def forward(
|
|
self,
|
|
positions: torch.Tensor,
|
|
hidden_states: torch.Tensor,
|
|
forward_batch: ForwardBatch,
|
|
residual: Optional[torch.Tensor],
|
|
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
if residual is None:
|
|
residual = hidden_states
|
|
hidden_states = self.input_layernorm(hidden_states)
|
|
else:
|
|
hidden_states, residual = self.input_layernorm(hidden_states, residual)
|
|
hidden_states = self.self_attn(
|
|
positions=positions,
|
|
hidden_states=hidden_states,
|
|
forward_batch=forward_batch,
|
|
)
|
|
|
|
hidden_states, residual = self.post_attention_layernorm(hidden_states, residual)
|
|
hidden_states = self.mlp(hidden_states)
|
|
|
|
return hidden_states, residual
|
|
|
|
|
|
class HYV3Model(nn.Module):
|
|
def __init__(
|
|
self,
|
|
config: PretrainedConfig,
|
|
quant_config: Optional[QuantizationConfig] = None,
|
|
prefix: str = "",
|
|
):
|
|
super().__init__()
|
|
self.config = config
|
|
self.quant_config = quant_config
|
|
|
|
self.embed_tokens = VocabParallelEmbedding(
|
|
config.vocab_size,
|
|
config.hidden_size,
|
|
prefix=f"{prefix}.embed_tokens",
|
|
)
|
|
|
|
self.alt_stream = get_stream("alt") if is_cuda() else None
|
|
|
|
self.layers = nn.ModuleList(
|
|
[
|
|
HYV3DecoderLayer(
|
|
config=config,
|
|
layer_id=i,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.layers.{i}",
|
|
alt_stream=self.alt_stream,
|
|
)
|
|
for i in range(config.num_hidden_layers)
|
|
]
|
|
)
|
|
self.norm = RMSNorm(config.hidden_size, config.rms_norm_eps)
|
|
|
|
@torch.no_grad()
|
|
def forward(
|
|
self,
|
|
input_ids: torch.Tensor,
|
|
positions: torch.Tensor,
|
|
forward_batch: ForwardBatch,
|
|
input_embeds: torch.Tensor = None,
|
|
) -> torch.Tensor:
|
|
if input_embeds is None:
|
|
hidden_states = self.embed_tokens(input_ids)
|
|
else:
|
|
hidden_states = input_embeds
|
|
residual = None
|
|
for layer in self.layers:
|
|
hidden_states, residual = layer(
|
|
positions, hidden_states, forward_batch, residual
|
|
)
|
|
|
|
hidden_states, _ = self.norm(hidden_states, residual)
|
|
return hidden_states
|
|
|
|
|
|
class HYV3ForCausalLM(nn.Module):
|
|
def __init__(
|
|
self,
|
|
config: PretrainedConfig,
|
|
quant_config: Optional[QuantizationConfig] = None,
|
|
prefix: str = "",
|
|
):
|
|
super().__init__()
|
|
self.config = config
|
|
self.quant_config = quant_config
|
|
|
|
self.model = HYV3Model(config, quant_config, prefix=f"{prefix}.model")
|
|
self.lm_head = ParallelLMHead(
|
|
config.vocab_size,
|
|
config.hidden_size,
|
|
quant_config=quant_config,
|
|
prefix=f"{prefix}.lm_head",
|
|
)
|
|
if getattr(self.config, "tie_word_embeddings", False):
|
|
self.lm_head.weight = self.model.embed_tokens.weight
|
|
self.logits_processor = LogitsProcessor(config)
|
|
|
|
@torch.no_grad()
|
|
def forward(
|
|
self,
|
|
input_ids: torch.Tensor,
|
|
positions: torch.Tensor,
|
|
forward_batch: ForwardBatch,
|
|
input_embeds: torch.Tensor = None,
|
|
) -> torch.Tensor:
|
|
hidden_states = self.model(input_ids, positions, forward_batch, input_embeds)
|
|
return self.logits_processor(
|
|
input_ids, hidden_states, self.lm_head, forward_batch
|
|
)
|
|
|
|
def get_embed_and_head(self):
|
|
return self.model.embed_tokens.weight, self.lm_head.weight
|
|
|
|
def set_embed_and_head(self, embed, head):
|
|
del self.model.embed_tokens.weight
|
|
del self.lm_head.weight
|
|
self.model.embed_tokens.weight = embed
|
|
self.lm_head.weight = head
|
|
torch.cuda.empty_cache()
|
|
torch.cuda.synchronize()
|
|
|
|
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
|
|
stacked_params_mapping = [
|
|
("qkv_proj", "q_proj", "q"),
|
|
("qkv_proj", "k_proj", "k"),
|
|
("qkv_proj", "v_proj", "v"),
|
|
("gate_up_proj", "gate_proj", 0),
|
|
("gate_up_proj", "up_proj", 1),
|
|
]
|
|
|
|
# Params for weights, fp8 weight scales, fp8 activation scales
|
|
# (param_name, weight_name, expert_id, shard_id)
|
|
expert_params_mapping = FusedMoE.make_expert_params_mapping(
|
|
ckpt_gate_proj_name="gate_proj",
|
|
ckpt_down_proj_name="down_proj",
|
|
ckpt_up_proj_name="up_proj",
|
|
num_experts=self.config.num_experts,
|
|
)
|
|
|
|
params_dict = dict(self.named_parameters())
|
|
num_nextn_layers = getattr(self.config, "num_nextn_predict_layers", 0)
|
|
|
|
for name, loaded_weight in weights:
|
|
if "lm_head.weight" in name and getattr(
|
|
self.config, "tie_word_embeddings", False
|
|
):
|
|
continue
|
|
|
|
if "rotary_emb.inv_freq" in name:
|
|
continue
|
|
|
|
if num_nextn_layers > 0 and name.startswith("model.layers."):
|
|
parts = name.split(".")
|
|
if len(parts) >= 3 and int(parts[2]) >= self.config.num_hidden_layers:
|
|
continue
|
|
|
|
is_found = False
|
|
for param_name, weight_name, shard_id in stacked_params_mapping:
|
|
if weight_name not in name:
|
|
continue
|
|
if "mlp.experts" in name:
|
|
continue
|
|
name = name.replace(weight_name, param_name)
|
|
if name not in params_dict:
|
|
continue
|
|
param = params_dict[name]
|
|
weight_loader = param.weight_loader
|
|
weight_loader(param, loaded_weight, shard_id)
|
|
is_found = True
|
|
break
|
|
if is_found:
|
|
continue
|
|
|
|
# Handle expert weights (including fp8 weight_scale, input_scale)
|
|
is_expert_weight = False
|
|
for mapping in expert_params_mapping:
|
|
param_name, weight_name, expert_id, shard_id = mapping
|
|
if weight_name not in name:
|
|
continue
|
|
is_expert_weight = True
|
|
name_mapped = name.replace(weight_name, param_name)
|
|
if name_mapped not in params_dict:
|
|
continue
|
|
param = params_dict[name_mapped]
|
|
weight_loader = param.weight_loader
|
|
weight_loader(
|
|
param,
|
|
loaded_weight,
|
|
name_mapped,
|
|
shard_id=shard_id,
|
|
expert_id=expert_id,
|
|
)
|
|
break
|
|
if is_expert_weight:
|
|
continue
|
|
|
|
if "router.gate." in name:
|
|
name = name.replace("router.", "")
|
|
if name not in params_dict:
|
|
continue
|
|
param = params_dict[name]
|
|
weight_loader = getattr(param, "weight_loader", default_weight_loader)
|
|
weight_loader(param, loaded_weight)
|
|
|
|
|
|
EntryClass = [HYV3ForCausalLM]
|