backends
¶
Modules¶
fastvideo.attention.backends.abstract
¶
Classes¶
fastvideo.attention.backends.abstract.AttentionBackend
¶
fastvideo.attention.backends.abstract.AttentionImpl
¶
AttentionImpl(num_heads: int, head_size: int, softmax_scale: float, causal: bool = False, num_kv_heads: int | None = None, prefix: str = '', **extra_impl_args)
Source code in fastvideo/attention/backends/abstract.py
Functions¶
fastvideo.attention.backends.abstract.AttentionImpl.postprocess_output
¶Postprocess the output tensor after the attention operation.
Default implementation returns the tensor unchanged. Subclasses can override this to implement custom postprocessing like untiling, scaling, or other transformations.
Called BEFORE all_to_all for distributed attention
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
Tensor
|
The output tensor from the attention operation |
required |
attn_metadata
|
T
|
Metadata for the attention operation |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Postprocessed output tensor |
Source code in fastvideo/attention/backends/abstract.py
fastvideo.attention.backends.abstract.AttentionImpl.preprocess_qkv
¶Preprocess QKV tensor before performing attention operation.
Default implementation returns the tensor unchanged. Subclasses can override this to implement custom preprocessing like reshaping, tiling, scaling, or other transformations.
Called AFTER all_to_all for distributed attention
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qkv
|
Tensor
|
The query-key-value tensor |
required |
attn_metadata
|
T
|
Metadata for the attention operation |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Processed QKV tensor |
Source code in fastvideo/attention/backends/abstract.py
fastvideo.attention.backends.abstract.AttentionMetadata
dataclass
¶
AttentionMetadata(current_timestep: int)
Attention metadata for prefill and decode batched together.
Functions¶
fastvideo.attention.backends.abstract.AttentionMetadata.asdict_zerocopy
¶Similar to dataclasses.asdict, but avoids deepcopying.
Source code in fastvideo/attention/backends/abstract.py
fastvideo.attention.backends.abstract.AttentionMetadataBuilder
¶
Abstract class for attention metadata builders.
Create the builder, remember some configuration and parameters.
Source code in fastvideo/attention/backends/abstract.py
fastvideo.attention.backends.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
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 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
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.
Source code in fastvideo/attention/backends/sla.py
fastvideo.attention.backends.video_sparse_attn
¶
Classes¶
Functions¶
fastvideo.attention.backends.video_sparse_attn.construct_variable_block_sizes
cached
¶
construct_variable_block_sizes(dit_seq_shape: tuple[int, int, int], num_tiles: tuple[int, int, int], device: device) -> LongTensor
Compute the number of valid (non‑padded) tokens inside every
(ts_t × ts_h × ts_w) tile after padding ‑‑ flattened in the order
(t‑tile, h‑tile, w‑tile) that rearrange uses.
Returns¶
torch.LongTensor # shape: [∏ full_window_size]
Source code in fastvideo/attention/backends/video_sparse_attn.py
fastvideo.attention.backends.vmoba
¶
Classes¶
fastvideo.attention.backends.vmoba.VMOBAAttentionImpl
¶
VMOBAAttentionImpl(num_heads, head_size, softmax_scale, causal=False, num_kv_heads=None, prefix='', **extra_impl_args)
Bases: AttentionImpl
Source code in fastvideo/attention/backends/vmoba.py
Functions¶
fastvideo.attention.backends.vmoba.VMOBAAttentionImpl.forward
¶forward(query: Tensor, key: Tensor, value: Tensor, attn_metadata: AttentionMetadata) -> Tensor
query: [B, L, H, D] key: [B, L, H, D] value: [B, L, H, D] attn_metadata: AttentionMetadata