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 5 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 |
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 |
|
Thanks @jschmied for reducing Spark2.5's resident checkpoint copies and sharing the checkpoint helpers with Gemma 4. I found one memory-accounting issue. The new persistent stacked-weight buffers are no longer included in checkpoint resident bytes, but the cache-budget calculation still counts only checkpoint bytes plus scratch. It therefore gives the cache memory already occupied by the stacked weights, allowing both the default budget and an explicit load limit to be exceeded. Please include every persistent stacked-weight buffer in the resident-memory total used for that budget. The tensor copies preserve the stored bytes and their ownership looks sound; this finding is limited to the missing memory accounting.
|
|
@ashhart Fixed in
The body names the new head. |
64508cc to
c9a2a50
Compare
|
Thanks for the memory-accounting fix, which closes the review's one issue. Decision: ready for our review and device run, which is ours - an M3/M4-class Mac with Spark2.5, checking the memory ratios (1.00x bf16, 1.13x 4-bit against disk) and the load-line accounting against your posted M3 Pro numbers. The run is ours, not yours. Your host test ( The not-rebased state and the changelog line are ours too.
|
Synthena Initial Review — Performance receipt required (
|
|
New head CI (run 46412884663): the failure is ours. All four failed steps ( [BLOCKER] affine stack bytes: I checked it, and the count is right. [WARNING] [WARNING] Mac: our Mac is offline, so this round has no device run. It is the Receipt: the claim is resident memory at load, not speed. |
Synthena Initial Review — Needs maintainer review (
|
|
This re-review is on the same head,
|
|
Thanks, @jschmied. The byte accounting is right — the bias offset already includes the words and the scales region, so the stacked total is the allocated length — and the two warnings are answered: the close runs only on success with the error path closing through the checkpoint's own deinit and every descriptor set to −1, and the describe call is metadata-only with its callers reading the shape fields, never a file location. The CI failure was the build roots missing the module, fixed in the three host roots. The claim is resident memory at load, so the receipt gate does not apply the way a speed claim's does; the memory ratios are what the device run checks. That run is ours as planned — the load ratios measured against resident memory on the M3 and M4 — and we will post the results here.
|
…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.
…sident The stacked q|k|v|gate and gate|up buffers moved out of the shards, so checkpoint residentBytes no longer held them, and the cache budget (shards plus scratch) handed their memory to the caches: both the default share and SPARK_LOAD_LIMIT_GB could be exceeded. Weights now sums each stack's bytes where its buffer is made, budget.resident adds them, and the load line names shards, stacks and scratch.
gemma4/weights.zig's `later` now calls checkpoint_metal's laterModules, so the owners, affine and prompt-head host roots compile that file's tests, whose file_cache reference reaches `core`. Each root re-exports safetensors and checkpoint_file_cache and is its own `core`, as residency's owners root does.
47b7303 to
8a452ca
Compare
Branch
metal-weights-shared@ 47b7303 on main33e716462(ashhart's rebase c9a2a50 plus one CI fix: Gemma 4's host test roots give checkpoint_metal.zig itscore): 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.Weights.stacked_bytes), and the cache budget counts themas resident:
budget.resident= shards + stacks + scratch. The load line names all three.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.
47b7303 (CI fix, Synthena 12-03):
zig/zig-bfailed atcheckpoint_metal.zig:6:28: no module named 'core'intest-gemma4-owners-host,-affine-hostand-prompt-head-host: Gemma'slaternow callscheckpoint_metal.laterModules, which pulls that file's tests (and itscorereference) into those roots. Each root now re-exportssafetensorsandcheckpoint_file_cacheand is its owncore, as the residency owners root does. x86 targeted on b546bab57 (47b7303 changes only three build-file comments): 73/73 steps, 898/930 tests (32 skipped),check-native-cuda-host compile-cuda-tests21/21, pr_check/fmt/lean 0 new.aarch64-macos -Dcpu=apple_m1cross-build oftf-spark-runcompiles; only the link stops (no macOS SDK here). No device run this round (our Mac is offline).64508cc (review fix, ashhart 11-28): the stacked buffers left the shards, so
residentBytes()stopped counting them and the budget handed their memory to the caches.metal_weights.stacked()now adds each stack buffer's size toWeights.stacked_bytes;metal_budget.resident(model)= shards + stacks + scratch, andnative/spark25.zigpasses it tocaches(). No new allocation; no Gemma 4 file changes, so its numbers stay as they were. New steptest-spark25-budget-metal-host. Mac (M3 Pro,-Dcpu=apple_m1, native md5 78cd070b, old c4e0bc9 f53a061b):metal_budget.zig"the stacked matrices count as resident...": 61/61 withtest-spark25-host. Planted control (old accounting,+ stacked_bytesdropped): red atmetal_budget.zig:63, expected 2021077184, found 1484206272.--context 4096, default limit: c4e0bc923.7 GiB for caches; 64508ccresident 2.726 GB (shards 0.932, stacks 1.384, scratch 0.410), 22.5 GiB for caches(1.29 GiB less = the stacks).SPARK_LOAD_LIMIT_GB=4.181484(shards + scratch + 2 GiB margin + half the stacks): c4e0bc9 loads with 0.6 GiB of caches; 64508cc refuses the load ("--context 4096: this Mac's Spark2.5 caches fit 0 tokens under its load limit").-Dfixed-cputest-host, test, golden, kernel-contract, security, fuzz, release paths: 2314/2314 steps, 22598 tests pass (166 skipped);check-native-cuda-host compile-cuda-tests17/17; pr_check, fmt, lean 0 new.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.