"examples/vision/vscode:/vscode.git/clone" did not exist on "606ed5e82c927effa15afe7841611127aa94b2fc"
README.md 4.25 KB
Newer Older
Byron Hsu's avatar
Byron Hsu committed
1
# SGLang Router
2
3
4

SGLang router is a standalone module implemented in Rust to achieve data parallelism across SGLang instances.

5
## User docs
Byron Hsu's avatar
Byron Hsu committed
6

Yineng Zhang's avatar
Yineng Zhang committed
7
Please check https://docs.sglang.ai/router/router.html
8

9
## Developer docs
10

11
### Prerequisites
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

- Rust and Cargo installed

```bash
# Install rustup (Rust installer and version manager)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Follow the installation prompts, then reload your shell
source $HOME/.cargo/env

# Verify installation
rustc --version
cargo --version
```

- Python with pip installed

29

Byron Hsu's avatar
Byron Hsu committed
30
### Build Process
31

Byron Hsu's avatar
Byron Hsu committed
32
#### 1. Build Rust Project
33
34

```bash
35
$ cargo build
36
37
```

Byron Hsu's avatar
Byron Hsu committed
38
#### 2. Build Python Binding
39

Byron Hsu's avatar
Byron Hsu committed
40
##### Option A: Build and Install Wheel
41
42
1. Build the wheel package:
```bash
43
44
$ pip install setuptools-rust wheel build
$ python -m build
45
46
47
48
```

2. Install the generated wheel:
```bash
49
50
51
52
53
54
55
$ pip install <path-to-wheel>
```

If you want one handy command to do build + install for every change you make:

```bash
$ python -m build && pip install --force-reinstall dist/*.whl
56
57
```

Byron Hsu's avatar
Byron Hsu committed
58
##### Option B: Development Mode
59
60

For development purposes, you can install the package in editable mode:
61
62
63

Warning: Using editable python binding can suffer from performance degradation!! Please build a fresh wheel for every update if you want to test performance.

64
```bash
65
$ pip install -e .
66
67
68
69
```

**Note:** When modifying Rust code, you must rebuild the wheel for changes to take effect.

70
71
72
73
74
75
76
77
78
79
80
81
82
83
### Logging

The SGL Router includes structured logging with console output by default. To enable log files:

```python
# Enable file logging when creating a router
router = Router(
    worker_urls=["http://worker1:8000", "http://worker2:8000"],
    log_dir="./logs"  # Daily log files will be created here
)
```

Use the `--verbose` flag with the CLI for more detailed logs.

84
85
86
87
88
89
90
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
### Kubernetes Service Discovery

SGL Router supports automatic service discovery for worker nodes in Kubernetes environments. When enabled, the router will automatically:

- Discover and add worker pods with matching labels
- Remove unhealthy or deleted worker pods
- Dynamically adjust the worker pool based on pod health and availability

#### Command Line Usage

```bash
python -m sglang_router.launch_router \
    --service-discovery \
    --selector app=sglang-worker role=inference \
    --service-discovery-port 8000 \
    --service-discovery-namespace default
```

#### Service Discovery Arguments

- `--service-discovery`: Enable Kubernetes service discovery feature
- `--selector`: One or more label key-value pairs for pod selection (format: key1=value1 key2=value2)
- `--service-discovery-port`: Port to use when generating worker URLs (default: 80)
- `--service-discovery-namespace`: Optional. Kubernetes namespace to watch for pods. If not provided, watches all namespaces (requires cluster-wide permissions)

#### RBAC Requirements

When using service discovery, you must configure proper Kubernetes RBAC permissions:

- **If using namespace-scoped discovery** (with `--service-discovery-namespace`):
  Set up a ServiceAccount, Role, and RoleBinding

- **If watching all namespaces** (without specifying namespace):
  Set up a ServiceAccount, ClusterRole, and ClusterRoleBinding with permissions to list/watch pods at the cluster level

119
120
121
122
123
124
125
126
127
128
### Troubleshooting

1. If rust analyzer is not working in VSCode, set `rust-analyzer.linkedProjects` to the absolute path of `Cargo.toml` in your repo. For example:

```json
{
  "rust-analyzer.linkedProjects":  ["/workspaces/sglang/sgl-router/Cargo.toml"]
}
```

Byron Hsu's avatar
Byron Hsu committed
129
### CI/CD Setup
130
131
132

The continuous integration pipeline consists of three main steps:

Byron Hsu's avatar
Byron Hsu committed
133
#### 1. Build Wheels
134
135
136
137
138
- Uses `cibuildwheel` to create manylinux x86_64 packages
- Compatible with major Linux distributions (Ubuntu, CentOS, etc.)
- Additional configurations can be added to support other OS/architectures
- Reference: [cibuildwheel documentation](https://cibuildwheel.pypa.io/en/stable/)

Byron Hsu's avatar
Byron Hsu committed
139
#### 2. Build Source Distribution
140
141
142
- Creates a source distribution containing the raw, unbuilt code
- Enables `pip` to build the package from source when prebuilt wheels are unavailable

Byron Hsu's avatar
Byron Hsu committed
143
#### 3. Publish to PyPI
144
145
146
- Uploads both wheels and source distribution to PyPI

The CI configuration is based on the [tiktoken workflow](https://github.com/openai/tiktoken/blob/63527649963def8c759b0f91f2eb69a40934e468/.github/workflows/build_wheels.yml#L1).