Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 167 additions & 3 deletions docs/design_docs/moe_ep_architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,178 @@ Expert-Parallel MoE with two execution modes:

Entry point: `MoEEpLayer(bootstrap, fleet_params, weights, fleet_knobs=(), backend=...)` → `MoEEpSplitLayer` or `MoEEpMegaLayer`.

## Layout
## Available backends

Backends resolve by name from the config object's `kernel_name` /
`backend_name` field (three registries: mega kernels, split kernels, split
comm fleets; deprecated aliases still resolve but warn).

### Mega (fused comm + MoE kernel)

Selected via `MegaConfig(megakernel=<config>)`. One symmetric-memory kernel
owns dispatch, expert compute, and combine; output is always BF16
`[num_tokens, hidden]`.

| Backend (alias) | Activation | Weight | Output | Arch | Tuning |
|---|---|---|---|---|---|
| `sm100_nvfp4_nvfp4_bf16_cutedsl` (`nvfp4_cutedsl`) | NVFP4 (block-16) | NVFP4 (block-16) | BF16 | SM100 family | `knobs=None` → token-count heuristic; `knobs=dict` → pinned; `knobs="auto"` → collective compile+time sweep at first forward (never in serving); winners cacheable via `FLASHINFER_MOE_EP_KNOB_CACHE` |
| `sm100_mxfp8_mxfp8_bf16_cutedsl` (`mxfp8_cutedsl`) | MXFP8 (block-32 UE8M0) | MXFP8 (block-32 UE8M0) | BF16 | SM100 family | same `knobs` surface as the NVFP4 backend |
| `sm100_fp8_fp4_bf16_deepgemm` (`deep_gemm_mega`) | FP8 (E4M3, block-32 UE8M0) | FP4 (int8-packed, block-32) | BF16 | SM100 family | — (DeepGEMM selects its own JIT configs internally) |
| `sm90_fp8_fp8_bf16_pull_cutedsl` (`sm90_pull_fp8`) | FP8 (E4M3/E5M2; per-tensor or DeepGEMM-style blockwise scales) | FP8 (same `fp8_scale_mode`) | BF16 | SM90 exactly | explicit geometry knobs on the config (`swap_ab`, `mma_tiler_mnk`); no tuner/knob-cache yet |
| `sm90_fp8_fp8_bf16_push_cuda` (`sm90_push_fp8`) | FP8 (E4M3) | FP8 (E4M3) | BF16 | SM90 | — (static dimensions/protocol choices only) |

The SM90 pull-style CuTeDSL tree is process-exclusive with the SM100 CuTeDSL
tree (module names collide). Weight inputs are canonical BF16 `MoEWeightPack`
by default (the backend quantizes at `preprocess_weights`); kernel-ready
pre-quantized weights can be supplied instead.

### Split (dispatch → inner kernel → combine)

Composed via `SplitConfig(comm=..., kernel=...)`; any comm backend pairs with
any kernel backend. The comm layer moves tokens (BF16 unless the kernel
backend packs them); the kernel backend computes on this rank's expert shard.

#### Comm backends (dispatch/combine transports)

| Backend | Config | Transport | Modes | Constraints |
|---|---|---|---|---|
| `nccl_ep` | `NcclEpConfig` | NCCL (nccl4py wheel) | LL `EXPERT_MAJOR` / `RANK_MAJOR`, HT `FLAT` | LL device kernel whitelists per-token row widths and caps top-k at 8 — see the runbook's "NCCL-EP low-latency device-kernel limits" |
| `nixl_ep` | `NvepConfig` | NIXL over UCX device API (GPU-initiated RDMA) | LL `EXPERT_MAJOR` only | needs `BUILD_NIXL_EP=1` (UCX v1.21+ device headers) and `BootstrapConfig.tcp_store`; `max_tokens_per_rank ≤ 1024`; hidden ∈ {2048, 2560, 3072, 4096, 5120, 6144, 7168, 8192}; handles top-k > 8 |

#### Kernel backends (post-dispatch inner compute)

| Backend | Activation | Weight | Output | Arch | Tuning |
|---|---|---|---|---|---|
| `identity` (`IdentityConfig`) | passthrough | none | dispatch tensor unchanged | any | — |
| `fused_moe` (`FusedMoeKernelConfig`) with `TrtllmBf16Config` | BF16 | BF16 | BF16 | SM100 family | inner `MoELayer` AutoTuner: per-runner tactic search + cross-backend winner per token bucket, up to `ExecutionConfig.tune_max_num_tokens` |
| `fused_moe` with `TrtllmFp4Config` | NVFP4 (block-16, quantized post-dispatch in the bridge) | NVFP4 (block-16) | BF16 | SM100/SM103/SM107 | same `MoELayer` AutoTuner |
| `fused_moe` with `CuteDslConfig` | NVFP4 (block-16) or BF16 (W4A16) | NVFP4 or 4-bit (W4A16) | BF16 | SM100/SM103 | same `MoELayer` AutoTuner |
| `sm100_mxfp8_mxfp4_bf16_cutedsl` (`Sm100_Mxfp8_Mxfp4_Bf16_Cutedsl_SplitConfig`) | MXFP8 (block-32 UE8M0; quantized post-dispatch, or pre-dispatch packed payload with `mxfp8_dispatch=True`, nccl_ep only, bit-identical) | MXFP4 (block-32 UE8M0) | BF16 | SM100/SM103 | `tactic=` pins a kernel tactic; unpinned runs the AutoTuner default tactic (tactic sweep only under an autotune context — not tuned in the shipped benchmarks) |

`fused_moe` accepts exactly one backend candidate per `MoEConfig` (weight
views are prepared for the first match only). The W4A8 split kernel requires
hidden and intermediate sizes to be multiples of 128.

## How tuning works

Two independent tuning systems, matching the two execution modes:

- **Split** kernels tune through the generic FlashInfer `AutoTuner`: each
`fused_moe` runner enumerates kernel *tactics*, the tuner times them per
token bucket (up to `ExecutionConfig.tune_max_num_tokens`), and `MoELayer`
additionally picks a cross-backend winner per bucket. Nothing moe_ep-specific
— see the fused_moe docs.
- **Mega** CuTeDSL kernels tune through the moe_ep-owned **knob** system
described below. The knob space is larger than a tactic id (tile, cluster,
warp-role, and scheduling choices that must be fixed at `cute.compile` time)
and every measurement is a **collective** — the fused kernel spans all EP
ranks, so candidates must compile and launch in lockstep on every rank.

### Knob resolution (mega CuTeDSL)

`Sm100_*_Cutedsl_MegaMoeConfig.knobs` accepts three values; resolution happens
when the symmetric-memory session is created (and, for `"auto"`, at the first
`compute()`):

```mermaid
flowchart TD
A["MegaConfig(megakernel=cfg)"] --> B{"cfg.knobs?"}
B -- "dict" --> C["validate via tuner.is_valid<br/>→ pin exactly these knobs"]
B -- "None (default)" --> D{"knob cache hit?<br/>FLASHINFER_MOE_EP_KNOB_CACHE<br/>key: device, dtype, world_size,<br/>geometry, combine_dtype + max_tokens bucket"}
D -- "hit" --> E["use recorded winner<br/>(pure dict lookup — no compiles,<br/>no collectives)"]
D -- "miss" --> F["tuner.default_knobs(max_tokens)<br/>measured token-count profiles<br/>(NVFP4: 4 profiles, MXFP8: 2)"]
B -- "auto" --> G["defer to first compute()"]
G --> H["COLLECTIVE candidate sweep<br/>(shim/autotune.py)"]
H --> I["per candidate, on every rank in lockstep:<br/>cute.compile → barrier →<br/>timed launches → barrier"]
I --> J["all-reduce per-candidate time with MAX<br/>(slowest rank = real collective latency)<br/>→ argmin winner identical on all ranks"]
J --> K["apply winner to the session"]
K --> L["rank 0 records winner into the knob cache<br/>→ later sessions with knobs=None hit E"]
C --> M["cute.compile session<br/>(memoized per knobs+geometry)"]
E --> M
F --> M
```

The knobs split into two classes (`kernel_src/cutedsl_megamoe/shim/tuner.py`):

- **correctness knobs** change a code path or the output and must be kept at
the validated value: `mma_tiler_mnk`, `cluster_shape_mnk`,
`token_back_mode`, `load_balance_mode`, `non_ubulk_fc2_store`, and
`in_kernel_fc2_reduce` (ikr — makes the accumulation order
nondeterministic; pin `False` for bit-reproducibility);
- **perf knobs** are output-neutral and free to sweep: `group_hint`,
`flag_batch`, `epi_flag_batch`.

A validity predicate (`tuner.is_valid`) mirrors the kernel team's
`inference_solver.filter_invalid` rules, so sweeps only enumerate
compilable combinations.

### Detailed example: `sm100_nvfp4_nvfp4_bf16_cutedsl`

**1. Default (`knobs=None`).** The session resolves the knob cache first;
on a miss it falls back to four measured token-count profiles keyed on the
compile-time buffer capacity (`max_tokens_per_rank * world`):

| bucket | profile |
|---|---|
| < 512 | small-batch latency: 128-wide N tile, `token_back_mode="epi_warps"` |
| 512–1023 | mid: `reuse_dispatch_warps` |
| 1024–2047 | mid-large: 256-wide N tile, `standalone_warps` |
| ≥ 2048 | large throughput: `flag_batch=8`, `reuse_dispatch_warps` |

**2. Pinned (`knobs=dict`).** E.g. a winner from the kernel team's tester
sweep:

```python
Sm100_Nvfp4_Nvfp4_Bf16_Cutedsl_MegaMoeConfig(
intermediate_size=2048, top_k=8,
knobs={"mma_tiler_mnk": (256, 128, 256), "cluster_shape_mnk": (2, 1, 1),
"token_back_mode": "reuse_dispatch_warps", "flag_batch": 8,
"group_hint": 512, "epi_flag_batch": (2, 4),
"load_balance_mode": "atomic_counter",
"in_kernel_fc2_reduce": False},
)
```

**3. Online (`knobs="auto"`).** At the first `compute()` every rank runs the
same ~24-candidate sweep (`nvfp4_candidates()`: tile {256×128, 256×256} ×
`flag_batch` {4, 8} × three `token_back_mode` values × ikr {off, on}, over a
fixed base of `cluster (2,1,1)`, `group_hint 512`, `epi_flag_batch (2,4)`,
`atomic_counter`). Each candidate costs one `cute.compile` (minutes), so the
sweep is a multi-minute, collective, once-per-session event — **never run it
inside a serving engine**. The winner is applied and rank 0 records it in the
knob cache.

**4. Offline CLI (the intended production flow).** Run the same sweep outside
the engine, with the production EP world size, GPU model, and geometry:

```shell
torchrun --nproc_per_node=4 -m flashinfer.moe_ep.tune \
--dtype nvfp4 --hidden 7168 --intermediate 2048 \
--num-experts 256 --topk 8 --max-tokens 8 512 2048
```

Winners land in the JSON knob cache (default
`~/.cache/flashinfer/moe_ep_knob_cache.json`; override or disable with
`FLASHINFER_MOE_EP_KNOB_CACHE`), keyed by (device, dtype, world_size, hidden,
intermediate, num_experts, topk, combine_dtype) plus a `max_tokens` bucket
(exact bucket when present, else the smallest recorded bucket ≥ the request,
else the largest below). The engine then constructs the layer with the
default `knobs=None` and gets the tuned winner as a pure lookup — no
compiles, no collectives, no timing on the hot path. Nondeterministic ikr
candidates are excluded from the CLI sweep unless
`--allow-nondeterministic` is passed.

Measured results, methodology, and the full knob reference live in
[kernel_src/cutedsl_megamoe/TUNING.md](../../flashinfer/moe_ep/kernel_src/cutedsl_megamoe/TUNING.md).

## Layout

```text
moe_ep/
config.py, tensors.py, weights.py, layer.py, algo_knobs.py, errors.py
core/comm, core/kernel, core/runtime, core/validation, core/bootstrap_utils.py
backends/split/comm/{nccl_ep,nixl_ep}
backends/split/kernel/{identity,fused_moe}
backends/split/kernel/{identity,fused_moe,sm100/mxfp8_mxfp4_bf16_cutedsl}
backends/mega/kernel/sm100/{bf16_bf16_bf16_cutedsl,nvfp4_nvfp4_bf16_cutedsl,mxfp8_mxfp8_bf16_cutedsl,fp8_fp4_bf16_deepgemm}
backends/mega/kernel/sm90/{fp8_fp8_bf16_pull_cutedsl,fp8_fp8_bf16_push_cuda}
kernel_src/cutedsl_megamoe/ ← Blackwell CuTeDSL kernel src (kernel team) + FI shim
Expand Down Expand Up @@ -75,7 +239,7 @@ Kernels register via `@register_split_kernel` / `@register_mega_kernel` when `ba
- LL **EXPERT_MAJOR** — `[num_local_experts, cap, hidden]` (`cap = max_tokens_per_rank * world`), each row pre-assigned to one expert; the bridge synthesizes `top_k=1` / `final_scales=1` and **combine owns the real top-k reweight**.
- LL **RANK_MAJOR** / **HT FLAT** — `[world, max_tokens_per_rank, hidden]` carrying received `topk_idx` / `topk_weights`; the runner uses the real `top_k` with non-local picks masked to weight 0, and combine just sums across ranks.

Both bf16 and NVFP4 are supported in the compute path (`MoEConfig.quant.variant`); NVFP4 activations are quantized in the bridge (linear SF layout). Correctness is currently validated for bf16 only (see **Tests**).
Both bf16 and NVFP4 are supported in the compute path (`MoEConfig.quant.variant`); NVFP4 activations are quantized in the bridge (linear SF layout). W4A8 (MXFP8 activations × MXFP4 weights) is a separate dedicated split kernel backend, `sm100_mxfp8_mxfp4_bf16_cutedsl`, with its own routing synthesis mirroring this bridge and an optional MXFP8-packed dispatch payload (see **Available backends**).

**Mega:** pass `MegaConfig(megakernel=...)`. Weights required as the layer's `weights` argument. Workspace allocated on first forward. Output is bf16 `[num_tokens, token_hidden_size]` where `num_tokens = MoEEpTensors.num_tokens` (may be `< max_tokens_per_rank`). `fleet_knobs` are ignored. NIXL-EP split layers require `BootstrapConfig.tcp_store` at init.

Expand Down
38 changes: 38 additions & 0 deletions docs/design_docs/moe_ep_runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,47 @@ Notes:
- `NPROC_SMOKE` / `NPROC_MULTIRANK` (default 4) override the rank count for
the `run_tests.sh` targets.
- UCX/ibverbs are **build-time** deps; the tests set no `NIXL_*`/`UCX_*` env.
When UCX lives in a non-default prefix (e.g. `/opt/ucx` from the source
build above), put its `lib/` on `LD_LIBRARY_PATH` at **runtime** too —
loading a different UCX than the one nixl was built against fails at fleet
creation with `registerMem(...) != NIXL_SUCCESS`.
- **Pin the `nixl-cu13` wheel to the 3rdparty/nixl submodule tag** (currently
`==1.3.1`; the build hook installs the pinned version via
`_NIXL_WHEEL_VERSION` in `build_backend.py`). `nixl_ep_cpp.so` compiles the
submodule's device kernels but loads the wheel's `libnixl.so` at runtime —
a skewed pair (e.g. a 1.4.x wheel over the v1.3.1 kernels) builds and
imports fine, then dies at the first dispatch with device asserts
(`nixlPut(...) == NIXL_IN_PROG` → `cudaErrorIllegalAddress`).
- **Do not run concurrent `BUILD_NIXL_EP=1` installs from one checkout**
(e.g. several SLURM jobs sharing a network-filesystem clone): the build
patches `3rdparty/nixl` in place and shares `build_nvep/`, so parallel
installs race and fail with `git apply` / meson `Unknown option` errors.
Serialize the first build; later installs reuse the staged `_libs/` .so.
- The install and the launcher must resolve to the **same interpreter**: an
editable install into a venv is invisible to a `torchrun` that resolves to
the system python (`ModuleNotFoundError: flashinfer` in the spawned ranks
while parent-shell imports work). When in doubt, launch with
`python -m torch.distributed.run` so the launcher is pinned to the python
that owns the install.
- NIXL-EP coverage today is smoke + multirank + mocked unit tests only; the
correctness/mega targets are NCCL-EP-only.

### NCCL-EP low-latency device-kernel limits

Two constraints of the `nccl.ep` LL device kernel (probed empirically on
nccl4py 0.3.1; not enforced by `validate_fleet_params`, so they surface as
device-side aborts):

- **Per-token row widths are whitelisted**: LL dispatch accepts bf16 rows of
{2048, 2560, 4096, 5120, 6144, 7168, 8192} elements only — 3072, sub-2048
widths, and all 1-byte payload dtypes are rejected with
`low_latency.cu 'Unsupported hidden'`. The sent row may be narrower than
`FleetParams.token_hidden_size` (recv buffers mirror the sent row), which
is what the split path's packed-MXFP8 dispatch relies on.
- **top-k is capped at 8** (`numTopk <= kNumMaxTopK`, `low_latency.cu`):
top-10 models (e.g. Qwen3.5) abort on NCCL-EP LL; use the HT algorithm or
NIXL-EP (which handles top-10 at LL).

### SM90 mega token sweep

Hopper-only (`sm90_fp8_fp8_bf16_pull_cutedsl`) correctness targets run in their own pytest
Expand Down
3 changes: 3 additions & 0 deletions flashinfer/moe_ep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
NCCLEPConfig,
NcclEpConfig,
NvepConfig,
Sm100_Mxfp8_Mxfp4_Bf16_Cutedsl_SplitConfig,
SplitConfig,
SplitKernelContext,
kernel_requires_weights,
Expand All @@ -169,6 +170,7 @@
"Nvfp4CutedslMegaMoeConfig",
"Sm90PullFp8MegaMoeConfig",
"Sm90PushFp8MegaMoeConfig",
"Sm90_Fp8_Fp8_Bf16_PushCuda_MegaMoeConfig",
"DispatchInputParams",
"DispatchOutput",
"EpAlgorithm",
Expand Down Expand Up @@ -205,6 +207,7 @@
"PrequantizedMoEWeights",
"UnquantizedMoEWeights",
"Sm100_Mxfp8_Mxfp8_Bf16_Cutedsl_MegaMoeConfig",
"Sm100_Mxfp8_Mxfp4_Bf16_Cutedsl_SplitConfig",
"NCCLEPConfig",
"NcclEpConfig",
"Sm100_Nvfp4_Nvfp4_Bf16_Cutedsl_MegaMoeConfig",
Expand Down
15 changes: 11 additions & 4 deletions flashinfer/moe_ep/backends/split/comm/nccl_ep/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ def _dispatch_ll(self, x) -> DispatchOutput:
_t = _pc() if _HP else None
world_size = self._fleet.bootstrap.world_size
max_per_rank = self._fleet.params.max_tokens_per_rank
hidden = self._fleet.params.token_hidden_size
# The recv row mirrors the sent row (shape AND dtype). This is
# FleetParams.token_hidden_size for plain BF16 dispatch, but a kernel
# backend's pack_dispatch_payload may send a narrower packed row (e.g.
# MXFP8 payload + scale bytes as uint8) within the transport's
# token_hidden_size * dtype_bytes byte budget.
hidden = x.shape[1]

# Fleet-cached recv buffer (a fresh Handle is created every forward, so
# per-handle caching never hits; the fleet persists).
Expand Down Expand Up @@ -307,7 +312,8 @@ def _dispatch_ll_rank_major(self, x) -> DispatchOutput:

world_size = self._world_size
max_per_rank = self._fleet.params.max_tokens_per_rank
hidden = self._fleet.params.token_hidden_size
# Recv row mirrors the sent row; see _dispatch_ll.
hidden = x.shape[1]
m = max_per_rank * world_size

tw = self._handle_knobs.get(HandleAlgoKnobTopKWeights)
Expand Down Expand Up @@ -371,7 +377,8 @@ def _dispatch_ht(self, x) -> DispatchOutput:
import torch

max_per_rank = self._fleet.params.max_tokens_per_rank
hidden = self._fleet.params.token_hidden_size
# Recv row mirrors the sent row; see _dispatch_ll.
hidden = x.shape[1]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
world = self._fleet.params.num_experts // self._num_local_experts
num_recv = max_per_rank * world

Expand Down Expand Up @@ -404,7 +411,7 @@ def _dispatch_ht(self, x) -> DispatchOutput:
cached = self._hot.get("ht_recv_bufs")
if (
cached is None
or cached[0].shape[0] != num_recv
or cached[0].shape != (num_recv, hidden)
or cached[1].shape[1] != self._top_k
or cached[0].dtype != x.dtype
or cached[0].device != x.device
Expand Down
4 changes: 2 additions & 2 deletions flashinfer/moe_ep/backends/split/kernel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Split-path compute backends (post-dispatch inner kernels)."""

from . import fused_moe, identity
from . import fused_moe, identity, sm100

__all__ = ["fused_moe", "identity"]
__all__ = ["fused_moe", "identity", "sm100"]
5 changes: 5 additions & 0 deletions flashinfer/moe_ep/backends/split/kernel/sm100/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""SM100-family split-path inner kernels."""

from . import mxfp8_mxfp4_bf16_cutedsl

__all__ = ["mxfp8_mxfp4_bf16_cutedsl"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .backend import Mxfp8Mxfp4CutedslSplitKernelBackend
from .config import Sm100_Mxfp8_Mxfp4_Bf16_Cutedsl_SplitConfig

__all__ = [
"Mxfp8Mxfp4CutedslSplitKernelBackend",
"Sm100_Mxfp8_Mxfp4_Bf16_Cutedsl_SplitConfig",
]
Loading
Loading