SGLang 文档#

我们建议新的贡献者从编写文档开始,这有助于您快速理解 SGLang 代码库。大多数文档文件位于 docs/ 文件夹下。我们偏好 Jupyter Notebooks 而不是 Markdown,这样所有示例都可以通过我们的文档 CI 流水线执行和验证。

文档工作流程#

安装依赖#

pip install -r requirements.txt

更新文档#

更新 docs/ 文件夹下相应子目录中的 Jupyter notebooks。如果您添加了新文件,请记住相应地更新 index.rst(或相关的 .rst 文件)。

  • 手动运行 pre-commit run --all-files 会执行所有配置的检查,并在可能的情况下应用修复。如果第一次运行失败,请重新运行以确保 lint 错误完全解决。在创建 Pull Request 之前,请确保您的代码通过所有检查。

  • 不要直接提交到 main 分支。总是创建一个新分支(例如 feature/my-new-feature),推送您的更改,并从该分支打开一个 PR。

# 1) Compile all Jupyter notebooks
make compile

# 2) Compile and Preview documentation locally with auto-build
# This will automatically rebuild docs when files change
# Open your browser at the displayed port to view the docs
bash serve.sh

# 2a) Alternative ways to serve documentation
# Directly use make serve
make serve
# With custom port
PORT=8080 make serve

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

# 4) Pre-commit checks and create a PR
# After these checks pass, push your changes and open a PR on your branch
pre-commit run --all-files

端口分配和 CI 效率#

启动和终止服务器

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
"""
)

wait_for_server(f"https://:{port}")

# Terminate Server
terminate_process(server_process)

启动和终止引擎

# Launch Engine
import sglang as sgl
import asyncio
from sglang.test.test_utils import is_in_ci

if is_in_ci():
    import patch

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

# Terminalte Engine
llm.shutdown()

为什么采用这种方法?#

  • 动态端口分配:通过在运行时选择可用端口来避免端口冲突,从而实现多个服务器实例并行运行。

  • 为 CI 优化:在 CI 环境中,launch_server_cmdsgl.Engine()patch 版本有助于动态管理 GPU 内存,防止冲突并提高测试并行性。

  • 更好的并行执行:通过避免固定端口冲突和优化内存使用,确保流畅的并发测试。

模型选择#

为了在文档中进行演示,请优先选择较小的模型,以减少内存消耗并加快推理速度。在 CI 中运行较大的模型可能会因内存限制而导致不稳定。

Prompt 对齐示例#

设计 prompts 时,请确保它们与 SGLang 的结构化格式对齐。例如

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

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

这使得响应与预期行为保持一致,并提高了跨不同文件的可靠性。