Convert formatting to use ruff instead of yapf + isort (#26247)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Harry Mellor
2025-10-05 07:06:22 -07:00
committed by GitHub
parent 17edd8a807
commit d6953beb91
1508 changed files with 115205 additions and 94107 deletions
+89 -77
View File
@@ -23,6 +23,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Inference-only EagleMiniCPM model compatible with HuggingFace weights."""
import math
from collections.abc import Iterable
from typing import Optional, Union
@@ -37,7 +38,10 @@ from vllm.model_executor.layers.layernorm import RMSNorm
from vllm.model_executor.layers.logits_processor import LogitsProcessor
from vllm.model_executor.layers.quantization import QuantizationConfig
from vllm.model_executor.layers.vocab_parallel_embedding import (
DEFAULT_VOCAB_PADDING_SIZE, ParallelLMHead, VocabParallelEmbedding)
DEFAULT_VOCAB_PADDING_SIZE,
ParallelLMHead,
VocabParallelEmbedding,
)
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
from vllm.sequence import IntermediateTensors
@@ -45,12 +49,15 @@ from .interfaces import SupportsLoRA, SupportsPP
from .minicpm import MiniCPMAttention as EagleMiniCPMAttention
from .minicpm import MiniCPMMLP as EagleMiniCPMMLP
from .minicpm import MiniCPMMoE as EagleMiniCPMMoE
from .utils import (AutoWeightsLoader, is_pp_missing_parameter,
make_empty_intermediate_tensors_factory, maybe_prefix)
from .utils import (
AutoWeightsLoader,
is_pp_missing_parameter,
make_empty_intermediate_tensors_factory,
maybe_prefix,
)
class EagleMiniCPMDecoderLayer(nn.Module):
def __init__(
self,
config: PretrainedConfig,
@@ -65,15 +72,15 @@ class EagleMiniCPMDecoderLayer(nn.Module):
self.hidden_size = config.hidden_size
self.rope_theta = getattr(config, "rope_theta", 10000)
self.rope_scaling = getattr(config, "rope_scaling", None)
self.max_position_embeddings = getattr(config,
"max_position_embeddings", 8192)
self.max_position_embeddings = getattr(config, "max_position_embeddings", 8192)
self.prefix = prefix
self._init_attn_block()
self._init_ffn_block()
def _init_attn_block(self):
self.input_layernorm = RMSNorm(self.config.hidden_size,
eps=self.config.rms_norm_eps)
self.input_layernorm = RMSNorm(
self.config.hidden_size, eps=self.config.rms_norm_eps
)
self.self_attn = EagleMiniCPMAttention(
hidden_size=self.hidden_size,
num_heads=self.config.num_attention_heads,
@@ -87,15 +94,16 @@ class EagleMiniCPMDecoderLayer(nn.Module):
)
def _init_ffn_block(self):
self.post_attention_layernorm = RMSNorm(self.config.hidden_size,
eps=self.config.rms_norm_eps)
self.post_attention_layernorm = RMSNorm(
self.config.hidden_size, eps=self.config.rms_norm_eps
)
self.num_experts = getattr(self.config, "num_experts", 0)
if self.num_experts == 0:
self.mlp = EagleMiniCPMMLP(
hidden_size=self.hidden_size,
intermediate_size=self.config.intermediate_size,
hidden_act=self.config.hidden_act,
hidden_act_param=getattr(self.config, "hidden_act_param", 0.),
hidden_act_param=getattr(self.config, "hidden_act_param", 0.0),
quant_config=self.quant_config,
)
else:
@@ -103,7 +111,8 @@ class EagleMiniCPMDecoderLayer(nn.Module):
num_experts=self.config.num_experts,
top_k=self.config.num_experts_per_tok,
hidden_size=self.config.hidden_size,
intermediate_size=self.config.intermediate_size)
intermediate_size=self.config.intermediate_size,
)
def forward(
self,
@@ -118,27 +127,26 @@ class EagleMiniCPMDecoderLayer(nn.Module):
positions=positions,
hidden_states=hidden_states,
)
hidden_states = residual + hidden_states * \
(self.config.scale_depth / math.sqrt(self.config.mup_denominator))
hidden_states = residual + hidden_states * (
self.config.scale_depth / math.sqrt(self.config.mup_denominator)
)
# Fully Connected
residual = hidden_states
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
hidden_states = residual + hidden_states * \
(self.config.scale_depth / math.sqrt(self.config.mup_denominator))
hidden_states = residual + hidden_states * (
self.config.scale_depth / math.sqrt(self.config.mup_denominator)
)
return hidden_states, None
@support_torch_compile
class EagleMiniCPMModel(nn.Module):
def __init__(self,
*,
vllm_config: VllmConfig,
prefix: str = "",
start_layer: int = 0):
def __init__(
self, *, vllm_config: VllmConfig, prefix: str = "", start_layer: int = 0
):
super().__init__()
config = vllm_config.speculative_config.draft_model_config.hf_config
@@ -149,13 +157,16 @@ class EagleMiniCPMModel(nn.Module):
self.config = config
self.cache_config = cache_config
self.quant_config = quant_config
lora_vocab = (lora_config.lora_extra_vocab_size *
(lora_config.max_loras or 1)) if lora_config else 0
lora_vocab = (
(lora_config.lora_extra_vocab_size * (lora_config.max_loras or 1))
if lora_config
else 0
)
self.vocab_size = config.vocab_size + lora_vocab
self.org_vocab_size = config.vocab_size
self.fc = torch.nn.Linear(self.config.hidden_size * 2,
self.config.hidden_size,
bias=False)
self.fc = torch.nn.Linear(
self.config.hidden_size * 2, self.config.hidden_size, bias=False
)
self.input_norm1 = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.input_norm2 = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.embed_tokens = VocabParallelEmbedding(
@@ -164,12 +175,11 @@ class EagleMiniCPMModel(nn.Module):
org_num_embeddings=config.vocab_size,
)
self.num_experts = getattr(self.config, "num_experts", 0)
self._init_layers(prefix, config, cache_config, quant_config,
start_layer)
self._init_layers(prefix, config, cache_config, quant_config, start_layer)
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.make_empty_intermediate_tensors = (
make_empty_intermediate_tensors_factory(
["hidden_states", "residual"], self.config.hidden_size))
self.make_empty_intermediate_tensors = make_empty_intermediate_tensors_factory(
["hidden_states", "residual"], self.config.hidden_size
)
def _init_layers(
self,
@@ -179,14 +189,17 @@ class EagleMiniCPMModel(nn.Module):
quant_config: Optional[QuantizationConfig],
start_layer: int,
):
self.eagle_layers = nn.ModuleList([
EagleMiniCPMDecoderLayer(
config,
cache_config,
quant_config,
f"{prefix}.eagle_layers.{i + start_layer}",
) for i in range(self.config.num_hidden_layers)
])
self.eagle_layers = nn.ModuleList(
[
EagleMiniCPMDecoderLayer(
config,
cache_config,
quant_config,
f"{prefix}.eagle_layers.{i + start_layer}",
)
for i in range(self.config.num_hidden_layers)
]
)
def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
embedding = self.embed_tokens(input_ids)
@@ -202,8 +215,7 @@ class EagleMiniCPMModel(nn.Module):
input_embeds = self.input_norm1(input_embeds)
hidden_states = self.input_norm2(hidden_states)
hidden_states = self.fc(
torch.cat((input_embeds, hidden_states), dim=-1))
hidden_states = self.fc(torch.cat((input_embeds, hidden_states), dim=-1))
residual = None
for layer in self.eagle_layers:
hidden_states, residual = layer(
@@ -214,8 +226,7 @@ class EagleMiniCPMModel(nn.Module):
return hidden_states, hidden_states
def load_weights(self, weights: Iterable[tuple[str,
torch.Tensor]]) -> set[str]:
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
stacked_params_mapping = [
# (param_name, shard_name, shard_id)
("qkv_proj", "q_proj", "q"),
@@ -226,8 +237,11 @@ class EagleMiniCPMModel(nn.Module):
]
expert_params_mapping = [
# (param_name, weight_name, expert_id)
("ws" if weight_name in ["w1", "w3"] else "w2s",
f"experts.{expert_id}.{weight_name}.weight", expert_id)
(
"ws" if weight_name in ["w1", "w3"] else "w2s",
f"experts.{expert_id}.{weight_name}.weight",
expert_id,
)
for expert_id in range(self.num_experts)
for weight_name in ["w1", "w2", "w3"]
]
@@ -237,12 +251,11 @@ class EagleMiniCPMModel(nn.Module):
for name, loaded_weight in weights:
if "rotary_emb.inv_freq" in name:
continue
if ("rotary_emb.cos_cached" in name
or "rotary_emb.sin_cached" in name):
if "rotary_emb.cos_cached" in name or "rotary_emb.sin_cached" in name:
# Models trained using ColossalAI may include these tensors in
# the checkpoint. Skip them.
continue
for (param_name, weight_name, shard_id) in stacked_params_mapping:
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)
@@ -264,10 +277,9 @@ class EagleMiniCPMModel(nn.Module):
continue
param = params_dict[name]
weight_loader = param.weight_loader
weight_loader(param,
loaded_weight,
weight_name,
expert_id=expert_id)
weight_loader(
param, loaded_weight, weight_name, expert_id=expert_id
)
break
else:
# Skip loading extra bias for GPTQ models.
@@ -276,8 +288,9 @@ class EagleMiniCPMModel(nn.Module):
if is_pp_missing_parameter(name, self):
continue
param = params_dict[name]
weight_loader = getattr(param, "weight_loader",
default_weight_loader)
weight_loader = getattr(
param, "weight_loader", default_weight_loader
)
weight_loader(param, loaded_weight)
loaded_params.add(name)
@@ -319,11 +332,14 @@ class EagleMiniCPMForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
self.quant_config = quant_config
target_layer_num = vllm_config.model_config.get_num_layers(
vllm_config.parallel_config)
vllm_config.parallel_config
)
self.model = self._init_model(vllm_config=vllm_config,
prefix=maybe_prefix(prefix, "model"),
start_layer=target_layer_num)
self.model = self._init_model(
vllm_config=vllm_config,
prefix=maybe_prefix(prefix, "model"),
start_layer=target_layer_num,
)
unpadded_vocab_size = config.vocab_size
if lora_config:
@@ -335,7 +351,8 @@ class EagleMiniCPMForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
padding_size=DEFAULT_VOCAB_PADDING_SIZE
# We need bigger padding if using lora for kernel
# compatibility
if not lora_config else lora_config.lora_vocab_padding_size,
if not lora_config
else lora_config.lora_vocab_padding_size,
quant_config=quant_config,
prefix=maybe_prefix(prefix, "lm_head"),
)
@@ -343,19 +360,17 @@ class EagleMiniCPMForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
self.lm_head = self.lm_head.tie_weights(self.model.embed_tokens)
self.scale_width = self.config.hidden_size / self.config.dim_model_base
self.logits_processor = LogitsProcessor(unpadded_vocab_size,
config.vocab_size)
self.logits_processor = LogitsProcessor(unpadded_vocab_size, config.vocab_size)
self.make_empty_intermediate_tensors = (
self.model.make_empty_intermediate_tensors)
self.model.make_empty_intermediate_tensors
)
def _init_model(self,
*,
vllm_config: VllmConfig,
prefix: str = "",
start_layer: int = 0):
return EagleMiniCPMModel(vllm_config=vllm_config,
prefix=prefix,
start_layer=start_layer)
def _init_model(
self, *, vllm_config: VllmConfig, prefix: str = "", start_layer: int = 0
):
return EagleMiniCPMModel(
vllm_config=vllm_config, prefix=prefix, start_layer=start_layer
)
def get_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor:
return self.model.get_input_embeddings(input_ids)
@@ -366,8 +381,7 @@ class EagleMiniCPMForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
positions: torch.Tensor,
hidden_states: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
hidden_states, hidden_states2 = self.model(input_ids, positions,
hidden_states)
hidden_states, hidden_states2 = self.model(input_ids, positions, hidden_states)
hidden_states = hidden_states / self.scale_width
hidden_states2 = hidden_states2 / self.scale_width
return hidden_states, hidden_states2
@@ -379,11 +393,9 @@ class EagleMiniCPMForCausalLM(nn.Module, SupportsLoRA, SupportsPP):
logits = self.logits_processor(self.lm_head, hidden_states)
return logits
def load_weights(self, weights: Iterable[tuple[str,
torch.Tensor]]) -> set[str]:
def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]:
loader = AutoWeightsLoader(
self,
skip_prefixes=(["lm_head."]
if self.config.tie_word_embeddings else None),
skip_prefixes=(["lm_head."] if self.config.tie_word_embeddings else None),
)
return loader.load_weights(weights)