sla
¶
Classes¶
fastvideo.attention.backends.sla.SLAAttentionBackend
¶
fastvideo.attention.backends.sla.SLAAttentionImpl
¶
SLAAttentionImpl(num_heads: int, head_size: int, causal: bool = False, softmax_scale: float | None = None, num_kv_heads: int | None = None, prefix: str = '', topk_ratio: float = 0.1, feature_map: str = 'softmax', BLKQ: int = 128, BLKK: int = 64, use_bf16: bool = True, **extra_impl_args)
Bases: AttentionImpl, Module
SLA attention implementation with learnable linear projection.
This implementation combines sparse attention with linear attention, using a learnable projection to blend the outputs. The sparse attention uses a block-sparse pattern determined by QK similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_heads
|
int
|
Number of attention heads |
required |
head_size
|
int
|
Dimension of each head |
required |
topk_ratio
|
float
|
Ratio of key blocks to attend to (0-1), default 0.5 |
0.1
|
feature_map
|
str
|
Feature map for linear attention ('softmax', 'elu', 'relu') |
'softmax'
|
BLKQ
|
int
|
Query block size for sparse attention |
128
|
BLKK
|
int
|
Key block size for sparse attention |
64
|
use_bf16
|
bool
|
Whether to use bfloat16 for computation |
True
|
Source code in fastvideo/attention/backends/sla.py
Functions¶
fastvideo.attention.backends.sla.SLAAttentionImpl.forward
¶
forward(query: Tensor, key: Tensor, value: Tensor, attn_metadata: AttentionMetadata) -> Tensor
Forward pass for SLA attention.
Input tensors are in FastVideo format: (B, L, H, D) Internally converted to SLA format: (B, H, L, D)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Tensor
|
Query tensor (B, L, H, D) |
required |
key
|
Tensor
|
Key tensor (B, L, H, D) |
required |
value
|
Tensor
|
Value tensor (B, L, H, D) |
required |
attn_metadata
|
AttentionMetadata
|
Attention metadata |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor (B, L, H, D) |
Source code in fastvideo/attention/backends/sla.py
fastvideo.attention.backends.sla.SLAAttentionMetadata
dataclass
¶
fastvideo.attention.backends.sla.SLAAttentionMetadataBuilder
¶
fastvideo.attention.backends.sla.SageSLAAttentionBackend
¶
fastvideo.attention.backends.sla.SageSLAAttentionImpl
¶
SageSLAAttentionImpl(num_heads: int, head_size: int, causal: bool = False, softmax_scale: float | None = None, num_kv_heads: int | None = None, prefix: str = '', topk_ratio: float = 0.5, feature_map: str = 'softmax', use_bf16: bool = True, **extra_impl_args)
Bases: AttentionImpl, Module
SageSLA attention implementation using quantized SageAttention kernels.
This uses INT8 quantization for Q/K and FP8 for V to achieve better performance while maintaining accuracy. Requires spas_sage_attn package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_heads
|
int
|
Number of attention heads |
required |
head_size
|
int
|
Dimension of each head (must be 64 or 128) |
required |
topk_ratio
|
float
|
Ratio of key blocks to attend to (0-1), default 0.5 |
0.5
|
feature_map
|
str
|
Feature map for linear attention ('softmax', 'elu', 'relu') |
'softmax'
|
use_bf16
|
bool
|
Whether to use bfloat16 for computation |
True
|
Source code in fastvideo/attention/backends/sla.py
Functions¶
fastvideo.attention.backends.sla.SageSLAAttentionImpl.forward
¶
forward(query: Tensor, key: Tensor, value: Tensor, attn_metadata: AttentionMetadata) -> Tensor
Forward pass for SageSLA attention with quantized kernels.
Input tensors are in FastVideo format: (B, L, H, D)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Tensor
|
Query tensor (B, L, H, D) |
required |
key
|
Tensor
|
Key tensor (B, L, H, D) |
required |
value
|
Tensor
|
Value tensor (B, L, H, D) |
required |
attn_metadata
|
AttentionMetadata
|
Attention metadata |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor (B, L, H, D) |
Source code in fastvideo/attention/backends/sla.py
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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |
Functions¶
fastvideo.attention.backends.sla.get_block_map
¶
get_block_map(q: Tensor, k: Tensor, topk_ratio: float, BLKQ: int = 64, BLKK: int = 64) -> tuple[Tensor, Tensor, int]
Compute sparse block map for attention based on QK similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor of shape (B, H, L, D) |
required |
k
|
Tensor
|
Key tensor of shape (B, H, L, D) |
required |
topk_ratio
|
float
|
Ratio of key blocks to attend to (0-1) |
required |
BLKQ
|
int
|
Query block size |
64
|
BLKK
|
int
|
Key block size |
64
|
Returns:
| Name | Type | Description |
|---|---|---|
sparse_map |
Tensor
|
Binary mask of shape (B, H, num_q_blocks, num_k_blocks) |
lut |
Tensor
|
Top-k indices of shape (B, H, num_q_blocks, topk) |
topk |
int
|
Number of key blocks selected |
Source code in fastvideo/attention/backends/sla.py
fastvideo.attention.backends.sla.mean_pool
¶
mean_pool(x: Tensor, BLK: int) -> Tensor
Mean pool tensor along sequence dimension with block size BLK.