Repository navigation
fix: wait for native dispatch jobs when a MoE layer call raises (#170) - #179
Merged
Merged
Conversation
A device tier raising between moe_disp_start and moe_disp_wait left workers running on the dispatcher's job buffers; the next start reallocated and refilled them under the workers (use-after-free) and a finished stale job overwrote the new job's state, so its result was never computed. - dispatch.c: moe_disp_start first waits for jobs a previous call posted and never collected. - layer.py: a forward that raises after a native start collects the posted jobs before re-raising. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
OffloadedLayer._forwardstarts the native dispatcher (moe_disp_start) before it launches the device tiers and collects the blocking tiers afterwards (moe_disp_wait). If a device tier raised in between (e.g. a GPU OOM), nothing waited: the workers kept running on the dispatcher's job buffers withd->nstill set. The next call'smoe_disp_startthen reallocated (reserve()) and refilled those buffers under the running worker (use-after-free), and the stale worker's finalstate = 2overwrote the new job'sstate = 1, so the new job was never run andmoe_disp_waitsummed the stale output. For the RNGD pipe tier a half-finished exchange could also desync the expert server.Changes
opt_prime/opt_prime/accel/platforms/cpu/csrc/dispatch.c(moe_disp_start, after pass 1, before any job buffer is touched): wait for the jobs a previous call posted and never collected (the spin loop ofmoe_disp_wait, without the sum), reset their state andd->n. The cached.sois keyed by the source hash, so it rebuilds automatically.opt_prime/opt_prime/accel/moe/layer.py:168-179: the device-tier launch loop is wrapped so that, when it raises after a native start, the posted jobs are collected withmoe_disp_waitbefore the exception propagates.opt_prime/tests/test_moe_review.py: two tests with a ctypes callback tier (sleeps 0.3 s on its first call) and a device tier that raises once:test_native_dispatch_is_waited_for_when_a_device_tier_raises: the failed forward returns only after its native job finished; the next forward runs a fresh job and gives the right output.test_native_start_drains_jobs_of_a_call_that_was_not_waited_for: callsmoe_disp_starttwice without a wait, thenmoe_disp_wait; both jobs run and the result is the second job's.Verification
CPU only (
CUDA_VISIBLE_DEVICES=1, no NPU), Python 3.10, torch 2.10.0+cu128, base optimus-rm @253be20.New tests on the unfixed code (from
opt_prime/):With only the
dispatch.cchange, the first test still fails (host.done == 0); with both changes they pass.Whole suite:
tests/test_moe_review.py tests/test_moe_offload.py: 108 passed, 1 skipped.Compatibility and limits
moe_disp_startcosts one load per previously posted job, and nothing in the normal path (whered->n == 0).moe_disp_startnow drains it, so the buffers are never reused under a worker.fix/89-moe-padding-overheadbranch; that branch adds annexpargument tomoe_disp_start, sotest_native_start_drains_jobs_of_a_call_that_was_not_waited_forneeds that argument added when both land.Related Issue
Closes #170
🤖 Generated with Claude Code