Skip to content

GLM loader: the checkpoint's cached pages are dropped before its reads - #600

Merged
ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:glm-drop-cached
Oct 10, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
BobClawblaw:glm-drop-cached

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

The small change you invited in #583: before the GLM loader reads the checkpoint, it drops each shard's pages from the file cache.

What changes

weights.dropCached maps each shard read-only and shared, calls msync(MS_INVALIDATE), and unmaps it. That needs no root, unlike purge. It also counts the pages that were cached (mincore), and the engine logs the amount, for example dropped 73.3 GiB of the checkpoint from the file cache before the load (2.95 s). If the drop fails, the load goes ahead and the log says so. A host test checks that a read file's pages are dropped and counted once.

On every load, not only past 70%. Your #583 reply expected a default load to have enough room. Measured, it doesn't once the checkpoint is warm. paced_read waits while vm.page_free_count is under 8 GiB, and the cached copy occupies the free pages the load needs. The kernel reclaims those clean pages only as far as its own free target, which is not above the floor.

Measurements

M5 Ultra 256 GB, macOS 27.0.1, GLM-5.3-Flash-MLX-4bit-MTP @ d2b17f52, --parallel 1 --context 8192, default limit (176.4 GB without the MTP head). Each run has memguard watching (kill under 6 GiB free, +12 GB swap or +24 GiB compressor) and a 300 s cap. "Warm" means the shards were read with cat until 73.3 GiB were cached, about what a pull leaves.

run cached before served truly free pages, lowest compressor peak
main, cold 0 17.5 s 8.1 GiB 3.0 GiB
main, warm 73.3 GiB not within 300 s 7.1 GiB 3.0 GiB
this PR, warm 73.3 GiB → dropped in 2.9 s 19.5 s 13.2 GiB 3.0 GiB

In the warm run on main, file-backed memory stayed at 117 GiB throughout and the readers slept in paced_read. With the drop, file-backed memory falls from 113 to 40 GiB within 4 s of startup. Pressure stayed normal in every run and nothing swapped.

One more finding for you, not changed here: the pacing floor

Late in a large load, truly free pages settle around 7.2 to 8.4 GiB on this Mac, which looks like the kernel's free target. paced_read's floor is max(8 GiB, 3% of RAM) = 8 GiB, right in that band. So whether the last reads go ahead or wait 10 s each is close to a coin flip:

  • Main cold, GLM_LOAD_LIMIT_GB=200 --context 32768 (180.6 GB with the MTP head): settled at 8.0 to 8.4 GiB and served in 21.6 s.
  • The same load warm with the drop (nothing cached after the first 4 s): settled at 7.2 to 7.9 GiB and did not serve within 300 s. A run before that stalled the same way past 9 minutes.

With free + inactive at about 128 GiB, the kernel had plenty to reclaim, but nothing makes it reclaim above its target. Possible fixes are a floor below the kernel's target (for example, the 3% without the 8 GiB minimum), or counting inactive pages, or waiting only while the memory pressure level is raised. Which one fits is your call, since e82ab69 is yours; I can send whichever you prefer.

The shards are read with F_NOCACHE, which keeps the load's reads out of the
file cache but evicts nothing an earlier pull or read left there. Each
shard's cached pages are dropped first (msync MS_INVALIDATE over a read-only
shared mapping, no root), and the log says how much was cached. On every
one-Mac or paired load, not only past 70% of RAM: the paced reads wait while
free pages are under 8 GiB, and a cached copy holds the free pages the load
needs, so with 73 GiB of the checkpoint cached a default load (176 GB,
--parallel 1 --context 8192, M5 Ultra 256 GB) did not serve in 300 s; with the
drop (73.3 GiB in 2.9 s) it serves in 19.5 s, as a cold load does (17.5 s).
@ashhart ashhart mentioned this pull request Oct 10, 2026
@ashhart
ashhart merged commit 27f77c5 into ashhart:main Oct 10, 2026
1 of 2 checks passed
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thanks, @BobClawblaw. This is in as a merge of your head, so it shows as merged. One commit of ours sits on top: the drop and its test moved from weights.zig into page_cache.zig (weights.zig was 608 lines with them, over our 600-line limit), and the doc comment is one line. No code changed.

We checked it on two M5 Ultras, each Mac loading its 93 GB half, with all 169 GiB of the checkpoint in each Mac's file cache. Main never came up in 309 seconds. One Mac started with under 6 GiB free and its paced reads waited from the first one. The other loaded 56 GiB, then sat at 7-8 GiB free, right at the 8 GiB floor, as you found. With your change the log said "dropped 169.2 GiB of the checkpoint from the file cache before the load (3.2 s)" and the pair served in 16 seconds. Cold, it served in 14 seconds against main's 11, with the same free-page minimum and the same 99 GiB peak. Replies on six prompts were identical to main's.

Two costs we measured, in case you want to take them on. The pass takes about 2.4 s even when nothing is cached. It also drops pages a load could have used: after any load about 37 GiB of the checkpoint stays cached, and main's next pair load uses them (ready in 9 s, against 14 s with the drop). Dropping only when free pages can't hold the load plus the pacing floor, and skipping msync on a shard whose mincore count is zero, would avoid both. Your note on the floor sitting inside the kernel's free target matches what we saw, and we'll follow up on it.

AdrianBinDC pushed a commit to AdrianBinDC/TensorFold that referenced this pull request Oct 10, 2026
…line comments

The drop and its test move from weights.zig (608 lines with them) to page_cache.zig, so weights.zig is
back under 600 lines, and the drop's four-line doc comment folds to one. No code changes.
AdrianBinDC pushed a commit to AdrianBinDC/TensorFold that referenced this pull request Oct 10, 2026
AdrianBinDC pushed a commit to AdrianBinDC/TensorFold that referenced this pull request Oct 10, 2026
AdrianBinDC pushed a commit to AdrianBinDC/TensorFold that referenced this pull request Oct 10, 2026
@BobClawblaw

Copy link
Copy Markdown
Contributor Author

Data for the follow-up you mentioned on the floor sitting inside the kernel's free target: a load that stalled on it.

Setup: one M5 Ultra (256 GB), 1.0.5 with a downstream patch that wires every weight buffer before the reads (the GLM_WIRE of #581), the full model (180.6 GB), --context 1048576. Other programs on the Mac held about 20 GiB of file cache (a node's data files and the like).

What happened:

  • Before the load: 198.1 GiB in vm.page_free_count, 20.2 GiB file-backed.
  • The buffers wired (~200 GiB), then the reads began. 5 minutes later the log was still on glm plan. 4.0 GiB free pages, 23 GiB file-backed, pressure normal, swap flat, 9% CPU. Nearly every paced_read.read sat out waitForRoom's 10 s: free pages stayed under the 8 GB floor because the kernel had no reason to free more.
  • The kernel's own targets on this Mac: vm.vm_page_free_target 4000 pages and vm_page_free_min 3500, which is 62.5 MiB and 55 MiB at 16 KiB pages. The floor is 8 GB, so free pages can settle anywhere between the two and the reads wait.
  • Downstream I skip the wait when the destination is already wired, because those reads commit no new page. Same state, the load took 22 s, with 3.4 GiB free pages and 21.3 GiB file-backed after, and replies equal to the earlier run's.

For main: I haven't reproduced it unwired. Main's reads commit pages as they go, so free pages fall during the load, and with other programs' file cache on the Mac they can land between the kernel's target and the floor the same way. A free count that includes what the kernel reclaims on demand (inactive and purgeable pages, or file-backed pages not in use) might be the closer measure of room. #640 uses the same floor and freeBytes to decide how much to drop, so whatever the floor becomes, #640 can follow it.

BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Oct 11, 2026
…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.
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 11, 2026
…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.
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
…line comments

The drop and its test move from weights.zig (608 lines with them) to page_cache.zig, so weights.zig is
back under 600 lines, and the drop's four-line doc comment folds to one. No code changes.
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
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…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.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…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.
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