mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #5433] [Engine] Auto-load sibling token-embd-bf16.gguf for sub-8-bit native token embeddings #5422
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5422
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/5433
Author: @bsorensen110
Created: 11/6/2026
Status: 🔄 Open
Base:
main← Head:feat/auto-bf16-embd-sidecar📝 Commits (2)
dbb1086[verified] feat(embed): auto-load sibling token-embd-bf16.gguf for sub-8-bit native embeddings9e3626bfix(embed): address review on BF16 sidecar auto-discovery📊 Changes
8 files changed (+237 additions, -9 deletions)
View changed files
📝
docs/QUANTS.md(+15 -5)📝
include/strata/core/native_head.hpp(+5 -0)📝
src/core/native_head.cpp(+45 -0)📝
src/kernels/native_expert_parity.cpp(+56 -0)📝
src/program/generate.cpp(+13 -2)📝
sycl/src/core/native_head.cpp(+46 -0)📝
sycl/src/program/generate.cpp(+13 -2)📝
tools/embd_bf16_pack.py(+44 -0)📄 Description
Plain-English overview
Think of
token_embd.weightas the dictionary at the very front door of the model.Before Layer 0 even starts, every word/token ID gets turned into a list of
2,560numbers (its vector) by looking up its row in this table.1. What the 1- to 4-bit GGUFs ship vs. what
docs/QUANTS.mdassumeddocs/QUANTS.mdassumed Unsloth kept this front-door dictionary at 8-bit (Q8_0, which is ~99.5% accurate) in every model.UD-Q4_K_XLand larger — but inUD-IQ4_XS(and all the 1- to 3-bit models), the front-door dictionary is compressed down to 3- or 4-bit (IQ4_XSinUD-IQ4_XS) to save ~890 MBof file size.21.9 dBSNR): Comparing those 4-bitIQ4_XSvectors against Qwen's original 16-bit (BF16) checkpoint vectors shows every token entering Layer 0 starts out ~8% off from its true coordinates. Every single layer (all 48 of them, plus the DFlash2/MTP drafters) then does math on a slightly blurred input signal.2. Why fixing it costs zero VRAM and zero decode speed
322 MiB->1,212 MiB) would eat GPU VRAM and slow down memory bandwidth.0 Bof VRAM: The dictionary has248,320words, but in any verify step the GPU only needs the 1 to 8 words currently being processed. Strata'sNativeEmbedkeeps the whole1.21 GiBdictionary in system RAM (pinned so the GPU reads it directly over PCIe) and only pulls the few rows it needs each step. The GPU keeps the exact same number of resident expert slots (10,992slots /25.15 GiBon a 32 GB R9700).15tokens total), reading 16-bit rows instead of 4-bit rows over PCIe 5.0 (55.8 GB/s) transfers an extra56 KBper step. At55.8 GB/s,56 KBtakes 1 microsecond (0.001 ms) out of a24 msstep (0.004%of step time).3. What the quality numbers actually mean
Feeding both setups the exact same
4,995tokens of real text (data/quant_quality/v1:code,docs,english,multilingual,agent) and comparing the model's next-word predictions at every single token position:95.12%(4.88%of tokens flip):244out of4,995positions — and nearly 1 out of 11 words ondocs), the 4-bit embedding noise was enough to tip a close race at the end of Layer 47 and make the model pick a different #1 next word than it picks with the clean BF16 input.0.02602(why this number is large):KL divergencemeasures how much the model's overall probability ranking over the next word gets distorted (0= identical).docs/QUANTS.md, shrinking all 48 layers of routed experts fromQ8_0(188 GB) down toUD-Q5_K_XL(158 GB) causes a KL distortion of0.0247, and shrinking the whole model all the way toUD-IQ4_XS(94 GB) causes0.0566.1.2 GBfront-door table removes0.0260of distortion — roughly the same magnitude of distribution shift as stepping up a full quantization tier, without touching VRAM or speed.PPL) drops on prose, docs, and multilingual (-0.09%to-0.76%):4.241->4.209), multilingual text (2.656->2.646), and technical docs (9.530->9.521).Problem and result
In
Qwen3.8-Flash-Next-UD-IQ4_XS(both the 3-shard38bb39eerelease and the 4-shardv3.1release) as well as the 1- to 3-bit packs (IQ3_S,IQ3_XXS,IQ2_XS,Q2_0,IQ1_M),token_embd.weight([2560, 248320]) is quantized to 3- or 4-bit (IQ4_XSinUD-IQ4_XS,322.07 MiB,1,360 B/row,4.25 bpw) rather thanQ8_0.Comparing
UD-IQ4_XS'sIQ4_XStoken_embd.weightdirectly against Qwen's original BF16 checkpoint embedding (token-embd-bf16.gguf,1,212.50 MiB,5,120 B/row) across the1,445active vocabulary tokens ofdata/quant_quality/v1(and a16,384-row uniform vocabulary sample) shows:8.074%mean relative L2 error (7.791%full-vocab; p999.619%),21.88 dBmean SNR, cosine similarity0.996722.4,995positions ofdata/quant_quality/v1(--resident-experts --ram-keep-gib 40 --pcie-frac 0):BF16_embdvsIQ4_XS_embd):95.12%—244 / 4,995(4.88%) of greedy top-1 token predictions flip solely from replacing the 4-bit input embedding with the BF16 checkpoint embedding (docs:91.19%top-1 agreement /88flips;agent:93.29%/67flips;english:94.99%/50flips).KL(BF16_embd || IQ4_XS_embd):0.02602 nats(p99 = 0.2580), nearly half the magnitude ofUD-IQ4_XS's total 48-layer quantization KL vsQ8_0(0.05660indocs/QUANTS.md).PPL,BF16_embdvsIQ4_XS_embd): improves onenglish(4.2413 -> 4.2090,-0.0323/-0.76%),multilingual(2.6562 -> 2.6464,-0.0098/-0.37%),docs(9.5298 -> 9.5213,-0.0085/-0.09%), and overall (3.9539 -> 3.9531).gfx1201, 32 GiB VRAM, PCIe 5.0 x16 at55.8 GB/s):0 B(NativeEmbedkeepstoken_embd.weightin pinned mapped host memory and gathers only theTactive rows over PCIe; resident expert cache remains identical at10,992slots /25.15 GiB).+890.4 MiB(1,212.5 MiBBF16 vs322.1 MiBunmappedIQ4_XS),+0.1 sstartup (0.1 -> 0.2 s).+3,760 B/token(+56.4 KB/~1.0 usper 15-row verify + DFlash2 step,< 0.005%of window time). Across 4 interleaved runs per arm (300tokens acrosscode,docs,englishattemp=0.0, 0.7, 1.0), decode throughput is unchanged within trajectory/sampling variance (sampled_t0.73-domain mean78.59vs77.82 tok/s;sampled_t1.03-domain mean71.77vs73.68 tok/s;codegreedy96.25 -> 104.56 tok/sdue to higher DFlash2 acceptance on the BF16 greedy trajectory).This PR adds
NativeEmbed::auto_bf16_sidecar(...)so that when--embd-ggufis omitted andSTRATA_AUTO_EMBD_BF16is not"0", the engine automatically uses a validated siblingtoken-embd-bf16.ggufwhenever the model'stoken_embd.weightis narrower than 8-bit, adds--fetchtotools/embd_bf16_pack.pyto download and SHA-256-verify Qwen'stoken-embd-bf16.gguf, and updatesdocs/QUANTS.md.Related discussion
Builds on PR #290 (
--embd-ggufandtools/embd_bf16_pack.py).Approach
include/strata/core/native_head.hpp:60-64,src/core/native_head.cpp:290-331,sycl/src/core/native_head.cpp:289-330: AddNativeEmbed::auto_bf16_sidecar(shards, n_embd, n_vocab, &replaced_type):std::filesystem::is_regular_fileif notoken-embd-bf16.ggufexists in<shard0_dir>/,<shard0_dir>/../, or<shard0_dir>/../embd/, or ifSTRATA_AUTO_EMBD_BF16=0.token_embd.weightinshardsand returns{}if it is already 8-bit or wider (F32=0,F16=1,Q8_0=8,BF16=30), leavingUD-Q4_K_XL,UD-Q5_K_XL,UD-Q6_K_XL, andQ8_0untouched unless--embd-ggufis explicitly passed.token-embd-bf16.ggufhasgeneral.architecture == "strata-embd",token_embd.weightof typeBF16(30), shape[n_embd, n_vocab], and complete in-bounds payload bytes before returning its path.src/program/generate.cpp:4918-4947,sycl/src/program/generate.cpp:2942-2961: CallNativeEmbed::auto_bf16_sidecarwheno.embd_gguf.empty()and report the replacement in the startup banner (token embedding BF16 (token-embd-bf16.gguf, replacing IQ4_XS) in mapped host memory (1212 MiB, 0.2 s)). Keeping auto-discovery at thegenerate.cppcaller preservesNativeEmbed::load(shards, ...)'s direct contract fornative_expert_parity --gguf-embd.src/kernels/native_expert_parity.cpp:749-794: Extend--bf16-embd(check_bf16_embd) with a synthetic GGUF regression test verifying thatauto_bf16_sidecarupgradesIQ4_XS(23) totoken-embd-bf16.gguf, leavesQ8_0(8) unchanged, and respectsSTRATA_AUTO_EMBD_BF16=0.tools/embd_bf16_pack.py: Add--fetchto download and SHA-256-verify Qwen3.8-Flash-Next'stoken-embd-bf16.gguf(1,271,398,656B, SHA-256386203af9070c7b43acb24ded8922876721d97bbad1a709b25cb49d694a7f51d, matchingsetup.py:297).docs/QUANTS.md: Document thatUD-IQ4_XSstorestoken_embd.weightasIQ4_XS(322 MiB) rather thanQ8_0, along with the measuredquant_quality/v1KL/top-1/PPL impact of replacing it withtoken-embd-bf16.gguf.Validation
Tested on revision
dbb1086aa(based onmain54828c7ef, Strata 0.2.10 / engine 0.1.57) on Linux (7.3.1-1-cachyos-server, AMD Radeon AI PRO R9700gfx120132 GB VRAM, 128 GB RAM, ROCm 10.2.0a20260914).Passed:
./build-hip/native_expert_parity --bf16-embdondbb1086aa:bf16 embedding: 97 gathered rows, 0 differ; dequant of 156160 values, 0 differ in any bit,bf16 sidecar auto-discovery: ok,native_expert_parity: 0 failures../build-hip/native_expert_parity --gguf-embd .../token-embd-bf16.gguf 512:248320 rows, 512 random rows gathered on the GPU, 0 rows / 0 values differ from ggml to_float; iq_dequant_f32 row: bitwise equal../build-hip/native_expert_parity --gguf-embd .../Qwen3.8-Flash-Next-UD-IQ4_XS-00002-of-00004.gguf 512:0 rows / 0 values differ from ggml to_float; iq_dequant_f32 row: bitwise equal.python tools/embd_bf16_pack.py --fetch --out .../token-embd-bf16.gguf:verified existing .../token-embd-bf16.gguf (1271398656 B).data/quant_quality/v1evaluation (4,995teacher-forced positions +20prompt logit dumps + 4 interleaved decode runs per arm) onUD-IQ4_XS v31on R9700 (gfx1201): confirmedtoken embedding BF16 (token-embd-bf16.gguf, replacing IQ4_XS) in mapped host memory (1212 MiB, 0.2 s)and10,992resident expert slots (25.15 GiBVRAM) in both arms.Failed:
Not run:
sycl/src/core/native_head.cppandsycl/src/program/generate.cppupdated in lockstep withsrc/).Limits
token-embd-bf16.gguf(1.27 GBon disk,+890 MiBpinned mapped host RAM overIQ4_XS) to be present in the shard directory, its parent directory, or../embd/(or passed via--embd-gguf); when absent, startup does not download anything and uses the shard's owntoken_embd.weightunchanged.Q8_0embeddings (such asUD-Q4_K_XLand above, whereQ8_0is already within0.56%ofBF16); users who wantBF16overQ8_0can still pass--embd-ggufexplicitly.Reproduction
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.