Repository navigation
[OPUS] moe_sorting_opus: allocate workspace tensor in Python instead of C++ - #3047
Merged
Merged
Conversation
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.
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
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]>
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.
No description provided.