59a0a3844c
PR Test AMD / cancel-on-close (push) Has been skipped
PR Test NVIDIA ARM / scan (push) Has been skipped
PR Test NVIDIA / cancel-on-close (push) Has been skipped
PR Test AMD / scan (push) Has been skipped
PR Test NVIDIA ARM / cancel-on-close (push) Has been skipped
PR Test NVIDIA / scan (push) Has been skipped
Release Docker Images / build (cu129-torch-2.11.0) (push) Has been skipped
Release Docker Images / build (cu130-torch-2.11.0) (push) Has been skipped
Release PyPI / publish (push) Has been skipped
Scheduler Python Test / test (push) Successful in 27m19s
Docs / build (push) Successful in 28m8s
Scheduler C++ Test / test (push) Successful in 28m19s
Scheduler C++ Test / test-flat (push) Successful in 28m18s
Docs / deploy (push) Has been cancelled
PR Test AMD / finish (push) Has been cancelled
PR Test NVIDIA / finish (push) Has been cancelled
PR Test NVIDIA ARM / finish (push) Has been cancelled
PR Test NVIDIA ARM / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
PR Test AMD / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
PR Test NVIDIA / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
496 lines
18 KiB
Python
496 lines
18 KiB
Python
# Copyright (c) 2026 LightSeek Foundation
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
# SOFTWARE.
|
|
|
|
"""Inference-only Qwen2 model compatible with HuggingFace weights."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterable
|
|
from typing import Any
|
|
|
|
import torch
|
|
from torch import nn
|
|
|
|
from tokenspeed.runtime.configs.qwen2_config import Qwen2Config
|
|
from tokenspeed.runtime.configs.utils import get_rope_theta
|
|
from tokenspeed.runtime.distributed.comm_ops import all_reduce
|
|
from tokenspeed.runtime.distributed.mapping import Mapping
|
|
from tokenspeed.runtime.execution.context import ForwardContext
|
|
from tokenspeed.runtime.layers.activation import SiluAndMul
|
|
from tokenspeed.runtime.layers.layernorm import RMSNorm
|
|
from tokenspeed.runtime.layers.linear import (
|
|
MergedColumnParallelLinear,
|
|
QKVParallelLinear,
|
|
RowParallelLinear,
|
|
)
|
|
from tokenspeed.runtime.layers.paged_attention import PagedAttention
|
|
from tokenspeed.runtime.layers.quantization.base_config import QuantizationConfig
|
|
from tokenspeed.runtime.layers.rotary_embedding import get_rope
|
|
from tokenspeed.runtime.layers.utils import get_layer_id
|
|
from tokenspeed.runtime.layers.vocab_parallel_embedding import VocabParallelEmbedding
|
|
from tokenspeed.runtime.model_loader.weight_utils import (
|
|
default_weight_loader,
|
|
kv_cache_scales_loader,
|
|
)
|
|
from tokenspeed.runtime.models.base import BaseCausalLM
|
|
from tokenspeed.runtime.models.utils import validate_attention_partition
|
|
from tokenspeed.runtime.utils import add_prefix, make_layers
|
|
from tokenspeed.runtime.utils.env import global_server_args_dict
|
|
|
|
|
|
class Qwen2MLP(nn.Module):
|
|
def __init__(
|
|
self,
|
|
hidden_size: int,
|
|
intermediate_size: int,
|
|
hidden_act: str,
|
|
quant_config: QuantizationConfig | None = None,
|
|
tp_rank: int | None = None,
|
|
tp_size: int | None = None,
|
|
tp_group: tuple[int, ...] | None = None,
|
|
) -> None:
|
|
super().__init__()
|
|
self.gate_up_proj = MergedColumnParallelLinear(
|
|
hidden_size,
|
|
[intermediate_size] * 2,
|
|
bias=False,
|
|
quant_config=quant_config,
|
|
tp_rank=tp_rank,
|
|
tp_size=tp_size,
|
|
tp_group=tp_group,
|
|
)
|
|
self.down_proj = RowParallelLinear(
|
|
intermediate_size,
|
|
hidden_size,
|
|
bias=False,
|
|
quant_config=quant_config,
|
|
reduce_results=False,
|
|
tp_rank=tp_rank,
|
|
tp_size=tp_size,
|
|
tp_group=tp_group,
|
|
)
|
|
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)
|
|
x = self.act_fn(gate_up)
|
|
x, _ = self.down_proj(x)
|
|
return x
|
|
|
|
|
|
class Qwen2Attention(nn.Module):
|
|
def __init__(
|
|
self,
|
|
config: Qwen2Config,
|
|
mapping: Mapping,
|
|
hidden_size: int,
|
|
num_heads: int,
|
|
num_kv_heads: int,
|
|
layer_id: int = 0,
|
|
rope_theta: float = 1000000,
|
|
rope_scaling: dict[str, Any] | None = None,
|
|
head_dim: int | None = None,
|
|
max_position_embeddings: int = 32768,
|
|
quant_config: QuantizationConfig | None = None,
|
|
prefix: str = "",
|
|
) -> None:
|
|
super().__init__()
|
|
self.mapping = mapping
|
|
self.hidden_size = hidden_size
|
|
self.tp_rank = self.mapping.attn.tp_rank
|
|
self.tp_size = self.mapping.attn.tp_size
|
|
self.total_num_heads = num_heads
|
|
self.total_num_kv_heads = num_kv_heads
|
|
validate_attention_partition(
|
|
self.total_num_heads,
|
|
self.total_num_kv_heads,
|
|
self.tp_size,
|
|
)
|
|
self.num_heads = self.total_num_heads // self.tp_size
|
|
self.num_kv_heads = max(1, self.total_num_kv_heads // self.tp_size)
|
|
self.head_dim = head_dim or 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.rope_theta = rope_theta
|
|
self.max_position_embeddings = max_position_embeddings
|
|
|
|
# Qwen2 uses biases on Q/K/V projections but not on the output projection.
|
|
self.qkv_proj = QKVParallelLinear(
|
|
hidden_size,
|
|
self.head_dim,
|
|
self.total_num_heads,
|
|
self.total_num_kv_heads,
|
|
bias=True,
|
|
quant_config=quant_config,
|
|
prefix=add_prefix("qkv_proj", prefix),
|
|
tp_rank=self.mapping.attn.tp_rank,
|
|
tp_size=self.mapping.attn.tp_size,
|
|
tp_group=self.mapping.attn.tp_group,
|
|
)
|
|
self.o_proj = RowParallelLinear(
|
|
self.total_num_heads * self.head_dim,
|
|
hidden_size,
|
|
bias=False,
|
|
quant_config=quant_config,
|
|
prefix=add_prefix("o_proj", prefix),
|
|
reduce_results=False,
|
|
tp_rank=self.mapping.attn.tp_rank,
|
|
tp_size=self.mapping.attn.tp_size,
|
|
tp_group=self.mapping.attn.tp_group,
|
|
)
|
|
|
|
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,
|
|
)
|
|
self.attn = PagedAttention(
|
|
self.num_heads,
|
|
self.head_dim,
|
|
self.scaling,
|
|
num_kv_heads=self.num_kv_heads,
|
|
layer_id=layer_id,
|
|
)
|
|
|
|
def forward(
|
|
self,
|
|
positions: torch.Tensor,
|
|
hidden_states: torch.Tensor,
|
|
ctx: ForwardContext,
|
|
out_cache_loc: torch.Tensor,
|
|
cos_sin: tuple[torch.Tensor, torch.Tensor] | None = None,
|
|
) -> torch.Tensor:
|
|
qkv, _ = self.qkv_proj(hidden_states)
|
|
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
|
|
q, k = self.rotary_emb(positions, q, k)
|
|
attn_output = self.attn(q, k, v, ctx, out_cache_loc)
|
|
if len(attn_output.size()) == 3:
|
|
attn_output = attn_output.reshape(attn_output.shape[0], -1)
|
|
output, _ = self.o_proj(attn_output)
|
|
return output
|
|
|
|
|
|
class Qwen2DecoderLayer(nn.Module):
|
|
def __init__(
|
|
self,
|
|
config: Qwen2Config,
|
|
mapping: Mapping,
|
|
layer_id: int = 0,
|
|
quant_config: QuantizationConfig | None = None,
|
|
prefix: str = "",
|
|
) -> None:
|
|
super().__init__()
|
|
self.mapping = mapping
|
|
if self.mapping.attn.tp_size != self.mapping.dense.tp_size:
|
|
raise ValueError(
|
|
"Qwen2 does not use CommManager and assumes attn_tp_size == dense_tp_size"
|
|
)
|
|
self.hidden_size = config.hidden_size
|
|
rope_theta = get_rope_theta(config, 1000000)
|
|
rope_scaling = getattr(config, "rope_scaling", None)
|
|
max_position_embeddings = getattr(config, "max_position_embeddings", 32768)
|
|
head_dim = getattr(config, "head_dim", None)
|
|
self.self_attn = Qwen2Attention(
|
|
config=config,
|
|
mapping=self.mapping,
|
|
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,
|
|
rope_scaling=rope_scaling,
|
|
head_dim=head_dim,
|
|
max_position_embeddings=max_position_embeddings,
|
|
quant_config=quant_config,
|
|
prefix=add_prefix("self_attn", prefix),
|
|
)
|
|
self.mlp = Qwen2MLP(
|
|
hidden_size=self.hidden_size,
|
|
intermediate_size=config.intermediate_size,
|
|
hidden_act=config.hidden_act,
|
|
quant_config=quant_config,
|
|
tp_rank=self.mapping.dense.tp_rank,
|
|
tp_size=self.mapping.dense.tp_size,
|
|
tp_group=self.mapping.dense.tp_group,
|
|
)
|
|
self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
self.post_attention_layernorm = RMSNorm(
|
|
config.hidden_size, eps=config.rms_norm_eps
|
|
)
|
|
|
|
def forward(
|
|
self,
|
|
positions: torch.Tensor,
|
|
hidden_states: torch.Tensor,
|
|
ctx: ForwardContext,
|
|
out_cache_loc: torch.Tensor,
|
|
residual: torch.Tensor | None,
|
|
cos_sin: tuple[torch.Tensor, torch.Tensor] | None,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
if residual is None:
|
|
residual = hidden_states
|
|
hidden_states = self.input_layernorm(hidden_states)
|
|
elif (
|
|
ctx.input_num_tokens > global_server_args_dict["comm_fusion_max_num_tokens"]
|
|
):
|
|
hidden_states = all_reduce(hidden_states, self.mapping.dense.tp_group)
|
|
hidden_states, residual = self.input_layernorm(hidden_states, residual)
|
|
else:
|
|
hidden_states, residual, *_ = (
|
|
self.input_layernorm.forward_with_allreduce_fusion(
|
|
self.mapping.dense.tp_rank,
|
|
self.mapping.dense.tp_group,
|
|
hidden_states,
|
|
residual,
|
|
)
|
|
)
|
|
|
|
hidden_states = self.self_attn(
|
|
positions=positions,
|
|
hidden_states=hidden_states,
|
|
ctx=ctx,
|
|
out_cache_loc=out_cache_loc,
|
|
cos_sin=cos_sin,
|
|
)
|
|
|
|
if ctx.input_num_tokens > global_server_args_dict["comm_fusion_max_num_tokens"]:
|
|
hidden_states = all_reduce(hidden_states, self.mapping.attn.tp_group)
|
|
hidden_states, residual = self.post_attention_layernorm(
|
|
hidden_states, residual
|
|
)
|
|
else:
|
|
hidden_states, residual, *_ = (
|
|
self.post_attention_layernorm.forward_with_allreduce_fusion(
|
|
self.mapping.attn.tp_rank,
|
|
self.mapping.attn.tp_group,
|
|
hidden_states,
|
|
residual,
|
|
)
|
|
)
|
|
hidden_states = self.mlp(hidden_states)
|
|
return hidden_states, residual
|
|
|
|
|
|
class Qwen2Model(nn.Module):
|
|
def __init__(
|
|
self,
|
|
config: Qwen2Config,
|
|
mapping: Mapping,
|
|
quant_config: QuantizationConfig | None = None,
|
|
prefix: str = "",
|
|
decoder_layer_type: type[nn.Module] = None,
|
|
) -> None:
|
|
super().__init__()
|
|
self.mapping = mapping
|
|
self.config = config
|
|
self.padding_idx = getattr(config, "pad_token_id", None)
|
|
self.vocab_size = config.vocab_size
|
|
self.embed_tokens = VocabParallelEmbedding(
|
|
config.vocab_size,
|
|
config.hidden_size,
|
|
quant_config=quant_config,
|
|
tp_rank=self.mapping.attn.tp_rank,
|
|
tp_size=self.mapping.attn.tp_size,
|
|
tp_group=self.mapping.attn.tp_group,
|
|
)
|
|
decoder_layer_type = decoder_layer_type or Qwen2DecoderLayer
|
|
self.layers = make_layers(
|
|
config.num_hidden_layers,
|
|
lambda idx, prefix: decoder_layer_type(
|
|
config=config,
|
|
mapping=self.mapping,
|
|
layer_id=idx,
|
|
quant_config=quant_config,
|
|
),
|
|
)
|
|
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
|
|
def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
|
|
if hasattr(self.config, "scale_emb"):
|
|
return self.embed_tokens(input_ids) * self.config.scale_emb
|
|
return self.embed_tokens(input_ids)
|
|
|
|
def forward(
|
|
self,
|
|
input_ids: torch.Tensor,
|
|
positions: torch.Tensor,
|
|
ctx: ForwardContext,
|
|
out_cache_loc: torch.Tensor,
|
|
input_embeds: torch.Tensor | None = None,
|
|
) -> tuple[torch.Tensor, None]:
|
|
if input_embeds is None:
|
|
hidden_states = self.embed_tokens(input_ids)
|
|
else:
|
|
hidden_states = input_embeds
|
|
residual = None
|
|
|
|
for i in range(len(self.layers)):
|
|
layer = self.layers[i]
|
|
hidden_states, residual = layer(
|
|
positions,
|
|
hidden_states,
|
|
ctx,
|
|
out_cache_loc,
|
|
residual,
|
|
cos_sin=None,
|
|
)
|
|
if ctx.input_num_tokens > global_server_args_dict["comm_fusion_max_num_tokens"]:
|
|
hidden_states = all_reduce(hidden_states, self.mapping.dense.tp_group)
|
|
hidden_states, _ = self.norm(hidden_states, residual)
|
|
else:
|
|
hidden_states, *_ = self.norm.forward_with_allreduce_fusion(
|
|
self.mapping.dense.tp_rank,
|
|
self.mapping.dense.tp_group,
|
|
hidden_states,
|
|
residual,
|
|
)
|
|
return hidden_states, None
|
|
|
|
def load_kv_cache_scales(self, quantization_param_path: str) -> None:
|
|
tp_size = self.mapping.attn.tp_size
|
|
tp_rank = self.mapping.attn.tp_rank
|
|
for layer_idx, scaling_factor in kv_cache_scales_loader(
|
|
quantization_param_path,
|
|
tp_rank,
|
|
tp_size,
|
|
self.config.num_hidden_layers,
|
|
self.config.__class__.model_type,
|
|
):
|
|
if not isinstance(self.layers[layer_idx], nn.Identity):
|
|
layer_self_attn = self.layers[layer_idx].self_attn
|
|
if hasattr(layer_self_attn.attn, "k_scale"):
|
|
layer_self_attn.attn.k_scale = scaling_factor
|
|
layer_self_attn.attn.v_scale = scaling_factor
|
|
else:
|
|
raise RuntimeError(
|
|
"Self attention has no KV cache scaling " "factor attribute!"
|
|
)
|
|
|
|
|
|
class Qwen2ForCausalLM(BaseCausalLM):
|
|
model_cls = Qwen2Model
|
|
|
|
default_bitsandbytes_target_modules = [
|
|
".gate_proj.",
|
|
".down_proj.",
|
|
".up_proj.",
|
|
".q_proj.",
|
|
".k_proj.",
|
|
".v_proj.",
|
|
".o_proj.",
|
|
]
|
|
bitsandbytes_stacked_params_mapping = {
|
|
"q_proj": ("qkv_proj", 0),
|
|
"k_proj": ("qkv_proj", 1),
|
|
"v_proj": ("qkv_proj", 2),
|
|
"gate_proj": ("gate_up_proj", 0),
|
|
"up_proj": ("gate_up_proj", 1),
|
|
}
|
|
|
|
def __init__(
|
|
self,
|
|
config: Qwen2Config,
|
|
mapping: Mapping,
|
|
quant_config: QuantizationConfig | None = None,
|
|
) -> None:
|
|
super().__init__(
|
|
config=config,
|
|
mapping=mapping,
|
|
quant_config=quant_config,
|
|
)
|
|
|
|
def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
|
|
return self.model.get_input_embeddings(input_ids)
|
|
|
|
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_dict = dict(self.named_parameters())
|
|
for name, loaded_weight in weights:
|
|
if "Embedding" in self.config.name_or_path:
|
|
name = add_prefix(name, "model")
|
|
layer_id = get_layer_id(name)
|
|
if (
|
|
layer_id is not None
|
|
and hasattr(self.model, "start_layer")
|
|
and (
|
|
layer_id < self.model.start_layer
|
|
or layer_id >= self.model.end_layer
|
|
)
|
|
):
|
|
continue
|
|
|
|
if "rotary_emb.inv_freq" in name or "projector" in name:
|
|
continue
|
|
if "rotary_emb.cos_cached" in name or "rotary_emb.sin_cached" in name:
|
|
continue
|
|
if self.config.tie_word_embeddings and "lm_head.weight" in name:
|
|
continue
|
|
if name.startswith("model.vision_tower") and name not in params_dict:
|
|
continue
|
|
|
|
for param_name, weight_name, shard_id in stacked_params_mapping:
|
|
if weight_name not in name:
|
|
continue
|
|
name = name.replace(weight_name, param_name)
|
|
if name.endswith(".bias") and name not in params_dict:
|
|
continue
|
|
param = params_dict[name]
|
|
weight_loader = param.weight_loader
|
|
weight_loader(param, loaded_weight, shard_id)
|
|
break
|
|
else:
|
|
if name.endswith(".bias") and name not in params_dict:
|
|
continue
|
|
param = params_dict[name]
|
|
weight_loader = getattr(param, "weight_loader", default_weight_loader)
|
|
weight_loader(param, loaded_weight)
|
|
|
|
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_kv_cache_scales(self, quantization_param_path: str) -> None:
|
|
self.model.load_kv_cache_scales(quantization_param_path)
|
|
|
|
|
|
EntryClass = Qwen2ForCausalLM
|