Skip to content

feat(deepseek_v41): opt-in per-forward expert-offload statistics - #4991

Closed
cropduster wants to merge 1 commit into
jundot:mainfrom
cropduster:feat/dsv41-offload-stats
Closed

cropduster wants to merge 1 commit into
jundot:mainfrom
cropduster:feat/dsv41-offload-stats

Conversation

@cropduster

@cropduster cropduster commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Problem

With MoE expert offload, a DeepSeek-V4.1 forward is a serial chain per layer: routing sync, expert reads, install, experts. The per-request hit/miss counters the scheduler logs since #4240 say how often experts are missing, but not where a forward's time goes, and not how prefill forwards differ from decode. #3910 exposes the cumulative counters through /v1/models/status; this PR is complementary and does not touch that path.

Change (omlx/patches/deepseek_v41/moe_offload.py, opt-in)

OMLX_DSV41_OFFLOAD_STATS=1 in the server's environment (read at model load; off by default) makes the V4.1 adapter log one line per prefill forward and one per decode window:

DSV41-OFFLOAD prefill req=4 fwd=3 wall_s=14.20 chunks=100 routes=134040 distinct=8614 hits=3787 misses=4827 read_gb=63.18 read_bytes=63178260480 wait_s=6.44 sync_s=0.02 ensure_s=10.90 lo_misses=3450 lo_distinct=5475 hi_misses=1377 hi_distinct=3139 hi_routes=15360
DSV41-OFFLOAD decode req=0 win=1-16 steps=16 reason=window rows_per_step=2.62 misses_per_step=71.50 miss_per_kroute=113.5 hits=6089 misses=1144 read_mb=14993.3 wall_ms_per_step=281.2 wait_ms_per_step=161.9 sync_ms_per_step=1.0 ensure_ms_per_step=168.3 tolist_ms_per_step=74.3 lo_misses=639 hi_misses=505

Both lines are from a real run: the prefill line is a full forward, the decode line the first window after a cold load.

  • wait = blocking wait on expert reads, sync = GPU drain before in-place writes, ensure = whole residency update, tolist = routing sync.
  • Decode windows: forwards 1–16, 17–64, then every 256 after a request starts. A pause > 2 s starts a new request; a small daemon thread flushes the window of a request that went idle.
  • lo/hi separate the layers that run on the whole prefill chunk from those that run on the CED decoder tail.

Off, no collector exists (stats is None) and the hot path is unchanged. On, each layer adds a few counter updates per call; expert selection and arithmetic are untouched.

Why it is useful

These lines produced the measurements behind #4951 and #4959. On an M4 Max 128 GB (DeepSeek-V4.1-Flash-1017-oQ2.5e-mtp, 131/384 resident, MTP on), e.g.:

wall read wait read
full 8K prefill forward (before #4959) 36.9 s 15.5 s 108 GB
same with #4959 32.0 s 8.7 s 79 GB
steady decode, per MTP cycle 167 ms 79 ms (+ 53 ms routing sync) ~50 misses

They also showed that the OS page cache does not help (disk reads ≈ read_bytes) and that each prefill forward has ~18 s of fixed read cost regardless of length.

Tests

tests/test_deepseek_v41_offload_stats.py (17): off by default and the environment switch; exact counters and window boundaries on a fake clock; idle flush; lo/hi split; outputs and slot hit/miss counters identical with statistics on and off. tests/test_deepseek_v41_moe_offload.py, test_deepseek_v41_offload.py, test_moe_expert_offload.py: 136 passed; the 13 failures on this machine fail identically on bare main (the installed app bundle lacks mlx_vlm.models.d1_lfm3_vl). Independent of #4959; the two merge without conflict in either order.

Developed with Claude Code; I reviewed and measured it on my machine.

With MoE expert offload, a DeepSeek-V4.1 forward spends its time in a few
serial phases per layer: the routing sync, the expert reads, the install,
the experts themselves. The per-request hit/miss counters the scheduler logs
since jundot#4240 show how often experts are missing, not where a forward's time
goes or how prefill and decode differ.

With OMLX_DSV41_OFFLOAD_STATS=1 in the server's environment (read at model
load, off by default), the V4.1 adapter logs one line per prefill forward
and one per decode window:

  DSV41-OFFLOAD prefill req fwd wall_s chunks routes distinct hits misses
      read_gb read_bytes wait_s sync_s ensure_s lo_/hi_ misses, distinct
  DSV41-OFFLOAD decode req win steps reason rows_per_step misses_per_step
      miss_per_kroute hits misses read_mb wall/wait/sync/ensure/tolist
      ms_per_step lo_misses hi_misses

Decode windows cover forwards 1-16, 17-64 and then 256 each after a request
starts; a pause longer than 2 s starts a new request, and a small daemon
thread flushes the window of a request that went idle. lo/hi split the
layers that run on the whole prefill chunk from those that run on the CED
decoder tail.

Off, the adapter keeps no collector (stats is None) and the hot path is
unchanged. On, each layer adds a few counter updates per call; expert
selection and arithmetic are untouched, and a test checks outputs and the
slot hit/miss counters are identical with statistics on and off.

These lines produced the measurements behind jundot#4951 and jundot#4959, e.g. on an M4
Max (oQ2.5e, 131/384 resident): a full 8K prefill forward 36.9 s wall, of
which 15.5 s blocking read wait for 108 GB; steady decode 167 ms per MTP
cycle, of which 79 ms read wait and 53 ms routing sync.

Tests: tests/test_deepseek_v41_offload_stats.py (17): off by default and
the environment switch, exact counters and window boundaries on a fake
clock, idle flush, lo/hi split, and unchanged outputs with statistics on.

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

jundot commented Oct 31, 2026

Copy link
Copy Markdown
Owner

The per-phase split is a useful way to see where an offloaded forward spends its time, and I can see it produced the numbers behind #4951 and #4959. I'm going to close this one, and I want to explain why.

I don't keep OMLX_* environment switches in the server code anymore. A toggle either becomes a setting users can see or it stays out of the tree, and a profiling logger isn't something I'd expose as a user setting. The PR also adds a watchdog thread and timing hooks around the expert reads, about 500 lines in total, for output that only a profiling session reads.

One thing I noticed while checking it. With CED prefill, a chunk that skips the decoder tail never runs the last MoE layer, so layer_done never closes that forward and several chunks end up in one prefill line with a combined wall_s.

I think this fits best as a local patch for your profiling runs, and the measurements you post on the offload PRs are still what I rely on when reviewing them.

@jundot jundot closed this Oct 31, 2026
@cropduster
cropduster deleted the feat/dsv41-offload-stats branch November 1, 2026 09:59
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