ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

metal: restore streamed-expert mlock pinning (fixes #532)

Open gilbert-barajas opened this issue 4 months ago • 1 comments

The "Add GLM 5.2 runtime support" refactor dropped all mlock pinning of streamed experts — the mlock calls, slab_lock_slot/unlock_slot, and the budget cap all went to zero. Without pinning, in the streaming regime (model ≫ RAM) the OS evicts the expert cache under memory pressure and every token re-streams from disk. That's the DeepSeek-Flash decode regression in #532 (~2–3× slower, ~6s first-token).

This ports the pinning back, adapted to the refactored slab allocator (slab_backed/slab_slot, 3 gate/up/down buffers per entry): whole-buffer mlock at alloc, per-slot lock on fill / unlock under relief, the routed-memory budget cap for graceful degradation, cap-on-failure, and a relief routine that unpins the coldest ~10% of victims under pressure. +382/−1, builds warning-free.

Bench (M5 Max 128 GB, DeepSeek-V4-Flash IQ2XXS, --simulate-used-memory 72 to force the streaming regime):

build gen t/s first-token
before (broken) 0.3–2.5 ~6.0 s
after (this PR) ~7.0 ~1.7 s
baseline 80ebbc3 ~6.1 fast

Recovers past baseline — the 427e281 kernel work pays off once experts stay resident.

Fixes #532

gilbert-barajas avatar Jul 20 '26 16:07 gilbert-barajas

I can confirm this dramatically improves memory management on the new ds4f-mxfp4 branch.

thombles avatar Aug 02 '26 00:08 thombles