Prhub

#32256 init sglang rust server project

原始 PR 作者 rainj-me 合并时间 2026-07-24 05:47 文件变更 4 提交数 1 评论 1 代码增减 +85 / -1

执行摘要

初始化 Rust Server 项目框架

PR 正文说明 "Split PR from https://github.com/sgl-project/sglang/pull/29799"。目的是将大型 PR 中 Rust server 基础框架独立合并,为后续实现纯 Rust 并发流水线(API Server → TokenizerManager → Tokenizer/Detokenizer)奠定模块结构。该架构旨在让 1-5 阶段不接触 Python 对象,避免 GIL 竞争。

该 PR 是 Rust 化演进的第一步架构决策,值得关注。设计上选择双 crate-type(cdylib + rlib)以支持从 Python 导入和 Rust 原生测试;通过 workspace 统一依赖版本管理;模块注释清晰描述了未来流水线的 GIL 控制思路。建议在后续功能 PR 中及时移除 #[allow(dead_code)] 并添加单元测试。

讨论亮点

无实质性讨论。仅有一个 Gemini Code Assist 自动评论提示服务已停止,未影响合并。Review 由仓库维护者 sherlockwu 直接批准。

实现拆解

  1. 修改 rust/Cargo.toml,将 sglang-server 加入 workspace members 并添加 bytes、futures、serde、thiserror 等公共依赖。
  2. 创建 rust/sglang-server/Cargo.toml,声明包元数据、crate-type(cdylib + rlib)及完整依赖列表,包括 axum、core_affinity、dynamo-tokenizers 等。
  3. 创建 rust/sglang-server/pyproject.toml,配置 maturin 构建后端,module-name 设为 _core,使 Python 可通过 sglang.srt.server._core 导入。
  4. 创建 rust/sglang-server/src/lib.rs,定义 _core PyO3 模块函数(当前为空),并附带详细的架构注释说明 GIL 边界设计。
文件 模块 状态 重要度
rust/sglang-server/src/lib.rs Rust 服务 added 6.59
rust/sglang-server/Cargo.toml 依赖配置 added 5.02
rust/sglang-server/pyproject.toml 构建脚本 added 4.31
rust/Cargo.toml 工作区 modified 3.95

关键符号

_core

关键源码片段

rust/sglang-server/src/lib.rs core-logic

核心入口文件,定义 PyO3 扩展模块 _core,当前为空函数但承载未来所有 Rust 逻辑的模块入口。

//! sglang-server: a multi-threaded Rust frontend (API server → TokenizerManager
//! → Tokenizer/Detokenizer) embedded in the Python scheduler process.
//!
//! Pipeline stages 1–5 are pure Rust and never touch a `PyObject`, so they run
//! concurrently with the Python scheduler without contending for the GIL. The
//! only GIL crossings are the boundary methods on [`Server`]:
//!   * `recv_requests` — Python scheduler thread drains the ingress ring.
//!   * `push_batch`    — Python scheduler thread pushes one output batch.
//!   * `push_result`   — Python scheduler thread pushes one control result.
//!
//! All are non-blocking, so the GIL is never held across a wait.
#![allow(dead_code)] // TODO: remove when the consumer PR landsuse pyo3::prelude::*;/// PyO3 扩展模块入口,Python 侧通过 `import sglang.srt.server._core` 加载。
/// 当前为空函数,后续将注册 `Server` 类的绑定方法。
#[pymodule]
fn _core(_m: &Bound<'_, PyModule>) -> PyResult<()> {
    Ok(())
}
rust/sglang-server/Cargo.toml configuration

crate 配置核心,声明 Python 模块映射、crate-type、完整依赖,决定了库的构建方式和外部生态。

[package]
name = "sglang-server"
description = "Sglang server in rust"
version.workspace = true
edition.workspace = true
license.workspace = true# Consumed by python/setup.py: registers this crate as a PyO3 extension module.
[package.metadata.sglang]
python-module = "sglang.srt.server._core"[lib]
name = "sglang_server"
# cdylib: the pyo3 module imported by the (embedded) Python scheduler process.
# rlib: so optional standalone bins / integration tests can link the core.
crate-type = ["cdylib", "rlib"][dependencies]
async-stream = { workspace = true }
bytes = { workspace = true }
futures = { workspace = true }
pyo3 = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-appender = { workspace = true }
uuid = { workspace = true }axum = { version = "0.8.9", features = ["json", "tokio"] } # HTTP 框架
core_affinity = "0.8" # CPU 核绑定
dynamo-tokenizers = "1.5.3" # tokenizer,与 hf-hub 同进退
flume = "0.12.0" # 多生产者 / 单消费者通道
hf-hub = { version = "0.4", default-features = false } # Hugging Face Hub
regex-syntax = "0.8"
rmp-serde = "1" # MessagePack
rmpv = { version = "1", features = ["with-serde"] } # MessagePack Value

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

当前风险极低:仅新增文件和配置,未修改现有运行路径。但需注意,未来在 lib.rs 中填充逻辑时,需确保 Python 与 Rust 间的 GIL 边界处理符合模块注释中的非阻塞原则。pyproject.toml 声明的 maturin 版本约束 >=1.13,<2.0 可能在未来版本产生冲突,需同步更新。当前无测试覆盖,后续功能实现时应配套单元测试和集成测试。

对用户无直接影响,这是一个基础设施 PR。对开发者,新增了一个 Rust crate 作为 Python 扩展模块的构建入口,后续开发人员需要熟悉 maturin 和 PyO3 绑定流程。对构建系统,maturin 将在 setup.py 中自动编译该 crate(通过 metadata.sglang 进行注册),增加编译时间。对 CI,当前未触发 Rust 相关流水线,后续可能需添加 Rust 测试步骤。

低风险基建 无测试覆盖

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论