Skip to content

llama: load an F32 matmul weight whose values are all BF16 values as BF16 - #36

Merged
lukekim merged 2 commits into
spiceai-b11999from
lukim/f32-bf16-weights
Nov 9, 2026
Merged

lukekim merged 2 commits into
spiceai-b11999from
lukim/f32-bf16-weights

Conversation

@lukekim

@lukekim lukekim commented Nov 9, 2026 •

Copy link
Copy Markdown

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:

  • MLA: attn_k_b and attn_v_b.
  • The lightning indexer's projections.
  • The MoE router: ffn_gate_inp.
  • The linear-attention gates: ssm_f_*, ssm_g_* and ssm_beta.
  • The hyper-connection weights: 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_f was 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.

  • The weight is a matrix used with GGML_OP_MUL_MAT or GGML_OP_MUL_MAT_ID, and is neither lazy nor duplicated.
  • It is a real load, not a metadata-only one such as the memory-fit pass, which sizes the weight at F32 and reads no weights.
  • Its buffer is not a host buffer, and is either the default buffer type of its device or a meta device's buffer type.
  • Every device behind that buffer type holds the weight in a buffer of its own. With mmap, a device that can wrap host memory (Metal) keeps the weight in the file's mapping, so it is left F32.
  • Every device keeps BF16 values as given: a four-value BF16 probe written into its default buffer type reads back unchanged. The values lie outside F16's range, so a backend that stores BF16 as F16 (OpenCL) fails the probe.
  • Its buffer type still serves the op at BF16 (weight_buft_supported). An RPC device now asks its server, below.
  • Every value is a BF16 value already: the low 16 bits of each float are zero. The loader reads the tensor's data once, 16 MiB at a time, when it creates the tensor.

In ggml:

  • RPC: a server's HELLO reports whether every backend it serves runs MUL_MAT and MUL_MAT_ID with BF16 weights (RPC_OP_CAP_BF16_MUL_MAT, beside RPC_OP_CAP_ARGMAX_SPLIT). ggml_backend_rpc_device_supports_op answers 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.
  • Meta devices: ggml_backend_meta_device_n_devs and ggml_backend_meta_device_get_dev give 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, so size_done still reaches size_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 -ub or turning CUDA fusion off, as measured below.

What the user sees: one line says what was narrowed, and LLAMA_KEEP_F32_WEIGHTS=1 turns narrowing off:

llama_model_loader: 376 F32 weights hold only BF16 values and load as BF16 (944.2 MiB instead of 1888.5 MiB); set LLAMA_KEEP_F32_WEIGHTS=1 to keep them F32

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-perplexity at this PR, one Spark

GLM-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 with LLAMA_KEEP_F32_WEIGHTS=1.

arm PPL mean KLD median P99 P99.9 max same top token
LLAMA_KEEP_F32_WEIGHTS=1 again 3.3163 0.000000 0.000000 0.000037 0.000050 0.000056 100.000%
this PR 3.3064 0.028970 0.007668 0.302 0.766 1.68 93.935%
base, -ub 256 (noise floor) 3.3045 0.028732 0.007242 0.306 0.696 2.04 94.052%
base, GGML_CUDA_DISABLE_FUSION=1 (noise floor) 3.2981 0.026781 0.006721 0.284 0.849 2.09 94.314%

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:

Mean KLD: -0.000000 ± 0.000000   Maximum KLD: 0.000062   Same top p: 100.000 ± 0.000 %

So this PR loads what the repack stores.

Decode through spiced, three Sparks

GLM-5.3-Flash UD-Q2_K_XL split tensor-parallel across all three nodes over RDMA RPC, with its MTP head (2 drafted), greedy.

  • Builds: spiced at the same commit, built twice in parallel, with llama-cpp-src at 250bdf93 (N) and at this PR (P).
  • Measure: 8 requests × 200 tokens, one at a time. One warm-up run is set aside and three counted, in sessions N, P, P, N. Each run's replies are compared, request by request, with N's first counted run.
session build tok/s time to 200 tokens, max of 8 replies equal to N1a
N1 250bdf93 47.48 / 47.51 / 47.05 4.93 / 4.85 / 4.97 s 8/8 each
P1 this PR 50.98 / 51.31 / 51.18 4.55 / 4.50 / 4.45 s 3/8 each
P2 this PR 49.76 / 49.38 / 50.15 4.64 / 4.64 / 4.57 s 3/8 each
N2 250bdf93 47.13 / 47.09 / 46.88 4.98 / 4.98 / 5.03 s 8/8 each
  • N: n=6, 46.88–47.51 tok/s, median 47.11.
  • P: n=6, 49.38–51.31 tok/s, median 50.57.

That'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.

  • Prompt reads: cold reads of a 3723-token prompt are unchanged, 7473–7569 ms against 7386–7685 ms.
  • Replies: P's equal N's on 3 of 8 prompts, in every P run. That's the change of arithmetic measured above. Every P run gives the same replies, as every N run does.
  • The log: every P session logged the narrowing: 376 F32 weights hold only BF16 values and load as BF16 (944.2 MiB instead of 1888.5 MiB).
  • The repack: the GGUF repacked as above, served by spiced at b20c7a2b, 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 Sparks

model narrowed A tok/s, n=6 B tok/s, n=6 slowest request to 200 tokens, A / B replies equal to A1a
DeepSeek 40 weights, 150.0 of 300.0 MiB 47.11–47.81 47.30–47.63 5.11–5.23 / 5.64–5.67 s 2/8
Qwen3.8 216 weights, 144.4 of 288.8 MiB 66.73–67.62 68.27–69.19 3.45–3.54 / 3.27–3.41 s 1/8
GLM-5.3 376 weights, 944.2 of 1888.5 MiB 44.75–47.62 49.29–51.47 4.94–5.26 / 4.41–4.74 s 3/8

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.

drafter measure A B
none decode tok/s, n=6 43.28–43.54 44.27–44.68
none multi-turn output tok/s 42.30, 41.99 43.23, 43.03
none slowest turn of 40 4.88, 4.87 s 4.76, 4.84 s
MTP decode tok/s, n=6 66.17–67.71 67.61–69.58
MTP multi-turn output tok/s 67.61, 66.48 66.73, 67.34
MTP slowest turn of 40 3.29, 3.40 s 3.64, 3.54 s
  • Without the drafter, B is faster on every measure: 2.8% on decode and 2.3% on multi-turn.
  • With MTP, multi-turn is unchanged: the sessions interleave, A1 67.61, B1 66.73, B2 67.34, A2 66.48.
  • With MTP, B's slowest turn is longer. 7 of 8 last-turn replies differ under B, and the drafter's acceptance moves with the reply.

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 through llama-server.

  • Model and layout: GLM-5.3-Flash UD-Q2_K_XL split with -sm tensor over three RPC servers (RDMA mesh), MTP 2 drafted, --load-mode dio, greedy.
  • Builds: O at 2e6a1cf10, and F at 2e6a1cf10 with this head's diff applied, which is the tree of 5b620811f.
  • Measure: the same 8 × 200 measure, in sessions O F F O, with the time from start to listening for each load.
session build tok/s time to 200 tokens, max of 8 replies equal to O1a start to listening
O1 2e6a1cf10 47.19 / 47.22 / 46.80 4.90 / 5.07 / 4.99 s 8/8 each 1:16.00
F1 this head 47.12 / 47.31 / 46.85 4.95 / 4.91 / 4.92 s 8/8 each 1:16.19
F2 this head 46.45 / 46.31 / 45.95 5.04 / 5.12 / 5.15 s 8/8 each 1:16.25
O2 2e6a1cf10 47.01 / 47.07 / 46.21 5.04 / 5.00 / 5.09 s 8/8 each 1:15.98
  • The same replies: every run of this head gives O1a's replies on all 8 prompts.
  • Speed: O 46.21–47.22 tok/s and F 45.95–47.31 overlap.
  • Load time: the same, about 1:16. An earlier version of these fixes checked each candidate weight's device through RPC and loaded in 2:04. This head checks each device once (keeps_bf16).
  • The first version of these fixes aborted this load at 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.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 08:11

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

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
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=1 opt-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

Comment thread src/llama-model-loader.cpp Resolved
Comment thread src/llama-model-loader.cpp Resolved
Comment thread src/llama-model-loader.cpp Resolved
Comment thread src/llama-model-loader.cpp Resolved
Comment thread src/llama-model-loader.cpp Resolved
Comment thread src/llama-model-loader.cpp Resolved
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.
Copilot AI balanced review requested due to automatic review settings November 9, 2026 16:48

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.

🟢 Approved

The review reports no active issues for the change to load eligible F32 weights as BF16.

No open issues 🎉

6 resolved since last review

Review effort: Balanced

@lukekim
lukekim merged commit e53115d into spiceai-b11999 Nov 9, 2026
23 of 44 checks passed
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)
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