attention
¶
Classes¶
fastvideo.attention.AttentionMetadata
dataclass
¶
AttentionMetadata(current_timestep: int)
Attention metadata for prefill and decode batched together.
Functions¶
fastvideo.attention.AttentionMetadata.asdict_zerocopy
¶
Similar to dataclasses.asdict, but avoids deepcopying.
Source code in fastvideo/attention/backends/abstract.py
fastvideo.attention.AttentionMetadataBuilder
¶
fastvideo.attention.DistributedAttention
¶
DistributedAttention(num_heads: int, head_size: int, num_kv_heads: int | None = None, softmax_scale: float | None = None, causal: bool = False, supported_attention_backends: tuple[AttentionBackendEnum, ...] | None = None, prefix: str = '', **extra_impl_args)
Bases: Module
Distributed attention layer.
Source code in fastvideo/attention/layer.py
Functions¶
fastvideo.attention.DistributedAttention.forward
¶
forward(q: Tensor, k: Tensor, v: Tensor, replicated_q: Tensor | None = None, replicated_k: Tensor | None = None, replicated_v: Tensor | None = None) -> tuple[Tensor, Tensor | None]
Forward pass for distributed attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor [batch_size, seq_len, num_heads, head_dim] |
required |
k
|
Tensor
|
Key tensor [batch_size, seq_len, num_heads, head_dim] |
required |
v
|
Tensor
|
Value tensor [batch_size, seq_len, num_heads, head_dim] |
required |
replicated_q
|
Optional[Tensor]
|
Replicated query tensor, typically for text tokens |
None
|
replicated_k
|
Optional[Tensor]
|
Replicated key tensor |
None
|
replicated_v
|
Optional[Tensor]
|
Replicated value tensor |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor | None]
|
Tuple[torch.Tensor, Optional[torch.Tensor]]: A tuple containing: - o (torch.Tensor): Output tensor after attention for the main sequence - replicated_o (Optional[torch.Tensor]): Output tensor for replicated tokens, if provided |
Source code in fastvideo/attention/layer.py
fastvideo.attention.DistributedAttention_VSA
¶
DistributedAttention_VSA(num_heads: int, head_size: int, num_kv_heads: int | None = None, softmax_scale: float | None = None, causal: bool = False, supported_attention_backends: tuple[AttentionBackendEnum, ...] | None = None, prefix: str = '', **extra_impl_args)
Bases: DistributedAttention
Distributed attention layer with VSA support.
Source code in fastvideo/attention/layer.py
Functions¶
fastvideo.attention.DistributedAttention_VSA.forward
¶
forward(q: Tensor, k: Tensor, v: Tensor, replicated_q: Tensor | None = None, replicated_k: Tensor | None = None, replicated_v: Tensor | None = None, gate_compress: Tensor | None = None) -> tuple[Tensor, Tensor | None]
Forward pass for distributed attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor [batch_size, seq_len, num_heads, head_dim] |
required |
k
|
Tensor
|
Key tensor [batch_size, seq_len, num_heads, head_dim] |
required |
v
|
Tensor
|
Value tensor [batch_size, seq_len, num_heads, head_dim] |
required |
gate_compress
|
Tensor
|
Gate compress tensor [batch_size, seq_len, num_heads, head_dim] |
None
|
replicated_q
|
Optional[Tensor]
|
Replicated query tensor, typically for text tokens |
None
|
replicated_k
|
Optional[Tensor]
|
Replicated key tensor |
None
|
replicated_v
|
Optional[Tensor]
|
Replicated value tensor |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor | None]
|
Tuple[torch.Tensor, Optional[torch.Tensor]]: A tuple containing: - o (torch.Tensor): Output tensor after attention for the main sequence - replicated_o (Optional[torch.Tensor]): Output tensor for replicated tokens, if provided |
Source code in fastvideo/attention/layer.py
fastvideo.attention.LocalAttention
¶
LocalAttention(num_heads: int, head_size: int, num_kv_heads: int | None = None, softmax_scale: float | None = None, causal: bool = False, supported_attention_backends: tuple[AttentionBackendEnum, ...] | None = None, **extra_impl_args)
Bases: Module
Attention layer.
Source code in fastvideo/attention/layer.py
Functions¶
fastvideo.attention.LocalAttention.forward
¶
Apply local attention between query, key and value tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor of shape [batch_size, seq_len, num_heads, head_dim] |
required |
k
|
Tensor
|
Key tensor of shape [batch_size, seq_len, num_heads, head_dim] |
required |
v
|
Tensor
|
Value tensor of shape [batch_size, seq_len, num_heads, head_dim] |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Output tensor after local attention |
Source code in fastvideo/attention/layer.py
Modules¶
fastvideo.attention.backends
¶
Modules¶
fastvideo.attention.backends.STA_configuration
¶
Functions¶
fastvideo.attention.backends.STA_configuration.average_head_losses
¶average_head_losses(results: list[dict[str, Any]], selected_masks: list[list[int]]) -> dict[str, dict[str, ndarray]]
Average losses across all prompts for each mask strategy.
Source code in fastvideo/attention/backends/STA_configuration.py
fastvideo.attention.backends.STA_configuration.configure_sta
¶configure_sta(mode: str = 'STA_searching', layer_num: int = 40, time_step_num: int = 50, head_num: int = 40, **kwargs) -> list[list[list[Any]]]
Configure Sliding Tile Attention (STA) parameters based on the specified mode.
Parameters:¶
mode : str The STA mode to use. Options are: - 'STA_searching': Generate a set of mask candidates for initial search - 'STA_tuning': Select best mask strategy based on previously saved results - 'STA_inference': Load and use a previously tuned mask strategy layer_num: int, number of layers time_step_num: int, number of timesteps head_num: int, number of heads
**kwargs : dict Mode-specific parameters:
For 'STA_searching':
- mask_candidates: list of str, optional, mask candidates to use
- mask_selected: list of int, optional, indices of selected masks
For 'STA_tuning':
- mask_search_files_path: str, required, path to mask search results
- mask_candidates: list of str, optional, mask candidates to use
- mask_selected: list of int, optional, indices of selected masks
- skip_time_steps: int, optional, number of time steps to use full attention (default 12)
- save_dir: str, optional, directory to save mask strategy (default "mask_candidates")
For 'STA_inference':
- load_path: str, optional, path to load mask strategy (default "mask_candidates/mask_strategy.json")
Source code in fastvideo/attention/backends/STA_configuration.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 | |
fastvideo.attention.backends.STA_configuration.read_specific_json_files
¶Read and parse JSON files containing mask search results.
Source code in fastvideo/attention/backends/STA_configuration.py
fastvideo.attention.backends.STA_configuration.select_best_mask_strategy
¶select_best_mask_strategy(averaged_results: dict[str, dict[str, ndarray]], selected_masks: list[list[int]], skip_time_steps: int = 12, timesteps: int = 50, head_num: int = 40) -> tuple[dict[str, list[int]], float, dict[str, int]]
Select the best mask strategy for each head based on loss minimization.
Source code in fastvideo/attention/backends/STA_configuration.py
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
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.
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.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
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
Source code in fastvideo/attention/backends/vmoba.py
Functions¶
fastvideo.attention.layer
¶
Classes¶
fastvideo.attention.layer.DistributedAttention
¶
DistributedAttention(num_heads: int, head_size: int, num_kv_heads: int | None = None, softmax_scale: float | None = None, causal: bool = False, supported_attention_backends: tuple[AttentionBackendEnum, ...] | None = None, prefix: str = '', **extra_impl_args)
Bases: Module
Distributed attention layer.
Source code in fastvideo/attention/layer.py
Functions¶
fastvideo.attention.layer.DistributedAttention.forward
¶forward(q: Tensor, k: Tensor, v: Tensor, replicated_q: Tensor | None = None, replicated_k: Tensor | None = None, replicated_v: Tensor | None = None) -> tuple[Tensor, Tensor | None]
Forward pass for distributed attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor [batch_size, seq_len, num_heads, head_dim] |
required |
k
|
Tensor
|
Key tensor [batch_size, seq_len, num_heads, head_dim] |
required |
v
|
Tensor
|
Value tensor [batch_size, seq_len, num_heads, head_dim] |
required |
replicated_q
|
Optional[Tensor]
|
Replicated query tensor, typically for text tokens |
None
|
replicated_k
|
Optional[Tensor]
|
Replicated key tensor |
None
|
replicated_v
|
Optional[Tensor]
|
Replicated value tensor |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor | None]
|
Tuple[torch.Tensor, Optional[torch.Tensor]]: A tuple containing: - o (torch.Tensor): Output tensor after attention for the main sequence - replicated_o (Optional[torch.Tensor]): Output tensor for replicated tokens, if provided |
Source code in fastvideo/attention/layer.py
fastvideo.attention.layer.DistributedAttention_VSA
¶
DistributedAttention_VSA(num_heads: int, head_size: int, num_kv_heads: int | None = None, softmax_scale: float | None = None, causal: bool = False, supported_attention_backends: tuple[AttentionBackendEnum, ...] | None = None, prefix: str = '', **extra_impl_args)
Bases: DistributedAttention
Distributed attention layer with VSA support.
Source code in fastvideo/attention/layer.py
Functions¶
fastvideo.attention.layer.DistributedAttention_VSA.forward
¶forward(q: Tensor, k: Tensor, v: Tensor, replicated_q: Tensor | None = None, replicated_k: Tensor | None = None, replicated_v: Tensor | None = None, gate_compress: Tensor | None = None) -> tuple[Tensor, Tensor | None]
Forward pass for distributed attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor [batch_size, seq_len, num_heads, head_dim] |
required |
k
|
Tensor
|
Key tensor [batch_size, seq_len, num_heads, head_dim] |
required |
v
|
Tensor
|
Value tensor [batch_size, seq_len, num_heads, head_dim] |
required |
gate_compress
|
Tensor
|
Gate compress tensor [batch_size, seq_len, num_heads, head_dim] |
None
|
replicated_q
|
Optional[Tensor]
|
Replicated query tensor, typically for text tokens |
None
|
replicated_k
|
Optional[Tensor]
|
Replicated key tensor |
None
|
replicated_v
|
Optional[Tensor]
|
Replicated value tensor |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor | None]
|
Tuple[torch.Tensor, Optional[torch.Tensor]]: A tuple containing: - o (torch.Tensor): Output tensor after attention for the main sequence - replicated_o (Optional[torch.Tensor]): Output tensor for replicated tokens, if provided |
Source code in fastvideo/attention/layer.py
fastvideo.attention.layer.LocalAttention
¶
LocalAttention(num_heads: int, head_size: int, num_kv_heads: int | None = None, softmax_scale: float | None = None, causal: bool = False, supported_attention_backends: tuple[AttentionBackendEnum, ...] | None = None, **extra_impl_args)
Bases: Module
Attention layer.
Source code in fastvideo/attention/layer.py
Functions¶
fastvideo.attention.layer.LocalAttention.forward
¶Apply local attention between query, key and value tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Query tensor of shape [batch_size, seq_len, num_heads, head_dim] |
required |
k
|
Tensor
|
Key tensor of shape [batch_size, seq_len, num_heads, head_dim] |
required |
v
|
Tensor
|
Value tensor of shape [batch_size, seq_len, num_heads, head_dim] |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Output tensor after local attention |
Source code in fastvideo/attention/layer.py
Functions¶
fastvideo.attention.selector
¶
Classes¶
Functions¶
fastvideo.attention.selector.backend_name_to_enum
¶
backend_name_to_enum(backend_name: str) -> AttentionBackendEnum | None
Convert a string backend name to a _Backend enum value.
Returns: * _Backend: enum value if backend_name is a valid in-tree type * None: otherwise it's an invalid in-tree type or an out-of-tree platform is loaded.
Source code in fastvideo/attention/selector.py
fastvideo.attention.selector.get_env_variable_attn_backend
¶
Get the backend override specified by the FastVideo attention backend environment variable, if one is specified.
Returns:
- _Backend enum value if an override is specified
- None otherwise
Source code in fastvideo/attention/selector.py
fastvideo.attention.selector.get_global_forced_attn_backend
¶
Get the currently-forced choice of attention backend, or None if auto-selection is currently enabled.
fastvideo.attention.selector.global_force_attn_backend
¶
Force all attention operations to use a specified backend.
Passing None for the argument re-enables automatic
backend selection.,
Arguments:
- attn_backend: backend selection (None to revert to auto)
Source code in fastvideo/attention/selector.py
fastvideo.attention.selector.global_force_attn_backend_context_manager
¶
global_force_attn_backend_context_manager(attn_backend: AttentionBackendEnum) -> Generator[None, None, None]
Globally force a FastVideo attention backend override within a context manager, reverting the global attention backend override to its prior state upon exiting the context manager.
Arguments:
- attn_backend: attention backend to force
Returns:
- Generator