Repository navigation
Add MLX's affine 4-bit quantizer and a check against shipped tensors - #709
Conversation
022fd1d to
9ad978b
Compare
|
Hello @philip-pentatonic, Thank you for the MLX affine 4-bit host encoder. quantizeMlx takes the larger-magnitude edge and stores the scale and bias as bf16 (affine4_host.zig:44). One change. The groups the engine serves are 32, 64, and 128. lora_quant.quantize sends every other group through quantizeMlx(128) (lora_quant.zig:36). A group of 256 writes count/128 scales into the table allocQuant sized as count/256 (lora_quant.zig:20). Please refuse a group other than 32, 64, or 128. Thank you again. |
quantizeMlx quantizes a dense tensor to MLX affine 4-bit with groups of 32, 64 or 128 the way MLX does: float32 arithmetic with a true division, the bias at the larger-magnitude edge of a group, a signed scale chosen so that zero is a grid point, round half away from zero, codes taken from the unrounded scale and bias, and bf16 rounding only for storage. When the dense source and the conversion scheme match how a checkpoint was made, it reproduces that checkpoint's words, scales and biases exactly. A non-finite value, an overflowing range, a zero adjusted scale and a scale or bias that rounds to a bf16 infinity are refused, naming the group. `tf-merge-check --quantizer DENSE SHIPPED MODULE...` quantizes each named dense tensor and compares it with the shipped one, reporting the share of equal scales, biases, codes and whole groups. The dense tensor must be an n x k f32, bf16 or f16 matrix of the shipped projection's shape and payload size, every required input must be a regular file, and a failed allocation unwinds cleanly. Tests pin the quantizer to nine groups produced by an independent float32 implementation of the published algorithm: two where true division and reciprocal multiplication differ, one that needs the unrounded scale and bias, a q0 tie at -8.5, the larger edge at the minimum and at the maximum, a negative flat group and a tiny group with q0 = 0; they also check a local reciprocal variant disagrees with them. Other tests cover group sizes 32, 64 and 128, the refusals above, and the check on synthetic dense and shipped folders. Build: `zig build test-merge`, `zig build tf-merge-check`.
9ad978b to
c107b4d
Compare
|
Thank you, done. |
|
Thank you, @philip-pentatonic, for adding the group-256 sentinel case that catches the original overrun. At c107b4d, The reported finding is closed in source. This is ready for the quantizer landing checks, including the sentinel regression. Posted by Ash's AI Assistant Synthena |
#709 and #892 each added their steps to build.zig, which was already past 600 lines. The same steps now come from zig/build/merge.zig (test-merge and tf-merge-check, host only on every OS) and glm_checks.zig's sampler (tf-glm-sample-check and test-glm-sampling); build.zig keeps one call for each. No step, module or option changes.
…VIEW names the refused GLM pair requests rightly One line each for #881, #882, #883, #889, #890, #709, #848, #892, #867, #868, #871 and #873, crediting their authors. ZIG-PREVIEW said a two-Mac GLM pair refuses "temperature-1" requests; it refuses any temperature above 0 (backend.zig and engine.zig both test temperature > 0).
|
Thank you, @philip-pentatonic, for the explicit BadGroup refusal before any write. #709 is included in 1.0.12 as merge 7a3d8fa, at your head c107b4d. The quantizer and tf-merge-check compare MLX affine codes, scales, biases and whole groups against a shipped checkpoint. Our 2931b7b moves the build steps into zig/build/merge.zig, and 4cf6816 puts the quantizer's comment on one line. On macOS and Linux, test-merge passes 42 of 42 and the quantizer's own tests pass 16 of 16. A comparison against a real dense checkpoint and its shipped quantized conversion remains owed on our side. The PR is still marked as a draft on GitHub; its commits are included in this release. Posted by Ash's AI Assistant Synthena |
…quantizer against shipped tensors
ashhart#709 and ashhart#892 each added their steps to build.zig, which was already past 600 lines. The same steps now come from zig/build/merge.zig (test-merge and tf-merge-check, host only on every OS) and glm_checks.zig's sampler (tf-glm-sample-check and test-glm-sampling); build.zig keeps one call for each. No step, module or option changes.
…e comment broke the lean rule)
…VIEW names the refused GLM pair requests rightly One line each for ashhart#881, ashhart#882, ashhart#883, ashhart#889, ashhart#890, ashhart#709, ashhart#848, ashhart#892, ashhart#867, ashhart#868, ashhart#871 and ashhart#873, crediting their authors. ZIG-PREVIEW said a two-Mac GLM pair refuses "temperature-1" requests; it refuses any temperature above 0 (backend.zig and engine.zig both test temperature > 0).
Part 1 of 2 for #666. The merge command in the second pull request builds on this one.
What this changes
Adds
affine4_host.quantizeMlx, a Zig implementation of the 4-bit affine quantizer thatmlx_lmused for the shipped MLX checkpoints, andtf-merge-check --quantizer DENSE SHIPPED MODULE..., which quantizes a dense bf16 tensor and compares the words, scales and biases with the shipped checkpoint's. The scheme: float32 arithmetic on the bf16 input, true division by 15, the bias at the group's larger-magnitude edge with a signed scale, the scale reset toedge / q0so zero is an exact grid point, codes from the unrounded scale and bias, round half away from zero, bf16 only for storage. Nothing in the engine calls it yet. One change: the quantizer and its check.Receipt
e7c523ebplus this commit, Zig 0.17.0, Linux x86_64 (AMD Threadripper PRO, 32 cores), host only, no GPU. Shipped checkpointTensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bitatd9d758fb(every file's sha256 checked against the Hub); dense sourcenvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16ata9904d24, only the named tensors fetched by HTTP range.tf-merge-check --quantizer DENSE SHIPPED backbone.layers.{0,2,30}.mixer.{in_proj,out_proj}: scales, biases, codes and whole groups 100.0000% identical on all six tensors (10304x2688 and 2688x4096, group 64), exit 0,quantizer check: byte-identical on every tensor.TensorFold/Qwen3.8-27B-MLX-4bitfromQwen/Qwen3.8-27B(public main, 11 October 2026) bit for bit. The Qwen tensors were not run through the Zig check. A reciprocal multiply instead of the division by 15 misses about 0.5% of groups, which the vector test asserts.zig build test-merge: 6/6 steps succeeded, 33/33 tests passed.zig build test: 39/39 steps succeeded, 463/463 tests passed.zig fmt --checkon every touched path: clean.python3 tools/zig/lean_check.py: 534 problems before and after, none added. Nine pinned vectors from the independent implementation (synthetic groups: division against reciprocal, a q0 tie, the edge at the minimum and at the maximum, a negative flat group, a tiny q0 = 0 group); non-finite input, an overflowing range, a zero adjusted scale and a bf16 overflow are refused.build.zigtest-step wiring is configure-checked foraarch64-macosonly); Windows; group sizes 32 and 128 on real checkpoints (unit tests only); Qwen3.8-27B through the Zig check; CI.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Not applicable.Rebased on 14 Oct onto main
2984ea01. Conflicts were limited to context inbuild.ziganddocs/quantization.md; the quantizer commit changed only in that context. After the rebase:zig fmt --checkis clean,zig build test tf-merge-checkpasses (545 of 550 tests, 5 skipped, Linux aarch64, no GPU) andzig build test-mergepasses 87 of 87. The real-checkpoint receipts above were measured one7c523eb; on 15 Oct they were re-run on the rebased stack (Linux x86_64, host only, no GPU) with the same results: the quantizer is byte-identical to the shipped checkpoints on the 6 Nemotron 3.5 Lightning and 15 Qwen3.8-27B tensors; a zero adapter leaves every file identical to the shipped one (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.