# Copyright 2026-2027 SGLang 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. # ============================================================================== """Inference-only GLM-4.7-Flash Speculative Decoding (NextN) compatible with HuggingFace weights.""" import logging from typing import Iterable, Optional, Tuple import torch from torch import nn from transformers import PretrainedConfig from sglang.srt.eplb.expert_distribution import get_global_expert_distribution_recorder from sglang.srt.layers.dp_attention import is_dp_attention_enabled from sglang.srt.layers.layernorm import RMSNorm from sglang.srt.layers.logits_processor import LogitsProcessor from sglang.srt.layers.quantization.base_config import QuantizationConfig from sglang.srt.layers.vocab_parallel_embedding import ( ParallelLMHead, VocabParallelEmbedding, ) from sglang.srt.model_executor.forward_batch_info import ForwardBatch from sglang.srt.models.glm4_moe_lite import ( Glm4MoeLiteDecoderLayer, Glm4MoeLiteForCausalLM, ) from sglang.srt.runtime_context import get_parallel, get_server_args from sglang.srt.utils import BumpAllocator, add_prefix, is_npu logger = logging.getLogger(__name__) class Glm4MoeLiteModelNextN(nn.Module): def __init__( self, config: PretrainedConfig, quant_config: Optional[QuantizationConfig] = None, prefix: str = "", ) -> None: super().__init__() if quant_config is not None and quant_config.get_name() == "modelopt_fp4": logger.warning( "Overriding Glm4MoeLiteForCausalLMNextN quant config for modelopt_fp4 " "GLM-4.7-Flash model." ) quant_config = None self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding( config.vocab_size, config.hidden_size, use_attn_tp_group=is_dp_attention_enabled(), prefix=add_prefix("embed_tokens", prefix), ) self.enorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.hnorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.eh_proj = nn.Linear(2 * config.hidden_size, config.hidden_size, bias=False) self.decoder = Glm4MoeLiteDecoderLayer( config, 0, quant_config=quant_config, is_nextn=True, prefix=add_prefix("decoder", prefix), ) self.shared_head = nn.Module() self.shared_head.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) def forward( self, input_ids: torch.Tensor, positions: torch.Tensor, forward_batch: ForwardBatch, input_embeds: torch.Tensor = None, ) -> torch.Tensor: # Glm4MoeLiteDecoderLayer uses DeepseekV2AttentionMLA, which requires a # zero_allocator (the GQA glm4_moe_nextn path does not pass one). zero_allocator = BumpAllocator( buffer_size=2, dtype=torch.float32, device=( input_embeds.device if input_embeds is not None else input_ids.device ), ) if input_embeds is None: hidden_states = self.embed_tokens(input_ids) else: hidden_states = input_embeds if hidden_states.shape[0] > 0: hidden_states = self.eh_proj( torch.cat( ( self.enorm(hidden_states), self.hnorm(forward_batch.spec_info.hidden_states), ), dim=-1, ) ) residual = None with get_global_expert_distribution_recorder().disable_this_region(): hidden_states, residual = self.decoder( positions, hidden_states, forward_batch, residual, zero_allocator ) if not forward_batch.forward_mode.is_idle(): if residual is not None: hidden_states, _ = self.shared_head.norm(hidden_states, residual) else: hidden_states = self.shared_head.norm(hidden_states) return hidden_states class Glm4MoeLiteForCausalLMNextN(Glm4MoeLiteForCausalLM): def __init__( self, config: PretrainedConfig, quant_config: Optional[QuantizationConfig] = None, prefix: str = "", ) -> None: nn.Module.__init__(self) self.config = config self.tp_size = get_parallel().tp_size if is_npu() and get_server_args().speculative_draft_model_quantization is None: quant_config = None self.quant_config = quant_config self.model = Glm4MoeLiteModelNextN( config, quant_config, prefix=add_prefix("model", prefix) ) self.lm_head = ParallelLMHead( config.vocab_size, config.hidden_size, quant_config=quant_config, prefix=add_prefix("model.shared_head.head", prefix), use_attn_tp_group=get_server_args().enable_dp_lm_head, ) self.logits_processor = LogitsProcessor(config) self.num_fused_shared_experts = ( 0 if get_server_args().disable_shared_experts_fusion else 1 ) @torch.no_grad() def forward( self, input_ids: torch.Tensor, positions: torch.Tensor, forward_batch: ForwardBatch, ) -> torch.Tensor: hidden_states = self.model(input_ids, positions, forward_batch) return self.logits_processor( input_ids, hidden_states, self.lm_head, forward_batch ) def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]): super().load_weights(weights, is_nextn=True) EntryClass = [Glm4MoeLiteForCausalLMNextN]