Skip to content

GLM loader: the cached-page drop covers only what free memory lacks, and skips shards with none cached - #640

Merged
ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:glm-drop-only-deficit
Oct 11, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:glm-drop-only-deficit

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

Follow-up to #600, the two costs you measured there.

What changes

  • page_cache.dropForLoad(gpa, dir, load_bytes): the drop runs only when free memory (vm.page_free_count) can't hold the load (plan_bytes + e.arena.bytes) plus the paced reads' floor (3% of RAM, at least 8 GB). Then it drops shard by shard, in the index's order, until the shortfall is covered, and stops. The rest of the checkpoint stays cached for the reads.
  • A shard whose mincore count is zero is not invalidated (msync walks every page of the mapping either way).
  • paced_read.floor and freeBytes are public so the drop uses the same floor the reads wait on.
  • The log line names what was dropped and the shortfall: dropped 75.9 GiB of the checkpoint from the file cache, the 74.3 GiB free memory lacked for the load (1.46 s). Nothing is logged when nothing is dropped.

Measured

One M5 Ultra (256 GB), GLM_LAYERS=23 (93 GB, about one Mac's half of the pair), --context 131072 --parallel 1, main 56150e6 against this branch, two rounds each. Ready is from process start to /v1/models answering; the cached amounts are mincore over the shards before and after.

state before the load main this branch
nothing cached 11.0-11.6 s (the drop pass 2.58-2.60 s) 7.9-9.0 s, no pass
the whole checkpoint cached (169.2 GiB) 11.6 s, all 169.2 GiB dropped (3.21 s), lowest free 25 GiB 9.5 s, 75.9 GiB dropped for a 74.3-74.4 GiB shortfall (1.46-1.48 s), 93.3 GiB stays cached, lowest free 14 GiB
what that load left: 89-93 GiB cached 11.6-12.0 s, all of it dropped (2.94 s) 8.4 s, nothing dropped, the cache kept, lowest free 14-15 GiB

Every load's reply to the same prompt was identical to main's. Free memory never went under the 8 GB floor; a memory watchdog (kill under 6 GiB free) never fired. One Mac's loads leave nothing cached by themselves (F_NOCACHE reads), so "after a load" on one Mac only differs from cold when an earlier drop kept pages, as above.

Not measured here: the pair. Its two halves read different shards, so which shards the drop takes first matters more there than on one Mac; the index's order is what this uses.

Checks

zig build test passes; page_cache's test now covers a drop of nothing wanted (no shard touched, the pages stay cached), a full drop (dropped equals cached), and a second pass over nothing cached (counted, not invalidated). zig fmt --check zig/src clean; engine.zig stays at 587 lines.

…and skips shards with none

ashhart#600 dropped every cached page of the checkpoint before each load, and msync walked every shard: about 2.5 s
with nothing cached, and a later load lost the pages it could have read. page_cache.dropForLoad now drops only
when free memory can't hold the load plus the paced reads' floor, shard by shard until the shortfall is covered,
and invalidates no shard whose mincore count is zero. paced_read's floor and freeBytes are public for it.
@BobClawblaw
BobClawblaw force-pushed the glm-drop-only-deficit branch from 5254014 to ac47e1c Compare October 11, 2026 01:26
@ashhart
ashhart merged commit 1ea2ff3 into ashhart:main Oct 11, 2026
2 checks passed
@ashhart

ashhart commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Thank you, @BobClawblaw. This is in 1.0.6 (1cdabd406, plus our commit f33e849b1, which makes the two new doc comments one line each). We measured the case you couldn't: the pair. Two M5 Ultras, A B B A (main, this PR, this PR, main), with the whole 169.2 GiB checkpoint cached, then what that load left, then nothing cached:

state before the load main this PR
whole checkpoint cached each Mac drops 169.2 GiB (3.2 s); ready 14-20 s rank 0 drops 79.9-99.9 GiB for a 78-96 GiB shortfall, rank 1 36.0-40.0 GiB (0.7-1.9 s); ready 12-18 s
what a load left (36.6 GiB a Mac) drops all of it (2.6 s); ready 14 s no pass; ready 10 s
nothing cached a 2.4 s pass that drops nothing; ready 14-15 s no pass; ready 11 s

All six prose replies were identical in all 12 arms. Wired memory peaked at 98-100 GiB a Mac in both builds. Free pages went lower with your change (the cache it keeps is clean and the kernel reclaims it), and no load stalled.

jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
…free memory lacks, and skips shards with none cached

Thanks @BobClawblaw.
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
@BobClawblaw
BobClawblaw deleted the glm-drop-only-deficit branch October 11, 2026 20:35
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…free memory lacks, and skips shards with none cached

Thanks @BobClawblaw.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
The author rebased ashhart#640 onto main after our merge; this keeps the PR's current head in the history with no change to the tree.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants