README.md 3.42 KB
Newer Older
Lianmin Zheng's avatar
Lianmin Zheng committed
1
# SGLang Documentation
2
We recommend new contributors start from writing documentation, which helps you quickly understand SGLang codebase. Most documentation files are located under the `docs/` folder. We prefer **Jupyter Notebooks** over Markdown so that all examples can be executed and validated by our docs CI pipeline.
Lianmin Zheng's avatar
Lianmin Zheng committed
3

4
## Docs Workflow
Lianmin Zheng's avatar
Lianmin Zheng committed
5

6
7
8
### Install Dependency

```bash
Lianmin Zheng's avatar
Lianmin Zheng committed
9
10
11
pip install -r requirements.txt
```

12
13
14
15
16
17
18
19
20
21
22
### Update Documentation

Update your Jupyter notebooks in the appropriate subdirectories under `docs/`. If you add new files, remember to update `index.rst` (or relevant `.rst` files) accordingly.

- **`pre-commit run --all-files`** manually runs all configured checks, applying fixes if possible. If it fails the first time, re-run it to ensure lint errors are fully resolved. Make sure your code passes all checks **before** creating a Pull Request.
- **Do not commit** directly to the `main` branch. Always create a new branch (e.g., `feature/my-new-feature`), push your changes, and open a PR from that branch.

```bash
# 1) Compile all Jupyter notebooks
make compile

23
# 2) Compile and Preview documentation locally
24
25
# Open your browser at the displayed port to view the docs
bash serve.sh
Lianmin Zheng's avatar
Lianmin Zheng committed
26

27
# 3) Clean notebook outputs
28
29
30
# nbstripout removes notebook outputs so your PR stays clean
pip install nbstripout
find . -name '*.ipynb' -exec nbstripout {} \;
31

32
# 4) Pre-commit checks and create a PR
33
34
# After these checks pass, push your changes and open a PR on your branch
pre-commit run --all-files
Lianmin Zheng's avatar
Lianmin Zheng committed
35
```
36
---
37

38
### **Port Allocation and CI Efficiency**
39

40
**To launch and kill the server:**
41
42

```python
43
44
45
46
47
48
49
50
51
52
53
54
55
from sglang.test.test_utils import is_in_ci
from sglang.utils import wait_for_server, print_highlight, terminate_process

if is_in_ci():
    from patch import launch_server_cmd
else:
    from sglang.utils import launch_server_cmd

server_process, port = launch_server_cmd(
    """
python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \
 --host 0.0.0.0
"""
56
57
)

58
wait_for_server(f"http://localhost:{port}")
59

60
# Terminate Server
61
terminate_process(server_process)
Lianmin Zheng's avatar
Lianmin Zheng committed
62
```
63
64

**To launch and kill the engine:**
Lianmin Zheng's avatar
Lianmin Zheng committed
65

66
67
68
69
```python
# Launch Engine
import sglang as sgl
import asyncio
70
71
72
73
from sglang.test.test_utils import is_in_ci

if is_in_ci():
    import patch
74
75
76
77
78

llm = sgl.Engine(model_path="meta-llama/Meta-Llama-3.1-8B-Instruct")

# Terminalte Engine
llm.shutdown()
79
```
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

### **Why this approach?**

- **Dynamic Port Allocation**: Avoids port conflicts by selecting an available port at runtime, enabling multiple server instances to run in parallel.
- **Optimized for CI**: The `patch` version of `launch_server_cmd` and `sgl.Engine()` in CI environments helps manage GPU memory dynamically, preventing conflicts and improving test parallelism.
- **Better Parallel Execution**: Ensures smooth concurrent tests by avoiding fixed port collisions and optimizing memory usage.

### **Model Selection**

For demonstrations in the docs, **prefer smaller models** to reduce memory consumption and speed up inference. Running larger models in CI can lead to instability due to memory constraints.

### **Prompt Alignment Example**

When designing prompts, ensure they align with SGLang’s structured formatting. For example:

```python
prompt = """You are an AI assistant. Answer concisely and accurately.

User: What is the capital of France?
Assistant: The capital of France is Paris."""
```

This keeps responses aligned with expected behavior and improves reliability across different files.