Skip to content

perf(cuda): two-rank sums through shared page-locked host memory where the GPUs cannot map each other - #357

Closed
gilby wants to merge 4 commits into
ashhart:mainfrom
gilby:tp2-host-sum
Closed

gilby wants to merge 4 commits into
ashhart:mainfrom
gilby:tp2-host-sum

Conversation

@gilby

@gilby gilby commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Stacked on #356: the earlier commits here are that stack's (including its same-patch copy of #355's commit, d233196 = 3c3d9bb); this pull request is the last commit. GPUs in one machine without a peer mapping (PCIe cards in a VM, where cudaIpcOpenMemHandle fails
with cudaErrorPeerAccessUnsupported) never installed #356's P2P sum, so every row-parallel sum went through NCCL.
HostSum puts both ranks' staging and flags in one /dev/shm buffer, page-locked and mapped by each GPU
(cudaHostRegister + cudaHostGetDevicePointer); the same sum kernel writes and reads the partials over PCIe, rank 0

  • rank 1 in fp32, rounded once, so the bits equal the NCCL gather. Waiting blocks poll the host flags with a short
    __nanosleep (TF_TP_HOST_SPIN_NS). install() picks P2P, else host memory when both ranks run on one machine
    (hostname and boot id match), else NCCL; TF_TP_HOST_SUM=0 keeps NCCL. Startup names the transport. Like perf(cuda): two-rank NVFP4 layers shard as they load; row-parallel sums over a peer mapping #356's sum it sits in the 27B engine's
    torch.distributed path, beside 0.6.4's tensorfold.cuda.comm; it maps onto a comm transport the same 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 perf(cuda): two-rank NVFP4 layers shard as they load; row-parallel sums over a peer mapping #356). 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: same bits as NCCL
    by construction; the two-process test that checks it skips on one GPU. 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 Tesla V100-PCIE-32GB in a VM, no P2P, own
    timing): an NCCL sum cost about 95 µs of host time, 128 a forward; the host-memory sum about 13 µs a launch. 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,595 / 2,848 / 2,738 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 6c7d083. 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, 83 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, the same selection as perf(cuda): two-rank NVFP4 layers shard as they load; row-parallel sums over a peer mapping #356:
    472 passed, 0 failed, 13 skipped (the new two-process host-sum test skips).
  • 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,596 → 2,589 / 2,845 → 2,819 / 2,736 → 2,701. (two pairs of runs disagree: −0.5 / −0.9% and −1.4 / −1.6% at 8k / 16k). 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; tools/bench_openai.py fibonacci-raw / gpu-chat 71.5 / 31.8 tok/s (T=1), 57.8 / 35.1 (T=0). These A40s map each other, so they take perf(cuda): two-rank NVFP4 layers shard as they load; row-parallel sums over a peer mapping #356's P2P sum, and the new host-sum test skips there. The host-memory sum itself: on two Tesla V100s (no P2P) on feat(volta): Qwen3.8-27B NVFP4 checkpoints on two ranks (--tp 2) on sm_70 #376's tree, the test passes (9 passed, 2 skipped), and the --tp 2 serve there is exact (see feat(volta): Qwen3.8-27B NVFP4 checkpoints on two ranks (--tp 2) on sm_70 #376).
  • Not run, and why: the host-memory sum served at this commit: on sm_70 this commit's P2P build gate is still 8.9, so
    two V100s serve it only from feat(volta): Qwen3.8-27B NVFP4 checkpoints on two ranks (--tp 2) on sm_70 #376's tree on (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.
    (On two NVLinked A40s this branch served exactly, but there the GPUs map each other, so the sums went over P2P,
    not this change's host-memory path. The host-memory sum was served exactly only on two Tesla V100s, as part of feat(volta): Qwen3.8-27B NVFP4 checkpoints on two ranks (--tp 2) on sm_70 #376.)
  • 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 on one DGX Spark against v0.6.5: within run-to-run noise (two pairs of runs gave −0.5 / −0.9% and −1.4 / −1.6% at 8k / 16k); see the v0.6.5 check.)
  • 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. (The new host-sum test skips where the GPUs map each other (A40s) and cannot run at this commit on sm_70; it passes on feat(volta): Qwen3.8-27B NVFP4 checkpoints on two ranks (--tp 2) on sm_70 #376's tree on two V100s, not shown failing before.)
  • 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 4 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.
…e the GPUs cannot map each other

GPUs without a peer mapping (PCIe cards in a VM, where cudaIpcOpenMemHandle fails with
cudaErrorPeerAccessUnsupported) never installed the P2P sum, so every row-parallel sum went through NCCL.
HostSum puts both ranks' staging and flags in one /dev/shm buffer, page-locked and mapped by each GPU
(cudaHostRegister + cudaHostGetDevicePointer); the same kernel writes and reads the partials over PCIe, rank 0 +
rank 1 in fp32, rounded once, so the bits equal the NCCL gather. Waiting blocks poll the host flags with a short
__nanosleep (TF_TP_HOST_SPIN_NS).

install() picks P2P, else host memory when both ranks run on one machine (hostname and boot id), else NCCL;
TF_TP_HOST_SUM=0 keeps NCCL. Startup names the transport.
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby. Putting both ranks' staging and flags in one page-locked shared-memory buffer that each GPU maps lets cards without peer access sum without NCCL, with the same bits. That's a practical fallback for PCIe cards in VMs.

The Python engine is frozen (#286), so this won't merge. Zig CUDA runs one GPU so far, and the Zig engine's Mac cluster sums ranks exactly over its own exchange. When Zig CUDA gets two ranks, this host-memory sum comes across as the transport after #356's peer mapping, with 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