vllm.model_executor.models.gemma4
¶
Gemma 4 model implementation for vLLM.
Classes:
-
Gemma4CrossDecoderLayers–Cross-decoder layers (YOCO second half, KV-shared).
-
Gemma4Model– -
Gemma4Router–Router for Gemma4 MoE that preprocesses input before projection.
-
Gemma4SelfDecoderLayers–Compiled wrapper: embedding + non-KV-shared layers (YOCO first half).
Functions:
-
gemma4_routing_function_topk_softmax–Gemma4 routing with the
topk_softmaxMoE custom op.
Gemma4Model
¶
Bases: Module, EagleModelMixin, SupportsQuant
Methods:
-
get_per_layer_inputs–Get per-layer embeddings from embed_tokens_per_layer.
-
project_per_layer_inputs–Project inputs_embeds and combine with per_layer_inputs.
get_per_layer_inputs(input_ids)
¶
project_per_layer_inputs(inputs_embeds, per_layer_inputs)
¶
Project inputs_embeds and combine with per_layer_inputs.
Steps: 1. Project inputs_embeds: hidden_size → total_ple_dim 2. Scale by hidden_size^{-0.5} 3. Reshape to (num_tokens, num_layers, per_layer_dim) 4. Normalize with per_layer_projection_norm 5. Combine: (projection + per_layer_inputs) * 1/sqrt(2)
Gemma4Router
¶
Bases: Module
Router for Gemma4 MoE that preprocesses input before projection.
Applies RMSNorm (no learned weight), root_size scaling (hidden_size^{-0.5}), then a learned per-dimension scale before projecting to expert logits.
This preprocessing is applied ONLY to the router's input, not to the expert MLPs' input.
Methods:
-
forward–Returns raw router logits [T, E].
-
routing_function–Softmax over ALL experts → top-k → renormalize.
forward(x)
¶
Returns raw router logits [T, E].
routing_function(hidden_states, gating_output, topk, renormalize)
¶
Softmax over ALL experts → top-k → renormalize.
MoERunner's built-in fused_topk scopes softmax differently, so Gemma4 needs its own routing for numerical correctness.
NOTE: self.per_expert_scale is read at call time (not captured into a local) so that torch.func.functional_call parameter substitution reaches the routing function correctly.
Gemma4SelfDecoderLayers
¶
Bases: Module
Compiled wrapper: embedding + non-KV-shared layers (YOCO first half).
Owns the embedding and PLE modules so they are inside the compiled graph. Gemma4Model delegates embedding methods here.
Methods:
-
get_per_layer_inputs–Get per-layer embeddings from embed_tokens_per_layer.
-
project_per_layer_inputs–Project inputs_embeds and combine with per_layer_inputs.
get_per_layer_inputs(input_ids)
¶
project_per_layer_inputs(inputs_embeds, per_layer_inputs)
¶
Project inputs_embeds and combine with per_layer_inputs.
Steps: 1. Project inputs_embeds: hidden_size → total_ple_dim 2. Scale by hidden_size^{-0.5} 3. Reshape to (num_tokens, num_layers, per_layer_dim) 4. Normalize with per_layer_projection_norm 5. Combine: (projection + per_layer_inputs) * 1/sqrt(2)
_gemma4_layer_weights_mapper(config)
¶
Stack each decoder layer's projections the way its modules are built.
gate/up pack into mlp.gate_up_proj. Layers with a qkv_proj pack q/k/v
into it; attention_k_eq_v full-attention layers ship k_proj only, so K
is loaded into both the K and V shards and no v_proj is expected. KV-shared
layers only have q_proj; the K/V tensors original checkpoints still ship
for them are dropped.
_get_text_config(config)
¶
Dereference text_config if config is a nested Gemma4Config.
Gemma4 checkpoints use architectures=["Gemma4ForConditionalGeneration"] which yields a Gemma4Config with nested text_config. This function transparently returns the text config regardless of nesting.
_run_decoder_layers(decoder_layers, layer_idx_start, positions, hidden_states, per_layer_inputs=None, **kwargs)
¶
Run a slice of decoder layers with PLE extraction.
gemma4_routing_function_topk_softmax(gating_output, topk, per_expert_scale)
¶
Gemma4 routing with the topk_softmax MoE custom op.
topk_softmax(renormalize=True) is the Gemma4 routing (softmax over all
experts, top-k, renormalize over the selected k); the per-expert scale is
then folded into the weights. Returns float32 weights and int32 expert ids
of shape [num_tokens, topk], like the Triton and torch paths.