vllm.model_executor.layers.attention.attention
¶
Classes:
-
Attention–Attention layer.
Functions:
-
get_attention_context–Extract attention context for a given layer.
-
set_default_quant_scales–Sets default quantization scales for the layer.
-
should_load_quant_weights–Returns whether the quantization method should load quantized weights.
-
unified_kv_cache_update–Returns a dummy that is passed to unified_attention to signal a side effect and
Attention
¶
Bases: Module, AttentionLayerBase
Attention layer.
This class takes query, key, and value tensors as input. The input tensors can either contain prompt tokens or generation tokens. The class does the following:
- Store the input key and value tensors in the KV cache.
- Perform (multi-head/multi-query/grouped-query) attention.
- Return the output tensor.
Methods:
-
__init__–The KV cache is stored inside this class and is accessed via
-
forward–The KV cache is stored inside this class and is accessed via
__init__(num_heads, head_size, scale, num_kv_heads=None, alibi_slopes=None, use_alibi_sqrt=None, cache_config=None, quant_config=None, logits_soft_cap=None, per_layer_sliding_window=None, prefix='', attn_type=AttentionType.DECODER, kv_sharing_target_layer_name=None, mm_prefix_clamp_sliding_window=False, attn_backend=None, head_size_v=None, query_pre_quantized=False, **extra_impl_args)
¶
The KV cache is stored inside this class and is accessed via
self.kv_cache.
forward(query, key, value, output_shape=None, output_dtype=None)
¶
The KV cache is stored inside this class and is accessed via
self.kv_cache.
Attention metadata (attn_metadata) is set using a context manager in
the model runner's execute_model method. It is accessed via forward
context using
vllm.forward_context.get_forward_context().attn_metadata.
_init_kv_cache_quant(layer, quant_config, prefix)
¶
Initializes KV cache scaling factors and quantization method.
This helper function sets up the KV cache quantization attributes that are shared between Attention and MLAAttention layers. It initializes scale tensors for query, key, value, and probability, and configures the quantization method if applicable.
Parameters:
_largest_kernel_block_within(attn_backend, per_token_bytes, page_budget, fallback, kv_cache_spec=None, divisor_of=None, layer_name=None)
¶
Largest supported kernel block size whose page fits in page_budget.
A padded spec (e.g. skip-quant layer) that pads its page up to a large shared page
wastes page_budget - block*per_token bytes per block. Picking the largest kernel
block whose natural page still fits under page_budget minimizes that waste.
MultipleOf declarations are expanded to the largest aligned block that fits.
Falls back to the smallest supported block when nothing fits.
With divisor_of, prefer the largest fitting block that divides it, so the
page can be scaled to divisor_of by an integer ratio instead of padded.
get_attention_context(layer_name)
¶
Extract attention context for a given layer.
This helper function extracts the attention metadata, attention layer instance, KV cache tensor, and slot mapping for a specific layer.
Parameters:
Returns:
-
Any–A tuple containing:
-
Attention | MLAAttention–- attn_metadata: Attention metadata for this specific layer, or None if no metadata available
-
Tensor–- attn_layer: The attention layer instance (Attention or MLAAttention)
-
Tensor–- kv_cache: The KV cache tensor for current forward pass
-
tuple[Any, Attention | MLAAttention, Tensor, Tensor]–- slot_mapping: The slot mapping for this specific layer
-
Note(tuple[Any, Attention | MLAAttention, Tensor, Tensor]) –attn_metadata may be None, but attn_layer and kv_cache are always
-
tuple[Any, Attention | MLAAttention, Tensor, Tensor]–extracted from the forward context.
set_default_quant_scales(layer, register_buffer=False)
¶
Sets default quantization scales for the layer.
should_load_quant_weights(quant_method)
¶
Returns whether the quantization method should load quantized weights.
unified_kv_cache_update(key, value, layer_name)
¶
Returns a dummy that is passed to unified_attention to signal a side effect and the data dependency between them to ensure torch.compile preserves ordering.