minimax_h3_vsa ¶
MiniMax H3 Video Sparse Attention for the native MLX runtime.
Ports the packed-sequence VSA-H3 contract from fastvideo/attention/backends/video_sparse_attn_h3.py:
- Tiles are
[segment-pure prefix chunks] + [3D video tiles]. - Tile sizes 64
(4, 4, 4)and 256(4, 8, 8). - Per-head pooled Q/K scoring and top-k routing.
- Prefix queries are always dense; prefix keys are
exempt(always kept) orcompete(FLOP-matched top-k). - Optional dense-first steps and per-layer dense overrides.
- Trained
to_gate_compresspooled-compression branch.
Execution backends:
- reference (
autodefault) — grouped gather plus batchedmx.fast.scaled_dot_product_attention. Correctness baseline. - simd — opt-in SIMD-group 8x8 matrix operations over the reference tile map for tile size 64 and head dimension 128. Unsupported shapes and kernel failures fall back to the reference backend.
Dense fused SDPA remains the default when VSA is disabled, the geometry is unsupported, or a dense-only checkpoint is loaded.
Classes¶
fastvideo.mlx_runtime.minimax_h3_vsa.DenseOnlyVSACheckpointError ¶
Bases: ValueError
VSA was requested for a checkpoint that dropped the gate weights.
fastvideo.mlx_runtime.minimax_h3_vsa.MiniMaxH3VSAConfig dataclass ¶
MiniMaxH3VSAConfig(enabled: bool = False, sparsity: float = 0.9, tile_size: int = 64, prefix_mode: PrefixMode = 'exempt', dense_first_n_steps: int = 0, dense_layers: tuple[int, ...] = (), impl: VSAImpl = 'auto')
Runtime VSA knobs. Defaults preserve dense MLX H3 behavior.
fastvideo.mlx_runtime.minimax_h3_vsa.MiniMaxH3VSAGeometry dataclass ¶
MiniMaxH3VSAGeometry(prefix_segments: tuple[int, ...], dit_seq_shape: tuple[int, int, int], tile_shape: tuple[int, int, int], tile_elems: int, total_seq_length: int, num_prefix_tiles: int, num_video_tiles: int, variable_block_sizes: ndarray, untile_combined_index: ndarray, tile_partition_indices: ndarray)
Packed-sequence tile map shared by routing, reference, and Metal paths.
fastvideo.mlx_runtime.minimax_h3_vsa.MiniMaxH3VSAStats dataclass ¶
MiniMaxH3VSAStats(configured_sparsity: float = 0.0, layer_sparsity: float = 0.0, tile_size: int = 64, prefix_mode: str = 'exempt', impl: str = 'dense', num_prefix_tiles: int = 0, num_video_tiles: int = 0, video_keep: float = 0.0, achieved_sparsity: float = 0.0, dense_fallback_reason: str | None = None, attention_calls: int = 0, sparse_calls: int = 0, impl_counts: dict[str, int] = dict(), fallback_reasons: list[str] = list())
Filled during a sparse forward so the pipeline can report achieved sparsity.
Methods:¶
fastvideo.mlx_runtime.minimax_h3_vsa.MiniMaxH3VSAStats.record ¶
record(call: MiniMaxH3VSAStats) -> None
Aggregate equally sized video-query tile maps across blocks and steps.
Source code in fastvideo/mlx_runtime/minimax_h3_vsa.py
Functions:¶
fastvideo.mlx_runtime.minimax_h3_vsa.build_block_mask ¶
build_block_mask(scores: ndarray, num_prefix_tiles: int, num_video_tiles: int, sparsity: float, exempt: bool) -> ndarray
scores: [..., n_tiles, n_tiles] -> bool mask, same shape.
Mirrors _build_block_mask in the PyTorch H3 backend.
Source code in fastvideo/mlx_runtime/minimax_h3_vsa.py
fastvideo.mlx_runtime.minimax_h3_vsa.build_h3_tile_geometry ¶
build_h3_tile_geometry(prefix_segments: tuple[int, ...], dit_seq_shape: tuple[int, int, int], tile_size: int = 64) -> MiniMaxH3VSAGeometry
Tile the packed sequence: segment-pure prefix chunks, then video tiles.
Source code in fastvideo/mlx_runtime/minimax_h3_vsa.py
fastvideo.mlx_runtime.minimax_h3_vsa.compute_topk ¶
Blocks to keep for a sparsity level, clamped to [1, num_blocks].
Source code in fastvideo/mlx_runtime/minimax_h3_vsa.py
fastvideo.mlx_runtime.minimax_h3_vsa.geometry_is_supported ¶
geometry_is_supported(prefix_segments: tuple[int, ...], dit_seq_shape: tuple[int, int, int], tile_size: int) -> str | None
Return a fallback reason, or None when VSA can run.
Source code in fastvideo/mlx_runtime/minimax_h3_vsa.py
fastvideo.mlx_runtime.minimax_h3_vsa.h3_vsa_attention ¶
h3_vsa_attention(query, key, value, geometry: MiniMaxH3VSAGeometry, *, sparsity: float, exempt: bool = True, gate_compress=None, impl: VSAImpl = 'auto', stats: MiniMaxH3VSAStats | None = None)
Packed [S, H, D] VSA attention. Falls back to dense SDPA when sparsity is 0.
Source code in fastvideo/mlx_runtime/minimax_h3_vsa.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | |
fastvideo.mlx_runtime.minimax_h3_vsa.prefix_segments_from_layout ¶
Segment sizes preceding the generated-video tail, matching the PyTorch stage.