Skip to content

feat: Least-Loaded Expert Parallelism with new upstream - #55

Merged
xrsrke merged 13 commits into
upstream-2026-10-02from
phuc/llep_optimized_v2
Mar 6, 2026
Merged

xrsrke merged 13 commits into
upstream-2026-10-02from
phuc/llep_optimized_v2

Conversation

@xrsrke

@xrsrke xrsrke commented Mar 4, 2026 •

Copy link
Copy Markdown

Summary

Implements Least-Loaded Expert Parallelism (LLEP), a dynamic load-balancing strategy for Expert Parallelism in MoE models. LLEP redistributes expert workloads across GPUs at runtime to eliminate straggler GPUs caused by token routing imbalance in models like DeepSeek-V3, Kimi-K2, and Qwen3.

Paper: Least-Loaded Expert Parallelism: Load Balancing An Imbalanced Mixture-of-Experts

How LLEP Works

Standard EP assigns each expert to a fixed GPU. When routing is imbalanced, "hot" GPUs become bottlenecks while others idle. LLEP fixes this by:

  1. Gather per-rank expert token counts via all_gather
  2. Plan a Longest-Processing-Time (LPT) assignment that splits overloaded experts across helper GPUs
  3. Transfer expert weights to helper GPUs via P2P
  4. Dispatch tokens via modified AllToAll routing
  5. Compute normal moe forward pass on each GPU's balanced workload
  6. Combine results via inverse AllToAll back to original token order

How to use it

Enable via TOML:

[llep]
enabled = true
min_tokens_per_gemm = 1
adaptive_threshold = 0.0

Benchmark

Model: debugmodel_ep8_llep_3b — 1.75B total params, 64 experts, EP=8, top_k=8, lbs=6, seq=4096
Hardware: 8×B200, 20 steps, no compile, no activation checkpointing

Speed (steps 5–20 average)

With LLEP Without LLEP Delta
Mean TPS 54,483 38,596 +41.2%

Per-GPU Memory (step 5)

With LLEP — all GPUs balanced within 1.86 GiB
GPU Memory Memory % TPS
0 41.68 GiB 23.4% 55,698
1 40.97 GiB 23.0% 56,287
2 41.05 GiB 23.0% 54,551
3 40.53 GiB 22.7% 56,998
4 40.99 GiB 23.0% 56,409
5 42.39 GiB 23.8% 57,000
6 41.16 GiB 23.1% 56,797
7 41.28 GiB 23.1% 56,774
Spread 1.86 GiB
Without LLEP — imbalanced, 13.11 GiB spread
GPU Memory Memory % TPS
0 56.54 GiB 31.7% 38,697
1 52.40 GiB 29.4% 39,475
2 50.64 GiB 28.4% 39,465
3 57.48 GiB 32.2% 39,274
4 49.39 GiB 27.7% 39,534
5 44.37 GiB 24.9% 39,211
6 47.08 GiB 26.4% 39,480
7 55.08 GiB 30.9% 39,536
Spread 13.11 GiB

Loss at step 20: 3.081 (LLEP) vs 3.082 (no LLEP) — numerically identical.

Reproduce

cd torchtitan

# WITH LLEP (20 steps)
torchrun --nproc_per_node=8 -m torchtitan.train \
  --job.config_file torchtitan/models/deepseek_v3/train_configs/debug_model_ep8_llep_3b.toml \
  --training.steps 20 --compile.no-enable

# WITHOUT LLEP (20 steps, same model)
torchrun --nproc_per_node=8 -m torchtitan.train \
  --job.config_file torchtitan/models/deepseek_v3/train_configs/debug_model_ep8_llep_3b.toml \
  --training.steps 20 --compile.no-enable --llep.enabled=False

Test Plan

  • test_llep.py — 3/3 passed (LPT planning, CPU)
  • test_llep_correctness.py — 5/5 passed (Triton kernels, 1 GPU)
  • test_llep_hooks.py — 57/59 passed (hook-based flow, 8 GPUs)
  • test_moe_ep_e2e.py — 28/28 passed (full MoE forward+backward, 2 GPUs)
  • E2E training 20 steps WITH LLEP — loss decreasing, memory balanced
  • E2E training 20 steps WITHOUT LLEP — same loss, 7× higher memory spread
# Unit tests (no GPU required)
python -m pytest tests/unit_tests/test_llep.py -v
python -m pytest tests/unit_tests/test_llep_correctness.py -v

# Distributed tests (requires 2+ GPUs)
torchrun --nproc_per_node=2 tests/unit_tests/test_llep_hooks.py
torchrun --nproc_per_node=2 -m pytest tests/unit_tests/test_moe_ep_e2e.py -v

xrsrke added 5 commits March 4, 2026 15:07
Rebased LLEP (Least-Loaded Expert Parallelism) from the old
phuc/kimi_k2_with_autotune_llep_optimized_llep branch onto the
latest upstream-2026-10-02 to resolve 86 merge conflicts caused
by 1000+ upstream commits since the original branch point.

New LLEP core files:
- torchtitan/distributed/llep.py: dispatch/combine with LPT routing
- torchtitan/distributed/llep_autotune.py: hyperparameter autotuning
- torchtitan/distributed/llep_kernels.py: Triton kernels

Integration points (surgical changes to upstream files):
- moe.py: LLEPConfig, fast_init_*, llep_state in GroupedExperts.forward
- expert_parallel.py: ExpertParallelLLEP class
- job_config.py: LLEP config dataclass
- llama4/parallelize.py: LLEP EP selection logic
- deepseek_v3/__init__.py: LLEP model variants
- train.py: LLEP autotune at startup
The [llep] TOML section (enabled, max_tokens_factor, etc.) was not being
applied to moe_args in DeepSeekV3ModelArgs.update_from_config(), so LLEP
was never actually activated. This caused OOM on the imbalanced GPU since
standard EP doesn't balance memory across ranks.
- Add debugmodel_ep8_llep_3b flavor (1.75B params, 64 experts, EP=8)
  and debug_model_ep8_llep_3b.toml for benchmarking on new upstream
  (the 9.5B config OOMs due to upstream memory regression)
- Copy 3 missing multinode Kimi K2 LLEP+Muon TOML configs from old branch
Keep only essential files:
- docs/llep.md (main documentation)
- debug_model_llep.toml (2-GPU smoke test)
- debug_model_ep8_llep_3b.toml (8-GPU benchmark)
- test_llep_toml_override.toml (unit test config)
- debugmodel_llep, debugmodel_ep8_llep_3b model flavors

Removed: optimization report, pr008 cleanup doc, loss comparison
scripts, baseline/stresstest/mini_kimi/kimi_k2/multinode TOMLs,
and their corresponding model flavor definitions.
@xrsrke
xrsrke marked this pull request as ready for review March 4, 2026 18:06
@xrsrke
xrsrke requested a review from jquesnelle March 4, 2026 18:06
- Get EP process group from experts' DTensor device_mesh["ep"] instead
  of non-existent MoE._ep_group attribute
- Use moe_module.use_llep and moe_module._llep_config instead of
  non-existent _llep_enabled/_llep_max_tokens_factor attributes
Comment thread torchtitan/models/moe/moe.py Resolved
xrsrke added 4 commits March 5, 2026 14:34
When both expert_parallel_comm_backend="deepep" and llep.enabled=true,
uses DeepEP for balanced steps and falls back to LLEP for imbalanced
steps based on adaptive_threshold.

New classes:
- DeepEPLLEPExpertParallel: per-step dispatch/combine hook that checks
  imbalance ratio and routes to DeepEP or LLEP path accordingly
- DeepEPLLEPMoE: MoE module that passes 5-tuple routing info to experts
  and handles async combine overlap with shared_experts

Wiring: args.py derives moe_impl="deepep_llep" when both flags set,
build_moe() creates DeepEPLLEPMoE, apply_moe_ep_tp() installs the
adaptive expert parallel hooks.
Replace the 3B debugmodel_ep8_llep_3b (too small for LLEP to help)
with the 9.5B debugmodel_ep8_llep (dim=2048, moe_inter_dim=1536,
16 layers, lbs=8) that stresses GPU memory and shows LLEP's benefit.

Benchmark results on 8xB200 (steps 5-20):
- Speed: +10.9% mean TPS (26,370 vs 23,780)
- Memory: 7 GiB spread vs 42 GiB, max 82% vs 97%
- Without LLEP at lbs=10: OOM. With LLEP: runs fine.
- Loss correctness: <0.001 diff by step 130
DeepEPLLEPMoE.forward() was identical to DeepEPMoE.forward(). The
behavioral difference comes entirely from which ExpertParallel hooks
get installed (DeepEPExpertParallel vs DeepEPLLEPExpertParallel),
not from the MoE class itself.
4-config comparison on 9.5B model, 8xB200:
- LLEP (standard): 26,250 TPS, 6 GiB spread
- DeepEP+LLEP (adaptive): 25,820 TPS, 7 GiB spread
- Standard EP: 21,940 TPS, 41 GiB spread
- DeepEP only: 19,640 TPS, 52 GiB spread
@xrsrke

xrsrke commented Mar 5, 2026

Copy link
Copy Markdown
Author

@claude

xrsrke and others added 3 commits March 6, 2026 13:12
The fused_silu_gate Triton kernel (fwd/bwd), FusedSiLUGate autograd
class, and silu_gate_reference were not wired into the training path
(llep.py never imported them). Delete the dead code from llep_kernels.py,
remove test_llep_correctness.py (all tests were for the deleted kernel),
and update docs/llep.md to reflect current state.
Move llep.py, llep_kernels.py, llep_autotune.py into
torchtitan/distributed/llep/ and docs/llep.md as its README.md.
All external imports (from torchtitan.distributed.llep import ...)
remain unchanged since llep/__init__.py preserves the public API.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…rmal_

Delete fast_init_trunc_normal_ and fast_init_normal_ from moe.py since
upstream utils.py already provides equivalent trunc_normal_ and normal_.
Wrap init_weights() call in test with torch.no_grad() to match how the
training pipeline invokes it.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@xrsrke
xrsrke merged commit 198ff4f into upstream-2026-10-02 Mar 6, 2026
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