vllm.v1.attention.backends.flex_attention
Attention layer with FlashAttention.
create_block_mask_compiled
module-attribute
¶
create_block_mask_compiled = compile(
create_block_mask,
fullgraph=True,
mode="reduce-overhead",
)
flex_attention_compiled
module-attribute
¶
flex_attention_compiled = compile(
flex_attention, fullgraph=True
)
FlexAttentionBackend
¶
Bases: AttentionBackend
Source code in vllm/v1/attention/backends/flex_attention.py
get_builder_cls
staticmethod
¶
get_builder_cls() -> type[FlexAttentionMetadataBuilder]
get_impl_cls
staticmethod
¶
get_impl_cls() -> type[FlexAttentionImpl]
get_kv_cache_shape
staticmethod
¶
get_metadata_cls
staticmethod
¶
get_metadata_cls() -> type[AttentionMetadata]
get_supported_head_sizes
staticmethod
¶
FlexAttentionImpl
¶
Bases: AttentionImpl
Source code in vllm/v1/attention/backends/flex_attention.py
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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
__init__
¶
__init__(
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: int,
alibi_slopes: Optional[list[float]],
sliding_window: Optional[int],
kv_cache_dtype: str,
blocksparse_params: Optional[dict[str, Any]] = None,
logits_soft_cap: Optional[float] = None,
attn_type: AttentionType = DECODER,
kv_sharing_target_layer_name: Optional[str] = None,
) -> None
Source code in vllm/v1/attention/backends/flex_attention.py
forward
¶
forward(
layer: Module,
query: Tensor,
key: Tensor,
value: Tensor,
kv_cache: Tensor,
attn_metadata: FlexAttentionMetadata,
output: Optional[Tensor] = None,
output_scale: Optional[Tensor] = None,
) -> Tensor
Forward pass with FLexAttention.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
Tensor
|
shape = [num_tokens, num_heads, head_size] |
required |
key
|
Tensor
|
shape = [num_tokens, num_kv_heads, head_size] |
required |
value
|
Tensor
|
shape = [num_tokens, num_kv_heads, head_size] |
required |
attn_metadata
|
FlexAttentionMetadata
|
Metadata for attention. |
required |
Returns: shape = [num_tokens, num_heads * head_size]
Source code in vllm/v1/attention/backends/flex_attention.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
view_as_4d
staticmethod
¶
FlexAttentionMetadata
dataclass
¶
Source code in vllm/v1/attention/backends/flex_attention.py
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 254 255 256 257 |
|
logical_mask_mod
class-attribute
instance-attribute
¶
logical_mask_mod: _mask_mod_signature = causal_mask_mod
__init__
¶
__init__(
num_actual_tokens: int,
max_query_len: int,
query_start_loc: Tensor,
max_seq_len: int,
seq_lens: Tensor,
block_table: Tensor,
slot_mapping: Tensor,
use_cascade: bool,
common_prefix_len: int,
cu_prefix_query_lens: Optional[Tensor],
prefix_kv_lens: Optional[Tensor],
suffix_kv_lens: Optional[Tensor],
total_cache_tokens: int,
block_size: int,
max_possible_sequence_length: int,
num_reqs: int,
physical_to_logical: Tensor,
decode_offset: Tensor,
num_input_tokens: int = 0,
block_mask: Optional[BlockMask] = None,
score_mod: Optional[_score_mod_signature] = None,
mask_mod: Optional[_mask_mod_signature] = None,
logical_mask_mod: _mask_mod_signature = causal_mask_mod,
) -> None
__post_init__
¶
Source code in vllm/v1/attention/backends/flex_attention.py
build_block_mask
¶
build_block_mask() -> BlockMask
Source code in vllm/v1/attention/backends/flex_attention.py
get_mask_mod
¶
Creates the mask_mod function for FlexAttention.
This function creates the combined mask mod function that handles
- The paged attention block mapping
- The mapping from packed query sequences to logical query entries
It also by defaults adds the decoding offset to the query indices. With this info we create the "logical" indices that are passed to mask_mod functions. This allows mask mod functions to be agnostic to layout of the query and key/value tensors.
TODO is_within_lower_bound: do sequences start on block_boundaries?
Source code in vllm/v1/attention/backends/flex_attention.py
FlexAttentionMetadataBuilder
¶
Bases: AttentionMetadataBuilder[FlexAttentionMetadata]
Source code in vllm/v1/attention/backends/flex_attention.py
__init__
¶
__init__(
runner: GPUModelRunner,
kv_cache_spec: AttentionSpec,
block_table: BlockTable,
)
Source code in vllm/v1/attention/backends/flex_attention.py
build
¶
build(
common_prefix_len: int,
common_attn_metadata: CommonAttentionMetadata,
)
Source code in vllm/v1/attention/backends/flex_attention.py
_offsets_to_doc_ids_tensor
¶
Source code in vllm/v1/attention/backends/flex_attention.py
causal_mask_mod
¶
physical_to_logical_mapping
¶
Creates an inverse mapping from physical block locations to logical indices.
The original block_table maps from logical blocks to physical locations:
Logical to Physical (Original block_table): ┌───────────────────────────────────────────┐ │ Request 0: │ │ │ │ Logical Blocks: 0 1 2 3 4 5 6 7 │ │ │ │ │ │ │ │ │ │ │ │ v v v v v v v v │ │ Physical Blocks: 3 5 1 7 4 2 0 6 │ └───────────────────────────────────────────┘
This function creates the inverse mapping:
Physical to Logical (Inverse mapping): ┌───────────────────────────────────────────┐ │ Request 0: │ │ │ │ Physical Blocks: 0 1 2 3 4 5 6 7 │ │ │ │ │ │ │ │ │ │ │ │ v v v v v v v v │ │ Logical Blocks: 6 2 5 0 4 1 7 3 │ └───────────────────────────────────────────┘
If multiple logical blocks map to the same physical block, this function returns the first (minimum) logical block index.
If a physical block is not mapped to by any logical block, its value in the result will be -1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_table
|
Tensor
|
Tensor of shape [max_reqs, max_num_blocks] mapping logical blocks to physical locations |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor of shape [max_reqs, max_physical_block] |