README.md 11.2 KB
Newer Older
1
2
3
4
5
<!--
SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->

6
# Running SGLang with Dynamo
7

8
This directory contains an SGLang component for Dynamo and reference implementations for deploying Large Language Models (LLMs) in various configurations using SGLang. SGLang internally uses ZMQ to communicate between the ingress and the engine processes. For Dynamo, we leverage the runtime to communicate directly with the engine processes and handle ingress and pre/post processing on our end.
9

10
11
12
13
14
15
16
17
18
19
20
21
## Use the Latest Release

We recommend using the latest stable release of dynamo to avoid breaking changes:

[![GitHub Release](https://img.shields.io/github/v/release/ai-dynamo/dynamo)](https://github.com/ai-dynamo/dynamo/releases/latest)

You can find the latest release [here](https://github.com/ai-dynamo/dynamo/releases/latest) and check out the corresponding branch with:

```bash
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
```

22
---
23

24
25
## Table of Contents
- [Feature Support Matrix](#feature-support-matrix)
26
- [Dynamo SGLang Integration](#dynamo-sglang-integration)
27
28
29
30
- [Quick Start](#quick-start)
- [Single Node Examples](#run-single-node-examples)
- [Multi-Node and Advanced Examples](#advanced-examples)
- [Deploy on SLURM or Kubernetes](#deployment)
31

32
## Feature Support Matrix
33

34
### Core Dynamo Features
35

36
37
| Feature | SGLang | Notes |
|---------|--------|-------|
38
39
40
41
42
43
44
| [**Disaggregated Serving**](../../architecture/disagg_serving.md) | ✅ |  |
| [**Conditional Disaggregation**](../../architecture/disagg_serving.md#conditional-disaggregation) | 🚧 | WIP [PR](https://github.com/sgl-project/sglang/pull/7730) |
| [**KV-Aware Routing**](../../architecture/kv_cache_routing.md) | ✅ |  |
| [**SLA-Based Planner**](../../architecture/sla_planner.md) | ✅ |  |
| [**Multimodal EPD Disaggregation**](multimodal_epd.md) | ✅ |  |
| [**Load Based Planner**](../../architecture/load_planner.md) | ❌ | Planned |
| [**KVBM**](../../architecture/kvbm_architecture.md) | ❌ | Planned |
45

46
47
### Large Scale P/D and WideEP Features

ishandhanani's avatar
ishandhanani committed
48
49
50
51
52
| Feature             | SGLang | Notes                                                        |
|---------------------|--------|--------------------------------------------------------------|
| **WideEP**          | ✅     | Full support on H100s/GB200                                  |
| **DP Rank Routing** | 🚧     | Direct routing supported. Dynamo KV router does not router to DP worker |
| **GB200 Support**   | ✅     |                                                              |
53
54


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
## Dynamo SGLang Integration

Dynamo SGLang integrates SGLang engines into Dynamo's distributed runtime, enabling advanced features like disaggregated serving, KV-aware routing, and request migration while maintaining full compatibility with SGLang's engine arguments.

### Argument Handling

Dynamo SGLang uses SGLang's native argument parser, so **most SGLang engine arguments work identically**. You can pass any SGLang argument (like `--model-path`, `--tp`, `--trust-remote-code`) directly to `dynamo.sglang`.

#### Dynamo-Specific Arguments

| Argument | Description | Default | SGLang Equivalent |
|----------|-------------|---------|-------------------|
| `--endpoint` | Dynamo endpoint in `dyn://namespace.component.endpoint` format | Auto-generated based on mode | N/A |
| `--migration-limit` | Max times a request can migrate between workers | `0` (disabled) | N/A |
| `--dyn-tool-call-parser` | Tool call parser for structured outputs (takes precedence over `--tool-call-parser`) | `None` | `--tool-call-parser` |
| `--dyn-reasoning-parser` | Reasoning parser for CoT models (takes precedence over `--reasoning-parser`) | `None` | `--reasoning-parser` |
| `--use-sglang-tokenizer` | Use SGLang's tokenizer instead of Dynamo's | `False` | N/A |
72
| `--custom-jinja-template` | Use custom chat template for that model (takes precedence over default chat template in model repo) | `None` | `--chat-template` |
73
74
75
76
77
78
79
80

#### Tokenizer Behavior

- **Default (`--use-sglang-tokenizer` not set)**: Dynamo handles tokenization and passes `input_ids` to SGLang
- **With `--use-sglang-tokenizer`**: SGLang handles tokenization, Dynamo passes raw prompts

> **Note**: When using `--use-sglang-tokenizer`, only `v1/chat/completions` endpoints are available through Dynamo's frontend.

81
## SGLang Quick Start
82

83
Below we provide a guide that lets you run all of our common deployment patterns on a single node.
84
85
86

### Start NATS and ETCD in the background

87
Start using [Docker Compose](../../../deploy/docker-compose.yml)
88

89
```bash
90
docker compose -f deploy/docker-compose.yml up -d
91
92
```

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
### Install `ai-dynamo[sglang]`

#### Install latest release
We suggest using uv to install the latest release of ai-dynamo[sglang]. You can install it with `curl -LsSf https://astral.sh/uv/install.sh | sh`

```bash
# create a virtual env
uv venv --python 3.12 --seed
# install the latest release
uv pip install "ai-dynamo[sglang]"
```

#### Installing editable version for development

<details>
<summary>Instructions</summary>

This requires having rust installed. We also recommend having a proper installation of the cuda toolkit as sglang requires `nvcc` to be available.

```bash
# create a virtual env
uv venv --python 3.12 --seed
# build dynamo runtime bindings
uv pip install maturin
cd $DYNAMO_HOME/lib/bindings/python
maturin develop --uv
cd $DYNAMO_HOME
120
121
122
# installs sglang supported version along with dynamo
# include the prerelease flag to install flashinfer rc versions
uv pip install --prerelease=allow -e .[sglang]
123
124
125
126
127
128
129
130
```

</details>

#### Using prebuilt docker containers

<details>
<summary>Instructions</summary>
131
132

```bash
133
docker pull nvcr.io/nvidia/ai-dynamo/sglang-runtime:my-tag
134
135
```

136
137
138
139
140
141
</details>

#### Building docker container from source

<details>
<summary>Instructions</summary>
142
143

```bash
144
145
146
147
148
./container/build.sh --framework sglang
# run container using prebuild wheel
./container/run.sh --framework sglang -it
# mount workspace for development
./container/run.sh --framework sglang --mount-workspace
149
150
```

151
152
</details>

153
## Run Single Node Examples
154

155
> [!IMPORTANT]
156
157
158
> Each example corresponds to a simple bash script that runs the OpenAI compatible server, processor, and optional router (written in Rust) and LLM engine (written in Python) in a single terminal. You can easily take each command and run them in separate terminals.
>
> Additionally - because we use sglang's argument parser, you can pass in any argument that sglang supports to the worker!
159

160
161

### Aggregated Serving
162
163

```bash
164
cd $DYNAMO_HOME/components/backends/sglang
165
./launch/agg.sh
166
```
167

168
### Aggregated Serving with KV Routing
169

170
171
172
> [!NOTE]
> Until sglang releases a version > v0.5.0rc0, you will have to install from source to use kv_routing. You can do this by running `git clone https://github.com/sgl-project/sglang.git && cd sglang && uv pip install -e "python[all]"`. We will update this section once sglang releases a newer version.

173
```bash
174
cd $DYNAMO_HOME/components/backends/sglang
175
./launch/agg_router.sh
176
177
```

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
### Aggregated Serving with Embeddings

Here's an example that uses the [Qwen/Qwen3-Embedding-4B](https://huggingface.co/Qwen/Qwen3-Embedding-4B) model.

```bash
cd $DYNAMO_HOME/components/backends/sglang
./launch/agg_embed.sh
```

Send the following request to verify your deployment:

```bash
curl localhost:8000/v1/embeddings \
  -H "Content-Type: application/json" \
  -d '{
    "model": "Qwen/Qwen3-Embedding-4B",
    "input": "Hello, world!"
  }'
```

198
### Disaggregated serving
199

200
<details>
201
<summary>Under the hood: SGLang Load Balancer vs Dynamo Discovery</summary>
202
203

SGLang uses a mini load balancer to route requests to handle disaggregated serving. The load balancer functions as follows:
204
205
206
207
208
209
210
211

1. The load balancer receives a request from the client
2. A random `(prefill, decode)` pair is selected from the pool of available workers
3. Request is sent to both `prefill` and `decode` workers via asyncio tasks
4. Internally disaggregation is done from prefill -> decode

Because Dynamo has a discovery mechanism, we do not use a load balancer. Instead, we first route to a random prefill worker, select a random decode worker, and then send the request to both. Internally, SGLang's bootstrap server (which is a part of the `tokenizer_manager`) is used in conjuction with NIXL to handle the kv transfer.

212
213
</details>

214
215
216
217
> [!IMPORTANT]
> Disaggregated serving in SGLang currently requires each worker to have the same tensor parallel size [unless you are using an MLA based model](https://github.com/sgl-project/sglang/pull/5922)

```bash
218
cd $DYNAMO_HOME/components/backends/sglang
219
./launch/disagg.sh
220
```
221

222
### Disaggregated Serving with Mixture-of-Experts (MoE) models and DP attention
223

224
You can use this configuration to test out disaggregated serving with dp attention and expert parallelism on a single node before scaling to the full DeepSeek-R1 model across multiple nodes.
225
226
227

```bash
# note this will require 4 GPUs
228
cd $DYNAMO_HOME/components/backends/sglang
229
./launch/disagg_dp_attn.sh
230
```
231

232
When using MoE models, you can also use the our implementation of the native SGLang endpoints to record expert distribution data. The `disagg_dp_attn.sh` script automatically sets up the SGLang HTTP server, the environment variable that controls the expert distribution recording directory, and sets up the expert distribution recording mode to `stat`. You can learn more about expert parallelism load balancing [here](expert-distribution-eplb.md).
233

234
235
236
237
238
239
240
241
### Testing the Deployment

Send a test request to verify your deployment:

```bash
curl localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
242
    "model": "Qwen/Qwen3-0.6B",
243
244
245
246
247
248
    "messages": [
    {
        "role": "user",
        "content": "Explain why Roger Federer is considered one of the greatest tennis players of all time"
    }
    ],
249
    "stream": true,
250
251
252
253
    "max_tokens": 30
  }'
```

254
255
## Request Migration

256
You can enable [request migration](../../../docs/architecture/request_migration.md) to handle worker failures gracefully. Use the `--migration-limit` flag to specify how many times a request can be migrated to another worker:
257
258
259
260
261

```bash
python3 -m dynamo.sglang ... --migration-limit=3
```

262
This allows a request to be migrated up to 3 times before failing. See the [Request Migration Architecture](../../../docs/architecture/request_migration.md) documentation for details on how this works.
263

264
265
266
267
## Advanced Examples

Below we provide a selected list of advanced examples. Please open up an issue if you'd like to see a specific example!

ishandhanani's avatar
ishandhanani committed
268
### Run a multi-node sized model
269
- **[Run a multi-node model](multinode-examples.md)**
270
271

### Large scale P/D disaggregation with WideEP
272
273
- **[Run DeepSeek-R1 on 104+ H100s](dsr1-wideep-h100.md)**
- **[Run DeepSeek-R1-FP8 on GB200s](dsr1-wideep-gb200.md)**
274

275
### Hierarchical Cache (HiCache)
276
- **[Enable SGLang Hierarchical Cache (HiCache)](sgl-hicache-example.md)**
277

278
### Multimodal Encode-Prefill-Decode (EPD) Disaggregation with NIXL
279
- **[Run a multimodal model with EPD Disaggregation](multimodal_epd.md)**
280

281
282
## Deployment

283
We currently provide deployment examples for Kubernetes and SLURM.
284

285
## Kubernetes
286
- **[Deploying Dynamo with SGLang on Kubernetes](../../../components/backends/sglang/deploy/README.md)**
287

288
## SLURM
289
- **[Deploying Dynamo with SGLang on SLURM](../../../components/backends/sglang/slurm_jobs/README.md)**