Skip to content

Add MoE token permute/reorder kernels - #711

Merged
AkshitaB merged 6 commits into
akshitab/moe-v2-corefrom
akshitab/moe-permute-reorder
Jun 9, 2026
Merged

AkshitaB merged 6 commits into
akshitab/moe-v2-corefrom
akshitab/moe-permute-reorder

Conversation

@AkshitaB

@AkshitaB AkshitaB commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

What this adds

CUDA + Triton/torch MoE token permute, reorder, and unpermute-backward kernels used to route tokens to experts and gather their outputs:

  • kernels/moe_chunk_reorder.py — chunked permute/unpermute by row-id map, with caller-provided out=/backward-grad buffers for comm-buffer reuse.
  • kernels/moe_permute_drop.py — fused permute-with-drop.
  • kernels/moe_unpermute_bwd.py — unpermute backward kernel.
  • kernels/cuda/* — the C++/CUDA sources, built lazily via the JIT extension loader.

The Python wrappers are CPU-import-safe; the CUDA path builds lazily on first use.

Testing

  • src/test/kernels/moe_chunk_reorder_test.py — CPU reference-path tests for the torch chunk permute/unpermute (out-of-range row zeroing, out= buffer honoring).
  • src/test/kernels/moe_unpermute_bwd_test.py — GPU test (@requires_gpu) checking the unpermute-backward kernel matches an autograd reference with an out= buffer.

make checks passes; CPU tests pass locally; the GPU test skips cleanly without a GPU.

@AkshitaB
AkshitaB force-pushed the akshitab/moe-scaled-grouped-mm branch from a175224 to fc2e3fe Compare June 9, 2026 01:05
@AkshitaB
AkshitaB force-pushed the akshitab/moe-permute-reorder branch from 590b903 to 6149204 Compare June 9, 2026 01:05
@AkshitaB
AkshitaB force-pushed the akshitab/moe-scaled-grouped-mm branch from fc2e3fe to 8e754fa Compare June 9, 2026 05:16
@AkshitaB
AkshitaB force-pushed the akshitab/moe-permute-reorder branch from 6149204 to 23fa8cc Compare June 9, 2026 05:16
@AkshitaB
AkshitaB force-pushed the akshitab/moe-scaled-grouped-mm branch from 8e754fa to a55fdcc Compare June 9, 2026 05:25
@AkshitaB
AkshitaB force-pushed the akshitab/moe-permute-reorder branch from 23fa8cc to ee8ffee Compare June 9, 2026 05:25
@AkshitaB
AkshitaB force-pushed the akshitab/moe-scaled-grouped-mm branch from a55fdcc to b241adc Compare June 9, 2026 05:29
@AkshitaB
AkshitaB force-pushed the akshitab/moe-permute-reorder branch from ee8ffee to e76dd17 Compare June 9, 2026 05:29
@AkshitaB
AkshitaB force-pushed the akshitab/moe-scaled-grouped-mm branch from b241adc to 8a28d7f Compare June 9, 2026 05:45
@AkshitaB
AkshitaB force-pushed the akshitab/moe-permute-reorder branch 2 times, most recently from 52b51f8 to f8a2371 Compare June 9, 2026 06:25
Base automatically changed from akshitab/moe-scaled-grouped-mm to akshitab/moe-v2-core June 9, 2026 06:37
TianhuaTao and others added 2 commits June 8, 2026 23:38
Three CUDA-backed kernels used to reshape token batches around the routing path:

  * moe_chunk_reorder  - moe_chunk_permute / moe_chunk_unpermute: reorder tokens
    between chunked layouts (e.g., grouping tokens by expert).
  * moe_permute_drop   - moe_permute_drop_fwd: fused permute-and-drop for tokens
    over per-expert capacity.
  * moe_unpermute_bwd  - backward pass that restores the original token order.

Each module ships its .cpp/.cu sources and loads the extension lazily via
load_cuda_extension, so the wrappers are import-safe on CPU.

Co-Authored-By: Akshita Bhagia <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@AkshitaB
AkshitaB force-pushed the akshitab/moe-permute-reorder branch from f8a2371 to eb25f6a Compare June 9, 2026 06:38
AkshitaB and others added 4 commits June 8, 2026 23:50
Document the public entry points (moe_chunk_permute, moe_chunk_unpermute,
moe_permute_drop_fwd, moe_unpermute_bwd) and declare __all__ at the
submodule level, matching the rest of the kernels package.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…9 lane

The .cu kernel extensions JIT-build with nvcc, which the existing GPU
images lack, so those tests fail with "CUDA_HOME not set". Skip
src/test/kernels on the torch 2.9 "Test (GPU)" job and add a trial job on
tianhuat/olmo-core-torch210-2404-cu128, which ships the CUDA toolkit.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Each kernel module had its own _load_cuda_extension() with duplicated
module-global caching and build/error-wrapping boilerplate. Add a
LazyCudaExtension helper in cuda_extension_utils that memoizes the build,
caches and re-raises (chained) build errors, and resolves sources relative
to the kernels/cuda directory. Convert grouped_mm and the permute/reorder
kernels to a small LazyCudaExtension config plus a thin wrapper.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Point the torch 2.10 MoE-kernels job at tianhuat/olmo-core-torch210-2404-cu128,
which ships the CUDA toolkit (nvcc) needed to JIT-build the .cu kernel
extensions, and drop the separate trial job.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@AkshitaB
AkshitaB marked this pull request as ready for review June 9, 2026 07:46
@AkshitaB
AkshitaB merged commit 3917e23 into akshitab/moe-v2-core Jun 9, 2026
24 checks passed
@AkshitaB
AkshitaB deleted the akshitab/moe-permute-reorder branch June 9, 2026 07:47
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