Repository navigation
llama: load an F32 matmul weight whose values are all BF16 values as BF16 - #36
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved BF16 narrowing checks can cause model-loading failures on Metal and RPC backends or duplicated tensors, while OpenCL conversion can silently alter weight values.
6 open issues
Retain F32 when RPC BF16 support is unknown · New Exclude mmap-backed buffers from BF16 narrowing · New Preserve F32 on backends with lossy BF16 storage · New Check tensor type before narrowing duplicate names · New Skip BF16 data scans for metadata-only loads · New Narrow and upload in bounded chunks · New
What changed in this PR
This PR adds automatic BF16 loading for eligible F32 matrix weights whose values are exactly representable in BF16, reducing device memory and matmul bandwidth use.
Changes:
- Adds eligibility checks for weight values, operations, and buffer support.
- Converts selected weights during loading while counting progress in original file bytes.
- Logs memory savings and provides the
LLAMA_KEEP_F32_WEIGHTS=1opt-out.
| File | Description |
|---|---|
src/llama-model-loader.h |
Declares the eligibility helper and tracks narrowed weights and bytes saved. |
src/llama-model-loader.cpp |
Implements eligibility scans, BF16 conversion and upload, logging, and the opt-out. |
Review effort: Balanced
Review fixes for narrowing F32 matmul weights whose values are all BF16 values: - RPC: a server reports in its HELLO whether every backend it serves runs MUL_MAT and MUL_MAT_ID with BF16 weights (RPC_OP_CAP_BF16_MUL_MAT); the client answers BF16 matmuls from that bit, so a server without it keeps the weight F32. - mmap: a weight on a device that wraps host memory stays in the file's mapping, which has no BF16 copy, so it is not narrowed. - A device narrows only after a small BF16 tensor in its default buffer type reads back the values written (keeps_bf16), checked once per device together with its properties. - load_all_data takes the narrowed path by the tensor's own type, not its name. - A no_alloc load narrows nothing and sizes these weights at F32. - The value scan and the load read 16 MiB of F32 at a time into a reused BF16 buffer; a meta (split) buffer, whose set_tensor takes only whole slices, gets the BF16 copy whole, once.
lukekim
added a commit
to spiceai/spiceai
that referenced
this pull request
Nov 9, 2026
…ly BF16 values loaded as BF16 Moves llama-cpp-src to e53115d2, the squash of spiceai/llama.cpp#36 on spiceai-b11999. An F32 weight of a matrix multiply whose every value is a BF16 value loads as BF16 where every device behind its buffer keeps BF16 values as given and multiplies BF16 weights, so those weights are read at two bytes a value instead of four. docs/dev/fork_patches.md: the new pin, a row for #36, and #36 in the perf-only gaps (62 GAP rows).
lukekim
added a commit
that referenced
this pull request
Nov 10, 2026
#37) * llama: narrow F32 weights to BF16 only when LLAMA_NARROW_F32_TO_BF16=1 #36 narrowed by default. On DeepSeek-V4.1-Flash split across three DGX Sparks the changed replies accept fewer drafted tokens, and the slowest 200-token request went from 5.21-5.42 s to 5.89-6.05 s. Keep F32 unless asked; LLAMA_KEEP_F32_WEIGHTS=1 still forces F32. * llama: shorten the narrowing opt-in comment
vatsalp2008
pushed a commit
to vatsalp2008/spiceai
that referenced
this pull request
Nov 11, 2026
…reduce-scatter mesh all-reduce and opt-in BF16 loads of F32 weights (spiceai#16174) * perf(llama-cpp): pin the llama.cpp fork with RDMA sends in flight and a reduce-scatter mesh all-reduce Moves llama-cpp-src to the squash of spiceai/llama.cpp#35 on spiceai-b11999, which adds spiceai#34 and spiceai#35 to the current pin: - spiceai#34: an RDMA connection keeps several sends in flight, from four send buffers. - spiceai#35: on a mesh of more than two ranks whose links are all RDMA, an all-reduce partial over 1 MiB is reduced by a reduce-scatter and an all-gather. The sums keep the same bits. Both are perf-only, so the fork-patch ledger gains two GAP rows. * perf(llama-cpp): pin the llama.cpp fork with F32 weights that hold only BF16 values loaded as BF16 Moves llama-cpp-src to e53115d2, the squash of spiceai/llama.cpp#36 on spiceai-b11999. An F32 weight of a matrix multiply whose every value is a BF16 value loads as BF16 where every device behind its buffer keeps BF16 values as given and multiplies BF16 weights, so those weights are read at two bytes a value instead of four. docs/dev/fork_patches.md: the new pin, a row for spiceai#36, and spiceai#36 in the perf-only gaps (62 GAP rows). * perf(llama-cpp): keep F32 weights F32 unless LLAMA_NARROW_F32_TO_BF16=1 Pins spiceai/llama.cpp#37's branch (TEMPORARY), which makes spiceai#36's BF16 narrowing opt-in. Narrowing slowed DeepSeek-V4.1's slowest 200-token request from 5.21-5.42 s to 5.89-6.05 s; the RDMA (spiceai#34) and mesh (spiceai#35) patches stay. * perf(llama-cpp): repin spiceai/llama.cpp#37's branch at b04e9151 * perf(llama-cpp): repin to spiceai/llama.cpp#37's squash on spiceai-b11999 (8829a408, same tree as b04e9151)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


What
GGUF files often keep a model's small matrices in F32 even when their values came from a BF16 checkpoint. GLM-5.3-Flash UD-Q2_K_XL holds 1.9 GiB of them:
attn_k_bandattn_v_b.ffn_gate_inp.ssm_f_*,ssm_g_*andssm_beta.hc_*_fn.Every token reads them at four bytes a value, where two would hold the same values. On a GLM-5.3 decode step split across three DGX Sparks,
mul_mat_vec_fwas about 22% of kernel time: 1.5–1.6 s of 6.7–7.1 s per rank, in Nsight Systems with--cuda-graph-trace=node.The change (
src/llama-model-loader.{h,cpp}): an F32 weight loads as BF16 when all of these hold.GGML_OP_MUL_MATorGGML_OP_MUL_MAT_ID, and is neither lazy nor duplicated.weight_buft_supported). An RPC device now asks its server, below.In ggml:
MUL_MATandMUL_MAT_IDwith BF16 weights (RPC_OP_CAP_BF16_MUL_MAT, besideRPC_OP_CAP_ARGMAX_SPLIT).ggml_backend_rpc_device_supports_opanswers BF16 matmuls from it, instead of always yes. A model stored as BF16, on a server without BF16 matmuls, now keeps those weights off that server instead of aborting on them.ggml_backend_meta_device_n_devsandggml_backend_meta_device_get_devgive a meta device's simple devices.At load: such a tensor reads 16 MiB of its F32 bytes at a time, from the mapping or through
llama_file::read_raw. It copies the high half of each float into an 8 MiB buffer reused across tensors, and uploads that at its offset. A meta (split) buffer takes only whole slices along its split axis, so there the BF16 copy is built whole and uploaded once. The tensor's own type picks this path, so an F32 tensor of the same name loads as before. Progress counts the file's bytes, sosize_donestill reachessize_data.What stays the same: the weights are unchanged. What changes is the arithmetic around them: a matmul with BF16 weights runs other kernels than one with F32 weights. It moves the logits by about as much as changing
-ubor turning CUDA fusion off, as measured below.What the user sees: one line says what was narrowed, and
LLAMA_KEEP_F32_WEIGHTS=1turns narrowing off:Not covered: host buffers (CPU), devices that hold weights in the file's mapping (Metal with mmap), and devices that fail the probe (OpenCL). They keep F32.
Tests
On three DGX Spark (GB10) nodes, 2026-11-09. Every arm's source is this branch's commit or its base,
250bdf93(#35).Parity:
llama-perplexityat this PR, one SparkGLM-5.3-Flash UD-Q2_K_XL, wikitext-2, 30 × 512,
-c 512 -b 512 -fa on -ngl 99. Every arm is scored against the logits saved withLLAMA_KEEP_F32_WEIGHTS=1.LLAMA_KEEP_F32_WEIGHTS=1again-ub 256(noise floor)GGML_CUDA_DISABLE_FUSION=1(noise floor)The two noise-floor rows ran at
b20c7a2b, the same loader for F32 weights. The PR's KLD is inside the band that any change of evaluation order moves this model by, and its PPL is within error of the others.The same weights as a lossless repack: a GGUF rewritten offline with these F32 weights stored as BF16, every tensor checked to widen back to the original bits, scores exactly 0 against this PR's own logits:
So this PR loads what the repack stores.
Decode through
spiced, three SparksGLM-5.3-Flash UD-Q2_K_XL split tensor-parallel across all three nodes over RDMA RPC, with its MTP head (2 drafted), greedy.
spicedat the same commit, built twice in parallel, withllama-cpp-srcat250bdf93(N) and at this PR (P).250bdf93250bdf93That's 7.3% faster at the median, and P's slowest run beats N's fastest. The slowest request to 200 tokens takes 4.45–4.64 s against 4.85–5.03 s.
376 F32 weights hold only BF16 values and load as BF16 (944.2 MiB instead of 1888.5 MiB).spicedatb20c7a2b, ran 49.49–49.82 tok/s against 46.40–47.46 in its own A/B, which had the head on another node.Three models through
spiced, three Sparksspicedat perf(llama-cpp): take a greedy request's picks from the argmax output on a split model spiceai#16172's head88c81c890e, built twice in parallel: A withllama-cpp-srcat250bdf93(ggml-rpc: reduce a large mesh partial by a reduce-scatter and an all-gather #35), B at2e6a1cf10.The slowest DeepSeek request is a changed reply. Its prompt 0 gets a different reply under B, and the drafter accepts less of it: 5.64–5.66 s against 5.13–5.19 s. On the two prompts whose replies are unchanged, B is 1–3% faster (3.58 against 3.60–3.64 s, and 3.21–3.22 against 3.31–3.36 s). With drafting, a change of arithmetic moves each prompt's speed with its reply, so per-prompt times shift in both directions.
Qwen3.8 without its drafter, so every step is one forward pass: the same binaries, sessions A B B A, adding multi-turn C1. Multi-turn is 8 distinct conversations x 5 turns x 200 tokens, one request at a time, with the whole history re-sent each turn.
This head against the measured one, three Sparks
The review fixes (
5b620811f) change how and where weights narrow, and not which values a narrowed weight holds. This check confirms that on a split model throughllama-server.-sm tensorover three RPC servers (RDMA mesh), MTP 2 drafted,--load-mode dio, greedy.2e6a1cf10, and F at2e6a1cf10with this head's diff applied, which is the tree of5b620811f.2e6a1cf102e6a1cf10keeps_bf16).ggml-backend-meta.cpp:1780(GGML_ASSERT(size % chunk_size_full == 0)), because it uploaded a split weight in chunks. This head uploads a split weight's BF16 copy whole.