Skip to content

feat(moe): report expert offload hit/miss counters in model status - #3910

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

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

Conversation

@beatakouchnir

@beatakouchnir beatakouchnir commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #2595 (expert offload primitive).

Problem

Expert offload keeps per-layer hit and miss counters, and moe_offload_stats() aggregates them. Since #4240 the scheduler logs them per request, but nothing exposes them through the API, so a monitoring client cannot see whether a residency is too low for its workload. The DeepSeek V4.1 adapter's counters are also out of reach: they live on OffloadedExpert.slots, which moe_offload_caches() never collects.

Change

  • GET /v1/models/status: each model carries moe_expert_offload_stats, {"layers", "hits", "misses", "hit_rate", "fetched_bytes"} since load (prefill included), or null when the model is not loaded, offload is off for it, or no layer was wrapped. EnginePool.get_status reads it through _moe_offload_stats, which turns any error into null so the status endpoint never fails on it.
  • BaseEngine.moe_offload_stats() walks the model returned by a _moe_offload_model() hook (None by default); BatchedEngine returns _model, VLMEngine returns _vlm_model. The walk runs only when moe_expert_offload_enabled is set for the model.
  • deepseek_v41/moe_offload.py: _ExpertSlots is marked moe_offload_counters = True. moe_offload_caches(model, counters=True) also collects slots carrying that marker, and moe_offload_stats uses it when it walks a model itself. The default walk is unchanged, so materialize_offload_state and perf(moe): faster prefill and decode when offloaded experts stream from SSD #4240's prefill slot release never see V4.1's slots, and the counters the engines hand the scheduler are as on main.

Example (Qwen3.8-Flash-Next-4bit, 50% residency, after one request):

"moe_expert_offload_stats": {"layers": 48, "hits": 92438, "misses": 13502, "hit_rate": 0.8726}

Verified

Measured before the rebase onto #4240, which added fetched_bytes to the counters and moved the cache walk into moe_offload_caches(). Through omlx serve on an M5 Max (128 GB), isolated base path, one cold request per model at temperature 0:

checkpoint engine residency layers hits / misses hit rate
mlx-community/OLMoE-1B-7B-0125-Instruct-4bit batched 25% 16 16,862 / 16,874 0.500
mlx-community/Qwen3.8-Flash-Next-4bit vlm 50% 48 92,438 / 13,502 0.873

Layer counts match the adapter's load log (wrapped 16 layers at 25.0% residency, wrapped 48 layers at 50.0% residency). /v1/models/status answered in 1.2 to 2.0 ms with the Flash-Next walk included.

Tests

  • test_moe_expert_offload.py: the engine method returns the same counters as moe_offload_stats, None with offload off, and None for a model with no wrapped layer.
  • test_deepseek_v41_moe_offload.py: after a load through the V4.1 loader, moe_offload_stats reports every OffloadedExpert layer with hits and misses equal to the sum over their slots (fails without counters=True), and the default moe_offload_caches() returns none of the slots.
  • test_engine_pool.py: get_status reports a loaded engine's counters, null when the engine raises, and null once unloaded.

1,199 passed across the offload (common, V4, V4.1, GLM), engine-pool, scheduler, server, batched and VLM engine, engine-core, compat and admin-template suites on the current base. ruff adds no findings beyond two Optional[Dict] annotations that match the surrounding style of engine/base.py.

Not in this PR

Showing the counters in the admin dashboard, and a misses-per-decode-token figure (it needs the decode token count separated from prefill accesses).

🤖 Generated with Claude Code

moe_offload_stats() existed but nothing outside the tests called it, so a
user could not tell whether a residency was too low for their workload.

- /v1/models/status: each model carries moe_expert_offload_stats (layers,
  hits, misses, hit_rate since load, prefill included), or null when the
  model is not loaded, offload is off, or no layer was wrapped. A failure
  reads as null so monitoring never breaks on it.
- BaseEngine.moe_offload_stats() behind a _moe_offload_model() hook, which
  BatchedEngine and VLMEngine implement; the walk runs only for models with
  offload enabled.
- DeepSeek V4.1 kept its counters on OffloadedExpert.slots, which the walk
  never reached; _ExpertSlots is now marked moe_offload_counters, and
  moe_offload_stats walks with moe_offload_caches(counters=True). The default
  walk still skips the slots, so materialize_offload_state and the prefill
  slot release never see them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@beatakouchnir
beatakouchnir force-pushed the feat/moe-offload-stats branch from 7f1c271 to 74d3190 Compare October 7, 2026 06:36
@beatakouchnir

Copy link
Copy Markdown
Contributor Author

Rebased onto 57fda05. #4240 moved the cache walk into moe_offload_caches(), which the slot release and materialize_offload_state also use, so V4.1's slots stay out of it by default; moe_offload_caches(model, counters=True) collects them, and moe_offload_stats walks with it when called on a model. The status field now carries fetched_bytes from #4240's counters. The V4.1 test also asserts the default walk returns none of the slots.

@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because it has merge conflicts and no new commits for over 30 days. This isn't a judgment on the change. If you'd like to continue, rebase on current main and open a new PR. See #4256 for the policy.

@github-actions github-actions Bot closed this Nov 30, 2026
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.

1 participant