Skip to content

vllm.v1.attention.backends.triton_attn

High-Performance Triton-only Attention layer.

Classes:

TritonAttentionBackend

Bases: AttentionBackend

Methods:

customize_spec(spec) classmethod

Per-token-head modes pack inline fp32 scales after each head's data, so the content is (data + one scale) per K/V side.

supports_attn_type(attn_type) classmethod

TritonAttention supports all attention types.

TritonAttentionImpl

Bases: AttentionImpl

Methods:

  • forward –

    Forward pass with Paged Attention impl. in Triton.

_ensure_scale_caches(kv_cache)

Extract per-head scale views from the padded content dimension.

The KV cache is packed as logical shape (num_blocks, nkv, block_size, 2 * (hs + pad)) where pad = sizeof(float32) / sizeof(cache_dtype). The content dim holds [K(hs) | K_scale(pad) | V(hs) | V_scale(pad)] per (head, slot); the last pad elements of each half hold one float32 scale. We create strided float32 views over those bytes. kv_cache must be the packed logical tensor (call before any transpose), but may have HND or NHD physical strides.

Scale shape: (num_blocks, block_size, num_kv_heads)

_forward_encoder_attention(query, key, value, output, attn_metadata, layer)

Forward pass for encoder attention without KV cache.

Parameters:

  • query

    (Tensor) –

    shape = [num_encoder_tokens, num_heads, head_size]

  • key

    (Tensor) –

    shape = [num_encoder_tokens, num_kv_heads, head_size]

  • value

    (Tensor) –

    shape = [num_encoder_tokens, num_kv_heads, head_size]

  • output

    (Tensor) –

    shape = [num_encoder_tokens, num_heads, head_size]

  • attn_metadata

    (TritonAttentionMetadata) –

    Encoder attention metadata

  • layer

    (Module) –

    The attention layer

_pth_key_value_caches(kv_cache)

Per-token-head K/V cache views (ensures scale caches; FP8 retyped).

forward(layer, query, key, value, kv_cache, attn_metadata, output, output_scale=None, output_block_scale=None)

Forward pass with Paged Attention impl. in Triton.

Parameters:

  • layer

    (Module) –

    The attention layer, providing the q/k/v quantization scales.

  • query

    (Tensor) –

    shape = [num_tokens, num_heads, head_size]

  • key

    (Tensor) –

    shape = [num_tokens, num_kv_heads, head_size]

  • value

    (Tensor) –

    shape = [num_tokens, num_kv_heads, head_size]

  • kv_cache

    (Tensor) –

    shape = [num_blocks, num_kv_heads, block_size, 2 * head_size]

  • attn_metadata

    (TritonAttentionMetadata) –

    Metadata for attention.

  • output

    (Tensor) –

    Tensor that the attention result is written into.

  • output_scale

    (Tensor | None, default: None ) –

    Scale for fused output quantization.

  • output_block_scale

    (Tensor | None, default: None ) –

    Block scale for fused output quantization; not supported by this backend.

Returns:

  • Tensor –

    shape = [num_tokens, num_heads * head_size]