Repository navigation
Spark2.5 on Metal holds its stacked matrices once: spark-x2.6 loads at its size on disk, not 1.6x - #3379
Spark2.5 on Metal holds its stacked matrices once: spark-x2.6 loads at its size on disk, not 1.6x#3379jschmied wants to merge 2 commits into
Conversation
|
The quiet-machine rerun of the one open cell, 4-bit decode at 28.6K, is in. It's level, so the -0.44 % in the description was noise from a busy machine. Same head
No cell is slower now, so I'm withdrawing the description's "shouldn't land until it is settled". I'll update the description to match. |
|
DEVICE. @jschmied, the ownership and stage-reset paths look sound. Native builds, all four Spark runtime libraries and the two new selector cases pass, with no new lean findings. We will run the device check on our side with the same checkpoint: full reply and raw-logit equality for both stored formats, plus loaded allocation, checkpoint residency and peak footprint measured separately. The reported memory saving still needs that receipt. Green zig, zig-windows, pytest and fmt CI at this head is also needed before landing.
|
|
@ashhart CI is green on a9ec9da (zig, zig-windows, pytest, fmt). The Linux zig job failed because |
|
@jschmied DEVICE. The Spark load-memory and exact-token check is still unchecked because its checkpoint is not placed on our test Mac. The new head only adds the two host Metal stand-in guards; it leaves the device test bodies unchanged. The PR’s zig, zig-windows, pytest and fmt results at this head are also unknown in the supplied evidence.
|
|
@jschmied, to settle the note above: CI is green at this head (zig, zig-windows, pytest and fmt), and the new commits only add the two host Metal stand-in guards, so the device code is the version you measured. The Spark x2.6 checkpoints are gated where we fetch from, so we can't rerun the load on our Mac. Your receipts carry the device side: peak GPU memory on main was 1.58x (bf16) and 1.69x (4-bit) the checkpoint's size on disk, this head loads at its size on disk, and replies are equal. This is stacked on #3350, so it lands right after #3350 does.
|
|
Thanks @jschmied. LAND after #3350. The full layout contract and runner registration are now reviewed, including the wrong-layout control and byte comparison with the interleaved path. The ownership paths look sound. The accepted device receipts and green CI still apply; the BF16 memory result is about 8.2 GB, while the 4-bit result is about 1.13 times its disk size.
|
|
I ran the staged-load tests and The branch trial-merges into On both the PR head and the resolved stack, Igor Lessio - AIFLOW LABS |
…rs Gemma 4 now shares Spark2.5's load kept every shard resident and copied q_k_v_proj+g_proj and gate_proj+up_proj into stacks, so spark-x2.6 took 1.6x its size on disk. - core/checkpoint_metal.zig gains laterModules (a Later for the layers' linears by module name), Checkpoint.describe (a kept or deferred tensor's dtype and shape) and Staged (a checkpoint read through a Stage, for any reader that takes get(name)). - Gemma 4's load uses laterModules and describe, with no change in behaviour; it keeps its own host-memory Stage (ashhart#3350), so Staged serves Spark2.5. - Spark2.5 leaves the stacked members in their files, stages them for stacked(), and closes the files after load. out, down, the embedding and the norms stay in place. spark-x2.6-4b now takes 8.2 GB of GPU memory, not 13.
…st targets' Metal stand-ins Gemma 4's and Spark2.5's test blocks now reach weights.zig, which uses checkpoint_metal, so the host-stub test modules (gemma4_recording_metal, gemma4_inert_metal, gemma4_head_metal) compiled its device tests against a Device with no init. They take the same comptime guard gemma4/weights.zig's load test has.
a9ec9da to
c4e0bc9
Compare
|
@ashhart #3350 is on main, so this PR is rebased onto main
The body names the new head. |
|
I checked the rebased On M5 Max (macOS 27.2, Zig 0.17.0), This checks the current loader changes together. Full-checkpoint memory numbers are from the author’s measurements. Igor Lessio - AIFLOW LABS |
Branch
metal-weights-shared@ c4e0bc9 on main99d23848c(rebased from a9ec9da now that #3350 has landed; #3350's commits are main's, only this PR's two remain): it moves #3350'spattern into core and puts Gemma 4 and Spark2.5 on it.
Problem
Spark2.5's Metal load (
metal_model.zig:60,addFile) kept every shard resident for the model's life. Thenmetal_weights.stacked()copiedq_k_v_proj+g_projandgate_proj+up_projinto new buffers, and nothingread the originals again. Measured on main with
footprint, peak GPU memory against the safetensors bytes on disk:1.58x for spark-x2.6-4b (bf16) and 1.69x for spark-x2.6-4b-4bit.
Change
Core (
core/checkpoint_metal.zig), shared:laterModules(comptime modules): aLaterpredicate for the layers' linears whose module name ends in one ofmodules, matching their weight, scales and biases.Checkpoint.describe(name): dtype, shape and size of a tensor, kept or deferred. This was Gemma's privateSrc.meta.Staged: a checkpoint read through aStage. It is anything withget(name), so the existing duck-typedreaders (
mlx_affine.read, Spark'sdense) take staged tensors unchanged.Gemma 4 (no behaviour change):
weights.later = laterModules(&.{"_proj"}), andSrc = Staged. The 12B'sreplies equal #3350's in every case below, at the same 7.0 GB of GPU memory.
Spark2.5 (family-only):
later = laterModules(.q_k_v_proj, .g_proj, .gate_proj, .up_proj)andaddFileSplit(..., later).Staged;stacked()copies them once, then thestage resets.
closeFiles()runs after load.out_proj,down_proj, the embedding/tied head and the norms stay in the shards, read in place as before.Evidence (M3 Pro 36 GB, no tensor units; base = #3350's head on main 4ddc28c)
Memory (peak
footprintGPU / phys, load and a 200-token request):Replies equal base (token_sha256) on both checkpoints:
Speed, A-B-B-A, 2 rounds, a fresh server per run, 128 greedy tokens:
Settled: the cell is level on a quiet machine (6 more A-B-B-A rounds on this head: paired median +0.02 %, 5 of 12 slower, spread 0.4 tok/s; see the comment below). On the busy machine it read as slower: 4-bit decode at 28.6K. Over 6 A-B-B-A rounds (12 pairs), the paired median is -0.44 % (mean -0.59 %), with 7 of 12 pairs slower and one reply hash throughout. Paired deltas: -1.02 +0.93 -1.07 -0.68 -0.92 +0.12 -2.23 +0.63 -3.84 +1.13 -0.21 +0.10 %. The pairs show a position effect: (A1,B1) is slower 6 of 6, and (B2,A2) is faster 5 of 6.
Isolation: the stacked buffers are allocated as before (
device.buffer). The only thing that moves is the in-place tensors' offsets (256-aligned in the packed buffer instead of file offsets). A variant with identical code but every tensor loaded in place, A-B-B-A against this head (4 rounds), is level on means (35.23 vs 35.19 tok/s, -0.1 %; paired median -0.67 %, 5 of 8), within this Mac's +/-6 % single-run spread. We found no mechanism. The quiet-machine rerun is in the comments. If you run your device check, this cell is the one to look at.Checks:
zig build testexit 0, including the newlaterModulesand Sparklaterhost tests.zig fmtclean; all files 600 lines or fewer. No kernel changes.Core vs family
laterModules,Checkpoint.describe,Stagedlater,reserve, staged stacking,closeFilesFlint has the same pattern (
stacked()of qkv and gate|up) but is not changed here: there is no Flint checkpointthat fits a 36 GB Mac to verify it on. It waits for a tester.
x86 CI (Linux,
-Dfixed-cpufull set on 03e5d75): 732/735 steps, 9,887 of 9,967 tests pass. The one failure iscli_config_test, the same as on main on that box (see #3350).tools/pr_check.py: lean 341 vs 341, 0 new.No refusal test: nothing new is refused. Speed and memory are measured against #3350's head (main plus #3350), not against main alone: this PR stacks on it.
Proposed CHANGELOG line
size on disk in GPU memory (8.2 GB, down from 13). Replies and speed are unchanged.
c4e0bc9 (rebase onto main 99d2384 after #3350 landed): only this PR's two commits remain; range-diff
=against a9ec9da's pair exceptgemma4/weights.zig, where #3350 landed its own host-memoryStage: Gemma'sSrcstays on that localStageand the three sizing calls readr.c.describe(...)(the resolution AIFlowML described). Mac (M3 Pro,-Dcpu=apple_m1): native build ok; focused steps (staged copies, gemma4 load-parts/shared/contract, checkpoint consumers/identity, step5,test-spark25-host) 241/241 tests;tf-kernel-contract'metal gemma4 gate|up split rows' 1 pass, wrong-layout control red. Memory, main 99d2384 -> c4e0bc9 (peak, load + 200-token request): BF16 spark-x2.6-4b (8.22 GB on disk) GPU 13.0 -> 8.237 GB, 4-bit (2.31 GB) GPU 3.92 -> 2.603 GB, the same as the rows above. The one red step,test-gemma4-owners-host, is main's own:core/row_projection.zig:209/:212(3bb1cef, #3854) needs a pointeridthat the Gemma inert-Metal fake does not have; it fails the same on main 99d2384. GB10 Linux on c4e0bc9:test-host-ci-linux-{fast,host,compile}, zig rc=0.a9ec9da (CI fix): the Linux
zigjob failed to compilecore/checkpoint_metal.zig's two device tests (:397,:427) against the host-stub Metal modules that_ = weights;now pulls in (Devicehas noinitthere). Both tests now open withif (comptime !@hasDecl(mtl.Device, "init")) return error.SkipZigTest;, the guardgemma4/weights.zig's load test already uses. No other change; the speed and memory rows above are on 03e5d75 and the served path is the same code. GitHub CI on a9ec9da: zig, zig-windows, pytest, fmt pass. x86 full set on a9ec9da: the only failure iscli_config_test, as on main on that box. Mac suite 853/853 steps.