Explore zero-copy mmap-backed expert paging #77
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Idea
Explore a zero-copy, mmap-backed routed-expert paging mode between the current fully mapped/resident path and explicit SSD streaming.
The model GGUF would remain a single read-only mmap. After each layer's router selects experts, Metal would receive page-aligned
newBufferWithBytesNoCopyviews for only the selected(layer, expert)gate/up/down ranges. A bounded cache would retain useful views across tokens; evicted ranges could receivePOSIX_MADV_DONTNEEDafter all GPU work using them has completed.This is an experiment, not yet a commitment to a third production mode. The first goal is to determine whether avoiding
preadcopies improves GLM-5.2 inference without losing the predictable memory bound of the existing streaming cache.Why this may work
Gguf::openalready mmaps the complete model file.newBufferWithBytesNoCopy.StreamExpertTable.For GLM-5.2 the access unit is a layer-specific expert, not a global expert file: the current shape has 79 layers, 3 leading dense layers, 256 experts, and top-8 routing. Splitting the artifact into one file per expert would either create thousands of layer/expert files or load unrelated layers together. GGUF tensor offsets already provide the required random-access ranges.
Non-goals
preadstreaming paths.Phase 1: reproducible baseline
Before changing the loader, record resident and SSD-streaming baselines on the same Mac with the same hashed GGUF, prompt tokens, context, sampling settings, prefill chunk, power setting, cache budget, and background load.
Use at least:
Run sequential repetitions with cooldown. Do not run DS4 and DS4Server simultaneously. Record:
preadbytes, andpreadtime;Define the minimum worthwhile improvement before reviewing spike results. It must exceed measured run-to-run noise; a suggested gate is at least 5% improvement in stable decode throughput or p95 token latency versus
preadstreaming at the same effective working-set budget.Phase 2: narrow mmap expert-view spike
Add one hidden, explicit experimental switch to the existing SSD-streaming path. Reuse its selected-ID handoff, expert table, budget, cache policy, metrics plumbing, and command-buffer lifetime tracking.
For a cache miss:
Ggufmapping alive for longer than every Metal view.POSIX_MADV_DONTNEEDfor pages not shared with another live view.Start without speculative prefetch or a new cache algorithm. If synchronous GPU page faults dominate, test the smallest next step:
WILLNEED/readahead after routing and before binding. Do not add prediction unless profiling proves that ordinary readahead cannot overlap enough I/O.Page alignment is a correctness boundary. Adjacent expert ranges may share a VM page, so an eviction must not advise away a page still referenced by another live view. Group or reference-count only those overlapping pages if the existing layout actually requires it.
Phase 3: correctness and safety gates
Phase 4: performance decision
Repeat the Phase 1 matrix with the mmap-view mode at exactly the same effective cache budget.
Promote the experiment only if all of the following hold:
If it does not pass, keep the current
preadcache and close this idea with the benchmark evidence. A zero-copy path that is merely different but not measurably better is not worth maintaining.Phase 5: production implementation, only after a passing spike
preadstreaming as supported choices.preadbytes so the UI and metrics do not mislabel zero-copy paging as copied streaming.Separate expert files or a new sharded artifact format should be a later issue only if profiling proves that page alignment, Metal VM-region limits, or GGUF tensor layout prevents the single-file approach from meeting the gate.
Likely code areas
src/engine/gguf.rssrc/engine/metal.rssrc/engine/metal/gpu.rssrc/engine/metal/glm.rsnative/metal/ds4_metal.msrc/metrics.rsCompletion
This issue is complete when the spike has produced reproducible A/B evidence and either: