fastwan ¶
FastWan-oriented helpers for the experimental MLX runtime path.
Classes¶
fastvideo.mlx_runtime.fastwan.MLXQuantizationSpec dataclass ¶
MLX quantized-matmul configuration for DiT linear weights.
fastvideo.mlx_runtime.fastwan.MLXWanDiT ¶
MLXWanDiT(weights: dict[str, array], blocks: list[MLXWanTransformerBlock], config: dict, *, compile: bool = False)
Experimental FP16 Wan/FastWan DiT forward path in MLX.
Source code in fastvideo/mlx_runtime/fastwan.py
fastvideo.mlx_runtime.fastwan.MLXWanTransformerBlock ¶
MLXWanTransformerBlock(weights: dict[str, array], *, dim: int, ffn_dim: int, num_heads: int, eps: float = 1e-06)
Dense T2V Wan transformer block for the experimental MLX runtime.
This mirrors the non-VSA PyTorch block for single-process dense attention. Rotary embeddings and sequence-parallel paths are intentionally left out of this first parity target.
Source code in fastvideo/mlx_runtime/fastwan.py
fastvideo.mlx_runtime.fastwan.UnsupportedMLXQuantizationError ¶
Bases: ValueError
A quantization mode the installed MLX build cannot execute.
Raised by :func:ensure_quantization_supported before any model weights are loaded, so callers (CLI flags, benchmark sweeps) can fail fast with an actionable message -- or skip the mode -- instead of crashing deep inside mx.quantize mid-load.
Functions:¶
fastvideo.mlx_runtime.fastwan.apply_rotary_emb ¶
apply_rotary_emb(x, cos, sin, *, is_neox_style: bool = False)
Apply FastVideo's rotary convention to MLX tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | [batch, seq, heads, head_dim] | required | |
cos/sin | [seq, head_dim] for Wan's full-dimension rotate-pair style, or [seq, head_dim // 2] for traditional RoPE. | required |
Source code in fastvideo/mlx_runtime/fastwan.py
fastvideo.mlx_runtime.fastwan.ensure_quantization_supported ¶
ensure_quantization_supported(spec: MLXQuantizationSpec | None) -> None
Raise :class:UnsupportedMLXQuantizationError if spec cannot run here.
Source code in fastvideo/mlx_runtime/fastwan.py
fastvideo.mlx_runtime.fastwan.fastwan_shape ¶
fastwan_shape(*, height: int, width: int, num_frames: int, vae_temporal_compression: int = 4, vae_spatial_compression: int = 8, patch_size: tuple[int, int, int] = (1, 2, 2), num_heads: int = 12, head_dim: int = 128) -> FastWanShape
Return the approximate DiT token shape for Wan/FastWan T2V inference.
Source code in fastvideo/mlx_runtime/fastwan.py
fastvideo.mlx_runtime.fastwan.gelu_tanh ¶
tanh-approximate GELU, as used by Wan's FFN.
mlx.nn.gelu_approx is the same tanh approximation behind a fused kernel. On the 1.3B FFN shape (32760x8960) it is bit-identical to the expanded expression below and 3.3x faster — 28.9ms -> 8.7ms per layer, which is 0.6s per denoise step across 30 layers.
Source code in fastvideo/mlx_runtime/fastwan.py
fastvideo.mlx_runtime.fastwan.mlx_block_weights_from_diffusers_safetensors ¶
mlx_block_weights_from_diffusers_safetensors(checkpoint_path: str | Path, *, block_index: int = 0, quantization: str | MLXQuantizationSpec | None = None, dtype=None) -> dict[str, array]
Load one Diffusers-format Wan block into the MLX dense-block key layout.
Source code in fastvideo/mlx_runtime/fastwan.py
fastvideo.mlx_runtime.fastwan.quantization_support_error ¶
quantization_support_error(spec: MLXQuantizationSpec) -> str | None
Probe whether the installed MLX build supports spec.
Runs a tiny mx.quantize + mx.quantized_matmul with exactly the arguments :func:quantize_matrix / :func:linear use, so the result reflects the real runtime path. The affine (int8/int4) modes are stable across MLX releases, but the mxfp8/mxfp4/nvfp4 mode strings require newer MLX builds and raise otherwise. Returns None when the mode works, else the underlying error message. Cached per spec.
Source code in fastvideo/mlx_runtime/fastwan.py
fastvideo.mlx_runtime.fastwan.torch_block_state_from_diffusers_safetensors ¶
torch_block_state_from_diffusers_safetensors(checkpoint_path: str | Path, *, block_index: int = 0) -> dict[str, Tensor]
Load one Diffusers-format Wan block into FastVideo's dense block keys.