video_sparse_attn_h3 ¶
VSA for MiniMax H3's packed mixed-modality self-attention.
H3 runs one joint bidirectional attention over [text | condition keyframes | audio | generated video], so this backend differs from the Wan-tuned video_sparse_attn:
- Tiles are
[segment-pure prefix chunks] + [3D video tiles]; prefix tiles never straddle segment boundaries. The tile size is selectable at metadata build time: 256 tokens(4,8,8)(default), 128 tokens(4,4,8), or 64 tokens(4,4,4)(seeVSA_H3_TILE_SHAPES). - The builder takes the packed sequence as ordered segments: dense row counts (text, audio, image references) and one 3-D sparse region per video, wherever it sits. The generated video is the last region; a Ref2VA PDD run adds one earlier region per reference video. Rows are permuted to
[dense chunks][region 0 tiles][region 1 tiles]...anduntile_combined_indexinverts the permutation, so the kernels see one dense prefix followed by video tiles. Every video query keeps its own top-k of EACH region (video_tile_spans/span_sparsities); the reference-video regions may use a different keep rate than the generated video. - Selection is pure Python on pooled tile scores; the block-sparse kernel consumes an explicit bool mask, so no kernel changes are needed.
- The compression branch is gated by
to_gate_compress, which the base H3 checkpoint does not carry: the loader zero-initializes it, so untrained inference is exactly pure sparse and finetuning can learn the gate. VSA-distilled students (e.g. FastVideo-Minimax-H3-Preview) ship trained gates, which load and activate the branch. - Non-video queries are always dense. Non-video keys are either always-selected for every query ("exempt", default) or compete in top-k under a FLOP-matched budget ("compete") — the ablation axis, switched per request via
generate_video(..., vsa_mode=...)(default: exempt). Per-request scheduling knobs (vsa_dense_first_n_steps,vsa_dense_layers) let mixed schedules run the diffuse steps/layers dense while pushing the rest harder.
At tile 256 this targets sm10.x through the FA4 CuTe 256-tile path (FASTVIDEO_VSA_CUTEDSL=1); the Triton 256→64 expansion is the fallback and keeps identical mask semantics. At tile 64 the block map is already at the kernels' native 64-token granularity, so both forward and backward run the Triton block-sparse kernels directly (no expansion, FASTVIDEO_VSA_CUTEDSL does not apply). A third, opt-in route exists for the tile-64 FORWARD only: FASTVIDEO_VSA_SM100A=1 sends no-grad forwards through the data-center Blackwell CUDA block-sparse kernel (fastvideo_kernel.block_sparse_attn_sm100a, which reads a separate q2k_num key-tile count per query tile) when the extension is built, the device is sm_100 or sm_103, and the geometry qualifies. The CUDA kernel assigns adjacent pairs of query tiles to CTAs, so an odd logical tile count receives one internal, zero-valid partner tile for the no-grad call only. Score search, the trained mask, gate-compress, and the returned packed sequence remain on the original logical tiles. Grad-tracking forwards and every backward stay on the Triton kernels. If the env is set but a precondition fails, the route logs one warning and falls back.
Tile 128 has exactly one implementation: the same sm_100a/sm_103a CUDA forward, which carries a 128-token block instantiation. It needs no opt-in, pads an odd tile count with the same zero-valid partner tile, runs no-grad forwards only, and fails closed (no Triton fallback) when the extension or device cannot run it.
Classes¶
fastvideo.attention.backends.video_sparse_attn_h3.MiniMaxH3VSAImpl ¶
MiniMaxH3VSAImpl(num_heads: int, head_size: int, causal: bool, softmax_scale: float, num_kv_heads: int | None = None, prefix: str = '', **extra_impl_args)
Bases: AttentionImpl
Source code in fastvideo/attention/backends/video_sparse_attn_h3.py
Methods:¶
fastvideo.attention.backends.video_sparse_attn_h3.MiniMaxH3VSAImpl.prepare_for_compile ¶
Tensorize per-layer state shared by every torch.compile route.
fastvideo.attention.backends.video_sparse_attn_h3.MiniMaxH3VSAImpl.prepare_for_regional_compile ¶
prepare_for_regional_compile(device: device) -> str | None
Resolve the inference-only sm_100a route before fullgraph capture.
The ordinary eager route probes the environment, extension, device, and tensor contract at every call so it can warn and fall back. Those Python/device-capability checks are not safe inside a regional fullgraph=True block. Probe one representative tile-64 input on the loaded model's device now, then let forward specialize on the resulting plain bool while Dynamo is compiling.
Source code in fastvideo/attention/backends/video_sparse_attn_h3.py
fastvideo.attention.backends.video_sparse_attn_h3.MiniMaxH3VSAImpl.tile ¶
Scatter rows into the padded tile buffer (pad positions stay zero).
Without grad tracking the returned tensor aliases the builder-owned buffer; callers must consume it before the next tile() (both call sites in forward() read it immediately). A grad-tracking forward instead receives a fresh buffer and leaves the holder untouched, so the builder never retains autograd state across steps. Odd no-grad sm100a requests (tile 128 always, tile 64 when opted in) carry one additional all-zero tile internally; metadata and all observable outputs retain the logical geometry.
Source code in fastvideo/attention/backends/video_sparse_attn_h3.py
fastvideo.attention.backends.video_sparse_attn_h3.MiniMaxH3VSAMetadataBuilder ¶
Bases: AttentionMetadataBuilder
Source code in fastvideo/attention/backends/video_sparse_attn_h3.py
Methods:¶
fastvideo.attention.backends.video_sparse_attn_h3.MiniMaxH3VSAMetadataBuilder.build ¶
build(current_timestep: int, patch_size: tuple[int, int, int], VSA_sparsity: float, packed_segments: tuple[int | tuple[int, int, int], ...], device: device, exempt: bool = True, dense_layers: tuple[int, ...] = (), tile_size: int = _TILE_ELEMS, ref_keep_rate: float | None = None, **kwargs: dict[str, Any]) -> MiniMaxH3VSAMetadata
Build per-step metadata for one packed H3 sequence.
packed_segments lists the sequence in packed order: an int is a dense segment's row count, and a (t, h, w) triple is the raw latent shape of one sparse video region. The last region is the generated video and follows VSA_sparsity; every earlier region is a reference video and keeps ref_keep_rate of its tiles, which a build with reference-video regions requires. exempt=False (compete mode) supports only the generated-video region.
Source code in fastvideo/attention/backends/video_sparse_attn_h3.py
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 | |
Functions:¶
fastvideo.attention.backends.video_sparse_attn_h3.token_tile_and_valid ¶
token_tile_and_valid(variable_block_sizes: Tensor, tile_elems: int = _TILE_ELEMS) -> tuple[Tensor, Tensor]
Per padded-token tile id and pad-validity mask.
The single encoding of the padding contract, shared by the probe and the test oracle so they cannot drift from the backend's tile geometry. tile_elems must match the metadata the sizes came from (MiniMaxH3VSAMetadata.tile_elems).