Skip to content

ggml-cuda : P2P direct-write path for the internal AllReduce - #516

Open
danielhanchen wants to merge 3 commits into
base/upstream-9a811bab3from
pin-p2p-allreduce
Open

danielhanchen wants to merge 3 commits into
base/upstream-9a811bab3from
pin-p2p-allreduce

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Summary

The unslothai/llama.cpp prebuilt is an aged upstream b#### tag plus the pins listed in scripts/unsloth/pr-set.json, merged in order. This pin is one commit on top of upstream b12966 (9a811bab3c4) and needs no other pin. Once upstream has an equivalent change, the pin is dropped.

The problem

With -sm tensor on two NVIDIA GPUs, the prebuilt (built without NCCL) runs the internal AllReduce in allreduce.cu. It always stages through pinned host memory: each GPU writes its contribution to host memory, the peer reads it back, and both spin on arrival tokens in host memory. On a pair with a direct peer link this crosses the link twice per element and polls host memory.

The fix

  • allreduce.cu: a P2P variant of the chunked AllReduce kernel writes the data and the arrival token straight into the peer's device memory and spins on a local flag; the large-tensor path copies peer to peer with one cudaMemcpyPeerAsync each way.
  • The path is enabled only when both directions report peer access, peer access can be enabled, and an integrity probe passes (the production kernel on exactly representable values plus one peer copy each way, every element checked).
  • Default (auto): P2P is kept only when a decode-sized AllReduce (32 KiB) is at least 3% faster than host staging on this machine (median of 3 trials each). GGML_CUDA_AR_P2P=0 disables the path, =1 uses it whenever the probe passes.
  • Peer access is refcounted per device pair, so freeing one pipeline does not revoke access under another live one on the same GPUs; access enabled elsewhere is never disabled here.
  • The wire type follows the same GGML_CUDA_AR_BF16_THRESHOLD default as host staging, so default output is unchanged.
  • Peer checks, peer access and peer copies use the physical CUDA device ids, since the pipeline's device ids can be GGML_CUDA_DEVICES virtual ids. Two virtual devices on one physical GPU keep host staging.
  • HIP and GGML_CUDA_NO_PEER_COPY builds keep host staging. The chosen transport is logged once at init (AllReduce transport: ...).

Evidence

Check Result
Background, measured on our fork with the same code (plans: 2x T4 Kaggle, PCIe host bridge, no NVLink) P2P bit-exact vs host staging (equal md5 in every cell; identical KLD on a B200 NVLink pair); 20.1 vs 25.9 us per 32 KiB AllReduce, but decode only -5..+3% vs host staging on T4, which is why auto mode times both transports
CUDA build sm_100a (ggml-cuda, test-backend-ops, llama-completion, llama-bench) clean
B200 NVLink pair (2 of 8 cards), -sm tensor -fa on, GGML_CUDA_ALLREDUCE=internal, greedy 128 tokens, each arm twice: auto vs GGML_CUDA_AR_P2P=1 vs =0 identical md5 within each model across all three arms: Qwen3-0.6B BF16 b9ec376840ca, Qwen3-0.6B Q4_K_M 814061ab2564, Llama-3.2-1B F16 faf5d87eb24e. Engagement (-lv 4): auto logs AllReduce transport: P2P direct write (probe passed (4 kernel rounds + 32 MiB peer copy each way), 32 KiB AllReduce 11.7 us P2P vs 18.5 us host-staged), wire BF16 for F32; =1 logs P2P forced; =0 logs host-staged. The =0 arm runs the unchanged host-staged code, so it stands in for the base here
Pin set sim (llama_pinset.py sim, private copy, b13023, 12-06): master pr-set + open candidates #485-#503 (without #495) + #505 + #508-#513 + #516 at their current heads, 74 pins (this pin at 750e4cddcbb; the current head b8b7cd99110 merges clean on that tree too) every pin merges (git or additive); merge_checks clean; pin_contract: all pins intact once #491 is listed at its current head e6e34ffd3c2 (the copy listed it at 131d93897cd, whose comment-trimmed line arrives through #505 / #512, a listing artifact). CUDA mmvq.cu / mmvf.cu / mmid-gemm.cu / fusion.cu compile on the merged tree (sm_75 + sm_100a)
2x RTX A6000 (sm_86, nvidia-smi topo -p2p r OK both ways), b13023 + master pr-set (A) vs A + this pin (B), plus GGML_CUDA_AR_P2P=1 (B1) and =0 (B0); GGML_CUDA_ALLREDUCE=internal -sm tensor -ngl 999 -fa on; greedy 128 tokens md5 identical across A / B / B1 / B0 on Qwen3.8-27B, Qwen3-0.6B, Llama-3.2-1B, Qwen4-35B-A3B and Qwen3.8-Flash-Next. Inkling-2-Mini UD-IQ1_S does not load with -sm tensor on any arm; with -sm layer it is identical and the path is not reached. B logs AllReduce transport: P2P direct write (probe passed ..., 32 KiB AllReduce ~14-15 us P2P vs 22-24 us host-staged)
Kaggle 2x T4 (sm_75, nvidia-smi topo -p2p r OK both ways), static build of 750e4cddcbb (A) vs A + the physical-id fix c29cfdfe6 (B, code-identical to b8b7cd99110); Qwen3-0.6B Q4_K_M, GGML_CUDA_ALLREDUCE=internal -sm tensor -ngl 999 -fa on -lv 4, greedy 96 tokens md5 3050476a9976 in all 10 cells. CUDA0,CUDA1 auto / =1 / =0: same transport on A and B (auto: P2P, 18.6 / 17.5 us P2P vs 23.8 / 22.6 us host-staged). GGML_CUDA_DEVICES=3, CUDA1,CUDA2 (physical 1, 0), =1: A logs host-staged (cudaDeviceCanAccessPeer(1 -> 2) = 0) (virtual id passed as physical), B logs P2P direct write (probe passed ...). CUDA0,CUDA2 (both physical 0), =1: A host-staged (cudaDeviceCanAccessPeer(0 -> 2) = 0), B host-staged (devices 0 and 2 share physical GPU 0)
Speed on the same box, Qwen3.8-27B, -sm tensor, median of 3 rotated rounds MTP 0: long-prompt pp 1397 -> 1623 tok/s (+16.1%; B1 +15.6%, B0 +0.1%, A/A spread 1.1%); decode -1.3% (long) / -1.4% (chat), both inside the A/A spread of 2.6% (B0 +0.1%). MTP 2: long-prompt pp +13.5% (B0 +0.3%, A/A 1.2%); decode +0.8% long (A/A 0.3%), +1.3% chat (A/A 1.0%). One B round of MTP 0 pp read 544 tok/s (first-run warm-up), hence medians

Not proven

  • Decode gain: none measurable on the A6000 pair (inside A/A at MTP 0, +0.8..1.3% at MTP 2); the win is prompt processing (+13..16%).
  • Speed on NVLink pairs and on GeForce cards was not measured (GeForce pairs usually have peer access blocked and keep host staging). On 2x T4 behind a host bridge the fork saw no decode gain.
  • HIP is unchanged (compiled out). Windows was not run.

On two GPUs with peer access, the chunked AllReduce kernel writes its data
and arrival token straight into the peer's device memory instead of staging
through pinned host memory, and the large-tensor path copies peer to peer.

The path is used only after an integrity probe (production kernel on known
values plus a peer copy each way, every element checked). In the default
auto mode it is also kept only when a decode-sized AllReduce is measurably
faster than host staging. GGML_CUDA_AR_P2P=0 disables it, =1 uses it
whenever the probe passes. Builds with GGML_CUDA_NO_PEER_COPY and HIP keep
host staging. The wire type follows the same GGML_CUDA_AR_BF16_THRESHOLD
default as host staging.
@danielhanchen
danielhanchen marked this pull request as ready for review December 6, 2026 09:04
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T09:13:04.413903Z b8b7cd9 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: b8b7cd9911

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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.

1 participant