# deepseek-ai/lplb architecture

> Architecture diagram of the GitHub repository deepseek-ai/lplb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/deepseek-ai/lplb
- Repository: https://github.com/deepseek-ai/lplb

## Overview

#### 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: [`lplb/planner.py`](https://github.com/deepseek-ai/lplb/blob/main/lplb/planner.py) (Planner), [`csrc/plugin.cpp`](https://github.com/deepseek-ai/lplb/blob/main/csrc/plugin.cpp) (Python CUDA plugin), [`csrc/deepep_rt_slim.h`](https://github.com/deepseek-ai/lplb/blob/main/csrc/deepep_rt_slim.h) (DeepEP runtime), [`lplb/eplb.py`](https://github.com/deepseek-ai/lplb/blob/main/lplb/eplb.py) (Embedded EPLB)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_python["Python planning"]
  node_public_api["Package exports<br/>Python package<br/>[__init__.py]"]
  node_planner["Planner<br/>routing orchestrator<br/>[planner.py]"]
  node_eplb["Embedded EPLB<br/>static placement<br/>[eplb.py]"]
  node_r2o["r2o topology mapping<br/>tensor contract"]
end

subgraph group_native["CUDA optimization"]
  node_plugin["Python CUDA plugin<br/>native extension bridge<br/>[plugin.cpp]"]
  node_deepep_header["Slim DeepEP interface<br/>native runtime interface<br/>[deepep_rt_slim.h]"]
  node_minilp["MiniLP CUDA solver<br/>interior-point LP kernel<br/>[minilp.cu]"]
end

subgraph group_distributed["Distributed runtime"]
  node_workload_state["Workload state<br/>capacity input"]
  node_ep_group{{"EP process group<br/>PyTorch distributed scope"}}
  node_deepep["DeepEP runtime<br/>optional communication runtime<br/>[deepep_rt_slim.h]"]
end

subgraph group_build["Build dependencies"]
  node_cuda_mathdx{{"CUDA and MathDx<br/>native toolchain"}}
  node_mathdx_download["MathDx downloader<br/>dependency script<br/>[download-mathdx.sh]"]
  node_package_build["Package build<br/>Python build metadata<br/>[setup.py]"]
  node_pyproject["Project metadata<br/>Python packaging<br/>[pyproject.toml]"]
end

node_logical_indices["Logical token indices<br/>model input"]
node_physical_indices["Physical expert indices<br/>routing output"]
node_topology_example["EP16 topology example<br/>runnable example"]

node_public_api -->|"exports"| node_planner
node_topology_example -->|"uses"| node_planner
node_logical_indices -->|"run input"| node_planner
node_planner -->|"redirected IDs"| node_physical_indices
node_r2o -->|"replica topology"| node_planner
node_eplb -->|"static ordering"| node_planner
node_planner -->|"applies"| node_eplb
node_workload_state -->|"availability counters"| node_planner
node_ep_group -->|"distributed scope"| node_planner
node_planner -.->|"optional synchronization"| node_deepep
node_deepep -->|"synchronizes"| node_workload_state
node_planner -->|"invokes LP solve"| node_plugin
node_plugin -->|"launches"| node_minilp
node_plugin -.->|"integrates optionally"| node_deepep_header
node_deepep_header -.->|"native access"| node_deepep
node_package_build -->|"compiles"| node_plugin
node_pyproject -->|"packaging config"| node_package_build
node_mathdx_download -->|"acquires components"| node_cuda_mathdx
node_cuda_mathdx -->|"builds and runs"| node_minilp

click node_public_api "https://github.com/deepseek-ai/lplb/blob/main/lplb/__init__.py"
click node_planner "https://github.com/deepseek-ai/lplb/blob/main/lplb/planner.py"
click node_eplb "https://github.com/deepseek-ai/lplb/blob/main/lplb/eplb.py"
click node_deepep "https://github.com/deepseek-ai/lplb/blob/main/csrc/deepep_rt_slim.h"
click node_plugin "https://github.com/deepseek-ai/lplb/blob/main/csrc/plugin.cpp"
click node_deepep_header "https://github.com/deepseek-ai/lplb/blob/main/csrc/deepep_rt_slim.h"
click node_minilp "https://github.com/deepseek-ai/lplb/blob/main/lplb/resources/csrc-tmpl/minilp.cu"
click node_mathdx_download "https://github.com/deepseek-ai/lplb/blob/main/download-mathdx.sh"
click node_package_build "https://github.com/deepseek-ai/lplb/blob/main/setup.py"
click node_pyproject "https://github.com/deepseek-ai/lplb/blob/main/pyproject.toml"
click node_topology_example "https://github.com/deepseek-ai/lplb/blob/main/scripts/run_ep16_cube8p2e.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_api,node_planner,node_eplb,node_r2o toneBlue
class node_plugin,node_deepep_header,node_minilp toneAmber
class node_workload_state,node_ep_group,node_deepep toneMint
class node_cuda_mathdx,node_mathdx_download,node_package_build,node_pyproject toneRose
class node_logical_indices,node_physical_indices,node_topology_example toneNeutral
```

## Components

### Python planning

- [Package exports](https://github.com/deepseek-ai/lplb/blob/main/lplb/__init__.py): Python package · Public import surface for Planner. · `lplb/__init__.py`
- [Planner](https://github.com/deepseek-ai/lplb/blob/main/lplb/planner.py): routing orchestrator · Per-batch boundary from logical routing to physical expert IDs. · `lplb/planner.py`
- [Embedded EPLB](https://github.com/deepseek-ai/lplb/blob/main/lplb/eplb.py): static placement · Selects and reorders experts before dynamic routing. · `lplb/eplb.py`
- **r2o topology mapping**: tensor contract · Maps redundant physical experts to original logical experts.

### CUDA optimization

- [Python CUDA plugin](https://github.com/deepseek-ai/lplb/blob/main/csrc/plugin.cpp): native extension bridge · Exposes compiled solver and runtime integration to Python. · `csrc/plugin.cpp`
- [Slim DeepEP interface](https://github.com/deepseek-ai/lplb/blob/main/csrc/deepep_rt_slim.h): native runtime interface · Provides optional DeepEP-facing extension facilities. · `csrc/deepep_rt_slim.h`
- [MiniLP CUDA solver](https://github.com/deepseek-ai/lplb/blob/main/lplb/resources/csrc-tmpl/minilp.cu): interior-point LP kernel · Single-SM LP solve for capacity-constrained redistribution. · `lplb/resources/csrc-tmpl/minilp.cu`

### Distributed runtime

- **Workload state**: capacity input · Caller counters or synchronized availability state.
- **EP process group**: PyTorch distributed scope · Defines ranks participating in planning.
- [DeepEP runtime](https://github.com/deepseek-ai/lplb/blob/main/csrc/deepep_rt_slim.h): optional communication runtime · Preferred low-overhead workload synchronization path. · `csrc/deepep_rt_slim.h`

### Build dependencies

- **CUDA and MathDx**: native toolchain · CUDA 12.6.3+, cuSolverDx, and cuBLASDx are required.
- [MathDx downloader](https://github.com/deepseek-ai/lplb/blob/main/download-mathdx.sh): dependency script · `download-mathdx.sh`
- [Package build](https://github.com/deepseek-ai/lplb/blob/main/setup.py): Python build metadata · Builds the CUDA extension for the Python package. · `setup.py`
- [Project metadata](https://github.com/deepseek-ai/lplb/blob/main/pyproject.toml): Python packaging · `pyproject.toml`

### Other components

- **Logical token indices**: model input
- **Physical expert indices**: routing output · Original routing shape with replica-selected physical IDs.
- [EP16 topology example](https://github.com/deepseek-ai/lplb/blob/main/scripts/run_ep16_cube8p2e.py): runnable example · Demonstrates cube-style redundant-expert setup. · `scripts/run_ep16_cube8p2e.py`

## Connections

- 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
