Skip to content

fix: wait for native dispatch jobs when a MoE layer call raises (#170) - #179

Merged
gspark-etri merged 2 commits into
optimus-rmfrom
fix/170-dispatch-drain
Oct 23, 2026
Merged

gspark-etri merged 2 commits into
optimus-rmfrom
fix/170-dispatch-drain

Conversation

@gspark-etri

Copy link
Copy Markdown
Collaborator

Background

OffloadedLayer._forward starts 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 with d->n still set. The next call's moe_disp_start then reallocated (reserve()) and refilled those buffers under the running worker (use-after-free), and the stale worker's final state = 2 overwrote the new job's state = 1, so the new job was never run and moe_disp_wait summed 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 of moe_disp_wait, without the sum), reset their state and d->n. The cached .so is 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 with moe_disp_wait before 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: calls moe_disp_start twice without a wait, then moe_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/):

TORCH_DEVICE_BACKEND_AUTOLOAD=0 CUDA_VISIBLE_DEVICES=1 PYTHONPATH=$PWD python3 -m pytest -q tests/test_moe_review.py -k "native_dispatch_is or drains"
E  AssertionError: the failed call returned while its native job was still running   (assert host.done == 0 == 1)
E  assert 1 == 2   (tier.calls: the second start's job never ran)
2 failed

With only the dispatch.c change, the first test still fails (host.done == 0); with both changes they pass.

Whole suite:

TORCH_DEVICE_BACKEND_AUTOLOAD=0 CUDA_VISIBLE_DEVICES=1 PYTHONPATH=$PWD python3 -m pytest -q tests
before (253be20): 400 passed, 23 skipped, 135 subtests passed
after:            402 passed, 23 skipped, 135 subtests passed

tests/test_moe_review.py tests/test_moe_offload.py: 108 passed, 1 skipped.

Compatibility and limits

  • No API change. The drain in moe_disp_start costs one load per previously posted job, and nothing in the normal path (where d->n == 0).
  • An exception raised elsewhere between start and wait (outside the device-tier loop) is not collected in Python, but the next moe_disp_start now drains it, so the buffers are never reused under a worker.
  • The native tier's error code is ignored on the exception path; the device tier's exception is the one raised.
  • Merges cleanly with the local fix/89-moe-padding-overhead branch; that branch adds an nexp argument to moe_disp_start, so test_native_start_drains_jobs_of_a_call_that_was_not_waited_for needs that argument added when both land.

Related Issue

Closes #170

🤖 Generated with Claude Code

gspark-etri and others added 2 commits October 23, 2026 09:59
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>
@gspark-etri
gspark-etri merged commit 77892f9 into optimus-rm Oct 23, 2026
1 check passed
@gspark-etri
gspark-etri deleted the fix/170-dispatch-drain branch October 23, 2026 01:07
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