Repository navigation
GLM loader: the checkpoint's cached pages are dropped before its reads - #600
Conversation
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).
|
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. |
…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.
|
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 What happened:
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 |
…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.
…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.
… before its reads
…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.
…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.
…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.
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.dropCachedmaps each shard read-only and shared, callsmsync(MS_INVALIDATE), and unmaps it. That needs no root, unlikepurge. It also counts the pages that were cached (mincore), and the engine logs the amount, for exampledropped 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_readwaits whilevm.page_free_countis 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 withcatuntil 73.3 GiB were cached, about what a pull leaves.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 ismax(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: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.With
free + inactiveat 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.