How deepseek-ai/lplb works
Purpose and Public Entry Point
LPLB is a CUDA-backed Python library for dynamically balancing Mixture-of-Experts token assignments across an expert-parallel group. It maps model-selected logical expert indices to physical experts, using redundant-expert topology and a per-batch LP solve.
Primary API: lplb/planner.py (Planner). Package export surface begins at lplb/__init__.py. A runnable topology example is in scripts/run_ep16_cube8p2e.py.
Planning Orchestrator
Planner owns the end-to-end per-batch control flow: accept logical token indices, collect or receive workload state, invoke balancing, and return redirected physical expert indices.
• lplb/planner.py
• lplb/__init__.py
Its constructor receives the redundant-to-original mapping (r2o), physical/logical expert counts, and an optional PyTorch distributed EP group. run(indices, avail_counter, N_SMS) is the operational boundary between model routing and LPLB’s physical routing decision.
Static Expert Placement and Reordering
LPLB builds on embedded EPLB logic to handle static topology and expert ordering before dynamic routing. EPLB selects/reorders experts; LPLB applies the supplied redundancy topology so selected logical experts can be redirected to their replicas.
• lplb/eplb.py
• lplb/planner.py
The r2o tensor is the central topology contract: each redundant physical expert maps to an original logical expert. Cube, hypercube, torus, and custom graphs are represented through this mapping rather than separate topology services.
Dynamic Load-Balancing Stage
For every batch, selected logical expert indices are transformed into token demand/load statistics. The planner constrains reassignment by replica edges and their current capacities, then solves for a redistribution intended to reduce total per-rank token imbalance.
• lplb/planner.py
• lplb/resources/csrc-tmpl/minilp.cu
The output preserves the model-facing routing shape but changes values from logical expert IDs to chosen physical expert IDs. The available-success counter is a CUDA-side synchronization/control input used by the planner interface.
Native LP Solver and Python Extension Boundary
The performance-critical optimization is implemented as a compiled CUDA extension. The solver is a small single-SM interior-point-method LP implementation, instantiated from the CUDA template and exposed to Python through a plugin module.
• csrc/plugin.cpp
• csrc/deepep_rt_slim.h
• lplb/resources/csrc-tmpl/minilp.cu
This boundary isolates CUDA kernels, solver state, and optional DeepEP runtime integration from Python planning code. The slim DeepEP runtime header indicates the extension can access DeepEP-related communication/runtime facilities without making Python responsible for low-level transport.
Distributed Workload and Communication Boundary
Workload statistics may come from caller-provided tensors, PyTorch distributed communication, or a DeepEP buffer initialized through the planner. DeepEP is optional at installation but is the preferred production path for lower-overhead workload synchronization, potentially using NVLINK/NVSHMEM rather than standard distributed all-reduce.
• lplb/planner.py
• csrc/deepep_rt_slim.h
The EP process group is the distributed scope for planning; topology can span ranks and therefore makes communication cost a material part of solver usefulness.
Runtime and Build Dependencies
The runtime is Python + PyTorch on CUDA. Native compilation and solver execution require CUDA Toolkit 12.6.3+ and NVIDIA MathDx components, specifically cuSolverDx and cuBLASDx; dependency acquisition is supported by download-mathdx.sh.
• setup.py
• pyproject.toml
• download-mathdx.sh
DeepEP and NVSHMEM are optional integration dependencies, while EPLB is embedded in the lplb package rather than consumed as a separate runtime service.
Main entry points: planner.py Planner · plugin.cpp Python CUDA plugin · deepep_rt_slim.h DeepEP runtime · eplb.py Embedded EPLB
Components 17
Python planning
- Package exports · Public import surface for Planner.
lplb/__init__.py - Planner · Per-batch boundary from logical routing to physical expert IDs.
lplb/planner.py - Embedded EPLB · Selects and reorders experts before dynamic routing.
lplb/eplb.py - r2o topology mapping · Maps redundant physical experts to original logical experts.
CUDA optimization
- Python CUDA plugin · Exposes compiled solver and runtime integration to Python.
csrc/plugin.cpp - Slim DeepEP interface · Provides optional DeepEP-facing extension facilities.
csrc/deepep_rt_slim.h - MiniLP CUDA solver · Single-SM LP solve for capacity-constrained redistribution.
lplb/resources/csrc-tmpl/minilp.cu
Distributed runtime
- Workload state · Caller counters or synchronized availability state.
- EP process group · Defines ranks participating in planning.
- DeepEP runtime · Preferred low-overhead workload synchronization path.
csrc/deepep_rt_slim.h
Build dependencies
- CUDA and MathDx · CUDA 12.6.3+, cuSolverDx, and cuBLASDx are required.
- MathDx downloader
download-mathdx.sh - Package build · Builds the CUDA extension for the Python package.
setup.py - Project metadata
pyproject.toml
Other components
- Logical token indices
- Physical expert indices · Original routing shape with replica-selected physical IDs.
- EP16 topology example · Demonstrates cube-style redundant-expert setup.
scripts/run_ep16_cube8p2e.py
Connections 19
- Package exports → Planner: exports
- EP16 topology example → Planner: uses
- Logical token indices → Planner: run input
- Planner → Physical expert indices: redirected IDs
- r2o topology mapping → Planner: replica topology
- Embedded EPLB → Planner: static ordering
- Planner → Embedded EPLB: applies
- Workload state → Planner: availability counters
- EP process group → Planner: distributed scope
- Planner → DeepEP runtime: optional synchronization
- DeepEP runtime → Workload state: synchronizes
- Planner → Python CUDA plugin: invokes LP solve
- Python CUDA plugin → MiniLP CUDA solver: launches
- Python CUDA plugin → Slim DeepEP interface: integrates optionally
- Slim DeepEP interface → DeepEP runtime: native access
- Package build → Python CUDA plugin: compiles
- Project metadata → Package build: packaging config
- MathDx downloader → CUDA and MathDx: acquires components
- CUDA and MathDx → MiniLP CUDA solver: builds and runs