> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/yu-yan-mo-xing/sglang.md).

# SGLang

在 Clore.ai GPU 上部署 SGLang，借助 RadixAttention 实现高性能 LLM 服务

SGLang（Structured Generation Language）是由 LMSYS 团队开发的高性能 LLM 服务框架，该团队以 Vicuna 和 Chatbot Arena 方面的工作而闻名。它具有用于 KV 缓存共享的 RadixAttention、高效的 MoE（专家混合）支持，以及兼容 OpenAI 的 API——使其成为 CLORE.AI GPU 服务器上可用的最快开源推理引擎之一。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 服务器要求

| 参数   | 最低                        | 推荐                 |
| ---- | ------------------------- | ------------------ |
| RAM  | 16 GB                     | 32 GB+             |
| VRAM | 8 GB                      | 24 GB+             |
| 磁盘   | 50 GB                     | 200 GB+            |
| GPU  | NVIDIA Turing+（RTX 2000+） | A100、H100、RTX 4090 |

{% hint style="info" %}
SGLang 在启用 FlashInfer 的 Ampere+ GPU 上可获得最佳性能。对于 Mixtral 或 DeepSeek 之类的 MoE 模型，建议使用多 GPU 设置。
{% endhint %}

## 在 CLORE.AI 上快速部署

**Docker 镜像：** `lmsysorg/sglang:latest`

**端口：** `22/tcp`, `30000/http`

**环境变量：**

| 变量                     | 示例          | 描述                     |
| ---------------------- | ----------- | ---------------------- |
| `HF_TOKEN`             | `hf_xxx...` | 用于受限模型的 HuggingFace 令牌 |
| `CUDA_VISIBLE_DEVICES` | `0,1`       | 要使用的 GPU               |

## 逐步设置

### 1. 在 CLORE.AI 上租用 GPU 服务器

访问 [CLORE.AI 市场](https://clore.ai/marketplace) 并选择一台服务器：

* **7B 模型**：最低 16 GB 显存（RTX 4080、A10）
* **13B 模型**：24 GB 显存（RTX 3090、RTX 4090、A5000）
* **70B 模型**：80 GB+ 显存（A100 80GB）或多 GPU
* **MoE 模型（Mixtral 8x7B）**：48 GB 显存或 2×24 GB

### 2. SSH 登录到你的服务器

```bash
ssh -p <PORT> root@<SERVER_IP>
```

### 3. 拉取 SGLang Docker 镜像

```bash
docker pull lmsysorg/sglang:latest
```

### 4. 启动 SGLang 服务器

**基础启动（Llama 3.1 8B）：**

```bash
docker run -d \\
  --name sglang \\
  --gpus all \\
  --shm-size 16g \\
  --ipc host \\
  -p 30000:30000 \\
  -v /root/models:/root/.cache/huggingface \\
  lmsysorg/sglang:latest \\
  python3 -m sglang.launch_server \\
    --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\
    --host 0.0.0.0 \\
    --port 30000
```

**使用 HuggingFace 令牌：**

```bash
docker run -d \\
  --name sglang \\
  --gpus all \\
  --shm-size 16g \\
  --ipc host \\
  -p 30000:30000 \\
  -v /root/models:/root/.cache/huggingface \\
  -e HF_TOKEN=hf_your_token_here \\
  lmsysorg/sglang:latest \\
  python3 -m sglang.launch_server \\
    --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\
    --host 0.0.0.0 \\
    --port 30000 \\
    --dtype bfloat16
```

**多 GPU 上的 Qwen2.5 72B：**

```bash
docker run -d \\
  --name sglang \\
  --gpus all \\
  --shm-size 32g \\
  --ipc host \\
  -p 30000:30000 \\
  -v /root/models:/root/.cache/huggingface \\
  lmsysorg/sglang:latest \\
  python3 -m sglang.launch_server \\
    --model-path Qwen/Qwen2.5-72B-Instruct \\
    --host 0.0.0.0 \\
    --port 30000 \\
    --tp 2 \\
    --dtype bfloat16
```

**DeepSeek-V2（MoE 模型）：**

```bash
docker run -d \\
  --name sglang \\
  --gpus all \\
  --shm-size 32g \\
  --ipc host \\
  -p 30000:30000 \\
  -v /root/models:/root/.cache/huggingface \\
  lmsysorg/sglang:latest \\
  python3 -m sglang.launch_server \\
    --model-path deepseek-ai/DeepSeek-V2-Lite-Chat \\
    --host 0.0.0.0 \\
    --port 30000 \\
    --trust-remote-code \\
    --tp 1
```

### 5. 检查服务器健康状态

```bash
# 查看日志
docker logs -f sglang

# 健康检查（等待约 2-3 分钟让模型加载）
curl http://localhost:30000/health

# 获取模型信息
curl http://localhost:30000/get_model_info
```

### 6. 通过 CLORE.AI 代理从外部访问

你的 CLORE.AI 仪表板提供了一个 `http_pub` 端口 30000 的 URL：

```
https://<order-id>-30000.clore.ai/
```

在任何兼容 OpenAI 的客户端中，将此 URL 作为你的 base URL。

***

## 使用示例

### 示例 1：兼容 OpenAI 的聊天补全

```bash
curl http://localhost:30000/v1/chat/completions \\
  -X POST \\
  -H 'Content-Type: application/json' \\
  -d '{
    "model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
    "messages": [
      {"role": "system", "content": "你是一位乐于助人的编程助手。"},
      {"role": "user", "content": "用 Python 编写一个快速排序实现。"}
    ],
    "max_tokens": 512,
    "temperature": 0.2
  }'
```

### 示例 2：流式响应

```bash
curl http://localhost:30000/v1/chat/completions \\
  -X POST \\
  -H 'Content-Type: application/json' \\
  -d '{
    "model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
    "messages": [
      {"role": "user", "content": "解释 transformer 注意力是如何工作的。"}
    ],
    "max_tokens": 800,
    "stream": true
  }' \\
  --no-buffer
```

### 示例 3：Python OpenAI 客户端

```python
from openai import OpenAI

# 指向你的 CLORE.AI SGLang 服务器
client = OpenAI(
    base_url="http://localhost:30000/v1",
    api_key="none",  # SGLang 默认不需要认证
)

response = client.chat.completions.create(
    model="meta-llama/Meta-Llama-3.1-8B-Instruct",
    messages=[
        {"role": "system", "content": "你是一位数据科学专家。"},
        {"role": "user", "content": "什么是梯度提升？"},
    ],
    max_tokens=400,
    temperature=0.7,
)

print(response.choices[0].message.content)
```

### 示例 4：使用 SGLang 原生 API 进行批量推理

SGLang 的原生 API 提供了额外的控制：

```python
import requests

# 生成补全
response = requests.post(
    "http://localhost:30000/generate",
    json={
        "text": "AI 的未来是",
        "sampling_params": {
            "max_new_tokens": 200,
            "temperature": 0.8,
            "top_p": 0.95,
        },
    },
)
print(response.json()["text"])
```

### 示例 5：受约束的 JSON 输出

SGLang 支持结构化输出生成：

```python
import requests

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "city": {"type": "string"},
    },
    "required": ["name", "age", "city"],
}

response = requests.post(
    "http://localhost:30000/generate",
    json={
        "text": "提取信息：John Smith，35 岁，住在纽约。",
        "sampling_params": {
            "max_new_tokens": 100,
            "temperature": 0.0,
        },
        "json_schema": schema,
    },
)
print(response.json()["text"])
# 输出：{"name": "John Smith", "age": 35, "city": "New York"}
```

***

## 配置

### 关键启动参数

| 参数                      | 默认值         | 描述                               |
| ----------------------- | ----------- | -------------------------------- |
| `--model-path`          | 必需          | HuggingFace 模型 ID 或本地路径          |
| `--host`                | `127.0.0.1` | 绑定主机（使用 `0.0.0.0` 用于外部访问）        |
| `--port`                | `30000`     | 服务器端口                            |
| `--tp`                  | `1`         | 张量并行度（GPU 数量）                    |
| `--dp`                  | `1`         | 数据并行度                            |
| `--dtype`               | `auto`      | `float16`, `bfloat16`, `float32` |
| `--mem-fraction-static` | `0.88`      | 用于 KV 缓存的显存比例                    |
| `--max-prefill-tokens`  | auto        | 单次预填充步骤中的最大 token 数              |
| `--context-length`      | 模型最大值       | 覆盖最大上下文长度                        |
| `--trust-remote-code`   | false       | 允许自定义模型代码                        |
| `--quantization`        | none        | `awq`, `gptq`, `fp8`             |
| `--load-format`         | `auto`      | `auto`, `pt`, `safetensors`      |
| `--tokenizer-path`      | 与模型相同       | 自定义 tokenizer 路径                 |

### 量化选项

**AWQ（推荐用于速度）：**

```bash
python3 -m sglang.launch_server \\
  --model-path casperhansen/mistral-7b-instruct-v0.2-awq \\
  --quantization awq \\
  --host 0.0.0.0 \\
  --port 30000
```

**FP8（适用于 H100/A100）：**

```bash
python3 -m sglang.launch_server \\
  --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\
  --quantization fp8 \\
  --host 0.0.0.0 \\
  --port 30000
```

***

## 性能提示

### 1. RadixAttention —— 关键优势

SGLang 的 RadixAttention 会自动为共享的提示前缀复用 KV 缓存。这对于以下场景尤其强大：

* 具有长系统提示的聊天机器人
* 带有重复上下文的 RAG 应用
* 共享相同前缀的批量 API 调用

无需额外配置——始终启用。

### 2. 增加 KV 缓存大小

```bash
--mem-fraction-static 0.90  # 将 90% 的显存用于 KV 缓存
```

注意不要设得太高——要为模型权重留出空间。

### 3. 长上下文的分块预填充

```bash
--chunked-prefill-size 4096  # 将长提示分块处理
```

### 4. 启用 FlashInfer 后端

SGLang 在可用时会自动使用 FlashInfer（Ampere+ GPU）：

```bash
--attention-backend flashinfer
```

### 5. 多 GPU 张量并行

对于无法装入单个 GPU 的模型：

```bash
--tp 4  # 使用 4 个 GPU
```

每个 GPU 必须有足够的显存来容纳模型的一部分。

### 6. 针对吞吐量与延迟进行调优

**低延迟（单用户）：**

```bash
--max-running-requests 4
```

**高吞吐量（多用户）：**

```bash
--max-running-requests 64 \\
--schedule-policy lpm  # 最长前缀匹配调度
```

***

## 故障排查

### 问题：“torch.cuda.OutOfMemoryError”

```
torch.cuda.OutOfMemoryError：CUDA 内存不足
```

**解决方案：** 降低内存比例或使用量化：

```bash
--mem-fraction-static 0.80
# 或
--quantization awq
```

### 问题：服务器无法启动（卡在加载）

```bash
# 检查 CUDA 是否可用
docker exec -it sglang nvidia-smi

# 检查模型下载进度
docker logs -f sglang 2>&1 | tail -50
```

### 问题：“trust\_remote\_code required”

在 `--trust-remote-code` 添加到启动命令中，用于具有自定义架构的模型（DeepSeek、Falcon 等）。

### 问题：MoE 模型生成缓慢

MoE 模型（Mixtral、DeepSeek）受内存带宽限制。确保你使用的是：

```bash
--dtype bfloat16  # 对 MoE 来说比 float16 更好
--tp 2            # 如果可用，则跨 GPU 拆分
```

### 问题：上下文长度错误

```bash
# 覆盖上下文长度
--context-length 32768
```

### 问题：端口 30000 无法访问

请确认该端口已在你的 CLORE.AI 订单配置中暴露。检查订单仪表板中的 http\_pub URL，而不是 localhost。

***

## 链接

* [GitHub](https://github.com/sgl-project/sglang)
* [文档](https://sgl-project.github.io/start/install.html)
* [Docker Hub](https://hub.docker.com/r/lmsysorg/sglang)
* [支持的模型](https://github.com/sgl-project/sglang?tab=readme-ov-file#supported-models)
* [CLORE.AI 市场](https://clore.ai/marketplace)

***

## Clore.ai GPU 推荐

| 使用场景         | 推荐 GPU           | Clore.ai 预计成本     |
| ------------ | ---------------- | ----------------- |
| 开发/测试        | RTX 3090（24GB）   | $0.07–0.21/gpu/hr |
| 生产环境（7B–13B） | RTX 4090（24GB）   | $0.14–0.42/gpu/hr |
| 大模型（70B+）    | A100 80GB / H100 | \~$1.04/gpu/hr    |

> 💡 本指南中的所有示例都可以部署在 [Clore.ai](https://clore.ai/marketplace) GPU 服务器上。浏览可用 GPU 并按小时租用——无需承诺，拥有完整 root 访问权限。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/yu-yan-mo-xing/sglang.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
