minimax_h3 ¶
MiniMax H3 joint audio-video DiT for the Apple Silicon MLX runtime.
A faithful MLX port of the upstream CUDA reference:
- DiT:
fastvideo/models/dits/minimax_h3.py(merged upstream in #1674). Single-stream packed transformer, per-head qk RMSNorm, 3-axis MM-RoPE (96 of 128 head dims rotated), row-indexed AdaLN keyed by(timestep, modality), dual video/audio output heads, 2-block text token refiner. - Scheduler:
fastvideo/models/schedulers/scheduling_minimax_h3.py. Rectified-flow Euler with H3's clean-time convention:t = 1 - sigma, data-ward velocity (x0 = x_t + sigma * v), exponential-shift sigma grid (video shift 12.0, audio shift 3.0), fp32 Euler blend. - Packing:
fastvideo/pipelines/basic/minimax_h3/packing.py.[text | condition | audio | video]rows, float64 position grids.
Apple Silicon memory controls:
- AdaLN precompute cache. ~40% of H3's parameters live in per-block AdaLN projections whose output depends only on
(timestep, modality). For a fixed step schedule the full set of timesteps is known at load time, so :meth:MLXMiniMaxH3DiT.precompute_adalnevaluates every modulation table once and (optionally) drops the projection weights. This also removes the repeated AdaLN projection work from each denoising step. - Affine INT8, INT6, or INT4 quantization of attention/FFN matrices (group size 64). Modulation, embeddings, norms, and input/output projections remain in higher precision. Quantization is weight-only: attention Q/K/V stay BF16 (or the selected activation dtype).
- Optional VSA. Dense conversion still drops
transformer_blocks.*.attn.to_gate_compress.weight.--include-vsakeeps those 50 projections, quantizes them with the same affine grid, and recordsvsa.capablein the manifest. Runtime VSA is opt-in and never enabled for dense-only checkpoints.
Checkpoint layout: the released H3 checkpoint uses the diffusers reference module names 1:1 (transformer_blocks.{i}.attn.to_out.0.weight, ff.net.0.proj.weight, time_embedder.linear_1.weight, ...). This module keeps those names as the MLX weight keys — no renaming contract. proj_in / audio_proj_in / time_embedder / proj_out / audio_proj_out are fp32 in the release and are kept fp32 here.
Nothing in this file requires the CUDA stack; it imports fastvideo.logger only. Parity tests against the torch reference live in fastvideo/tests/mlx/test_mlx_minimax_h3_parity.py.
Classes¶
fastvideo.mlx_runtime.minimax_h3.MLXMiniMaxH3DiT ¶
MLXMiniMaxH3DiT(weights: dict[str, Any], blocks: list[dict[str, Any]], refiner: list[dict[str, Any]], config: dict[str, Any])
MiniMax H3 joint audio-video DiT in MLX (batch-1 packed forward).
Weight dicts keep the released checkpoint's key names. blocks holds the main transformer blocks, refiner the text refiner blocks; each is a dict of arrays / :class:QuantizedMatrix.
Source code in fastvideo/mlx_runtime/minimax_h3.py
Methods:¶
fastvideo.mlx_runtime.minimax_h3.MLXMiniMaxH3DiT.compute_temb ¶
(n,) timesteps -> (n, time_embed_dim) through time_proj + MLP.
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.MLXMiniMaxH3DiT.forward ¶
forward(video_rows, audio_rows, text_rows, *, position_ids, token_tags, timestep_indices, timesteps, video_indices, audio_indices, text_indices)
Faithful port of the torch forward (batch-1, rows already patchified).
Returns (video_output, audio_output) rows. The torch reference projects all packed rows through both heads and then selects; this selects first and projects only the relevant rows — row-wise identical math at a fraction of the cost.
This entrypoint is dense-only. VSA requires forward_with_cache with a packed layout and step index.
Source code in fastvideo/mlx_runtime/minimax_h3.py
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | |
fastvideo.mlx_runtime.minimax_h3.MLXMiniMaxH3DiT.forward_with_cache ¶
forward_with_cache(video_rows, audio_rows, text_rows, *, layout: MiniMaxH3PackedLayout, step_timesteps: ndarray, row_timestep_inverse: ndarray, step_index: int = 0)
Denoise-step forward served entirely from the AdaLN cache.
step_timesteps are this step's unique timesteps (sorted); row_timestep_inverse maps each packed row to one of them (the build_row_timesteps inverse). The cache must cover every value in step_timesteps.
Source code in fastvideo/mlx_runtime/minimax_h3.py
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 | |
fastvideo.mlx_runtime.minimax_h3.MLXMiniMaxH3DiT.precompute_adaln ¶
precompute_adaln(timesteps: ndarray, *, drop_weights: bool = True) -> MiniMaxH3StepCache
Evaluate every modulation table for a fixed schedule, once.
timesteps is the union (sorted, unique) of all per-step video/audio/condition timesteps the sampler will use. With drop_weights=True the per-block adaln_proj.linear weights are released afterwards — the memory win this runtime exists for.
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.MiniMaxH3PackedLayout dataclass ¶
MiniMaxH3PackedLayout(sequence_length: int, position_ids: ndarray, token_tags: ndarray, video_indices: ndarray, audio_indices: ndarray, text_indices: ndarray, num_condition_video_rows: int, num_condition_audio_rows: int, num_video_latent_frames: int, latent_height: int, latent_width: int, num_audio_latents: int)
One packed joint sequence and the geometry needed to interpret it.
Arrays are NumPy (position_ids in float64, indices int64) and converted to MLX at the model boundary.
fastvideo.mlx_runtime.minimax_h3.MiniMaxH3SchedulerState dataclass ¶
MiniMaxH3SchedulerState(shift: float, sigmas: ndarray, timesteps: ndarray)
One rectified-flow scheduler (use two: video shift 12, audio shift 3).
Methods:¶
fastvideo.mlx_runtime.minimax_h3.MiniMaxH3SchedulerState.scale_noise ¶
scale_noise(sample, timestep: float, noise)
Conditioning noise-aug: tsample + (1-t)noise (t=0.999 for keyframes).
fastvideo.mlx_runtime.minimax_h3.MiniMaxH3SchedulerState.step ¶
step(model_output, step_index: int, sample)
Data-ward Euler: x0 = x_t + sigma*v; blend toward x0 by sigma ratio.
fp32 blend for fp16/bf16 samples, matching the reference.
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.MiniMaxH3StepCache dataclass ¶
MiniMaxH3StepCache(timesteps: ndarray, block_tables: list[tuple[Any, ...]], norm_out_shift: Any, norm_out_scale: Any)
Precomputed AdaLN tables + norm_out modulation for a fixed schedule.
Holds one row set per distinct timestep in timesteps (the union of every denoise step's video/audio/condition timesteps). Blocks then index tables directly and the per-block adaln_proj weights can be freed — ~40% of H3's parameters never need to be resident during denoise.
Methods:¶
fastvideo.mlx_runtime.minimax_h3.MiniMaxH3StepCache.positions ¶
Map a step's unique timesteps to rows in the cached union.
Source code in fastvideo/mlx_runtime/minimax_h3.py
Functions:¶
fastvideo.mlx_runtime.minimax_h3.apply_h3_rotary ¶
Rotate the RoPE prefix of each head, preserving the rest.
x: (S, H, D); cos/sin: (S, R) with R <= D. Half-split (GPT-NeoX style) rotation inside the rotary prefix, computed in fp32.
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.build_packed_layout ¶
build_packed_layout(num_text_tokens: int, num_latent_frames: int, latent_height: int, latent_width: int, num_audio_latents: int, patch_size: tuple[int, int, int] = (1, 2, 2), keyframe_anchors: tuple[str, ...] = (), text_token_tags: ndarray | None = None, video_temporal_scale: float = 1.0) -> MiniMaxH3PackedLayout
Build the [text | condition | audio | video] layout (T2VA / FL2VA).
Source code in fastvideo/mlx_runtime/minimax_h3.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
fastvideo.mlx_runtime.minimax_h3.build_row_timesteps ¶
build_row_timesteps(layout: MiniMaxH3PackedLayout, video_timestep: float, audio_timestep: float, condition_video_timestep: float = 1.0, condition_audio_timestep: float = 1.0) -> tuple[ndarray, ndarray]
Per-row timesteps -> (unique timesteps sorted ascending, per-row indices).
Matches torch.unique(..., sorted=True, return_inverse=True).
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.linear ¶
Run an H3 linear with its measured wide-row affine dispatch enabled.
fastvideo.mlx_runtime.minimax_h3.load_mlx_h3_checkpoint ¶
load_mlx_h3_checkpoint(checkpoint_dir: str | Path) -> MLXMiniMaxH3DiT
Rebuild an H3 DiT saved by :func:save_mlx_h3_checkpoint.
Refuses a quantization grid the installed MLX cannot execute, loudly — never silently dequantizes onto a different grid.
Source code in fastvideo/mlx_runtime/minimax_h3.py
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 | |
fastvideo.mlx_runtime.minimax_h3.minimax_h3_sigmas ¶
Sigma grid of length num_denoise_steps + 1 (descending, ending at 0).
The reference set_timesteps(num_inference_steps=N) builds N sigmas and N-1 timesteps; this wrapper takes the denoise step count directly, which is what a sampler actually schedules.
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.mlx_h3_checkpoint_vsa_capable ¶
True when a saved MLX H3 checkpoint retained the VSA gate projections.
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.mlx_h3_dit_from_diffusers_safetensors ¶
mlx_h3_dit_from_diffusers_safetensors(transformer_path: str | Path, config: dict[str, Any] | None = None, *, dtype: str = 'fp16', num_blocks: int | None = None, quantization: str | MLXQuantizationSpec | None = None, adaln_cache_timesteps: ndarray | None = None, include_vsa: bool = False) -> MLXMiniMaxH3DiT
Load the released H3 transformer (diffusers layout) into MLX.
transformer_path is the transformer/ directory of the HF repo (or a single safetensors file, e.g. a student checkpoint). config defaults to config.json next to the weights. fp32-release modules stay fp32; attention/FFN matrices are quantized when quantization is set.
When adaln_cache_timesteps is provided, AdaLN tables are computed one block at a time while the checkpoint is read. The 13B projection weights are never retained together and are omitted from the returned model. This is the memory-bounded build path for the released 33B student.
include_vsa keeps attn.to_gate_compress matrices and quantizes them with the same affine grid as the other linear weights. Dense conversion continues to drop them.
Source code in fastvideo/mlx_runtime/minimax_h3.py
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 | |
fastvideo.mlx_runtime.minimax_h3.patchify_video_latents ¶
(B, C, T, H, W) -> (BT'H'W', Cptphpw), channel-major patch features.
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.rope_cos_sin ¶
(S, 3) positions -> (cos, sin) each (S, 6*rope_freq_dim), fp32.
Shared 16-freq inv_freq across the three axes; the (t, h, w) blocks are concatenated and doubled, so the rotary width is 6 * rope_freq_dim (96 of the 128 head dims at full size).
Source code in fastvideo/mlx_runtime/minimax_h3.py
fastvideo.mlx_runtime.minimax_h3.save_mlx_h3_checkpoint ¶
save_mlx_h3_checkpoint(dit: MLXMiniMaxH3DiT, checkpoint_dir: str | Path) -> Path
Persist a (possibly quantized, possibly AdaLN-dropped) H3 DiT.
Source code in fastvideo/mlx_runtime/minimax_h3.py
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 | |
fastvideo.mlx_runtime.minimax_h3.unpack_audio_tokens ¶
unpack_audio_tokens(rows: ndarray, num_audio_latents: int) -> ndarray
(2*num_audio_latents, feat) channel-major rows -> (2, feat, num_audio_latents).