Skip to content

Opt-in prompt-processing levers for the tensor split: prompt-sized KV padding for graph reuse, BF16 all-reduce wire - #12

Merged
lukekim merged 8 commits into
spiceai-b11803from
lukim/b11803-nccl-wire-bf16
Oct 27, 2026
Merged

lukekim merged 8 commits into
spiceai-b11803from
lukim/b11803-nccl-wire-bf16

Conversation

@lukekim

@lukekim lukekim commented Oct 26, 2026 •

Copy link
Copy Markdown

What this changes

Two opt-in switches for prompt processing on the tensor split, plus a diagnostic, each off unless set. Stacked on #9 (its two commits are the base of this branch; retarget to spiceai-b11803 once #9 merges).

switch what it does
LLAMA_KV_PROMPT_PAD=<power of two ≥ 256> While a ubatch reads a prompt (more than one token), the visible KV range (llama_kv_cache::get_n_kv) and the QSA indexer's key-pool list (llama_memory_hybrid_idx_context::get_n_kpool) are padded to that bucket instead of their 256-cell and 64-pool steps, so the chunks of one prompt share a graph shape and the split graph is reused across them instead of being rebuilt and re-sent per chunk. Decode steps keep the tight padding.
GGML_NCCL_WIRE_BF16=1 The RPC path's NCCL all-reduce (ggml_backend_cuda_nccl_allreduce) converts an F32 tensor of 32768 elements or more to BF16 on the device, reduces in BF16, and converts back — the rule the direct multi-GPU communicator already applies.
LLAMA_DEBUG_REUSE=1 (existing) Now also prints DBGREUSE kpool … naming the key-pool comparison that refused reuse.

Why, with the measurements (two DGX Spark ranks over NCCL, Qwen3.8-Flash-Next UD-Q3_K_XL, -ub 512, cold 3,754-token prompt, median of 3, 2026-10-26; evidence in the lab's goal80/prefill/README.md)

  • One Spark prefills this model at 1,013 tok/s; the two-rank split at ≈800 and the production three-rank mesh at ≈590. A rank-0 nsys trace of one cold prompt on the two-rank split splits a 512-token chunk into ≈213 ms of expert mat-mul (mul_mat_q, the divided work), ≈134 ms of ncclDevKernel_AllReduce_f32_RING (26 % of kernel time), ≈124 ms of per-token work every rank repeats, and ≈100 ms of GPU idle.
  • LLAMA_DEBUG_REUSE=1 showed the head rebuilding and re-sending the graph on every chunk (0 of 69 reused): llm_graph_input_mem_hybrid refused because n_kv moved past a 256-cell boundary each chunk, and llm_graph_input_kpool because its pool count grew (192 → 320 → 448 … per chunk at kpool_pad 64). With both padded to the 4096 bucket the chunks reuse the graph (DT reused=1 on 30 of 36 chunk calls; the 6 are each prompt's first chunk), and the head's per-chunk time drops from 75 to 19 ms (build 8 → 0, submit 64 → 16).
  • That reuse does not move the wall clock on the two-rank path (4,686 vs 4,680 ms): the ranks' own time bounds the chunk. It is kept because it is cheap, correct (padded cells and pools point at the sentinel rows, as the single pool kpool_pad always adds), and removes the head's work where the head is the bottleneck (not measured here: the three-rank mesh path, where the head's submit is heavier).
  • GGML_NCCL_WIRE_BF16=1 is the measured gain: 4,836 → 4,393 ms in its own session (+10 %) and 4,680 → 4,267 ms with the pad (+10 %); decode 47.0–51.3 → 50.7–52.1 tok/s (session noise).
arm (same build, three sessions) 966 tok 3754 tok tok/s decode (8 prompts)
off 1,445 ms 4,680 802 51.29
LLAMA_KV_PROMPT_PAD=4096 1,562 4,686 801 50.26
pad + GGML_NCCL_WIRE_BF16=1 1,395 4,267 880 52.06

Review fixes

  • The full-context key-pool list is sized for every pool the cache can hold (graph reallocs 0 / 0 on the fixed build, 6–59 before).
  • A prompt is told from a batched decode step by the longest sequence's tokens, so -np N -kvu decoding keeps the 256-cell padding.
  • The two ranks of a pair agree the BF16 wire in their NCCL handshake byte; one rank set alone warns and keeps F32 (the pre-fix pair hung).

What is not covered

  • Texts. The 8-prompt decode arm's greedy texts differ from the FP32 arm's on most prompts for both switches (the padded attention tiling and the BF16 reduction both move Qwen's near ties). The near-tie walk against two plain single-Spark oracles (upstream 9febe63 and fork main 3185bd4; each oracle's own texts walk 8/8 with 0 departures) shows each switch on its own inside the floor the plain TP2 split already sets (8/8 texts, every departure within 0.63 nats; the levers-off arm: 8/8, within 0.50), while the two together fail one text on both oracles at the same place (340 / 85 where the oracles write 340 ÷ 85); that step's margin cannot be read through the server, which coalesces the multi-byte ÷ into one entry carrying only its last byte's probabilities, so the pair is "not shown to be a near tie" there rather than measured as a wide departure. So both switches stay opt-in, and the pair together is not a candidate default. Details: Opt-in prompt-processing levers for the tensor split: prompt-sized KV padding for graph reuse, BF16 all-reduce wire #12 (comment)
  • Not measured: the three-rank mesh path (tp4i), GLM and DeepSeek, Metal (the pad is backend-independent; the BF16 wire is CUDA/NCCL only), and the prompt-pad's cost at contexts far below the bucket (attention over masked padding).
  • The DBGREUSE kpool line is a diagnostic like the existing DBGREUSE output; it prints only under LLAMA_DEBUG_REUSE=1.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 14:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Pool reservations are undersized, NCCL ranks can select incompatible wire types, and batched decode receives prompt padding.

3 open issues
What changed in this PR

Adds opt-in prompt-processing optimizations for tensor-split inference, building on #9's graph-cache fixes. Both switches remain disabled by default.

Changes:

  • Adds configurable KV and key-pool padding to improve graph reuse across prompt chunks.
  • Adds BF16 transport for large F32 reductions on the CUDA/NCCL RPC path.
  • Extends reuse diagnostics to report key-pool shape mismatches.
File Description
src/​models/​qwen4exp.cpp Logs key-pool comparisons that prevent graph reuse.
src/​llama-memory-hybrid-idx.cpp Tracks and applies prompt-sized key-pool padding.
src/​llama-kv-cache.h Declares the shared prompt-padding setting accessor.
src/​llama-kv-cache.cpp Parses the setting and adjusts visible KV padding.
ggml/​src/​ggml-cuda/​ggml-cuda.cu Adds optional BF16 conversion around large NCCL reductions.

Review effort: Balanced

Comment thread ggml/src/ggml-cuda/ggml-cuda.cu Resolved
Comment thread src/llama-kv-cache.cpp Resolved
Comment thread src/llama-memory-hybrid-idx.cpp Resolved
Copilot AI balanced review requested due to automatic review settings October 26, 2026 15:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

RPC initialization can hang with older peers or crash when a loaded CUDA backend lacks the optional setter.

3 open issues
2 resolved since last review

Review effort: Balanced

Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Resolved
Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Resolved
@lukekim
lukekim added this pull request to stack #13 October 26, 2026 15:34
Copilot AI balanced review requested due to automatic review settings October 26, 2026 15:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Both options change generated text, and output-quality checks and broader configuration validation remain incomplete.

2 open issues
1 resolved since last review

Review effort: Balanced

@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

Lab results on the fixed build (64cc0aa; the kv-cache and key-pool code is unchanged at ce1d4be), TP2 NCCL over the RoCE cables, Qwen3.8 UD-Q3_K_XL, -ub 512, MTP draft n_max 3 — every arm reports graph reallocs = 0 / 0:

arm graph reuse (512-token chunk calls) cold 966 tok cold 3754 tok tok/s decode
off 0 / 36 1503 ms 4662 ms 805 50.10
LLAMA_KV_PROMPT_PAD=4096 30 / 36 1462 4628 811 50.71
GGML_NCCL_WIRE_BF16=1 (both ranks) 0 / 36 1370 4358 861 (+7 %) 50.46
both 30 / 36 1442 4376 858 49.79

(medians of 3 cold single-token requests per length; decode is the pooled rate of 8 prompts × 200 tokens through /completion.)

Texts. Each arm's 8 raw texts were walked token by token against two plain single-Spark oracles (upstream 9febe63 and fork main 3185bd4), each oracle first walking its own texts as the control (8/8, 0 departures on both). The walk follows the arm's bytes through the oracle's greedy decoding; every departure must be a near tie (margin ≤ 1 nat, token within the oracle's top-5):

label upstream oracle fork oracle
off (the TP2 split alone) 8/8, 12 departures, widest 0.40 nats 8/8, 14, widest 0.50
pad4096 8/8, 16, widest 0.63 8/8, 14, widest 0.37
bf16 8/8, 14, widest 0.36 8/8, 15, widest 0.43
pad4096 + bf16 7/8 7/8

Each switch on its own stays inside the floor the plain TP2 split already sets. The two together fail one text on both oracles at the same place (the train-arithmetic prompt: 340 / 85 where the oracles and the other arms write 340 ÷ 85, a choice outside the near-tie bound). So both switches stay opt-in as proposed, and the PR description notes that the pair together moved one choice beyond a near tie on this corpus.

Artifacts: goal80/prefill/chain74/ (head logs per arm, c74u-report.json, c74f-report.json, tp3raw.jsonl), chain-74.sh, bench_raw.py.

@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

Follow-up on the one walk failure (prompt 5, pad4096 + bf16, 340 / 85 vs the oracles' 340 ÷ 85): asked directly, both oracles continue …Time = Distance with ÷, but llama-server reports the multi-byte ÷ as one completion_probabilities entry carrying only its second byte's top-N (p = 1), and a response ending in the lone first byte carries no probabilities at all — so the margin of / against ÷ at the deciding step cannot be read through the server. The verdict for that text is therefore "not shown to be a near tie" rather than a measured wide departure; the conclusion is unchanged (both switches opt-in, the pair not a candidate default). Artifacts: goal80/prefill/chain76d/chain78.log, chain-78.sh, chain-79.sh.

@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

Addressed #12 (review).

The two issues this review listed are both resolved on this HEAD (ce1d4be), with lab evidence in their threads:

The headline — both switches change generated text — is answered by the near-tie walk in #12 (comment) and the follow-up #12 (comment): each switch on its own keeps every departure inside the levers-off floor against two independent single-Spark oracles (8/8 texts), the pair together moves one choice beyond a near tie on one prompt, and both stay opt-in by default, so default output is byte-identical. No code change in this round.

Base automatically changed from lukim/b11803-review-followups to spiceai-b11803 October 27, 2026 17:03
…d bucket while a ubatch reads a prompt

A tensor-parallel head rebuilds and re-sends the split graph for every 512-token prompt chunk, because each chunk moves
n_kv past a 256-cell boundary and the attention mask's shape changes (llm_graph_input_mem_hybrid and the QSA key-pool input
refuse reuse on every chunk; chain63 on the lab, 0 of 69 chunk calls reused, ~124 ms of build and submit per chunk). With
LLAMA_KV_PROMPT_PAD=<power of two> the visible range is padded to that bucket while the ubatch has more than one token,
so the chunks of one prompt share a shape; decode steps keep the 256 padding. Off unless set; a measurement lever first.
…e wire as BF16

The RPC path's all-reduce (ggml_backend_cuda_nccl_allreduce) sums every tensor in FP32. A prompt chunk's partial sums are
512 x n_embd floats, bandwidth-bound on the fabric, and a rank-0 trace of one 3,786-token prompt on two Sparks spent 26 %
of its kernel time in ncclDevKernel_AllReduce_f32_RING (chain65 on the lab). The direct multi-GPU communicator already
reduces tensors of 32768 elements and more as BF16 and converts back; with GGML_NCCL_WIRE_BF16=1 the RPC path does the
same. Off unless set, so the texts and the time can be compared first.
…for the bucket while a prompt is read

With the visible range padded, the attention input reuses the graph across a prompt's chunks, but the QSA key-pool input
still refused on every chunk: its pool count follows the real position (chain71 on the lab: n_pool 192 -> 320 -> 448 ...
per 512-token chunk, kpool_pad at 64). The pool list is now sized for the whole bucket while the ubatch has more than one
token, the padded pools pointing at the sentinel row as the one pool kpool_pad always adds; decode steps keep the tight
padding. The env parsing moves to llama_kv_prompt_pad() so both caches read one setting.
…e can hold

The full-cache context used for graph reservation built its key-pool state through kpool_build_sizes() and the
reserve path, which left n_pool_pad at its 64-pool default while n_pool_real was raised to the cache capacity, so
get_n_kpool() reserved a 64-pool list and ggml-alloc regrew the buffers during inference, with the switch unset too
(the head log of a TP2 run reported 'graph reallocs = 6 / 0' and up to 59, against 0 / 0 before the padded list).
Both constructors now set the padding from the pool count, and set_input_kpool asserts the list is never sized below
what the cache holds.
…uence, not the ubatch size

LLAMA_KV_PROMPT_PAD applied the prompt bucket whenever the first stream of the slot held more than one token, which
under a unified KV cache is every batched decode step (one token for each of several sequences), and the key-pool
list used the ubatch's token count the same way. The slot now records how many tokens the ubatch's longest sequence
contributes (llama_kv_prompt_tokens, shared with the key-pool list), so a decode step keeps the 256 padding however
many sequences it serves.
GGML_NCCL_WIRE_BF16 was read by each RPC daemon on its own, so a pair with the switch on one rank only would sum one
side in BF16 and the other in F32, which NCCL does not allow. The CUDA communicator handle now carries the wire its
ranks agreed on: comm_init exchanges each rank's setting after the ranks agree the communicator exists, the wire is
BF16 only when both asked for it, a mismatch is logged with the fix, and the all-reduce reads the handle rather than
the environment.
The wish for the BF16 wire was exchanged as a byte of its own after the ranks agreed their communicators exist, so a
pair with a server that predates it would wait for a byte the peer never sends. The agreement byte now carries the
wish in its second bit: such a server sends 0 or 1, so the pair keeps the F32 wire with no further exchange, and a
backend without the setter (an older CUDA library loaded by a newer server) is left on F32 instead of called through a
null pointer.
@lukekim
lukekim force-pushed the lukim/b11803-nccl-wire-bf16 branch from ce1d4be to e398643 Compare October 27, 2026 17:03
Copilot AI balanced review requested due to automatic review settings October 27, 2026 17:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cross-rank precision changes and shared cache-padding behavior need human sign-off beyond the documented two-rank validation.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@lukekim
lukekim merged commit 1e45d28 into spiceai-b11803 Oct 27, 2026
20 of 30 checks passed
lukekim added a commit that referenced this pull request Oct 27, 2026
…) into the tensor-parallel mesh branch

#9 decides backend sampling again after the sampler chain is built; without it a response_format request
aborted llama-server under LLAMA_ARG_BACKEND_SAMPLING=1, which failed this branch's "Tests (Backend sampling)"
job. #12 conflicted in rpc_server::comm_state: the merge keeps its nccl_wire_bf16 field beside the mesh fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants