Skip to content

perf(cuda): two-rank NVFP4 layers shard as they load; row-parallel sums over a peer mapping - #356

Closed
gilby wants to merge 3 commits into
ashhart:mainfrom
gilby:tp2-shard-load-p2p-sum
Closed

gilby wants to merge 3 commits into
ashhart:mainfrom
gilby:tp2-shard-load-p2p-sum

Conversation

@gilby

@gilby gilby commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Stacked on #355 and #354: the earlier commits here are #354's commit and a same-patch copy of #355's (d233196, the same patch as 3c3d9bb); this pull request is the last commit. Two things for NVFP4 at --tp 2:

  • Shard as they load. load_nvfp4 takes a per-layer hook, and two ranks split each layer to their shard as it
    loads, so a rank holds one full layer at most. Admission counts layer tensors halved and no startup copy; before, a
    rank needed twice the full checkpoint at startup and the 27B was refused on 48 GB cards.
  • Row-parallel sums over a peer mapping (tensorfold.cuda.p2p). When both ranks' GPUs can map each other, a
    row-parallel sum is one kernel: each rank stages its fp32 partial in a cudaIpc buffer the peer has mapped, blocks
    meet their peer blocks on flags that only grow, and both ranks add rank 0 + rank 1 in fp32 and round once to bf16 —
    the bits of the gathered rank-ordered sum (two fp32 values add the same in either order). Sums larger than the
    staging buffer (prompt chunks) keep NCCL; TF_TP_P2P=0 turns the kernel off. Two ranks on one machine reach it with
    --device 0 and --device 1.

On #219's interface: the 27B engine's two ranks still run on a torch.distributed process group, so these sums sit
beside tensorfold.cuda.comm rather than inside it. If you'd like them as comm transports, the smallest step we see
is an optional rank_sum on Comm (falling back to all_gather + rank-ordered add) and the 27B engine opening its
communicator with open_comm; we're happy to restructure this PR that way.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: this
    change on 0.6.4 (before its rebase onto v0.6.5, on feat(cuda): --device picks the GPU a rank runs on #354 and feat(qwen3.8 cuda): NVFP4 checkpoints on two ranks (--tp 2) #355). PyTorch 2.13.0a0 (NGC 26.07 container, CUDA 13.3), Triton 3.7.1; one DGX Spark (GB10, sm_121); real checkpoint RadixArk/Qwen3.8-27B-NVFP4-BF16-LMHead @ 009632f with z-lab/Qwen3.8-27B-DFlash2 @ 50307d4.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: the P2P sum gives
    the NCCL gather's bits by construction; the two-GPU test that checks it skips on one GPU (below). Resumed == fresh, on v0.6.5 (one DGX Spark): the same 2,682-token prompt three times on one server at temperature 0: fresh, resumed (2,677 tokens from the prompt cache) and resumed with "draft": false (one rank), all token_sha 03c531b17784.
  • Decode speed before and after, from tools/bench_openai.py: ours (two NVLinked NVIDIA A40s in a VM, 0.6.2 + the
    parent changes + this, RadixArk/Qwen3.8-27B-NVFP4-BF16-LMHead, own timing): a 27B NVFP4 forward went from 75 to
    52 ms (NCCL all_gather + add + cast cost about 250 µs of host time each, 128 a forward; the P2P launch about 10 µs);
    13.6 GiB resident a GPU at --tp 2. Not yet measured with upstream's tools.
  • Prompt speed before and after, from tools/prefill_cold.py: prompt-sized sums keep NCCL. One rank only, measured later on v0.6.5: 2,602 / 2,840 / 2,731 tok/s at 2k / 8k / 16k against v0.6.5's 2,610 / 2,848 / 2,713. --tp 2: not run.
  • On v0.6.5: this branch rebased onto v0.6.5 (609ca41) as 762f141. Mac host suite (M5 Ultra): 5003 passed, 1 failed, 596 skipped; the one failure, test_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, fails on v0.6.5 too. CUDA on v0.6.5 (one DGX Spark, GB10): pytest tests/cuda tests/test_cuda_build.py -k "qwen27 or qwen36 or nvfp4 or b16 or qmm or moe or build or distributed or dflash or dspark or exl3 or graph": 692 passed, 0 failed, 82 skipped.
  • Tests run, with pass, fail and skip counts: Mac host suite on this branch (M5 Ultra): 4695 passed, 1 failed, 595 skipped (the one failure, test_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, fails on 0.6.4 too). On the DGX Spark, pytest tests/cuda tests/test_cuda_build.py -k "qwen27 or nvfp4 or b16 or qmm or build or distributed" plus tests/test_cuda_device.py:
    472 passed, 0 failed, 12 skipped. The new two-process P2P test needs two GPUs in one machine and skips here.
  • Checked on v0.6.5 (2026-10-04): one DGX Spark (GB10), tools/prefill_cold.py (2k / 8k / 16k, 3 reps, median tok/s; runs v0.6.5, this, this, v0.6.5, each side the mean of its two) on RadixArk/Qwen3.8-27B-NVFP4-BF16-LMHead + z-lab/Qwen3.8-27B-DFlash2 at --context 131072 --parallel 1, a checkpoint v0.6.5 serves: v0.6.5 → this 2,604 → 2,595 / 2,852 → 2,846 / 2,738 → 2,733. Two NVLinked NVIDIA A40s, this branch plus feat(cuda): Ampere (sm_80/86) runs the bf16 kernels; FP8 prompts stay at 8.9 and NVFP4 runs W4A16 below it #363's Ampere commit, --tp 2 --parallel 4 --context 65536 with --device 0 / --device 1, rank sums over P2P: fresh == resumed (2,296 of 2,301 tokens from the prompt cache) == "draft": false, token_sha db7ba9d1c2bf; 19 × 23 drafted == "draft": false 7773d573571d; tools/bench_concurrent.py --levels 2,4 --alone --serial: 0 failed, 0 unequal in all 8 cells; code aggregate N=2 / N=4 80.6 / 161.1 tok/s (T=1). tests/cuda/test_qwen27_distributed.py on the two A40s: copied into the parent's tree 1 failed, 8 passed, 1 skipped; here 9 passed, 1 skipped.
  • Not run, and why: a DGX Spark has one GPU, and two Sparks cannot map each other (they keep NCCL by design); the
    two-rank receipt is on two A40s (the v0.6.5 check).

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Two ranks on two A40s, with feat(cuda): Ampere (sm_80/86) runs the bf16 kernels; FP8 prompts stay at 8.9 and NVFP4 runs W4A16 below it #363's Ampere commit: see the v0.6.5 check.)
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Prompt processing is no slower than the last release. (One rank: 2,602 / 2,840 / 2,731 tok/s at 2k / 8k / 16k, against v0.6.5's 2,610 / 2,848 / 2,713.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (tests/cuda/test_qwen27_distributed.py on two A40s: 1 failed on the parent's tree, 9 passed and 1 skipped here; the two-GPU cases skip on one GPU.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

gilby added 3 commits October 3, 2026 22:03
--device N (TF_CUDA_DEVICE) makes device N the current CUDA device before an engine loads, in every CUDA engine,
and the unified-memory check reads the current device. Two ranks on one machine can then both see both GPUs and
each run on its own, so their transfers can map the peer GPU. Without the flag nothing changes: device 0.
An NVFP4 checkpoint (NVFP4 MLP, FP8 attention and DeltaNet, bf16 gates and head) now shards as the MLX checkpoint
does, every stored byte unchanged. Fp4Linear and Fp8Linear gain outputs() (whole 64-output tiles of the loaded
tiling, a zero tile padding an odd count) and inputs() (whole 64-input groups, four e4m3-scaled 16-input blocks
each, the per-tensor scale kept), and partial(), the lane matmul with an unrounded fp32 epilogue for the
rank-ordered sum. Plain and Plain8 split their rows or columns as stored. split_weights keeps the checkpoint's
precision settings, and the two-rank DFlash2 drafter takes its half of the draft vocabulary from the bf16 head.
The engine no longer refuses --tp 2 for NVFP4; EXL3 packs and checkpoint math still run on one GPU.
…ms over a peer mapping

load_nvfp4 takes a per-layer hook, and two ranks split each layer to their shard as it loads, so a rank holds one
full layer at most; admission counts layer tensors halved and no startup copy, where before a rank needed twice
the full checkpoint at startup.

tensorfold.cuda.p2p: when both ranks' GPUs can map each other, a row-parallel sum is one kernel. Each rank stages
its fp32 partial in a cudaIpc buffer the peer has mapped, blocks meet their peer blocks on flags that only grow,
and both ranks add rank 0 + rank 1 in fp32 and round once to bf16: the bits of the gathered rank-ordered sum.
Sums larger than the staging buffer (prompt chunks) keep NCCL; TF_TP_P2P=0 turns the kernel off. Two ranks on
one machine reach it with --device 0 and --device 1.
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby. Sharding NVFP4 layers as they load, so a rank never holds more than one full layer, and summing row-parallel partials through a peer mapping, rank 0 plus rank 1 in fp32 and rounded once, keeps NCCL's bits at a fraction of the host cost.

The Python engine is frozen (#286), so this won't merge. Zig CUDA runs one GPU so far. The Zig engine's Mac cluster already sums ranks exactly the same way, in rank order over one-sided writes and flag words. When Zig CUDA gets two ranks, your peer-mapped sum is the first transport to try, ahead of #357's host-memory sum and NCCL, and it carries your credit.

@ashhart ashhart closed this Oct 6, 2026
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