Repository navigation
Opt-in prompt-processing levers for the tensor split: prompt-sized KV padding for graph reuse, BF16 all-reduce wire - #12
Conversation
There was a problem hiding this comment.
🟡 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
|
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
(medians of 3 cold single-token requests per length; decode is the pooled rate of 8 prompts × 200 tokens through 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):
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: Artifacts: |
|
Follow-up on the one walk failure (prompt 5, pad4096 + bf16, |
|
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. |
…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.
… LLAMA_DEBUG_REUSE (diagnostic)
…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.
ce1d4be to
e398643
Compare
…) 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.


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-b11803once #9 merges).LLAMA_KV_PROMPT_PAD=<power of two ≥ 256>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=1ggml_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)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'sgoal80/prefill/README.md)nsystrace 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 ofncclDevKernel_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=1showed the head rebuilding and re-sending the graph on every chunk (0 of 69 reused):llm_graph_input_mem_hybridrefused becausen_kvmoved past a 256-cell boundary each chunk, andllm_graph_input_kpoolbecause its pool count grew (192 → 320 → 448 … per chunk atkpool_pad64). With both padded to the 4096 bucket the chunks reuse the graph (DTreused=1on 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).kpool_padalways 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=1is 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).LLAMA_KV_PROMPT_PAD=4096GGML_NCCL_WIRE_BF16=1Review fixes
0 / 0on the fixed build,6–59before).-np N -kvudecoding keeps the 256-cell padding.What is not covered
340 / 85where the oracles write340 ÷ 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)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).DBGREUSE kpoolline is a diagnostic like the existingDBGREUSEoutput; it prints only underLLAMA_DEBUG_REUSE=1.