Skip to content

vllm.model_executor.layers.attention.mm_encoder_attention

Classes:

  • MMEncoderAttention –

    Multi-headed attention without any cache, used for multimodal encoder.

MMEncoderAttention

Bases: CustomOp

Multi-headed attention without any cache, used for multimodal encoder.

Methods:

__init__(num_heads, head_size, scale=None, num_kv_heads=None, prefix='')

Args: num_heads: number of attention heads per partition. head_size: hidden_size per attention head. scale: scale factor. num_kv_heads: number of kv heads. prefix: This has no effect, it is only here to make it easier to swap between Attention and MultiHeadAttention

_forward_fa(query, key, value, cu_seqlens=None, max_seqlen=None)

Input shape: (batch_size x seq_len x hidden_size) or (batch_size x seq_len x num_heads x head_size)

_forward_sdpa(query, key, value, cu_seqlens=None)

Input shape: (batch_size x seq_len x hidden_size) or (batch_size x seq_len x num_heads x head_size)

_forward_triton(query, key, value, cu_seqlens=None, max_seqlen=None, sequence_lengths=None)

Input shape: (batch_size x seq_len x hidden_size) or (batch_size x seq_len x num_heads x head_size)

_init_fp8_state()

Initialize FP8 attention state from multimodal config.

No-op if FP8 is not requested. Raises ValueError if FP8 is requested but the platform does not support it.

_record_amax_and_update_scales(query, key, value)

Record Q/K/V amax into circular history and recompute scales.

All work stays on GPU with no device-to-host sync. The Python-side history position counter is mutated, so this method must NOT be called inside CUDA graph capture/replay. When CUDA graphs are used for the encoder, dynamic scaling should be disabled by providing a static scale file via --mm-encoder-fp8-scale-path.

process_weights_after_loading()

Populate FP8 scale buffers (always float32) after weights are loaded.

view_qkv_to_4d(query, key, value, bsz, q_len, kv_len)

Reshape query, key, value to 4D tensors: (batch_size, seq_len, num_heads, head_size)

_load_fp8_scales_file(path) cached

Load per-layer FP8 Q/K/V scales from a JSON file. Results are cached.

Expected format (keys q_scale / k_scale / v_scale also accepted)::

{
    "visual.blocks.0.attn.attn": {"q": 224.0, "k": 198.0, "v": 210.0},
    "visual.blocks.1.attn.attn": {"q": 218.0, "k": 195.0, "v": 207.0},
}

To produce such a file, run with mm_encoder_fp8_scale_save_path set.

_maybe_save_fp8_scales(layer_name, q_scale, k_scale, v_scale, buffer_wrapped)

Accumulate a layer's scale tensors; on the first amax buffer wrap, dump all accumulated scales to mm_encoder_fp8_scale_save_path.

No-op unless auto-save is configured. Tensor references are stored on every call (no GPU->CPU sync); .item() is only called at the single save point to avoid stalling the forward path.