Skip to content

vllm.v1.worker.gpu.spec_decode.gemma4.speculator

Gemma4 MTP (Multi-Token Prediction) speculator for speculative decoding.

The Gemma4 assistant model runs all decoder layers per draft step (producing one token), and all its attention layers share KV cache with the target model via cross-model KV sharing.

Classes:

Gemma4Speculator

Bases: TargetDependentARSpeculator

_create_draft_vllm_config()

Preserve the target's forced TRITON_ATTN backend for draft layers.

Gemma4 forces TRITON_ATTN due to heterogeneous head dimensions (head_dim=256 sliding, global_head_dim=512 full). The base class resets attention_config.backend to None for draft models, causing sliding layers to fall back to FLASH_ATTN which cannot handle KV-shared cache. Override to carry the target's backend through.

_setup_gemma4_kv_sharing(model, target_attn_layer_names)

Wire draft layers to share KV and KV scales with the target model.

Each draft decoder layer is mapped to the last non-KV-shared target layer of the same attention type (sliding or full).

_copy_target_kv_scales(attn, target_attn)

Copy target KV scales while preserving their tensor representation.

Default attention scales are scalar buffers, while some quantization methods replace them with length-one or per-head parameters. Preserve the draft layer's registration type so the shared KV cache is interpreted with the target's values and shapes without aliasing target parameters.