[grpc] style fix for grpc compilation. (#11175)

This commit is contained in:
Liangsheng Yin
2025-10-03 01:44:29 +08:00
committed by GitHub
parent 458611de77
commit bfcd9b2433
3 changed files with 33 additions and 24 deletions

View File

@@ -1,5 +1,4 @@
default_stages: [pre-commit, pre-push, manual] default_stages: [pre-commit, pre-push, manual]
exclude: ^python/sglang/srt/grpc/
repos: repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks

View File

@@ -66,7 +66,9 @@ dependencies = [
"transformers==4.56.1", "transformers==4.56.1",
"uvicorn", "uvicorn",
"uvloop", "uvloop",
"xgrammar==0.1.24" "xgrammar==0.1.24",
"grpcio==1.74.0", # keep it align with compile_proto.py
"grpcio-tools==1.74.0" # keep it align with compile_proto.py
] ]
[project.optional-dependencies] [project.optional-dependencies]

52
python/sglang/srt/grpc/compile_proto.py Normal file → Executable file
View File

@@ -16,20 +16,21 @@ Options:
--proto-file Specify proto file (default: sglang_scheduler.proto) --proto-file Specify proto file (default: sglang_scheduler.proto)
### Install Dependencies ### Install Dependencies
pip install grpcio grpcio-tools pip install "grpcio==1.74.0" "grpcio-tools==1.74.0"
### Run Script ### Run Script
cd python/sglang/srt/grpc cd python/sglang/srt/grpc
python compile_proto.py python compile_proto.py
""" """
import argparse import argparse
import os
import sys
import subprocess import subprocess
import time import sys
from importlib.metadata import version
from pathlib import Path from pathlib import Path
from typing import List, Optional
GRPC_VERSION = "1.74.0"
def get_file_mtime(path: Path) -> float: def get_file_mtime(path: Path) -> float:
@@ -72,17 +73,28 @@ def compile_proto(proto_file: Path, output_dir: Path, verbose: bool = True) -> b
import grpc_tools.protoc import grpc_tools.protoc
except ImportError: except ImportError:
print("Error: grpcio-tools not installed") print("Error: grpcio-tools not installed")
print("Install with: pip install grpcio-tools") print(
f'Install with: pip install "grpcio-tools=={GRPC_VERSION}" "grpcio=={GRPC_VERSION}"'
)
return False return False
grpc_tools_version = version("grpcio-tools")
grpc_version = version("grpcio")
if grpc_tools_version != GRPC_VERSION or grpc_version != GRPC_VERSION:
raise RuntimeError(
f"Error: grpcio-tools version {grpc_tools_version} and grpcio version {grpc_version} detected, but {GRPC_VERSION} is required."
)
# Compile command # Compile command
cmd = [ cmd = [
sys.executable, "-m", "grpc_tools.protoc", sys.executable,
"-m",
"grpc_tools.protoc",
f"-I{proto_file.parent}", f"-I{proto_file.parent}",
f"--python_out={output_dir}", f"--python_out={output_dir}",
f"--grpc_python_out={output_dir}", f"--grpc_python_out={output_dir}",
f"--pyi_out={output_dir}", # Generate type stubs f"--pyi_out={output_dir}", # Generate type stubs
str(proto_file.name) str(proto_file.name),
] ]
if verbose: if verbose:
@@ -102,7 +114,7 @@ def compile_proto(proto_file: Path, output_dir: Path, verbose: bool = True) -> b
generated_files = [ generated_files = [
f"{proto_file.stem}_pb2.py", f"{proto_file.stem}_pb2.py",
f"{proto_file.stem}_pb2_grpc.py", f"{proto_file.stem}_pb2_grpc.py",
f"{proto_file.stem}_pb2.pyi" f"{proto_file.stem}_pb2.pyi",
] ]
missing_files = [] missing_files = []
@@ -149,10 +161,7 @@ def add_generation_header(output_dir: Path, proto_stem: str) -> None:
""" """
files_to_update = [ files_to_update = [f"{proto_stem}_pb2.py", f"{proto_stem}_pb2_grpc.py"]
f"{proto_stem}_pb2.py",
f"{proto_stem}_pb2_grpc.py"
]
for filename in files_to_update: for filename in files_to_update:
file_path = output_dir / filename file_path = output_dir / filename
@@ -167,33 +176,32 @@ def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Compile protobuf files for SGLang gRPC server", description="Compile protobuf files for SGLang gRPC server",
formatter_class=argparse.RawDescriptionHelpFormatter, formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__ epilog=__doc__,
) )
parser.add_argument( parser.add_argument(
"--check", "--check",
action="store_true", action="store_true",
help="Check if regeneration is needed (exit 1 if needed)" help="Check if regeneration is needed (exit 1 if needed)",
) )
parser.add_argument( parser.add_argument(
"--proto-file", "--proto-file",
type=str, type=str,
default="sglang_scheduler.proto", default="sglang_scheduler.proto",
help="Proto file to compile (default: sglang_scheduler.proto)" help="Proto file to compile (default: sglang_scheduler.proto)",
) )
parser.add_argument( parser.add_argument(
"-v", "--verbose", "-v",
"--verbose",
action="store_true", action="store_true",
default=True, default=True,
help="Verbose output (default: True)" help="Verbose output (default: True)",
) )
parser.add_argument( parser.add_argument(
"-q", "--quiet", "-q", "--quiet", action="store_true", help="Quiet mode (overrides verbose)"
action="store_true",
help="Quiet mode (overrides verbose)"
) )
args = parser.parse_args() args = parser.parse_args()
@@ -234,4 +242,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
main() main()