Repository navigation
core: one MLX affine module reader for the Metal families (#2460 item 7) - #2583
Conversation
|
@jschmied thanks, a clean first step for item 7. Against main, all five families accept exactly what they did and get the same views, so loads are byte-equal. A few things before it lands:
|
|
Thanks @ashhart. Pushed ed588c6 and 11084be:
Merge state: the only conflict with main is |
|
@jschmied, thanks for the quick turnaround. The Spark move and the shared Step-5 check both read the same as before: the same views and accepted shapes, with Spark's and Step-5's own error names kept for their other tensors. Your reasons for keeping
Optional: a Spark form (fixed width, group 64, rank 2) in the file-backed test in |
|
@ashhart thanks. Both points on the head
|
|
Thanks @jschmied. Both points are covered on Nothing else from us. The only conflict with main is |
|
@jschmied our Metal cells on
|
core/quant/mlx_affine.zig reads a module's .weight, .scales and .biases and checks them as one projection (u32 words, bf16 tables, every byte accounted for), with the bits and group fixed by the family or read from the shapes. row_projection.affine makes the row kernels' Weights from a checked triple at the tensors' own offsets. qwen3_5, lfm2_moe, lfm3 (Metal), minimax_m3 (with its DFlash2 and DSpark drafters) and step5 drop their own copies; the loaders return the same views as before. Step-5's loader tests move to core/quant/mlx_affine_test.zig. Refs ashhart#2460 (item 7)
…step5 and spark25 share its check spark25/metal_weights.zig's affine (fixed bits, groups of 64, rank 2) is now mlx_affine.read plus row_projection.affine, so a refused module reports UnexpectedAffineTensor and names itself. Its and step5's exact-extent require call mlx_affine.require and keep their own error names. lfm2_moe logs the module whose tensors are misaligned again. Refs ashhart#2460 (item 7)
…fixed width in groups of 64 at rank 2
a8d6385 to
c84e442
Compare
|
Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
|
Thanks @jschmied! This is in TensorFold 1.2.8, out now: https://github.com/ashhart/TensorFold/releases/tag/v1.2.8
|
What this changes
The first step of item 7 of #2460 ("one loader per weight format"). Before: five Metal loaders each built
{module}.weight,.scalesand.biasesby hand and checked the three shapes in their own way:qwen3_5(
projection, fixed 4-bit g64),lfm2_moe(affine, fixed bits, g64,count 0= rank 2),lfm3(Loader.affine,width from the config), and
minimax_m3andstep5(two identical copies ofcheckedAffine, bits and group read fromthe stored shapes; the MiniMax DFlash2 and DSpark drafters call MiniMax's). After: one reader in
core/quant/mlx_affine.zig:read(checkpoint, module, want)gets the three tensors andcheckrefuses anythingbut u32 words
[.., n, k * bits / 32]and bf16 tables[.., n, k / group]with every byte accounted for.wantgivesthe stack (1 = one projection, rank 2),
n,k, and the bits and group when the family fixes them (otherwise theycome from the shapes, as MiniMax and Step-5 did).
row_projection.affineturns a checked triple into the row kernels'Weightsat the tensors' own buffer offsets. The reader returns views of the checkpoint's tensors, so the bytes thekernels read are the same bytes as before.
Per family, what stays its own:
lfm2_moekeeps its 4-byte offset check;lfm3keeps itstensor()(bounds,alignment, and the
seenset it uses to refuse stray tensors) and takes only the dims from core;qwen3_5keeps itsLineartype; MiniMax's norm check now calls the corerequire(same rule, same error name). Step-5's two loader testsmove to
core/quant/mlx_affine_test.zigunchanged (error nameUnexpectedAffineTensor).Not in this PR (next):
gemma4/weights.zig,spark25/{metal_,}weights.zig,qwen27/checkpoint.zig,nemotron,glm/weights.zig, Flash-Next (load_weights,rows_load,pack*), and the XPU loaders (lfm3/xpu_model.zig) readother forms (deferred tensors, packed caches, host copies) and need their own step. The three affine metadata modules
(
flashnext/affine.zigon safetensors entries +Spec,glm/affine.zigon refs/runs,qwen27/affine.zigonconfig specs) have three different APIs, so merging them is not a byte-equal move; listed as the next PR.
lfm3/weights.zig(header schema) is unchanged:test-lfm3-hostbuilds it as a module rooted infamilies/lfm3, whichcannot import core by path.
atc overlap --json: one shared file with Metal: the 27B verifies 32 rows a pass on pre-M5 Macs #1841 (clean merge).Checks on this head
python3 tools/zig/lean_check.py, on the base and on this head: 116 on the base (5874551), 116 on this head, 0 new.zig build test test-golden test-cuda-host(aarch64host,
-Dnvcc): 612/612 steps, 7581 passed, 0 failed, 63 skipped.zig build -Dfixed-cpu test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths: green.zig build check-native-cuda-host compile-cuda-tests -Dtarget=x86_64-linux-gnu: green.python3 tools/pr_check.py: 0 errors (1 warning: theUnreleased line needs
--pr).zig fmt --check build.zig zigandgit diff --check: clean.New tests (
core/quant/mlx_affine_test.zig, run bytest-quant-geometry-host): Step-5's two cases (expert axes,rank, bytes, packed widths); a fixed width refuses another width's shapes, a table one group too long and fp16
tables;
dimsfor stacks and uneven K; a file-backed safetensors with one module in each family's form (fixed andinferred widths, 2 to 8 bits, groups 32 to 128, stacks) read back with the written shapes, dtypes and byte hashes.
Each fails against a fault planted in a scratch copy: tables one group too long in
dims(5 tests fail), scaleschecked as fp16 (4), inferred bits off by one (3).
CUDA serving: none of the changed code is in the CUDA build;
zig build native -Dsm=121of main and ofthis head in the same worktree path gives the same
tensorfold-native(md5 eeb95dd7 both), so no served CUDA run.Receipt
Main against branch, same loader, on Linux with a recording stand-in for the Metal device (host memory buffers): each
family's real
load()on the local checkpoint, every affine module and tensor dumped (buffer, offsets, n, k, bits,group, expert count and repeat, Wyhash of the bytes the kernels read).
Qwen3.5-2B, MiniMax-M3.1-Flash and Step-5: no checkpoint on this machine. Main's per-module code (copied from
5874551) against the new path on synthetic triples: widths 2 to 8 bits, groups 16 to 128, stacks 1 to 8, n 8 to 40,
K 64 to 512, each as stored and with 39 random planted changes (one dim, a dtype, an offset, a table byte count, a
dropped rank):
checkedAffine(MiniMax, Step-5)lfm2_moeaffineqwen3_5projectionlfm3Loader.affine(stored width / another width)"Only main accepts" is one case alone: a table whose byte count is not its shape's extent, which main's Qwen and
LFM2 readers did not check. The safetensors parser already refuses such a tensor (
end - beginmust equal theshape's bytes), so no checkpoint that loads on main is refused on this head.
Metal, M3 Pro:
zig build test760/760 steps on both, 11654/11746 main and 11696/11788 branch (92 skipped each; +42 =the new reader tests);
test-step5-host+compile-step5-host14/14. Qwen3.5-2B MLX 4-bit served greedy, main ==branch byte-equal on 4 prompts (text and token hash), solo and 4 concurrent. LFM2.5-24B, LFM3, MiniMax and Step-5
have no loadable MLX checkpoint on that Mac (size or quantization): Linux load dumps and unit tests cover them.
Boxes it reaches: the Metal load of
qwen3_5,lfm2_moe,lfm3,minimax_m3(+ DFlash2, DSpark) andstep5.New switch? None.
Environment: TensorFold cfa7c87 on main 5874551; Zig 0.17.0; aarch64 Linux (GB10) for the host checks.
Exactness: the loaders return the same views (offsets, shapes, widths); no kernel or numeric change.
Decode speed before and after: n/a, load-time only. Prompt speed: n/a.
Licence of any new weights or drafter: n/a.
Checklist
The title and description match the head commit, including the receipt and the stack.
New tests fail before the change, pass after it, and skip cleanly without their dependency.
Comments and docstrings are one line. No measurements or history in the source.
No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
Drafted output equals
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.No precision traded for speed. If the bits change, the description says which sums change and why.
Decode and prompt processing are no slower than main at one stream and the widest admitted; with
--drafter, no slower than without it, greedy and sampled