vllm.attention.backends.abstract ¶
AttentionBackend ¶
Bases: ABC
Abstract class for attention backends.
Source code in vllm/attention/backends/abstract.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
supported_kernel_block_sizes class-attribute ¶
supported_kernel_block_sizes: list[int | MultipleOf] = [
MultipleOf(1)
]
full_cls_name classmethod ¶
get_builder_cls abstractmethod staticmethod ¶
get_impl_cls abstractmethod staticmethod ¶
get_impl_cls() -> type[AttentionImpl]
get_kv_cache_shape abstractmethod staticmethod ¶
get_kv_cache_stride_order staticmethod ¶
Get the physical (memory layout) ordering of the kv cache dimensions. e.g. if the KV cache shape is [2, num_blocks, block_size, num_heads, head_size], and get_kv_cache_stride_order returns (1, 3, 0, 2, 4) then the physical ordering of dimensions is [num_blocks, num_heads, 2, block_size, head_size].
If this function is unimplemented / raises NotImplementedError, the physical layout of the KV cache will match the logical shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_num_layers_dimension | bool | if True, includes an additional num_layers dimension, which is assumed to be prepended to the logical KV cache shape. With the above example, a return value (2, 4, 0, 1, 3, 5) corresponds to [num_blocks, num_heads, num_layers, 2, block_size, head_size]. If an additional dimension is NOT included in the returned tuple, the physical layout will not include a layers dimension. | False |
Returns:
| Type | Description |
|---|---|
tuple[int, ...] | A tuple of ints which is a permutation of range(len(shape)). |
Source code in vllm/attention/backends/abstract.py
get_required_kv_cache_layout classmethod ¶
get_required_kv_cache_layout() -> KVCacheLayoutType | None
get_supported_head_sizes classmethod ¶
supports_attn_type classmethod ¶
Check if backend supports a given attention type.
By default, only supports decoder attention. Backends should override this to support other attention types.
Source code in vllm/attention/backends/abstract.py
supports_block_size classmethod ¶
Source code in vllm/attention/backends/abstract.py
supports_combination classmethod ¶
supports_combination(
head_size: int,
dtype: dtype,
kv_cache_dtype: CacheDType | None,
block_size: int | None,
use_mla: bool,
has_sink: bool,
use_sparse: bool,
device_capability: DeviceCapability,
) -> str | None
Source code in vllm/attention/backends/abstract.py
supports_compute_capability classmethod ¶
supports_compute_capability(
capability: DeviceCapability,
) -> bool
supports_dtype classmethod ¶
supports_head_size classmethod ¶
supports_kv_cache_dtype classmethod ¶
supports_kv_cache_dtype(
kv_cache_dtype: CacheDType | None,
) -> bool
Source code in vllm/attention/backends/abstract.py
validate_configuration classmethod ¶
validate_configuration(
head_size: int,
dtype: dtype,
kv_cache_dtype: CacheDType | None,
block_size: int | None,
use_mla: bool,
has_sink: bool,
use_sparse: bool,
device_capability: DeviceCapability,
attn_type: str,
) -> list[str]
Source code in vllm/attention/backends/abstract.py
AttentionImpl ¶
Source code in vllm/attention/backends/abstract.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | |
can_return_lse_for_decode class-attribute instance-attribute ¶
can_return_lse_for_decode: bool = False
need_to_return_lse_for_decode class-attribute instance-attribute ¶
need_to_return_lse_for_decode: bool = False
__init__ abstractmethod ¶
__init__(
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: int | None = None,
alibi_slopes: list[float] | None = None,
sliding_window: int | None = None,
kv_cache_dtype: str = "auto",
logits_soft_cap: float | None = None,
attn_type: str = DECODER,
kv_sharing_target_layer_name: str | None = None,
) -> None
Source code in vllm/attention/backends/abstract.py
__new__ ¶
Source code in vllm/attention/backends/abstract.py
forward abstractmethod ¶
forward(
layer: AttentionLayer,
query: Tensor,
key: Tensor,
value: Tensor,
kv_cache: Tensor,
attn_metadata: T,
output: Tensor | None = None,
output_scale: Tensor | None = None,
output_block_scale: Tensor | None = None,
) -> Tensor
Source code in vllm/attention/backends/abstract.py
fused_output_quant_supported ¶
fused_output_quant_supported(quant_key: QuantKey)
Does this attention implementation support fused output quantization. This is used by the AttnFusionPass to only fuse output quantization onto implementations that support it.
:param quant_key: QuantKey object that describes the quantization op :return: is fusion supported for this type of quantization
Source code in vllm/attention/backends/abstract.py
supports_quant_query_input ¶
supports_quant_query_input() -> bool
Check if this attention implementation supports pre-quantized query input.
When True, the attention layer will quantize queries before passing them to this backend, allowing torch.compile to fuse the quantization with previous operations. This is typically supported when using FP8 KV cache with compatible attention kernels (e.g., TRT-LLM). TODO add support to more backends: https://github.com/vllm-project/vllm/issues/25584
Returns:
| Name | Type | Description |
|---|---|---|
bool | bool | True if the implementation can accept pre-quantized queries. |
Source code in vllm/attention/backends/abstract.py
AttentionLayer ¶
Bases: Protocol
Source code in vllm/attention/backends/abstract.py
AttentionMetadata ¶
AttentionType ¶
Attention type. Use string to be compatible with torch.compile.
Source code in vllm/attention/backends/abstract.py
DECODER class-attribute instance-attribute ¶
Decoder attention between previous layer Q/K/V.
ENCODER class-attribute instance-attribute ¶
Encoder attention between previous layer Q/K/V for encoder-decoder.
ENCODER_DECODER class-attribute instance-attribute ¶
Attention between dec. Q and enc. K/V for encoder-decoder.
ENCODER_ONLY class-attribute instance-attribute ¶
Encoder attention between previous layer Q/K/V.
MLAAttentionImpl ¶
Bases: AttentionImpl[T], Generic[T]
Source code in vllm/attention/backends/abstract.py
__init__ abstractmethod ¶
__init__(
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: int,
alibi_slopes: list[float] | None,
sliding_window: int | None,
kv_cache_dtype: str,
logits_soft_cap: float | None,
attn_type: str,
kv_sharing_target_layer_name: str | None,
q_lora_rank: int | None,
kv_lora_rank: int,
qk_nope_head_dim: int,
qk_rope_head_dim: int,
qk_head_dim: int,
v_head_dim: int,
kv_b_proj: ColumnParallelLinear,
indexer: object | None = None,
) -> None
Source code in vllm/attention/backends/abstract.py
forward abstractmethod ¶
forward(
layer: AttentionLayer,
hidden_states_or_cq: Tensor,
kv_c_normed: Tensor,
k_pe: Tensor,
kv_cache: Tensor,
attn_metadata: T,
output: Tensor | None = None,
output_scale: Tensor | None = None,
output_block_scale: Tensor | None = None,
) -> Tensor