diff --git a/.github/workflows/_pr-test-check-changes.yml b/.github/workflows/_pr-test-check-changes.yml index 9a7fa183af..be22f0e3bb 100644 --- a/.github/workflows/_pr-test-check-changes.yml +++ b/.github/workflows/_pr-test-check-changes.yml @@ -101,7 +101,7 @@ jobs: - "test/**/!(*.md)" - "rust/*" - "rust/!(sglang-renderer)/**" - - "proto/sglang/runtime/v1/sglang.proto" + - "proto/**" renderer: - "rust/sglang-renderer/**" simulator: @@ -138,7 +138,7 @@ jobs: # Gates the cargo test in test/registered/rust/; exported to the # CPU stage as its negation, SGLANG_SKIP_RUST_TESTS. - "rust/**" - - "proto/sglang/runtime/v1/sglang.proto" + - "proto/**" - "test/registered/rust/**" - ".github/workflows/_pr-test-*.yml" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 03d827b632..af88cf5af5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -141,16 +141,16 @@ repos: # left (combining --fix with -D warnings can abort the fix phase), then # cover sglang-mm's PyO3 bindings + rayon fan-out — both sit behind # non-default features, so `--workspace` alone never compiles them. protoc - # is not required: sglang-grpc's build.rs falls back to a vendored binary. + # is not required: the shared protocol crate falls back to a vendored binary. entry: bash -c 'cd rust && cargo clippy --workspace --fix --allow-dirty --allow-staged && cargo clippy --workspace -- -D warnings && cargo clippy -p sglang-mm --features python,parallel --lib -- -D warnings && cargo clippy --manifest-path sglang-radix-tree/Cargo.toml --all-targets --no-default-features --features tch/doc-only -- -D warnings' language: system - files: ^rust/.*\.rs$ + files: ^(rust/.*\.(rs|toml)|proto/.*\.(rs|proto|toml))$ pass_filenames: false - id: rustfmt-rust-workspace name: rustfmt rust/ workspace entry: bash -c 'cd rust && cargo fmt && cargo fmt --manifest-path sglang-radix-tree/Cargo.toml' language: system - files: ^rust/.*\.rs$ + files: ^(rust/.*\.(rs|toml)|proto/.*\.(rs|proto|toml))$ pass_filenames: false - id: mint-broken-links name: check docs links (Mintlify; opt-in, requires `mint` on PATH) diff --git a/docker/Dockerfile b/docker/Dockerfile index c1d3cee09b..f235c15ff8 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -190,10 +190,9 @@ RUN --mount=type=cache,target=/root/.cache/pip \ # --no-deps prevents pip from pulling torch from default PyPI && python3 -m pip install sglang-kernel==${SGL_KERNEL_VERSION} --force-reinstall --no-deps -# Copy dep spec + Rust crate source + proto files. setuptools-rust compiles the -# Rust extension during the stub wheel build; the crate's build.rs references -# ../../proto for tonic_build. Split from the pip install so source changes to -# these paths invalidate the dep-install layer, but Python source changes don't. +# Copy the dependency spec, Rust extension sources, and shared protocol crate. +# Split from the pip install so changes to these build inputs invalidate the +# dependency layer, while unrelated Python source changes do not. COPY python/pyproject.toml /tmp/sglang_deps/python/pyproject.toml COPY rust/sglang-grpc /tmp/sglang_deps/rust/sglang-grpc COPY rust/sglang-mm /tmp/sglang_deps/rust/sglang-mm diff --git a/docker/Dockerfile.cu134 b/docker/Dockerfile.cu134 index 57c01d2adf..a6e321109f 100644 --- a/docker/Dockerfile.cu134 +++ b/docker/Dockerfile.cu134 @@ -439,10 +439,9 @@ RUN --mount=type=cache,target=/root/.cache/pip \ ; \ fi -# Copy dep spec + Rust crate source + proto files. setuptools-rust compiles the -# Rust extension during the stub wheel build; the crate's build.rs references -# ../../proto for tonic_build. Split from the pip install so source changes to -# these paths invalidate the dep-install layer, but Python source changes don't. +# Copy the dependency spec, Rust extension sources, and shared protocol crate. +# Split from the pip install so changes to these build inputs invalidate the +# dependency layer, while unrelated Python source changes do not. COPY python/pyproject.toml /tmp/sglang_deps/python/pyproject.toml COPY rust/sglang-grpc /tmp/sglang_deps/rust/sglang-grpc COPY rust/sglang-mm /tmp/sglang_deps/rust/sglang-mm diff --git a/proto/Cargo.toml b/proto/Cargo.toml new file mode 100644 index 0000000000..18ac4bde20 --- /dev/null +++ b/proto/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "sglang-grpc-types" +description = "Generated Rust bindings for the SGLang runtime.v1 API" +version = "0.1.0" +edition.workspace = true +rust-version = "1.92" +license.workspace = true +repository = "https://github.com/sgl-project/sglang" +readme = "README.md" +workspace = "../rust" +include = [ + "build.rs", + "README.md", + "src/**", + "sglang/**/*.proto", +] + +[dependencies] +prost = { workspace = true } +prost-types = { workspace = true } +tonic = { workspace = true } + +[build-dependencies] +prost-build = { workspace = true } +protoc-bin-vendored = { workspace = true } +tonic-build = { workspace = true } diff --git a/proto/README.md b/proto/README.md new file mode 100644 index 0000000000..7e13b8baa5 --- /dev/null +++ b/proto/README.md @@ -0,0 +1,15 @@ +# SGLang gRPC types + +This crate provides the generated Prost messages and Tonic client/server +interfaces for SGLang's canonical `sglang.runtime.v1` API. + +The language-independent contract remains in +`sglang/runtime/v1/sglang.proto`. Rust bindings are generated from that schema +at build time with a vendored `protoc`, so consumers do not need a system +protobuf compiler. + +The generated API currently uses Tonic 0.12 to remain compatible with +SGLang's existing gRPC server. + +This crate contains protocol types only. SGLang's request handling and server +implementation remain in their respective frontend crates. diff --git a/proto/build.rs b/proto/build.rs new file mode 100644 index 0000000000..c095519802 --- /dev/null +++ b/proto/build.rs @@ -0,0 +1,21 @@ +fn main() -> Result<(), Box> { + // Prefer an explicitly configured protoc; otherwise use the vendored + // binary so builds remain self-contained on machines without protobuf. + if std::env::var_os("PROTOC").is_none() + && let Ok(vendored) = protoc_bin_vendored::protoc_bin_path() + { + // SAFETY: build scripts are single-threaded at this point. + unsafe { std::env::set_var("PROTOC", vendored) }; + } + + let proto_path = "sglang/runtime/v1/sglang.proto"; + + tonic_build::configure() + .build_server(true) + .build_client(true) + .protoc_arg("--experimental_allow_proto3_optional") + .compile_protos(&[proto_path], &["."])?; + + println!("cargo:rerun-if-changed={proto_path}"); + Ok(()) +} diff --git a/proto/src/lib.rs b/proto/src/lib.rs new file mode 100644 index 0000000000..1e1e198008 --- /dev/null +++ b/proto/src/lib.rs @@ -0,0 +1,13 @@ +//! Rust bindings for SGLang's canonical `sglang.runtime.v1` protobuf API. +//! +//! The generated messages and Tonic client/server interfaces intentionally +//! contain no frontend implementation or transport policy. + +/// Protobuf packages exposed with the same namespace as the wire contract. +pub mod sglang { + pub mod runtime { + pub mod v1 { + tonic::include_proto!("sglang.runtime.v1"); + } + } +} diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index f3571906ff..d682610a3b 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -381,8 +381,8 @@ class Envs: # =================================================================== SGLANG_IS_IN_CI = EnvBool(False) SGLANG_IS_IN_CI_AMD = EnvBool(False) - # Set to true by the check-changes CI job when a PR touches nothing under - # rust/; default false so local and scheduled runs never skip the cargo tests. + # Set to true by the check-changes CI job when a PR touches no Rust workspace + # inputs; default false so local and scheduled runs never skip the cargo tests. SGLANG_SKIP_RUST_TESTS = EnvBool(False) SGLANG_TEST_MAX_RETRY = EnvInt(None) # Expand jit_kernel test grids to their full parameter ranges (nightly). diff --git a/rust/Cargo.lock b/rust/Cargo.lock index bb11019def..9e54ca7ceb 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -3693,21 +3693,31 @@ name = "sglang-grpc" version = "0.1.0" dependencies = [ "async-stream", - "prost", "prost-types", - "protoc-bin-vendored", "pyo3", "serde_json", + "sglang-grpc-types", "tokenizers", "tokio", "tokio-stream", "tonic", - "tonic-build", "tracing", "tracing-subscriber", "uuid", ] +[[package]] +name = "sglang-grpc-types" +version = "0.1.0" +dependencies = [ + "prost", + "prost-build", + "prost-types", + "protoc-bin-vendored", + "tonic", + "tonic-build", +] + [[package]] name = "sglang-mm" version = "0.1.0" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 85126fccba..a0736c2ab2 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,7 @@ [workspace] resolver = "3" members = [ + "../proto", "sglang-grpc", "sglang-mm", "sglang-renderer", @@ -26,6 +27,12 @@ async-stream = "0.3" bytes = "1" futures = "0.3" pyo3 = { version = "0.29.0", features = ["extension-module"] } +# These versions define the generated public gRPC API. Update them together +# and deliberately rather than changing bindings during an unrelated build. +prost = "=0.13.5" +prost-types = "=0.13.5" +prost-build = "=0.13.5" +protoc-bin-vendored = "=3.2.0" serde = { version = "1", features = ["derive"] } serde_json = "1" socket2 = "0.6" @@ -35,6 +42,8 @@ tokio-stream = { version = "0.1", features = ["net"] } tracing = "0.1" tracing-appender = "0.2" tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tonic = { version = "=0.12.3", features = ["gzip", "transport"] } +tonic-build = "=0.12.3" uuid = { version = "1", features = ["v4"] } # Profiles only take effect at the workspace root; per-member profile sections diff --git a/rust/sglang-grpc/Cargo.toml b/rust/sglang-grpc/Cargo.toml index 739e445337..9be39b42b5 100644 --- a/rust/sglang-grpc/Cargo.toml +++ b/rust/sglang-grpc/Cargo.toml @@ -9,8 +9,8 @@ license.workspace = true # of the main sglang wheel at the given import path. [package.metadata.sglang] python-module = "sglang.srt.rust_extensions._grpc" -# build.rs compiles schemas from outside the Rust workspace; include them in -# the source-loader artifact fingerprint. +# The shared protocol dependency lives outside rust/; include it in the +# source-loader artifact fingerprint. source-inputs = ["../../proto"] # Always build optimized, even for an editable install. debug = false @@ -26,23 +26,17 @@ crate-type = ["cdylib"] [dependencies] async-stream = { workspace = true } pyo3 = { workspace = true } +sglang-grpc-types = { path = "../../proto" } serde_json = { workspace = true } tokio = { workspace = true } tokio-stream = { workspace = true } +tonic = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true } uuid = { workspace = true } -prost = "0.13" -prost-types = "0.13" +prost-types = { workspace = true } tokenizers = { version = "0.21", default-features = false, features = ["onig"] } -tonic = { version = "0.12", features = ["gzip", "transport"] } - -[build-dependencies] -tonic-build = "0.12" -# Fallback protoc for machines without a system install (see build.rs); keeps -# `cargo clippy/check/build` and lint CI working without apt/brew protobuf. -protoc-bin-vendored = "3" [features] default = ["pyo3/extension-module"] diff --git a/rust/sglang-grpc/build.rs b/rust/sglang-grpc/build.rs deleted file mode 100644 index 1079581a6f..0000000000 --- a/rust/sglang-grpc/build.rs +++ /dev/null @@ -1,28 +0,0 @@ -fn main() -> Result<(), Box> { - // Prefer an explicitly configured protoc; otherwise use the vendored - // binary so builds (including `cargo clippy` and the pre-commit hook on - // machines/CI runners without protobuf installed) are self-contained. - // protoc_bin_path() errs on platforms the vendored crate doesn't cover; - // those fall back to prost-build's own lookup of `protoc` on PATH. - if std::env::var_os("PROTOC").is_none() - && let Ok(vendored) = protoc_bin_vendored::protoc_bin_path() - { - // SAFETY: build scripts are single-threaded at this point. - unsafe { std::env::set_var("PROTOC", vendored) }; - } - - let proto_path = "../../proto/sglang/runtime/v1/sglang.proto"; - - tonic_build::configure() - .build_server(true) - .build_client(false) - .protoc_arg("--experimental_allow_proto3_optional") - .file_descriptor_set_path( - std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()) - .join("sglang_descriptor.bin"), - ) - .compile_protos(&[proto_path], &["../../proto"])?; - - println!("cargo:rerun-if-changed={}", proto_path); - Ok(()) -} diff --git a/rust/sglang-grpc/pyproject.toml b/rust/sglang-grpc/pyproject.toml index 20db0e3765..0ab76d3cef 100644 --- a/rust/sglang-grpc/pyproject.toml +++ b/rust/sglang-grpc/pyproject.toml @@ -1,8 +1,9 @@ # Standalone dev builds only (`maturin build` / `maturin develop`); the # extension ships to users inside the main sglang wheel via setuptools-rust -# (python/pyproject.toml, target sglang.srt.rust_extensions._grpc). Requires a -# repo checkout (build.rs reads ../../proto), so this is not publishable as an -# sdist. protoc is not required: build.rs falls back to a vendored binary. +# (python/pyproject.toml, target sglang.srt.rust_extensions._grpc). Its shared +# protocol dependency lives in ../../proto, so standalone builds require a +# repository checkout. A system protoc is unnecessary; that crate uses a +# vendored binary. [build-system] requires = ["maturin>=1.5,<2"] build-backend = "maturin" diff --git a/rust/sglang-grpc/src/lib.rs b/rust/sglang-grpc/src/lib.rs index e9d0cd607c..6a2b42d4ed 100644 --- a/rust/sglang-grpc/src/lib.rs +++ b/rust/sglang-grpc/src/lib.rs @@ -3,10 +3,6 @@ pub mod server; pub mod tokenizers; pub(crate) mod utils; -pub mod proto { - tonic::include_proto!("sglang.runtime.v1"); -} - use pyo3::prelude::*; use std::net::{SocketAddr, TcpListener}; use std::sync::Arc; diff --git a/rust/sglang-grpc/src/server.rs b/rust/sglang-grpc/src/server.rs index c1f939e7e1..3e72497ea2 100644 --- a/rust/sglang-grpc/src/server.rs +++ b/rust/sglang-grpc/src/server.rs @@ -13,11 +13,11 @@ use tokio_stream::wrappers::TcpListenerStream; use tonic::{Request, Response, Status}; use crate::bridge::{PyBridge, ResponseChunk, TerminalError}; -use crate::proto; use crate::utils::{ build_classify_dict, build_embed_dict, build_generate_dict, build_text_embed_dict, build_text_generate_dict, extract_model_path, }; +use sglang_grpc_types::sglang::runtime::v1 as proto; pub struct SglangServiceImpl { pub bridge: Arc, diff --git a/rust/sglang-grpc/src/utils/request_utils.rs b/rust/sglang-grpc/src/utils/request_utils.rs index 248a723951..8697d8d716 100644 --- a/rust/sglang-grpc/src/utils/request_utils.rs +++ b/rust/sglang-grpc/src/utils/request_utils.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use crate::proto; +use sglang_grpc_types::sglang::runtime::v1 as proto; fn regex_escape_literal(value: &str) -> String { let mut escaped = String::with_capacity(value.len()); diff --git a/test/registered/rust/test_run_rust_tests.py b/test/registered/rust/test_run_rust_tests.py index 712d404958..09345d6df5 100644 --- a/test/registered/rust/test_run_rust_tests.py +++ b/test/registered/rust/test_run_rust_tests.py @@ -11,15 +11,17 @@ from sglang.test.test_utils import CustomTestCase BUILD_AND_RUN_TIMEOUT_S = 900 RUST_WORKSPACE = Path(__file__).resolve().parents[3] / "rust" -register_cpu_ci(est_time=153, suite="base-a-test-cpu") +GRPC_TYPES_PACKAGE = "sglang-grpc-types" +register_cpu_ci(est_time=170, suite="base-a-test-cpu") # Exported by _pr-test-stage-cpu.yml as the negation of the check-changes # rust_workspace paths filter; it defaults to false, so only a CI run that -# positively detected no rust/ changes skips the cargo build. +# positively detected no Rust workspace input changes skips the cargo build. @unittest.skipIf( envs.SGLANG_SKIP_RUST_TESTS.get(), - "SGLANG_SKIP_RUST_TESTS is set (no rust/ workspace changes per CI check-changes)", + "SGLANG_SKIP_RUST_TESTS is set " + "(no Rust workspace input changes per CI check-changes)", ) class TestCargoWorkspace(CustomTestCase): def _run_cargo(self, args: list[str], *, cwd: Path, env: dict | None = None): @@ -55,6 +57,18 @@ class TestCargoWorkspace(CustomTestCase): ) self._run_cargo(["test", "--workspace"], cwd=RUST_WORKSPACE) + # Verify that the public crate builds with only its packaged inputs. + self._run_cargo( + [ + "package", + "-p", + GRPC_TYPES_PACKAGE, + "--locked", + "--allow-dirty", + "--offline", + ], + cwd=RUST_WORKSPACE, + ) if __name__ == "__main__": diff --git a/test/registered/rust/test_run_sglang_radix_tree_rust_tests.py b/test/registered/rust/test_run_sglang_radix_tree_rust_tests.py index eafb0d188a..43f08bd89f 100644 --- a/test/registered/rust/test_run_sglang_radix_tree_rust_tests.py +++ b/test/registered/rust/test_run_sglang_radix_tree_rust_tests.py @@ -19,7 +19,8 @@ register_cpu_ci(est_time=80, suite="base-a-test-cpu") @unittest.skipIf( envs.SGLANG_SKIP_RUST_TESTS.get(), - "SGLANG_SKIP_RUST_TESTS is set (no rust/ workspace changes per CI check-changes)", + "SGLANG_SKIP_RUST_TESTS is set " + "(no Rust workspace input changes per CI check-changes)", ) class TestSGLangRadixTreeCargo(CustomTestCase): def test_sglang_radix_tree_native_tests(self):