vllm.v1.attention.backends.mla.flashmla_sparse ¶
logger module-attribute
¶
logger = init_logger(__name__)
NOTE: FlashMLA Sparse uses an fp8 cache with the following format
In the "FP8 with scale" format, each token's KV cache is 656 Bytes, structured as: - First 512 bytes: The "quantized NoPE" part, containing 512 float8_e4m3
values. - Next 16 bytes: Scale factors, containing 4 float32
values. The first float32
is the scale for the first 128 float8_e4m3
values, the second for the next 128, and so on. - Last 128 bytes: The "RoPE" part, containing 64 bfloat16
values. This part is not quantized for accuracy.
FlashMLASparseBackend ¶
Bases: AttentionBackend
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
get_builder_cls staticmethod
¶
get_builder_cls() -> type[FlashMLASparseMetadataBuilder]
get_impl_cls staticmethod
¶
get_impl_cls() -> type[FlashMLASparseImpl]
get_kv_cache_shape staticmethod
¶
get_kv_cache_shape(
num_blocks: int,
block_size: int,
num_kv_heads: int,
head_size: int,
cache_dtype_str: str = "auto",
) -> tuple[int, ...]
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
get_metadata_cls staticmethod
¶
get_metadata_cls() -> type[AttentionMetadata]
get_supported_dtypes classmethod
¶
FlashMLASparseDecodeAndContextMetadata dataclass
¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
prefill_context_lengths class-attribute
instance-attribute
¶
prefill_new_k_start_locs class-attribute
instance-attribute
¶
__init__ ¶
__init__(
scheduler_metadata: Tensor = None,
num_splits: Tensor = None,
cache_lens: Tensor = None,
prefill_context_lengths: Optional[Tensor] = None,
prefill_new_k_start_locs: Optional[Tensor] = None,
dummy_block_table: Tensor = None,
) -> None
filter_prefill_indices ¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
FlashMLASparseImpl ¶
Bases: MLACommonBaseImpl[FlashMLASparseMetadata]
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
|
__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,
logits_soft_cap: Optional[float],
attn_type: str,
kv_sharing_target_layer_name: Optional[str],
topk_indice_buffer: Optional[Tensor] = None,
indexer: Optional[Indexer] = None,
**mla_args,
) -> None
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_forward_bf16_kv ¶
_forward_bf16_kv(
q: Tensor,
kv_c_and_k_pe_cache: Tensor,
topk_indices: Tensor,
attn_metadata: FlashMLASparseMetadata,
) -> Tensor
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_forward_fp8_kv ¶
_forward_fp8_kv(
q: Tensor,
kv_c_and_k_pe_cache: Tensor,
topk_indices: Tensor,
attn_metadata: FlashMLASparseMetadata,
) -> Tensor
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
forward ¶
forward(
layer: AttentionLayer,
q: Tensor,
k_c_normed: Tensor,
k_pe: Tensor,
kv_cache: Tensor,
attn_metadata: FlashMLASparseMetadata,
output: Optional[Tensor] = None,
output_scale: Optional[Tensor] = None,
output_block_scale: Optional[Tensor] = None,
) -> Tensor
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
|
FlashMLASparseMetadata dataclass
¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
fp8_extra_metadata class-attribute
instance-attribute
¶
fp8_extra_metadata: Optional[FP8KernelMetadata] = None
FP8KernelMetadata dataclass
¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
__init__ ¶
__init__(
num_reqs: int,
max_query_len: int,
max_seq_len: int,
num_actual_tokens: int,
query_start_loc: Tensor,
slot_mapping: Tensor,
block_table: Tensor,
req_id_per_token: Tensor,
block_size: int = 64,
topk_tokens: int = 2048,
fp8_extra_metadata: Optional[FP8KernelMetadata] = None,
) -> None
FlashMLASparseMetadataBuilder dataclass
¶
Bases: AttentionMetadataBuilder[FlashMLASparseMetadata]
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
270 271 272 273 274 275 276 277 278 279 280 281 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 388 389 |
|
dummy_block_table instance-attribute
¶
dummy_block_table = empty(
(1, 1), dtype=int32, device=device
)
max_model_len_tensor instance-attribute
¶
max_model_len_tensor = tensor(
[max_model_len], device=device, dtype=int32
)
req_id_per_token_buffer instance-attribute
¶
req_id_per_token_buffer = empty(
(max_num_batched_tokens,), dtype=int32, device=device
)
tile_scheduler_metadata_buffer instance-attribute
¶
tile_scheduler_metadata_buffer = empty(
(max_num_sm_parts, 8), dtype=int32, device=device
)
topk_tokens_tensor instance-attribute
¶
topk_tokens_tensor = tensor(
[topk_tokens], device=device, dtype=int32
)
__init__ ¶
__init__(
kv_cache_spec: AttentionSpec,
layer_names: list[str],
vllm_config: VllmConfig,
device: device,
)
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
build ¶
build(
common_prefix_len: int,
common_attn_metadata: CommonAttentionMetadata,
fast_build: bool = False,
) -> FlashMLASparseMetadata
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
MLASparsePrefillMetadata dataclass
¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_convert_req_index_to_global_index_kernel ¶
_convert_req_index_to_global_index_kernel(
req_id_ptr,
block_table_ptr,
token_indices_ptr,
out_ptr,
max_num_blocks_per_req: constexpr,
BLOCK_SIZE: constexpr,
BLOCK_N: constexpr,
bt_stride0,
bt_stride1,
ti_stride0,
ti_stride1,
out_stride0,
out_stride1,
)
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_lse2_to_lse ¶
triton_convert_req_index_to_global_index ¶
triton_convert_req_index_to_global_index(
req_id: Tensor,
block_table: Tensor,
token_indices: Tensor,
BLOCK_SIZE: int = 64,
NUM_TOPK_TOKENS: int = 2048,
BLOCK_N: int = 128,
)
out[token_id, indice_id] = block_table[req_id[token_id], token_indices[token_id, indice_id] // BLOCK_SIZE] * BLOCK_SIZE + token_indices[token_id, indice_id] % BLOCK_SIZE
Only when token_indices[token_id, indice_id] == -1 do we output -1. For safety, we also output -1 if the derived block_id would be out-of-bounds.