dynamo_run.md 17.4 KB
Newer Older
1
# Dynamo Run
2

3
`dynamo-run` is a Rust binary that lets you easily run a model, explore the Dynamo components, and demonstrates the Rust API. It supports the `mistral.rs` and `llama.cpp` engines. `mistralrs` is the default engine.
4

5
It is primarily for development and rapid prototyping. For production use we recommend the Python wrapped components, see the main project README.
6

7
## Basics
8

9
Usage: See `dynamo-run --help`
10

atchernych's avatar
atchernych committed
11
Example: `dynamo-run Qwen/Qwen3-0.6B`
12

13
Set the environment variable `DYN_LOG` to adjust the logging level; for example, `export DYN_LOG=debug`. It has the same syntax as `RUST_LOG`.
14

15
16
17
To adjust verbosity, use `-v` to enable debug logging or `-vv` to enable full trace logging. For example:

```bash
18
19
dynamo-run in=http out=mistralrs <model> -v  # enables debug logging
dynamo-run in=text out=llamacpp <model> -vv  # enables full trace logging
20
21
```

22
### Use model from Hugging Face
23

24
To automatically download Qwen3 4B from Hugging Face (16 GiB download) and to start it in interactive text mode:
25
```
26
dynamo-run Qwen/Qwen3-4B
27
28
```

29
The general format for HF download follows this pattern:
30
```
atchernych's avatar
atchernych committed
31
dynamo-run out=<engine> <HUGGING_FACE_ORGANIZATION/MODEL_NAME>
32
33
```

34
For gated models (such as meta-llama/Llama-3.2-3B-Instruct), you must set an `HF_TOKEN` environment variable.
35

36
The parameter can be the ID of a HuggingFace repository (which will be downloaded) or a folder containing safetensors, config.json, or similar (perhaps a locally checked out HuggingFace repository).
37

38
### Run a model from local file
39

40
41
42
43
44
To run a model from local file:
- Download the model from Hugging Face
- Run the model from local file

See the following sections for details.
45

46
#### Download model from Hugging Face
47
This model available from Hugging Face should be high quality and fast on almost any machine: https://huggingface.co/Qwen/Qwen3-0.6B
48

49
50
51
To run the model:

*Text interface*
52
```
53
dynamo-run Qwen/Qwen3-0.6B
54
```
55

56
57
You can also pipe a prompt into `dynamo-run`:
```
58
echo 'What is the capital of Tuvalu?' | dynamo-run Qwen/Qwen3-0.6B --context-length 4096
59
60
```

61
*HTTP interface*
62
```
63
dynamo-run in=http out=mistralrs Qwen/Qwen3-0.6B
64
```
65
You can also list models or send a request:
66

67
*List the models*
68
```
69
curl localhost:8080/v1/models
70
```
71

72
*Send a request*
73
```
74
curl -d '{"model": "Qwen/Qwen3-0.6B", "max_completion_tokens": 2049, "messages":[{"role":"user", "content": "What is the capital of South Africa?" }]}' -H 'Content-Type: application/json' http://localhost:8080/v1/chat/completions
75
```
76

77
## Distributed System
78
79

You can run the ingress side (HTTP server and pre-processing) on one machine, for example a CPU node, and the worker on a different machine (a GPU node).
80

81
You will need [etcd](https://etcd.io/) and [nats](https://nats.io) with jetstream installed and accessible from both nodes. For development I run NATS like this: `nats-server -js --trace --store_dir $(mktemp -d)`.
82

83
**Node 1:** OpenAI compliant HTTP server, optional pre-processing, worker discovery:
84

85
```
86
dynamo-run in=http out=auto
87
```
88

89
**Node 2:** Engine. Receives and returns requests over the network:
90

91
```
92
dynamo-run in=dyn://llama3B.backend.generate out=mistralrs ~/llms/Llama-3.2-3B-Instruct
93
```
94

95
96
This uses etcd to auto-discover the model and NATS to talk to it. You can
run multiple instances on the same endpoint; it picks one based on the
97
`--router-mode` (round-robin by default if left unspecified).
98

99
Run `dynamo-run --help` for more options.
100

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
### Network names

The `in=dyn://` URLs have the format `dyn://namespace.component.endpoint`. For quickstart just use any string `dyn://test`, `dynamo-run` will default any missing parts for you. The pieces matter for a larger system.

* *Namespace*: A pipeline. Usually a model. e.g "llama_8b". Just a name.
* *Component*: A load balanced service needed to run that pipeline. "backend", "prefill", "decode", "preprocessor", "draft", etc. This typically has some configuration (which model to use, for example).
* *Endpoint*: Like a URL. "generate", "load_metrics".
* *Instance*: A process. Unique. Dynamo assigns each one a unique instance_id. The thing that is running is always an instance. Namespace/component/endpoint can refer to multiple instances.

If you run two models, that is two pipelines. An exception would be if doing speculative decoding. The draft model is part of the pipeline of a bigger model.

If you run two instances of the same model ("data parallel") they are the same namespace+component+endpoint but different instances. The router will spread traffic over all the instances of a namespace+component+endpoint. If you have four prefill workers in a pipeline, they all have the same namespace+component+endpoint and are automatically assigned unique instance_ids.

Example 1: Data parallel load balanced, one model one pipeline two instances.
```
116
117
Node 1: dynamo-run in=dyn://qwen3-32b.backend.generate /data/Qwen3-32B
Node 2: dynamo-run in=dyn://qwen3-32b.backend.generate /data/Qwen3-32B
118
119
120
121
```

Example 2: Two models, two pipelines.
```
122
123
Node 1: dynamo-run in=dyn://qwen3-32b.backend.generate /data/Qwen3-32B
Node 2: dynamo-run in=dyn://llama3-1-8b.backend.generate /data/Llama-3.1-8B-Instruct/
124
125
126
127
128
129
```

Example 3: Different endpoints.

The KV metrics publisher in VLLM adds a `load_metrics` endpoint to the current component. If the `llama3-1-8b.backend` component above is using patched vllm it will also expose `llama3-1-8b.backend.load_metrics`.

130
Example 4: Multiple component in a pipeline.
131

132
In the P/D disaggregated setup you would have `deepseek-distill-llama8b.prefill.generate` (possibly multiple instances of this) and `deepseek-distill-llama8b.decode.generate`.
133

134
135
For output it is always only `out=auto`. This tells Dynamo to auto-discover the instances, group them by model, and load balance appropriately (depending on `--router-mode` flag). The exception is static workers, see that section.

136
137
138
### KV-aware routing

```
139
dynamo-run in=http out=auto --router-mode kv
140
141
```

142
The only difference from the distributed system above is `--router-mode kv`. vllm announces when a KV block is created or removed. The Dynamo router finds the worker with the best match for those KV blocks and directs the traffic to that node.
143

144
For performance testing, compare a typical workload with `--router-mode random|round-robin` to see if it can benefit from KV-aware routing.
145

146
147
148
149
150
151
152
The KV-aware routing arguments:

- `--kv-overlap-score-weight`: Sets the amount of weighting on overlaps with prefix caches, which directly contributes to the prefill cost. A large weight is expected to yield a better TTFT (at the expense of worse ITL). When set to 0, prefix caches are not considered at all (falling back to pure load balancing behavior on the active blocks).

- `--router-temperature`: Sets the temperature when randomly selecting workers to route to via softmax sampling on the router cost logits. Setting it to 0 recovers the deterministic behavior where the min logit is picked.

- `--use-kv-events`: Sets whether to listen to KV events for maintaining the global view of cached blocks. If true, then we use the `KvIndexer` to listen to the block creation and deletion events. If false, `ApproxKvIndexer`, which assumes the kv cache of historical prompts exists for fixed time durations (hard-coded to 120s), is used to predict the kv cache hit ratio in each engine. Set false if your backend engine does not emit KV events.
153

154
155
### Request Migration

156
In a [Distributed System](#distributed-system), you can enable [request migration](../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:
157
158

```bash
159
dynamo-run in=dyn://... out=<engine> ... --migration-limit=3
160
161
```

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

164
165
166
167
168
169
### Request Cancellation

When using the HTTP interface (`in=http`), if the HTTP request connection is dropped by the client, Dynamo automatically cancels the downstream request to the worker. This ensures that computational resources are not wasted on generating responses that are no longer needed.

For detailed information about how request cancellation works across the system, see the [Request Cancellation Architecture](../architecture/request_cancellation.md) documentation.

170
## Development
171

172
`dynamo-run` is also an example of what can be built in Rust with the `dynamo-llm` and `dynamo-runtime` crates. The following guide shows how to build from source with all the features.
173

174
### Step 1: Install libraries
175
**Ubuntu:**
176
```
177
sudo apt install -y build-essential libhwloc-dev libudev-dev pkg-config libssl-dev libclang-dev protobuf-compiler python3-dev cmake
178
179
```

180
**macOS:**
181
182
183
184
185
186
187
- [Homebrew](https://brew.sh/)
```
# if brew is not installed on your system, install it
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
- [Xcode](https://developer.apple.com/xcode/)

188
189
```
brew install cmake protobuf
190

191
## Check that Metal is accessible
192
193
xcrun -sdk macosx metal
```
194
If Metal is accessible, you should see an error like `metal: error: no input files`, which confirms it is installed correctly.
195

196
### Step 2: Install Rust
197
```
198
199
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
200
```
201

202
### Step 3: Build
203

204
- Linux with GPU and CUDA (tested on Ubuntu):
205
```
206
cargo build --features cuda
207
```
208

209
- macOS with Metal:
210
```
211
cargo build --features metal
212
213
```

214
- CPU only:
215
```
216
cargo build
217
218
```

219
220
221
Optionally you can run `cargo build` from any location with arguments:

```
222
223
--target-dir /path/to/target_directory # specify target_directory with write privileges
--manifest-path /path/to/project/Cargo.toml # if cargo build is run outside of `launch/` directory
224
225
```

226
The binary is called `dynamo-run` in `target/debug`
227
```
228
cd target/debug
229
230
```

231
232
Build with `--release` for a smaller binary and better performance, but longer build times. The binary will be in `target/release`.

233

234
## Engines
235

236
The input defaults to `in=text`. The output defaults to `out=mistralrs` engine, unless it is disabled with `--no-default-features` in which case an engine that echo's back your input is used.
237

238
### mistralrs
239

240
[mistral.rs](https://github.com/EricLBuehler/mistral.rs) is a pure Rust engine that is fast to run and fast to load, and runs well on CPU as well as GPU. For those reasons it is the default engine.
241
242

```
243
dynamo-run Qwen/Qwen3-4B
244
245
246
247
248
```

is equivalent to

```
249
dynamo-run in=text out=mistralrs Qwen/Qwen3-4B
250
251
```

252
If you have multiple GPUs, `mistral.rs` does automatic tensor parallelism. You do not need to pass any extra flags to dynamo-run to enable it.
253

254
### Mocker engine
Graham King's avatar
Graham King committed
255

256
The mocker engine is a mock vLLM implementation designed for testing and development purposes. It simulates realistic token generation timing without requiring actual model inference, making it useful for:
257

258
259
260
261
- Testing distributed system components without GPU resources
- Benchmarking infrastructure and networking overhead
- Developing and debugging Dynamo components
- Load testing and performance analysis
Graham King's avatar
Graham King committed
262

263
**Basic usage:**
264

265
The `--model-path` is required but can point to any valid model path - the mocker doesn't actually load the model weights (but the pre-processor needs the tokenizer). The arguments `block_size`, `num_gpu_blocks`, `max_num_seqs`, `max_num_batched_tokens`, `enable_prefix_caching`, and `enable_chunked_prefill` are common arguments shared with the real VLLM engine.
266

267
268
269
270
And below are arguments that are mocker-specific:
- `speedup_ratio`: Speed multiplier for token generation (default: 1.0). Higher values make the simulation engines run faster.
- `dp_size`: Number of data parallel workers to simulate (default: 1)
- `watermark`: KV cache watermark threshold as a fraction (default: 0.01). This argument also exists for the real VLLM engine but cannot be passed as an engine arg.
271

272
273
274
275
```bash
echo '{"speedup_ratio": 10.0}' > mocker_args.json
dynamo-run in=dyn://dynamo.mocker.generate out=mocker --model-path TinyLlama/TinyLlama-1.1B-Chat-v1.0 --extra-engine-args mocker_args.json
dynamo-run in=http out=auto --router-mode kv
276
```
277

278
### echo
279

280
The `echo` engine echoes the prompt back as the response.
281
282

```
283
dynamo-run in=http out=echo --model-name my_model
284
285
```

286
The echo engine uses a configurable delay between tokens to simulate generation speed. You can adjust this using the `DYN_TOKEN_ECHO_DELAY_MS` environment variable:
287
288
289

```
# Set token echo delay to 1ms (1000 tokens per second)
290
DYN_TOKEN_ECHO_DELAY_MS=1 dynamo-run in=http out=echo
291
292
293
```

The default delay is 10ms, which produces approximately 100 tokens per second.
294

295
296
297
298
299
300
301
### Other engines, multi-node, production

`vllm`, `sglang` and `trtllm` production grade engines are available in `components/backends`. They run as Python components, using the Rust bindings. See the main README.

`dynamo-run` is an exploration, development and prototyping tool, as well as an example of using the Rust API. Multi-node and production setups should be using the main engine components.

## Batch mode
302

303
`dynamo-run` can take a jsonl file full of prompts and evaluate them all:
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

```
dynamo-run in=batch:prompts.jsonl out=llamacpp <model>
```

The input file should look like this:
```
{"text": "What is the capital of France?"}
{"text": "What is the capital of Spain?"}
```

Each one is passed as a prompt to the model. The output is written back to the same folder in `output.jsonl`. At the end of the run some statistics are printed.
The output looks like this:
```
{"text":"What is the capital of France?","response":"The capital of France is Paris.","tokens_in":7,"tokens_out":7,"elapsed_ms":1566}
{"text":"What is the capital of Spain?","response":".The capital of Spain is Madrid.","tokens_in":7,"tokens_out":7,"elapsed_ms":855}
```

322
## Writing your own engine in Python
323

324
The [dynamo](https://pypi.org/project/ai-dynamo/) Python library allows you to build your own engine and attach it to Dynamo. All of the main backend components in `components/backends/` work like this.
325
326
327
328
329
330
331

The Python file must do three things:
1. Decorate a function to get the runtime
2. Register on the network
3. Attach a request handler

```
332
from dynamo.llm import ModelInput, ModelType, register_llm
333
334
from dynamo.runtime import DistributedRuntime, dynamo_worker

335
336
337
338
   # 1. Decorate a function to get the runtime
   #
   @dynamo_worker(static=False)
   async def worker(runtime: DistributedRuntime):
339
340
341
342
343

    # 2. Register ourselves on the network
    #
    component = runtime.namespace("namespace").component("component")
    await component.create_service()
344
    model_path = "Qwen/Qwen3-0.6B" # or "/data/models/Qwen3-0.6B"
345
346
    model_input = ModelInput.Tokens # or ModelInput.Text if engine handles pre-processing
    model_type = ModelType.Chat # or ModelType.Chat | ModelType.Completions if model can be deployed on chat and completions endpoints
347
    endpoint = component.endpoint("endpoint")
348
    # Optional last param to register_llm is model_name. If not present derives it from model_path
349
    await register_llm(model_input, model_type, endpoint, model_path)
350
351
352
353
354
355

    # Initialize your engine here
    # engine = ...

    # 3. Attach request handler
    #
356
    await endpoint.serve_endpoint(RequestHandler(engine).generate)
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374

class RequestHandler:

    def __init__(self, engine):
        ...

    async def generate(self, request):
        # Call the engine
        # yield result dict
        ...

if __name__ == "__main__":
    uvloop.install()
    asyncio.run(worker())
```


The `model_path` can be:
375
- A HuggingFace repo ID, optionally prefixed with `hf://`. It is downloaded and cached locally.
376
377
- The path to a checkout of a HuggingFace repo - any folder containing safetensor files as well as `config.json`, `tokenizer.json` and `tokenizer_config.json`.

378
379
380
381
The `model_input` can be:
- ModelInput.Tokens. Your engine expects pre-processed input (token IDs). Dynamo handles tokenization and pre-processing.
- ModelInput.Text. Your engine expects raw text input and handles its own tokenization and pre-processing.

382
The `model_type` can be:
383
384
- ModelType.Chat. Your `generate` method receives a `request` and must return a response dict of type [OpenAI Chat Completion](https://platform.openai.com/docs/api-reference/chat).
- ModelType.Completions. Your `generate` method receives a `request` and must return a response dict of the older [Completions](https://platform.openai.com/docs/api-reference/completions).
385

386
`register_llm` can also take the following kwargs:
387
- `model_name`: The name to call the model. Your incoming HTTP requests model name must match this. Defaults to the hugging face repo name, or the folder name.
388
389
- `context_length`: Max model length in tokens. Defaults to the model's set max. Only set this if you need to reduce KV cache allocation to fit into VRAM.
- `kv_cache_block_size`: Size of a KV block for the engine, in tokens. Defaults to 16.
390
- `user_data`: Optional dictionary containing custom metadata for worker behavior (e.g., LoRA configuration). Defaults to None.
391

392
Here are some example engines:
393
394
395
396
397
398
399

- Backend:
    * [vllm](https://github.com/ai-dynamo/dynamo/blob/main/lib/bindings/python/examples/hello_world/server_vllm.py)
    * [sglang](https://github.com/ai-dynamo/dynamo/blob/main/lib/bindings/python/examples/hello_world/server_sglang.py)
- Chat:
    * [sglang](https://github.com/ai-dynamo/dynamo/blob/main/lib/bindings/python/examples/hello_world/server_sglang_tok.py)

400
More fully-featured Python engines are in `components/backends`.
401

402
## Debugging
403
404
405
406
407
408
409
410

`dynamo-run` and `dynamo-runtime` support [tokio-console](https://github.com/tokio-rs/console). Build with the feature to enable:
```
cargo build --features cuda,tokio-console -p dynamo-run
```

The listener uses the default tokio console port, and all interfaces (0.0.0.0).