Skip to content

[OPUS] moe_sorting_opus: allocate workspace tensor in Python instead of C++ - #3047

Merged
valarLip merged 1 commit into
mainfrom
carhuang/opus_sorting_alloc_ws_in_py
May 6, 2026
Merged

valarLip merged 1 commit into
mainfrom
carhuang/opus_sorting_alloc_ws_in_py

Conversation

@carlushuang

Copy link
Copy Markdown
Collaborator

No description provided.

Expose moe_sorting_opus_get_workspace_size() to Python so callers can
pre-allocate the workspace as a torch.Tensor, which lets PyTorch track
the allocation. moe_sorting_opus_fwd() now accepts an optional workspace
tensor instead of allocating an AiterTensor internally.
@carlushuang
carlushuang requested a review from a team May 6, 2026 08:40
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests
ci:atom ATOM benchmark (DeepSeek-R1 + GPT-OSS)
ci:vllm vLLM benchmark
ci:all All of the above

Add labels via the sidebar or gh pr edit 3047 --add-label <label>

@valarLip
valarLip merged commit 3a01114 into main May 6, 2026
34 of 38 checks passed
@valarLip
valarLip deleted the carhuang/opus_sorting_alloc_ws_in_py branch May 6, 2026 10:39
Liang-jianhao97 pushed a commit that referenced this pull request May 7, 2026
…3047)

Expose moe_sorting_opus_get_workspace_size() to Python so callers can
pre-allocate the workspace as a torch.Tensor, which lets PyTorch track
the allocation. moe_sorting_opus_fwd() now accepts an optional workspace
tensor instead of allocating an AiterTensor internally.
yelkhamra added a commit to yelkhamra/aiter that referenced this pull request Nov 5, 2026
…call

flydsl_moe_sorting_fwd kept the multiphase sort workspace in a module-level
_workspace_cache keyed only by device and passed the same buffer to every
call. Token counts above the oneshot limit (e.g. M >= 17 for E=384,
topk=6) write their expert mesh and per-expert counts there. Graphs captured
while the same cached allocation is reused record the same scratch address.
Two such graphs replayed at the same time on different streams overwrite
each other's scratch, and sorted_ids, sorted_weights, sorted_expert_ids
and num_valid_ids come back wrong although each graph's inputs are valid.

Allocate the workspace with torch.empty on every call, as _moe_sorting_impl
already does for the Opus workspace (ROCm#3047). Each call writes every
workspace word it uses before reading it, so nothing depends on an earlier
call. Under capture the buffer comes from the capturing graph's memory
pool; in eager mode the caching allocator keeps reuse stream-ordered.
Kernels, launch arguments, outputs and the public signature are unchanged.

Merge the companion P23 LDS synchronization fix before this change: the
exact concurrent regression also detects that separate, pre-existing race.

Tests:
- tests/test_flydsl_moe_sorting_workspace.py (CPU): two multiphase calls
  must receive distinct workspaces, oneshot calls none. Fails before this
  change, passes after.
- op_tests/test_moe_sorting.py: test_moe_sorting_flydsl_concurrent_graph_replay
  captures eight FlyDSL sorts in each of two graphs and replays both on two
  streams. Each attempt queues both graphs behind one gate event that a
  spin kernel keeps pending; an attempt counts only if the gate is still
  pending after both launches, i.e. neither graph could start before the
  other was queued. Ten such attempts are required within twenty (the spin
  doubles after an unconfirmed attempt, up to 2^24 cycles); otherwise the
  test raises RuntimeError (precondition not confirmed) instead of passing.
  Every sort must match the torch reference exactly (checkAllclose with
  rtol=0, atol=0), including weight 0 in padding slots. Oneshot shapes
  (no workspace) are skipped. An earlier version of this test without the
  gate passed on the unpatched wrapper on gfx950 while a standalone
  reproducer showed failures on the same GPU.

Signed-off-by: Yaakoub El Khamra <[email protected]>
yelkhamra added a commit to yelkhamra/aiter that referenced this pull request Nov 8, 2026
…call

flydsl_moe_sorting_fwd kept the multiphase sort workspace in a module-level
_workspace_cache keyed only by device and passed the same buffer to every
call. Token counts above the oneshot limit (e.g. M >= 17 for E=384,
topk=6) write their expert mesh and per-expert counts there. Graphs captured
while the same cached allocation is reused record the same scratch address.
Two such graphs replayed at the same time on different streams overwrite
each other's scratch, and sorted_ids, sorted_weights, sorted_expert_ids
and num_valid_ids come back wrong although each graph's inputs are valid.

Allocate the workspace with torch.empty on every call, as _moe_sorting_impl
already does for the Opus workspace (ROCm#3047). Each call writes every
workspace word it uses before reading it, so nothing depends on an earlier
call. Under capture the buffer comes from the capturing graph's memory
pool; in eager mode the caching allocator keeps reuse stream-ordered.
Kernels, launch arguments, outputs and the public signature are unchanged.

Merge the companion P23 LDS synchronization fix before this change: the
exact concurrent regression also detects that separate, pre-existing race.

Tests:
- tests/test_flydsl_moe_sorting_workspace.py (CPU): two multiphase calls
  must receive distinct workspaces, oneshot calls none. Fails before this
  change, passes after.
- op_tests/test_moe_sorting.py: test_moe_sorting_flydsl_concurrent_graph_replay
  captures eight FlyDSL sorts in each of two graphs and replays both on two
  streams. Each attempt queues both graphs behind one gate event that a
  spin kernel keeps pending; an attempt counts only if the gate is still
  pending after both launches, i.e. neither graph could start before the
  other was queued. Ten such attempts are required within twenty (the spin
  doubles after an unconfirmed attempt, up to 2^24 cycles); otherwise the
  test raises RuntimeError (precondition not confirmed) instead of passing.
  Every sort must match the torch reference exactly (checkAllclose with
  rtol=0, atol=0), including weight 0 in padding slots. Oneshot shapes
  (no workspace) are skipped. An earlier version of this test without the
  gate passed on the unpatched wrapper on gfx950 while a standalone
  reproducer showed failures on the same GPU.

Signed-off-by: Yaakoub El Khamra <[email protected]>
yelkhamra added a commit to yelkhamra/aiter that referenced this pull request Dec 2, 2026
…call

flydsl_moe_sorting_fwd kept the multiphase sort workspace in a module-level
_workspace_cache keyed only by device and passed the same buffer to every
call. Token counts above the oneshot limit (e.g. M >= 17 for E=384,
topk=6) write their expert mesh and per-expert counts there. Graphs captured
while the same cached allocation is reused record the same scratch address.
Two such graphs replayed at the same time on different streams overwrite
each other's scratch, and sorted_ids, sorted_weights, sorted_expert_ids
and num_valid_ids come back wrong although each graph's inputs are valid.

Allocate the workspace with torch.empty on every call, as _moe_sorting_impl
already does for the Opus workspace (ROCm#3047). Each call writes every
workspace word it uses before reading it, so nothing depends on an earlier
call. Under capture the buffer comes from the capturing graph's memory
pool; in eager mode the caching allocator keeps reuse stream-ordered.
Kernels, launch arguments, outputs and the public signature are unchanged.

The p23 LDS barrier that the concurrent regression also exercises is on
main via ROCm#7349 (4fb090d). This commit is rebased onto that main and
carries only the workspace change; its companion commit is dropped.

Tests:
- tests/test_flydsl_moe_sorting_workspace.py (CPU): two multiphase calls
  must receive distinct workspaces, oneshot calls none. Fails before this
  change, passes after.
- op_tests/test_moe_sorting.py: test_moe_sorting_flydsl_concurrent_graph_replay
  captures eight FlyDSL sorts in each of two graphs and replays both on two
  streams. Each attempt queues both graphs behind one gate event that a
  spin kernel keeps pending; an attempt counts only if the gate is still
  pending after both launches, i.e. neither graph could start before the
  other was queued. Ten such attempts are required within twenty (the spin
  doubles after an unconfirmed attempt, up to 2^24 cycles); otherwise the
  test raises RuntimeError (precondition not confirmed) instead of passing.
  Every sort must match the torch reference exactly (checkAllclose with
  rtol=0, atol=0), including weight 0 in padding slots. Oneshot shapes
  (no workspace) are skipped. An earlier version of this test without the
  gate passed on the unpatched wrapper on gfx950 while a standalone
  reproducer showed failures on the same GPU.

Signed-off-by: Yaakoub El Khamra <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants