Repository navigation
Add tensorfold merge: fold a PEFT LoRA into a quantized checkpoint - #710
Conversation
|
Pushed two follow-up commits on top of the first (not a rewrite, so the earlier review stays valid):
The description is updated with the receipts for both models: Nemotron 3.5 Lightning (as before) and now Qwen3.8-27B, where the zero adapter reproduces all 17 files of the shipped checkpoint byte for byte and a random adapter over 15 linear-attention, attention and MLP modules matches its recomputation on all of about 12.9 million groups. Not run: serving a merged folder on any engine, Metal and macOS builds. |
|
An end-to-end run with a real trained adapter, on a model small enough to also check at the model level. Setup: dense Model level, on 20,480 positions of English text, KL(unmerged adapter on the bf16 base || variant), the merged weights read back from the tool's own output:
Per sequence the merged model is 2.6 to 4.3 times closer (median 3.4). A separate PyTorch implementation of the same quantizer gives 0.0802, and the tool's weights equal it exactly on the modules I compared. One adapter and one 4B model, so a measurement, not a rate. Two caveats a reviewer should know:
|
14a1ad9 to
2207b49
Compare
|
Hello @philip-pentatonic, Thank you for tensorfold merge. It copies the shipped folder, adds the adapter in f64, and writes the changed rows in place. The default stays the fold, which keeps the shipped scales. Fresh re-quantize of the dense row plus the adapter (lora_merge.zig:64) becomes an explicit flag. Post the error of that fresh quantize and the error of the fold, both against the f64 dense-plus-adapter row, on a real checkpoint. We keep the flag if the fresh quantize measures closer to the adapter than the fold. Thank you again. |
2207b49 to
b7ac8c2
Compare
|
Thank you. The numbers you asked for, on a real checkpoint, plus the new heads (the quantizer change from #709 is folded into the first commit; the stack is now Setup. A real trained LoRA (rank 64, alpha 16) over all 252 attention and MLP projections of Qwen3-4B; dense bf16 base; a shipped 4-bit checkpoint at group 64. For every projection the target is
Fresh is closer to On the default. By this measurement a fold default would leave a LoRA essentially unapplied (median 0.1% landed), so I would keep this pull request as it is: fresh quantization from the dense base, behind the explicit |
|
Thank you, @philip-pentatonic, for measuring how much of the trained adapter survives: 99.6% with fresh quantization versus 0.1% with folding makes the difference clear. At b7ac8c2, We'll come back on the fresh/fold choice. Please keep the adapter-landing measurement alongside pooled error in the comparison, so that choice reflects both. Posted by Ash's AI Assistant Synthena |
…LoRA into a quantized checkpoint `tensorfold merge DENSE ADAPTER OUT --quantize SHIPPED` merges a PEFT LoRA into the dense (bf16) weights of only the tensors the adapter names, quantizes those fresh with the MLX quantizer at the shipped tensor's bits and group, and copies every other tensor, shard and index byte for byte from the shipped MLX affine 4-bit checkpoint. Folding an adapter into already-quantized weights keeps little of its effect, so the dense base is required. Host-only. The adapter's B*A is accumulated in FP64 in order and scaled once; a zero sum gives an exactly zero delta, and a sum that is not exact in FP64 is not made exact. The merge is staged in a sibling folder and renamed to OUT only after every projection has succeeded, so a failure before publication leaves no OUT; the rename replaces an empty OUT atomically and never a non-empty one. It refuses, naming the tensor: other modules (only the Nemotron mixer in_proj/out_proj and shared experts' up_proj/down_proj and the Qwen dense q/k/v/o_proj are served), other bit widths, DoRA and rsLoRA, an adapter r or alpha out of range, duplicate PEFT keys, shard names that leave the folder, subdirectories, dangling symlinks and special files among the inputs, shards whose tensor ranges overlap, non-finite weights or factors, and mis-shaped tensors. tf-merge-check DENSE ADAPTER OUT SHIPPED gates a merged folder: the exact file set and lengths and every byte outside the touched tensors identical; every written group equal, byte for byte, to a recomputation from the dense weights and the adapter, with the number of groups compared reported; aggregate RMS error at most 1.05x the shipped quantization's, both against an FP64 reference. The worst-column ratio is printed but not gated, being an extreme of two differently referenced tensors. An adapter with no effect (alpha 0, an all-zero factor, negative zero included) must leave every byte and give ratios of exactly 1. The recomputation shares the merge's arithmetic, so it checks that what was written is what the code computed; the independent evidence is the pinned vectors, including one merge with a non-power-of-two alpha/r, and the quantizer's identity with shipped tensors. Tests use synthetic folders: a zero adapter reproduces every file, a random adapter changes only the named tensors, each refusal names its tensor, a failure on the second projection or at any allocation leaves no OUT, a non-empty OUT that appears before publication is never overwritten, and the gates fail on appended bytes, extra files, a wrong group and a collapsed grid.
The merge no longer admits modules by a list of name suffixes. A module is merged when the shipped checkpoint holds its .weight, .scales and .biases as rank-2 tensors in the 4-bit affine layout and the dense source holds a rank-2 .weight of the same shape, so attention, MLP and linear-attention projections qualify alike. Anything else is refused naming the module and the reason: a module not quantized in the shipped checkpoint (a norm or an embedding), a stacked or rank-3 expert tensor, or a module the dense source lacks. Every other check is unchanged. A dense module name is resolved to the shipped one by the exact name, then by one table of known prefix rewrites (model.language_model. to language_model.model.), so a dense source and an MLX conversion that name the same tensor differently still match. A module that resolves under no candidate, or under more than one, is refused naming each name tried. The leaves are read and written under the shipped name. Tests use synthetic folders: MLP and linear-attention names are merged, the prefix mismatch is resolved and passes the gates, and a stacked expert tensor, an unquantized norm, a module absent from the dense source, an ambiguous name and an unresolvable name are each refused with their reason.
…her namespace The aggregate-RMS comparison with the shipped quantization is no longer a gate at 1.05. A delta that widens a group's range legitimately coarsens that group's grid, so on sound merges of real adapters the ratio read 1.0518 and 1.0515 against that limit; it measures the adapter, not a defect. Both RMS ratios and the worst-column ratio are printed, and the check fails only when the merged RMS error exceeds twice the shipped one, a net for a gross fault such as a wrong scale or a sign error and not a quality gate. The deterministic gates are unchanged: the exact file set and lengths, every byte outside the touched payloads, every written group equal to its recomputation, and exact identity for an adapter with no effect. The test of the 1.05 limit is replaced by one where 1.06 and exactly 2.0 pass and 2.01 fails. A module may now be given in either namespace. The exact name is tried first, then the table of prefix rewrites forwards and in reverse; the pair of names must be present in both the dense source and the shipped checkpoint. A name that resolves to no pair, or to more than one, is refused naming every pair tried, and the quantizer check prints which form matched. The merge resolves adapter module names the same way. Tests cover a module resolving from either side, an ambiguous name, an unresolvable name, and an adapter naming the shipped spelling.
b7ac8c2 to
6f7d0da
Compare
|
Thank you for the review of the choice, and noted: I will keep the adapter-landing figure beside the pooled error whenever the comparison is quoted (99.6% landed with fresh quantization, 0.1% with the fold, pooled error 0.981 against 1.000). This pull request is rebased onto main now that #709 is in, so it is three commits ( |
… the merge subcommand cli/cli.zig imports "core" for `tensorfold merge` since #710, and on macOS it compiles inside main.zig's module, which named the engine only "tensorfold": a plain `zig build` on a Mac stopped there (no named step builds that program). The engine programs now import the engine under both names; Linux builds are unchanged.
…ollow-ups named as ours After review of the fourteen lines that overstated: #710's fresh-quantization gain is one measured case; #902's stop and group cleanup hold where the watch lines are open and through the shared helper; #887 says its chunks change the attention bits below 8,192, on M5 too, with no larger error against float64; #899 records the ring start and the wider refusal; #802 keeps the author's B70 receipts apart from our host-checked lifetime fixes; #836's equalities are the GB10's and its Windows build the author's; #888 says a failed restore's pinned memory is kept, never freed; #734's tree rounds are checked against the host's, not float64; the Living Weights lines (#876, #877, #878, #891) say which runs their numbers come from, the ten-fact costs, and the M3 Ultra's four-fact result; #946 and #910 read plainer.
|
Thank you, @philip-pentatonic, for the native LoRA merge and the comparison of adapter retention with pooled error. We merged your work for 1.1.0 in 743d6d5. The default folds onto the checkpoint's own scales and biases; The fixtures cover unchanged files and tables, zero-effect identity, signed grids and group refusals. Output is staged until the merge succeeds. Posted by Ash's AI Assistant Synthena |
… the merge subcommand cli/cli.zig imports "core" for `tensorfold merge` since ashhart#710, and on macOS it compiles inside main.zig's module, which named the engine only "tensorfold": a plain `zig build` on a Mac stopped there (no named step builds that program). The engine programs now import the engine under both names; Linux builds are unchanged.
…ollow-ups named as ours After review of the fourteen lines that overstated: ashhart#710's fresh-quantization gain is one measured case; ashhart#902's stop and group cleanup hold where the watch lines are open and through the shared helper; ashhart#887 says its chunks change the attention bits below 8,192, on M5 too, with no larger error against float64; ashhart#899 records the ring start and the wider refusal; ashhart#802 keeps the author's B70 receipts apart from our host-checked lifetime fixes; ashhart#836's equalities are the GB10's and its Windows build the author's; ashhart#888 says a failed restore's pinned memory is kept, never freed; ashhart#734's tree rounds are checked against the host's, not float64; the Living Weights lines (ashhart#876, ashhart#877, ashhart#878, ashhart#891) say which runs their numbers come from, the ten-fact costs, and the M3 Ultra's four-fact result; ashhart#946 and ashhart#910 read plainer.
Part 2 of 2 for #666. The quantizer (#709) is merged; this is the merge command on top of it, three commits:
31a2632athe merge command,4d480806which modules and names,6f7d0dadthe sanity bound and namespaces. The closing note of #160 ("a fine-tune merged into the weights and quantized in the same format serves today as its own model") is what this makes possible without Python or MLX.What this changes
tensorfold merge DENSE ADAPTER OUT --quantize SHIPPEDmerges a PEFT LoRA into the dense bf16 weights of only the tensors the adapter names, quantizes those fresh with the quantizer from the first pull request at SHIPPED's bits and group (32, 64 or 128), and copies every other tensor, shard and index byte for byte from SHIPPED. The work happens in a sibling staging folder that is renamed to OUT only after every projection succeeded.tf-merge-check DENSE ADAPTER OUT SHIPPEDgates the result.Which modules are merged is read from the checkpoints, not from a name list: a module is merged when SHIPPED holds
<module>.weight,.scalesand.biasesas rank-2 tensors in the 4-bit affine layout and DENSE holds a rank-2.weightof the same shape. That covers attention, MLP, linear-attention and Mamba mixer projections alike. Anything else (a module not quantized in SHIPPED, a norm or embedding, stacked rank-3 expert tensors, DoRA, rsLoRA, duplicate PEFT keys, non-finite or mis-shaped tensors, out-of-rangeroralpha, a non-regular file, an overlapping shard layout) is refused with the module and the reason. A module may be named in either namespace: the exact name first, then one small table of known dense-to-shipped prefix rewrites (model.language_model.andlanguage_model.model., in either direction), refusing when none or more than one distinct pair resolves and listing every pair tried. About 60% of the diff is tests and fixtures.Why the dense base: folding an adapter into already-quantized weights loses most of it. On one real rank-64 adapter (alpha 16, a 4B Qwen model, attention and MLP projections), measured from the weights alone and then on 20,480 positions of English text: the adapter's change is 2 to 4% of one 4-bit step per weight; merging it into the quantized grid recovers about a quarter of the divergence from the unmerged adapter, and merging into the dense weights and quantizing fresh lands at the plain 4-bit model's own noise floor (KL 0.080 against 0.274 for ignoring the adapter and 0.231 for merging into the grid). One adapter, so a measurement, not a rate.
Choices a reviewer may want to weigh in on:
B*Ais accumulated in FP64 over the rank and scaled once byalpha/r(the way PEFT applies it); an exactly zero sum gives an exactly zero delta. Exact cancellation of every factor matrix is not claimed.alpha/r), both produced by a separate float32 implementation, and the byte identity against shipped tensors.Receipt
e7c523ebplus these commits, Zig 0.17.0, Linux x86_64 (AMD Threadripper PRO, 32 cores), host only, no GPU. Every shipped file's sha256 was checked against the Hub; dense tensors fetched by HTTP range from the public repos.TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bitatd9d758fb(14 files, 18 GB), densenvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16ata9904d24; adapters rank 16,alpha32, bf16 factors,in_projandout_projof layers 0, 2 and 30.tensorfold-native merge27 s, 0.4 GB peak;cmpof all 14 files: 0 differ;tf-merge-check:identity: 14 files byte-identical, 0 touched: ok,merge check: all gates pass.TensorFold/Qwen3.8-27B-MLX-4bitat22d8d538(3 shards, 15 GB), denseQwen/Qwen3.8-27Bat1d4bf0f2, which name the same tensors differently (model.language_model.layers.N...andlanguage_model.model.layers.N...). 15 modules across layers 0 and 3: linear-attention (in_proj_qkv/z/a/b,out_proj), attention (q/k/v/o_proj) and MLP (gate/up/down_proj).tf-merge-check --quantizer: scales, biases, codes and whole groups 100.0000% identical on all 15, given shipped-style names.cmpof all 17 files: 0 differ;merge check: all gates pass. Merge 49 s, 2.4 GB peak; check 2 min 28 s, 10 GB.1 of 491520 groups differ from OUT(27B),1 of 172032(Nemotron),merge check: FAILED.zig build test-merge: 80/80 tests (cold build, 12/12 steps).zig build test: 39/39 steps, 171/171 tests.zig fmt --check: clean.python3 tools/zig/lean_check.py: 534 problems before and after. No source file over 600 lines.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Not applicable.zig build test.Rebased on 16 Oct onto main
0a591781, after #709 merged. The build steps oftf-merge-checkandtest-mergenow live inzig/build/merge.zigas on main, and the HIP and XPU checkpoint-CLI modules get thecoreimport the merge command needs.zig fmt --checkis clean, the lean check shows 0 new problems against main, andzig build test test-merge tf-merge-checkpasses (754 of 761 tests, 7 skipped, Linux aarch64, no GPU). The real-checkpoint receipts above were re-run on this head (Linux x86_64, host only): the quantizer is byte-identical to the shipped checkpoints on 6 Nemotron 3.5 Lightning and 15 Qwen3.8-27B tensors; a zero adapter leaves every file identical (13 and 16 files); a random adapter touches only the shards that hold its projections (2 of 13 and 1 of 16), the recompute gate finds 0 differing groups and the check passes; and flipping one bit of one scale of a merged projection makes the check fail.