Skip to content

feat(moe): size each residency option and fit the largest that admission accepts - #3676

Closed
beatakouchnir wants to merge 1 commit into
jundot:mainfrom
beatakouchnir:feat/moe-offload-budget-fit
Closed

beatakouchnir wants to merge 1 commit into
jundot:mainfrom
beatakouchnir:feat/moe-offload-budget-fit

Conversation

@beatakouchnir

@beatakouchnir beatakouchnir commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #2595 (expert offload primitive) and #3628 (V4.1 fit_resident_fraction).

Problem

The residency field accepts 5–95 percent with no indication of what a value costs in memory or whether admission will accept it. Users map their machine to a fraction by hand, or find out at load time. A fit existed only for the DeepSeek V4.1 adapter, and only as a library call.

Change

  • moe_expert_offload.py: the header scan behind estimate_offload_admission_bytes is factored into a cached layout (keyed on the config and shards' stat signature) plus pure arithmetic; the estimate's contract is unchanged, and mtp_resident (from feat(moe): Lightning MTP coexists with DeepSeek V4.1 expert offload (resident DSpark draft head) #3784/feat(moe): keep the GLM-5.3 draft head resident so Lightning MTP coexists with glm5_next expert offload #3841) is part of the layout's cache key, so a resident draft head is never counted as offloadable. New offload_capacity_fractions lists every fraction that maps to a distinct whole-expert capacity, so a chosen value round-trips through the setting exactly, and fit_resident_fraction(model_path, full_size, budget_bytes, *, mtp_resident=False) bisects it for the largest fraction whose estimate fits (1.0 when the resident model fits, None when even the floor does not, as for V4.1). deepseek_v41: capacity_fractions.
  • EnginePool.moe_offload_admission_bytes(entry, settings, fraction) runs _entry_runtime_resident_size on a probe copy of the settings, and fit_moe_offload_fraction(entry, settings, budget_bytes) bisects the adapter's candidates (built with the settings' mtp_enabled) through it, so every adapter (common, V4.1 with either Engram mode, Qwen4-Exp with the PLE table) is sized by the arithmetic admission uses. _entry_runtime_resident_size gains an optional ceiling (default None, the previous behavior) that reaches the PLE and Engram status calls. The fit passes the budget as that ceiling: a resident table that stops fitting is forced to SSD exactly as admission would force it, and the larger fractions are held to its mmap size, which keeps the fitting fractions a prefix. Sizing probes pass log_decision=False to the PLE and Engram status calls, so the admin view's per-poll sizing neither logs a forced offload for a fraction nobody chose nor resets the per-model dedup state from fix(engine_pool): log a forced-offload decision only when it changes #4100.
  • GET /api/models: moe_expert_offload_presets (fraction, bytes, fits per option), moe_expert_offload_fit_fraction and moe_expert_offload_fit_bytes (null/0 when the ceiling is unknown, the model is not in the pool, or nothing fits). A sizing error is logged at debug and leaves the fields empty.
  • Dashboard: under the residency field (feat(admin): bounded percentage field for the MoE expert offload resident fraction #3714) the fit is offered as a clickable hint, Fit to memory: 41.4% resident · ~8.0 GB, that fills the field; when even the routing floor exceeds the ceiling, a one-line notice replaces it. Two i18n keys (English in every locale file, like the existing offload keys). Docs gain a Sizing a residency to your memory section.

Verified

Header-only on cached checkpoints (M5 Max 128 GB, nothing loaded). The 107 GiB budget is the documented Metal working-set limit on a 128 GB machine. Every call took at most 0.04 s.

checkpoint size budget fit resident at fit
mlx-community/gemma-4-26b-a4b-it-4bit (128 experts) 15.0 GiB 8 GiB 53/128 = 0.4141 8.0 GiB
same 6 GiB 31/128 = 0.2422 5.9 GiB
mlx-community/OLMoE-1B-7B-0125-Instruct-4bit (64 experts) 3.8 GiB 2 GiB 29/64 = 0.4531 2.0 GiB
mlx-community/Qwen3.8-Flash-Next-4bit (512 experts) 109.1 GiB 107 GiB 1.0, PLE forced to SSD 77.8 GiB
same 64 GiB 416/512 = 0.8125 63.9 GiB
Jundot/DeepSeek-V4.1-Flash-oQ3e-mtp (384 experts) 347.4 GiB 107 GiB 158/384 = 0.4115 106.6 GiB
same 64 GiB 87/384 = 0.2266 63.6 GiB

The V4.1 row reproduces the 41% the docs quote for fit_resident_fraction at 107.5 GiB. The Flash-Next row is the non-monotone case: at 75% the PLE table is resident (90.6 GiB), at 100% the resident estimate exceeds the ceiling, the table is forced to SSD and the model fits at 77.8 GiB. For gemma and OLMoE the pool's fit equals the adapter-only fit at every budget.

Tests

  • test_moe_expert_offload.py::TestBudgetFit: candidates are whole experts in some container (a stacked 32-expert and a per-expert 16-expert container in one checkpoint); at every candidate's estimate and one byte below it, the fit is the largest candidate whose estimate fits; dense checkpoint and a missing path fit only at the full size; the layout cache follows a checkpoint edit; with mtp_resident the draft head's container is neither discounted nor a candidate, even after the cache was populated without the flag; the pool's admission and fit equal the adapter's at every candidate, and the probe never edits the caller's settings.
  • test_deepseek_v41_moe_offload.py: capacity_fractions, and the pool's fit equals the adapter's SSD fit at every capacity's size and one byte below it, in both Engram modes; after the fits, no forced-offload decision has been recorded.
  • test_moe_offload_compat.py: the payload with a ceiling (sizes, fits, fit, the ceiling passed to every call), without one, and on a sizing error.
  • moe_expert_offload_ui.test.cjs: the fit hint's label, apply and no-fit states and its markup.

338 passed across the offload, offload-compat, engine-pool and Qwen4-Exp residency suites on the current base; the UI test passes; ruff is clean on the changed lines; new code is black-formatted, existing files were not reformatted.

Not in this PR

An "auto" residency that resolves to the fit at load time. The pieces are here; it needs a settings-schema decision (sentinel fraction or separate flag) and the native app's DTO.

🤖 Generated with Claude Code

@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-budget-fit branch 5 times, most recently from 0532fc5 to 9c33d0d Compare September 21, 2026 15:53
@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-budget-fit branch from 9c33d0d to 81b146f Compare September 24, 2026 04:24
@beatakouchnir

beatakouchnir commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor Author

A few changes since this was opened:

UI now follows #3714. The residency control is now the percentage field, so the per-option size labels and the "Fit to memory" dropdown entry in the original description are gone. The fit appears as a clickable hint under the field (Fit to memory: 41.4% resident · ~8.0 GB), and clicking it fills the field. When even the routing floor exceeds the ceiling, a one-line notice replaces it. That leaves two i18n keys, not three. /api/models still reports the 12.5/25/50/75 sizes in moe_expert_offload_presets, and the docs section describes the field.

Resident MTP draft head (#3784, #3841). The cached layout scan behind estimate_offload_admission_bytes now takes mtp_resident and includes it in the cache key, so a layout scanned without MTP is not reused for a model loaded with it. offload_capacity_fractions, fit_resident_fraction and the pool's candidate list pass the flag through (from mtp_enabled in the settings). With Lightning MTP on, the fit therefore does not count the draft head as offloadable. test_resident_draft_head_is_not_offloadable populates the cache without the flag first, so it fails if the flag is left out of the key. The V4.1 path already took it through _plan.

Separate commit, fixes main: tests/moe_expert_offload_ui.test.cjs fails on main with ReferenceError: selectedModel is not defined, because #3784's :disabled expression reads selectedModel.moe_offload_allows_mtp and the test's scope does not define it. 81b146f8 adds it.

@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-budget-fit branch from 81b146f to 4acc499 Compare September 25, 2026 00:24
@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-budget-fit branch from 4acc499 to 525cbde Compare September 27, 2026 00:42
@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-budget-fit branch 2 times, most recently from 8634ad5 to 7755be6 Compare October 6, 2026 04:54
@beatakouchnir

Copy link
Copy Markdown
Contributor Author

Rebased onto cd6bf3e. Since #4100, the PLE and Engram status calls record and log forced-offload decisions; the fit and per-option sizes call them at hypothetical fractions on every admin poll, so sizing probes now pass log_decision=False (V4.1 fit test asserts nothing is recorded). The separate UI-test commit is dropped; main fixed that test.

@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-budget-fit branch 2 times, most recently from e23ae21 to 1012212 Compare October 9, 2026 18:40
@beatakouchnir

Copy link
Copy Markdown
Contributor Author

Rebased onto c0b1056. #4396 removed the OMLX_MOE_EXPERT_OFFLOAD kill switch, but this PR's offload_capacity_fractions still checked it, so I removed that check and the test case that set it; offload is now disabled only through model settings, as on main. The UI and docs changes follow #4359 and the docs reorganization to apps/omlx-web/ and docs/experimental/.

@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-budget-fit branch from 1012212 to 293e0c0 Compare October 11, 2026 18:10
@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-budget-fit branch from 293e0c0 to a065962 Compare October 19, 2026 04:18
…ion accepts

The residency selector offered four fractions with no indication of what
each costs or whether it fits; users mapped their memory to a fraction by
hand, or found out at load time. DeepSeek V4.1 already had
fit_resident_fraction, but only as a library call and only for that adapter.

- moe_expert_offload: the header scan behind estimate_offload_admission_bytes
  is factored into a cached layout (keyed on the shards' stat signature) plus
  pure arithmetic; the estimate's contract is unchanged. New
  offload_capacity_fractions (every fraction that maps to a distinct
  whole-expert capacity, so a chosen value round-trips through the setting)
  and fit_resident_fraction (largest one whose estimate fits a budget).
- deepseek_v41: capacity_fractions for the same purpose.
- EnginePool.moe_offload_admission_bytes and fit_moe_offload_fraction run the
  pool's own _entry_runtime_resident_size on a probe copy of the settings, so
  every adapter (common, V4.1, Qwen4 with PLE) is sized by the arithmetic
  admission uses. The budget is threaded through as the ceiling the SSD
  fallbacks decide against, which keeps the fitting fractions a prefix.
- /api/models reports moe_expert_offload_presets (bytes and fits per option),
  moe_expert_offload_fit_fraction and moe_expert_offload_fit_bytes against the
  admission ceiling; the dashboard annotates each option with its size, marks
  the ones over the ceiling, and offers the fit as an option when it is not
  one of the presets.

Tests pin the adapter fit to the estimate over every candidate, the pool to
the adapter (common and V4.1, both Engram modes), the route payload with and
without a ceiling and on a sizing error, and the dashboard labels.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@jundot

jundot commented Oct 26, 2026

Copy link
Copy Markdown
Owner

@beatakouchnir The fit in this PR picks the largest residency whose estimate fits under the load ceiling, so the suggested value always lands right under that ceiling. I ran it on an M3 Ultra with the ceiling held at 12 GiB (balanced tier). For gemma-4-26b-a4b 4bit and Qwen3.6-35B-A3B 4bit the suggested 74.2% and 57.8% were refused at load, because admission also counts the server process. Where it does load, the weights sit above the prompt line (ceiling x tier headroom), so prompts get a 400.

I opened a smaller version in #4858 with you as co-author on the commit. It runs the same admission arithmetic through _entry_runtime_resident_size with your log_decision probe flag, but the target is the prompt line minus about 4 GiB, after the hot cache and other loaded models. A PLE or Engram table stays resident when a lower percent fits that way. On the same 12 GiB setup it suggests 33% and 28%, and both models served a 16K-token prompt. The hint is fetched when the settings modal opens, not on every /api/models poll.

I'm not merging the PR itself because the fix changed the core of the fit. Once the target moved, the per-option preset sizes, the adapter-level bisect and the cached layout in the admission estimate were no longer needed, and asking you to remove all of that after this sat open so long didn't seem fair. The delay is on me, sorry about that.

The sizing work from #2595 and #3628 is what made this small, and keeping this branch rebased through all the churn took real effort. Thanks for that. Closing this in favor of #4858.

@jundot jundot closed this Oct 26, 2026
@beatakouchnir
beatakouchnir deleted the feat/moe-offload-budget-fit branch October 26, 2026 18:12
@beatakouchnir

Copy link
Copy Markdown
Contributor Author

Thanks @jundot, and no need to apologize. Targeting the prompt line instead of the load ceiling is the right fix: the fit rows here only checked that admission accepted the load, never that a prompt still fit afterward. Glad the sizing pieces were useful in #4858.

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