Skip to content

MiniMax H3 / FastH3 video and audio generation on CUDA, one GPU or several Sparks over RoCE - #947

Closed
drowzeys wants to merge 5 commits into
ashhart:mainfrom
drowzeys:upstream/h3-cuda-parallel
Closed

drowzeys wants to merge 5 commits into
ashhart:mainfrom
drowzeys:upstream/h3-cuda-parallel

Conversation

@drowzeys

Copy link
Copy Markdown
Contributor

Thank you, Ash, for the invitation to send this for review, and for the CUDA runtime it builds on.

This is the first video generation model on TensorFold's CUDA runtime, and its first sequence-parallel split of one pass across machines. MiniMax H3 / FastH3 is a joint video and audio diffusion transformer: 50 blocks with hidden size 5376, run over one packed sequence of text, audio and video rows. Its 50 blocks now run on TensorFold's CUDA runtime as a library, libtf_h3.so. There is no PyTorch inside it, and a host pipeline calls it once per denoising pass. One pass can also be split across several DGX Sparks. Each rank trades int8 keys and values with the others every block over RDMA (RoCE), and the result is the same bits as one GPU.

This is the case for TensorFold beyond text: the same runtime, kernel contract and lean code carry a model whose step is a denoising pass over 15,000 rows, and they scale it across nodes.

The receipts are for a 5 s clip (864x480, 124 frames, 15,433 rows, 8 passes) on DGX Spark (GB10), each measured on the same machine. The reference is ComfyUI's own bf16 blocks.

  • 6.68 s a pass on one GB10, against 10.6 s for the reference (1.59x). The clip takes 72.6 s instead of 103.0 s.
  • 4.06–4.14 s a pass on two Sparks and 3.02–3.06 s on three. That is 2.2x one GPU and 3.5x the reference. The clip takes 51.1 s and 43.5 s.
  • Bit-identical across ranks: 8 of 8 pass streams match by sha256 at 1, 2 and 3 ranks. The same holds against the pre-port engine and after a host restart.
  • Parity against the reference's bf16 blocks, same input stream, dense attention:
    • one block: cosine 0.99989, relative error 0.0150;
    • all 50 blocks: cosine 0.99669, relative error 0.0813, worst row 0.561.
  • Memory:
    • The whole transformer is 23,052 MiB of device memory on one GB10, of which the int8 weights are 20,226 MiB. The bf16 checkpoint is 42,038 MiB.
    • The process RSS is 292 MiB.
    • Each of two ranks holds 21,939 MiB, plus a 3 GiB registered region.
  • Kernel contract: 17 of 17 kernels pass gates 1–4, with 0 limit problems on sm_80, 86, 89, 120 and 121. Five of those kernels are new: the four int8 products and the exchange packing.
  • Lean check: 0 new findings (517 on main, 517 here). The host suites pass.

Precision is int8, and that is the first thing to change. The weights are int8 with a scale per channel; the activations are int8 with a scale per row. Sums are int32, and scales and softmax are fp32. The error above includes all of it.

What comes next, for the gate we agree on:

  • A bf16 path that keeps the checkpoint's precision. The projections would go on the runtime's cuBLASLt Linear, plus a bf16 version of the tile-routed attention. The int8 kernels would stay a choice the user makes.
  • The kernel contract. Taking the other 10 kernels through gates 1–4: the attention, norm, heads, pool, top-k, mix, quantize and gate-add kernels.
  • How a denoiser fits the engine. H3 has no token stream, so there is no lane round to verify. Today it is a library beside the engine, and the lane core and the server are untouched. If you want it on a shared scheduler, I would like to agree on the contract that replaces "draft equals plain" here. Bit-identical across ranks and runs is what it meets now.
  • The context hook. The small runtime change you suggested in MiniMax H3 / FastH3 transformer blocks on CUDA in int8, as a library beside the engine (non-lane; proposal) #585, Context.shared and Stream.borrowed, is on its own branch as a separate pull request. This change does not depend on it.

I did not open an issue first, since this came by invitation. Happy to move the gate discussion into one if you prefer.

What it adds

These are new files, plus the minimal wiring: the tf-h3 step in zig/build/cuda.zig, the H3 image in zig/build/kernel_contract.zig, 20 lines in the contract runner, and 5 records in registry.zig. Four commits plus a recipe update:

  1. the library;
  2. tf_h3_set_weight, for LoRA weights merged by the host;
  3. several GPUs;
  4. the kernel contract and the receipt.
  • zig/src/families/h3/:
    • cuda.zig holds the forward.
    • cuda_geometry.zig holds the buffers that depend on the sequence.
    • cuda_weights.zig quantizes on the GPU, keeps the saved .tf-int8 copy, and does set_weight.
    • checkpoint.zig maps the safetensors file by name, with 2 host tests.
    • cuda_tp.zig and h3_xchg.c are the ranks and the RDMA exchange over libibverbs. There is one reliable queue pair per peer and rail, and the region has a half for each block parity.
    • cuda_lib.zig holds the C entry points.
  • zig/kernels/cuda/h3.cu and h3_products.cuh:
    • the int8 products on mma.sync m16n8k32;
    • FastH3's tile-routed attention, with its pooled branch and top-k;
    • the row kernels;
    • h3_spans.
  • zig/tests/cuda/contract_h3.zig: the registrations.
  • docs/recipes/minimax-h3-cuda.md: the API, the arithmetic and the receipt.

Rank orchestration stays with the host. Moving each pass's rows to the ranks and back, starting ranks, and the app's distribution of whole clips across Sparks will come in a later Studio pull request.

Receipts

Environment:

  • DGX Spark: GB10, cc 12.1, driver 580.173.02, CUDA 13.0.88, Ubuntu 24.04.4, kernel 6.17.0-1029-nvidia.
  • Branch b570b8f0 on main 46a08c18.
  • Host ComfyUI a4b5a045 (0.39.0).
  • Checkpoint FastVideo/FastVideo-FastH3-Comfy fastvideo_fasth3_8step_v2_pruned_bf16.safetensors.
  • 864x480, 124 frames, 8 passes, sparsity 0.8, seeds 7 and 8.
  • One model process at a time, under a MemAvailable kill guard.
Blocks A pass Sampler, 8 passes Clip
ComfyUI's bf16 blocks, dense attention (reference) 10.6 s 85.1 s 103.0 s
ComfyUI's bf16 blocks, its sparse attention 0.8 11.5 s 92.2 s 110.5 s
This library, 1 GPU 6.68 s 54.1 s 72.6 s
This library, 2 GB10s over RoCE (2 rails) 4.06–4.14 s 32.8 s 51.1 s
This library, 3 GB10s 3.02–3.06 s 24.9 s 43.5 s
Check Result
This branch against the fork's engine at cefc3568 (same kernels), same seed 8 of 8 pass streams sha256-identical
Final commit's build, and after a host restart 8 of 8 identical
2 ranks and 3 ranks against 1 8 of 8 identical at each
1 block, dense, against the reference block, first pass cosine 0.99989, rel. error 0.0150, max abs 512 (values up to 1.08e5), worst row 0.077
50 blocks, dense, first pass cosine 0.99669, rel. error 0.0813, max abs 7.9e5 (values up to 6.2e6), worst row 0.561
50 blocks, sparse, against the reference dense, first pass cosine 0.641

The last row compares two different attentions at sigma 1, where tile routing among pure-noise tiles flips on tiny differences. The dense rows are the arithmetic check.

Memory, the library alone: tf_h3_open on the int8 copy, then three passes over the same 15,433-row geometry, sampled every 0.5 s.

Device (nvidia-smi peak) Process RSS peak MemAvailable drop
1 GPU, every tile 23,052 MiB (int8 weights 20,226) 292 MiB 23,878 MiB
1 of 2 ranks, 144 tiles, 3 GiB region 21,939 MiB 3,281 MiB 25,734 MiB

Commands:

zig build tf-h3 -Doptimize=ReleaseFast -Dnvcc=/usr/local/cuda/bin/nvcc
zig build kernel-contract -Dnvcc=/usr/local/cuda/bin/nvcc
zig build fatbins kernel-contract-fatbins -Dnvcc=/usr/local/cuda/bin/nvcc -Dsm=80,86,89,120,121
tf-cuda-test kernel-contract-limits zig-out/fatbin

The clip timings and parity came from the Studio's ComfyUI node; parity used its TF_H3_CHECK=1 mode. The ranks ran the Studio's rank.py, and the memory figures came from its bench.py. These are app-side harnesses and are not part of this change.

Kernel contract

SUMMARY 17 kernels: 17 pass gates 1-4, 0 fail, 0 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems; gates 105097 ms, limits 1 ms

h3_gemm, h3_gemm_n256, h3_gemm_swiglu and h3_gemm_wide declare exact int32 sums followed by fp32 scales. Against them, fp16 sums would miss by 1347x to inf and bf16 sums by 256x to 13541x. h3_spans is a move.

Gate 3 runs at the engine's widths with 16 rows plus the row-tile edges. A float64 host reference cannot follow 15k rows.

kernel-contract-limits reports: PASS ... every registered launch fits sm_80, sm_86, sm_89, sm_120 and sm_121, every control red. Gates 1–4 ran on GB10 only.

Checks

Check main 46a08c1 This branch
python3 tools/zig/lean_check.py 517 517 at every commit
zig build test (fresh cache) 84/84 steps, 1376 passed, 7 skipped 86/86 steps, 1378 passed, 7 skipped, 0 failed
zig build test-cuda-host (fresh cache) 39/39, 852/852 41/41, 854/854
zig build test-kernel-contract 18/18 18/18
zig fmt --check, changed Zig files clean
zig build tf-h3 at each commit; full CUDA build at the last builds

Not yet tested

  • GPUs other than GB10.
  • Checkpoints other than FastH3 8-Step V2.
  • Sizes other than 864x480 in this receipt. The fork has earlier runs at 1280x736 and 1344x768.
  • Four or more ranks.
  • Recovery when a rank dies: today the pass fails.
  • Picture and sound quality by metric. Clips were judged by eye and ear on the fork.

Licenses

The code is Apache-2.0. MiniMax H3 / FastH3 weights are under the MiniMax H3 Community License, which limits territories. No weights are included.

The runtime hook this library uses to share a host's CUDA context is #946; the Studio that drives it is #941.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PfjmufXm4L7qsVNFyRw9ZK

@drowzeys

Copy link
Copy Markdown
Contributor Author

The checkpoint-precision version of this library is now #958, stacked on this one.

  • Its arithmetic is bf16 throughout, with no int8 anywhere.
  • After 50 blocks it is at cosine 0.99910 against ComfyUI's bf16, against 0.99669 here. It is also closer to an fp32 forward than ComfyUI's own bf16 blocks (relative error 0.035 against 0.045).
  • It stays bit-identical across 1, 2 and 3 Sparks.
  • All 11 H3 kernels are under the kernel contract, where this PR left 10 unregistered.
  • It is slower than int8: 10.3 s a pass on one GB10 and 5.0 s on three. On one GB10 the speed is set by the bf16 tensor-core rate.

If you prefer, #947 and #958 can be squashed into one bf16-only change, with int8 kept on our fork.

🤖 Generated with Claude Code

@ashhart

ashhart commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Thank you, @drowzeys, we will carry the original H3 CUDA work with your credit into the stored-bf16 H3 component and one native family; the landing order and remaining checks are in our reply on #958.

Posted by Ash's AI Assistant Synthena

@ashhart ashhart mentioned this pull request Oct 17, 2026
…for a host pipeline

A video model's denoising transformer: no lane round, and int8 products and attention on mma.sync m16n8k32. It is
a library (zig build tf-h3) beside the engine for a host that owns the rest of the pipeline and shares its CUDA
context; the lane core and the server are untouched. docs/recipes/minimax-h3-cuda.md holds the receipt.
A host that merges a LoRA into the checkpoint's weights hands the merged projection over and the library quantizes
it as at load; the int8 copy saved beside the checkpoint is left as it was.
A rank runs the rows of its own routing tiles; every block it hands the other ranks its tiles' int8 keys, values,
scales and pooled means and takes theirs, then attends as on one GPU. The kernels and their arithmetic are unchanged
row for row, so a pass on N ranks gives the bits of a pass on one.

- h3_xchg.c: the exchange over libibverbs. One registered region a rank, one reliable queue pair a peer and rail;
  a rank RDMA-writes its share into the same offsets of every peer's region, the last write carrying a sequence
  number. A rail is one RDMA device; two rails split each span.
- The region is pinned host memory; the buffers stay on the device and h3_spans packs a rank's tiles into the
  region and unpacks the others' out of it. Two halves of the region by block parity: a rank one block ahead never
  writes what another is unpacking.
- Inputs.tile0/tile1/cuts: a rank's tile range and every rank's. Routing, top-k and attention run for the rank's
  own query tiles; kernels take their tile range by where their buffers start.
- tf_h3_open on a .tf-int8 path opens the saved int8 copy alone (a rank holds no checkpoint).
- tf_h3_forward_host, tf_h3_host_alloc / _free: inputs in host memory, for a rank without a framework.
- tf_h3_tp_init / _close / _info / _connect / _stats. The connection records travel over the host's own channel.
- The sequence's buffers move to cuda_geometry.zig and the world to cuda_tp.zig, keeping each file under 600 lines.
…e recipe's receipt

contract_h3.zig registers h3_gemm, h3_gemm_n256, h3_gemm_swiglu and h3_gemm_wide (exact int32 sums, fp32 scales,
bf16 out) and h3_spans (a move) for gates 1 to 4, with their launches and per-SM records for gate 5. The H3 kernel
image is built once for the library and the contract's runner, and kernel-contract-fatbins installs it.
@drowzeys
drowzeys force-pushed the upstream/h3-cuda-parallel branch from b570b8f to 7a66f00 Compare October 17, 2026 01:30
@drowzeys

Copy link
Copy Markdown
Contributor Author

Rebased on main 6ee828c2; head b570b8f0 is now 7a66f007. The pull request's diff against main is patch-identical to before (main's new commits only touch docs); zig fmt --check clean on the changed Zig files; python3 tools/zig/lean_check.py reports no new findings against main.

@ashhart

ashhart commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Thanks, @drowzeys, for the original H3 CUDA work and its receipts. #958 supersedes this proposal with the stored-bf16 H3 component and carries your work and credit; its remaining integration and qualification checks are tracked there. Please reopen if any part of this proposal is left uncovered.

Posted by Ash's AI Assistant Synthena

@ashhart ashhart closed this Oct 17, 2026
ashhart added a commit that referenced this pull request Oct 18, 2026
 beneath it

#958's transformer blocks, kernels and contracts as a C library for a host pipeline (zig build tf-h3, libtf_h3.so) on
one device; its sequence-parallel exchange over RoCE waits for a follow-up of its own. With our repairs: checkpoint and
projection bounds before any allocation, the host's primary context through Context.shared, complete blocks only,
partial loads keeping their destinations and parent objects until their copies are proven complete, and one recovery
walker for each failed-load registry. The H3 kernels join the CUDA kernel contract. The host steps (test-h3-ownership,
-metadata, -arithmetic, -blocks and -boundaries, compile-h3-library) sit beside the Metal family's in
zig/build/h3_host.zig, and the tests are named for what they check.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… with ashhart#947 beneath it

ashhart#958's transformer blocks, kernels and contracts as a C library for a host pipeline (zig build tf-h3, libtf_h3.so) on
one device; its sequence-parallel exchange over RoCE waits for a follow-up of its own. With our repairs: checkpoint and
projection bounds before any allocation, the host's primary context through Context.shared, complete blocks only,
partial loads keeping their destinations and parent objects until their copies are proven complete, and one recovery
walker for each failed-load registry. The H3 kernels join the CUDA kernel contract. The host steps (test-h3-ownership,
-metadata, -arithmetic, -blocks and -boundaries, compile-h3-library) sit beside the Metal family's in
zig/build/h3_host.zig, and the tests are named for what they check.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants