-
Notifications
You must be signed in to change notification settings - Fork 123
[Doc] Add doc for miles router #538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,93 @@ | ||||||
| # Miles Router | ||||||
|
|
||||||
| miles includes an optional Miles Router used during rollout / data generation. It is a lightweight HTTP router/proxy that sits in front of one or more SGLang worker servers and adds training-oriented capabilities that are not the main goal of serving-focused routers. | ||||||
|
|
||||||
| --- | ||||||
|
|
||||||
| ## 1. What is Miles Router? | ||||||
|
|
||||||
| Miles Router is a small FastAPI service that: | ||||||
|
|
||||||
| - Registers workers (SGLang HTTP servers) into a local pool | ||||||
| - Routes requests to a selected worker (simple least-inflight load balancing) | ||||||
| - Proxies arbitrary paths to the selected worker (e.g. `/generate`) | ||||||
|
Comment on lines
+11
to
+13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The MilesRouter implements a proxying mechanism that allows unauthenticated registration of arbitrary worker URLs via the /add_worker endpoint. This creates a significant Server-Side Request Forgery (SSRF) risk, as an attacker can register internal or restricted URLs and then use the router to access them. Since this documentation introduces the feature, it should include a prominent security warning, and the underlying implementation in miles/router/router.py should be updated to include authentication and URL validation. |
||||||
| - Runs periodic health checks and quarantines unhealthy workers | ||||||
| - Supports middleware plugins (via `--miles-router-middleware-paths`) to implement rollout-specific processing (e.g. caching, request/response transforms) | ||||||
|
|
||||||
| In miles's architecture, the router is part of the rollout system ("SGLang + router") that generates samples and pushes them into the data buffer. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The possessive form
Suggested change
zhaochenyang20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| ### How it is launched | ||||||
|
|
||||||
| In distributed training, miles will start a router automatically when `--sglang-router-ip` is not provided: | ||||||
|
|
||||||
| - If `--use-miles-router` is set, miles starts Miles Router | ||||||
zhaochenyang20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| - Otherwise, miles starts SGLang Model Gateway | ||||||
zhaochenyang20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| --- | ||||||
|
|
||||||
| ## 2. Why we need Miles Router | ||||||
|
|
||||||
| Unlike production inference, RL rollout needs to capture additional metadata for training: token-level logprobs, loss masks, and (for MoE models) expert routing decisions. Miles Router provides these capabilities through its middleware system and passthrough proxy design. | ||||||
|
|
||||||
| ### 2.1 Radix-tree cache (transparent token management) | ||||||
|
|
||||||
| > Use this when your rollout pipeline is text-in/text-out and you cannot reliably persist token IDs; if you already control token-in/token-out (e.g. search r1, multiturn VLM examples), you likely don't need the radix-tree cache. | ||||||
|
|
||||||
| Text-in text-out interfaces can cause token retokenization mismatches - re-tokenizing text at training time may produce different token sequences than rollout, breaking per-token alignment needed for PPO/GRPO losses. | ||||||
|
|
||||||
| The radix-tree cache solves this transparently: it intercepts text-based requests, tokenizes them, and stores trajectories (text, token IDs, logprobs, loss masks) keyed by the text prefix. After rollout finishes, calling `/retrieve_from_text` returns the exact token sequence with aligned metadata, without requiring any changes to your rollout code. | ||||||
|
|
||||||
| Implementation-wise, the radix-tree cache: | ||||||
|
|
||||||
| - Accepts text plus tokens/metadata and stores them in a radix tree | ||||||
| - Uses longest-prefix matching to reuse cached token sequences (enabling token-in/token-out downstream) | ||||||
| - Allows insertion of new text continuations as rollout proceeds (multiple trajectories per prompt, e.g. GRPO) | ||||||
| - Periodically cleans up stale nodes to control memory usage | ||||||
|
|
||||||
| Use the radix cache when you have text-based rollout code and want token-level precision without rewriting, or when running GRPO with multiple trajectories sharing the same prompt prefix. | ||||||
|
Comment on lines
+32
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||||||
|
|
||||||
| ### 2.2 Rollout routing replay (R3) for MoE | ||||||
|
|
||||||
| For MoE models, miles supports rollout routing replay (R3): record expert routing decisions during rollout and replay them during training to improve stability. | ||||||
|
|
||||||
| #### SGLang side | ||||||
|
|
||||||
| SGLang provides expert routing capture via: | ||||||
|
|
||||||
| - `--enable-return-routed-experts`: server argument to enable routing capture | ||||||
| - `RoutedExpertsCapturer`: captures `topk_ids` (selected expert IDs) at each MoE layer during forward pass | ||||||
| - `return_routed_experts`: request parameter to retrieve routing data | ||||||
| - Returns `routed_experts` in response `meta_info` - a `[seq_len - 1, num_layers, top_k]` tensor of expert IDs | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence could be clearer. I suggest rephrasing to explicitly state that
Suggested change
|
||||||
|
|
||||||
| #### miles side | ||||||
zhaochenyang20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| miles consumes the routing data and replays it during training: | ||||||
zhaochenyang20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| - `--use-miles-router --use-rollout-routing-replay`: both flags required to enable R3 | ||||||
| - Rollout sends `return_routed_experts=True` and stores results in `sample.rollout_routed_experts` | ||||||
| - Training calls `fill_routing_replay()` to load routing data into `RoutingReplay` objects | ||||||
| - During forward pass, recorded routing decisions are replayed instead of recomputed | ||||||
|
|
||||||
| #### Why Miles Router is needed | ||||||
|
|
||||||
| We need Miles Router because the SGLang worker returns routed experts in the response (`meta_info.routed_experts`) when the request sets `return_routed_experts=true`, and Miles Router preserves this field end-to-end. SGLang Model Gateway may drop this extra metadata when it reconstructs responses with a fixed schema (see section 3). | ||||||
|
|
||||||
| --- | ||||||
|
|
||||||
| ## 3. Differences vs SGLang Model Gateway | ||||||
|
|
||||||
| Miles Router and SGLang Model Gateway can both route requests to workers, but they are optimized for different goals. | ||||||
|
|
||||||
| ### Key differences | ||||||
|
|
||||||
| Miles Router is a lightweight Python/FastAPI proxy that acts as a passthrough to SGLang workers. This passthrough design enables RL-specific features like radix-tree trajectory caching and R3 (which require preserving raw response metadata like `routed_experts`). | ||||||
|
|
||||||
| SGLang Model Gateway is a high-performance Rust-based router optimized for large-scale inference: async non-blocking routing, advanced fault tolerance (retries, circuit breakers), multiple load balancing policies (including cache-aware routing), and PD disaggregation support. However, it reconstructs responses with a fixed schema, so it does not preserve the metadata needed for miles's R3 flow. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The possessive form
Suggested change
zhaochenyang20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| For more details on SGLang Model Gateway, see the [official documentation](https://docs.sglang.io/advanced_features/sgl_model_gateway.html). | ||||||
|
|
||||||
| ### When to use which | ||||||
|
|
||||||
| - Use Miles Router when you need R3 or radix-tree caching | ||||||
| - Use SGLang Model Gateway for everything else (recommended default) | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.