Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"lib/backend-common",
"lib/backend-common/examples/mocker",
"lib/vllm-rs-backend",
"lib/sglang-sidecar",
"lib/bindings/c",
"lib/bindings/python/codegen",
"deploy/inference-gateway/ext-proc",
Expand Down
54 changes: 54 additions & 0 deletions examples/backends/sglang/launch/sidecar_agg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Aggregated serving through SGLang's native gRPC server (1 GPU).
# Requires an SGLang build containing PR #23508 and, until it merges, PR #25185.

set -e
trap 'echo Cleaning up...; kill 0' EXIT

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
source "$SCRIPT_DIR/../../../common/launch_utils.sh"

MODEL="Qwen/Qwen3-0.6B"
EXTRA_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--model-path)
MODEL="$2"
shift 2
;;
-h|--help)
echo "Usage: $0 [--model-path <name>] [SGLang options...]"
exit 0
;;
*)
EXTRA_ARGS+=("$1")
shift
;;
esac
done

SGLANG_HOST="${SGLANG_HOST:-127.0.0.1}"
SGLANG_HTTP_PORT="${SGLANG_HTTP_PORT:-30000}"
SGLANG_GRPC_PORT="${SGLANG_GRPC_PORT:-30001}"
HTTP_PORT="${DYN_HTTP_PORT:-8000}"

print_launch_banner "Launching SGLang Native-gRPC Sidecar (1 GPU)" "$MODEL" "$HTTP_PORT"

python3 -m dynamo.frontend &

# --grpc-port enables the native Rust gRPC server alongside SGLang's HTTP API.
python3 -m sglang.launch_server \
Comment thread
connorcarpenter15 marked this conversation as resolved.
Resolved
--model-path "$MODEL" \
--host "$SGLANG_HOST" \
--port "$SGLANG_HTTP_PORT" \
--grpc-port "$SGLANG_GRPC_PORT" \
"${EXTRA_ARGS[@]}" &

DYN_SYSTEM_PORT=${DYN_SYSTEM_PORT:-8081} \
dynamo-sglang-sidecar \
--sglang-endpoint "${SGLANG_HOST}:${SGLANG_GRPC_PORT}" &

wait_any_exit
95 changes: 95 additions & 0 deletions examples/backends/sglang/launch/sidecar_disagg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Disaggregated serving through two SGLang native gRPC servers (2 GPUs).
# Requires SGLang PR #25185 plus server-side DisaggregatedParams forwarding.

set -e

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
source "$SCRIPT_DIR/../../../common/gpu_utils.sh"
source "$SCRIPT_DIR/../../../common/launch_utils.sh"

MODEL="Qwen/Qwen3-0.6B"
EXTRA_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--model-path)
if [[ $# -lt 2 || "$2" == -* ]]; then
echo "Missing value for --model-path"
echo "Use --help for usage information"
exit 1
fi
MODEL="$2"
shift 2
;;
-h|--help)
echo "Usage: $0 [--model-path <name>] [SGLang options...]"
echo
echo "Set SGLANG_PYTHON to the Python executable containing the patched SGLang build."
exit 0
;;
*)
EXTRA_ARGS+=("$1")
shift
;;
esac
done

trap 'echo Cleaning up...; kill 0' EXIT

SGLANG_PYTHON="${SGLANG_PYTHON:-python3}"
SGLANG_HOST="${SGLANG_HOST:-127.0.0.1}"
SGLANG_BOOTSTRAP_HOST="${SGLANG_BOOTSTRAP_HOST:-$SGLANG_HOST}"
SGLANG_DISAGGREGATION_BOOTSTRAP_PORT="${SGLANG_DISAGGREGATION_BOOTSTRAP_PORT:-8998}"
SGLANG_PREFILL_HTTP_PORT="${SGLANG_PREFILL_HTTP_PORT:-30000}"
SGLANG_PREFILL_GRPC_PORT="${SGLANG_PREFILL_GRPC_PORT:-30001}"
SGLANG_DECODE_HTTP_PORT="${SGLANG_DECODE_HTTP_PORT:-30010}"
SGLANG_DECODE_GRPC_PORT="${SGLANG_DECODE_GRPC_PORT:-30011}"
SGLANG_PREFILL_GPU="${SGLANG_PREFILL_GPU:-0}"
SGLANG_DECODE_GPU="${SGLANG_DECODE_GPU:-1}"
HTTP_PORT="${DYN_HTTP_PORT:-8000}"

GPU_MEM_ARGS=$(build_sglang_gpu_mem_args)

print_launch_banner "Launching SGLang Native-gRPC Disaggregated Serving (2 GPUs)" "$MODEL" "$HTTP_PORT"

python3 -m dynamo.frontend &

CUDA_VISIBLE_DEVICES="$SGLANG_PREFILL_GPU" \
"$SGLANG_PYTHON" -m sglang.launch_server \
--model-path "$MODEL" \
--host "$SGLANG_HOST" \
--port "$SGLANG_PREFILL_HTTP_PORT" \
--grpc-port "$SGLANG_PREFILL_GRPC_PORT" \
--disaggregation-mode prefill \
--disaggregation-bootstrap-port "$SGLANG_DISAGGREGATION_BOOTSTRAP_PORT" \
--disaggregation-transfer-backend nixl \
$GPU_MEM_ARGS \
"${EXTRA_ARGS[@]}" &

CUDA_VISIBLE_DEVICES="$SGLANG_DECODE_GPU" \
"$SGLANG_PYTHON" -m sglang.launch_server \
--model-path "$MODEL" \
--host "$SGLANG_HOST" \
--port "$SGLANG_DECODE_HTTP_PORT" \
--grpc-port "$SGLANG_DECODE_GRPC_PORT" \
--disaggregation-mode decode \
--disaggregation-bootstrap-port "$SGLANG_DISAGGREGATION_BOOTSTRAP_PORT" \
--disaggregation-transfer-backend nixl \
$GPU_MEM_ARGS \
"${EXTRA_ARGS[@]}" &

OTEL_SERVICE_NAME=dynamo-worker-prefill \
DYN_SYSTEM_PORT="${DYN_SYSTEM_PORT1:-8081}" \
dynamo-sglang-sidecar \
--sglang-endpoint "${SGLANG_HOST}:${SGLANG_PREFILL_GRPC_PORT}" \
--bootstrap-host "$SGLANG_BOOTSTRAP_HOST" &

OTEL_SERVICE_NAME=dynamo-worker-decode \
DYN_SYSTEM_PORT="${DYN_SYSTEM_PORT2:-8082}" \
dynamo-sglang-sidecar \
--sglang-endpoint "${SGLANG_HOST}:${SGLANG_DECODE_GRPC_PORT}" &

wait_any_exit
3 changes: 2 additions & 1 deletion lib/backend-common/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub use dynamo_llm::protocols::common::llm_backend::{
LLMEngineOutput, LogProbs, TopLogprob, TopLogprobs,
};
pub use dynamo_llm::protocols::common::preprocessor::{
BootstrapInfo, PrefillResult, PreprocessedRequest,
BootstrapInfo, MultimodalData, MultimodalDataMap, PrefillResult, PreprocessedRequest,
RoutingHints,
};
pub use dynamo_llm::protocols::common::{
FinishReason, GuidedDecodingOptions, OutputOptions, SamplingOptions, StopConditions,
Expand Down
45 changes: 45 additions & 0 deletions lib/sglang-sidecar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

[package]
name = "dynamo-sglang-sidecar"
version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
description = "Dynamo SGLang sidecar backend — drives an out-of-process SGLang engine over its native gRPC service through the backend-common LLMEngine trait."

[[bin]]
name = "dynamo-sglang-sidecar"
path = "src/main.rs"

[dependencies]
dynamo-backend-common = { workspace = true }
dynamo-runtime = { workspace = true }

anyhow = { workspace = true }
async-stream = { workspace = true }
async-trait = { workspace = true }
clap = { version = "4", features = ["derive", "env"] }
futures = { workspace = true }
rand = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
tokio-util = { workspace = true }
tracing = { workspace = true }
url = { workspace = true }

# tonic / prost are pinned per-crate (not workspace deps): tonic 0.13.1
# depends on prost 0.13.5, matching `lib/llm`. tonic-build 0.13.1 still
# bundles prost codegen via `compile_protos` (the 0.14 split to
# tonic-prost-build does not apply here).
prost = { version = "0.13.5" }
tonic = { version = "0.13.1" }

[build-dependencies]
tonic-build = { version = "0.13.1" }

[dev-dependencies]
dynamo-backend-common = { workspace = true, features = ["testing"] }
52 changes: 52 additions & 0 deletions lib/sglang-sidecar/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Fetches SGLang's pinned native gRPC contract and compiles client stubs.

use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
let fetch_script = manifest_dir.join("scripts/fetch_sglang_proto.py");
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let proto_path = out_dir.join("sglang.proto");
let python = env::var_os("PYTHON").unwrap_or_else(|| "python3".into());

let output = Command::new(&python)
Comment thread
connorcarpenter15 marked this conversation as resolved.
Resolved
.arg(&fetch_script)
.arg(&proto_path)
.output()
.map_err(|err| {
format!(
"failed to run {} with {:?}: {err}",
fetch_script.display(),
python
)
})?;

if !output.status.success() {
return Err(format!(
"failed to fetch SGLang gRPC proto (status {}):\n{}{}",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
)
.into());
}

// `--experimental_allow_proto3_optional` lets older protoc (the container's
// apt protoc 3.12) compile the proto3 `optional` fields in sglang.proto
// (e.g. `routed_dp_rank`). The flag was added in protoc 3.12 for exactly
// this and is a no-op on 3.15+ where proto3 optional is stable.
tonic_build::configure()
.protoc_arg("--experimental_allow_proto3_optional")
.compile_protos(&[proto_path.as_path()], &[out_dir.as_path()])?;

println!("cargo:rerun-if-changed={}", fetch_script.display());
println!("cargo:rerun-if-env-changed=PYTHON");
println!("cargo:rerun-if-env-changed=SGLANG_PROTO_PATH");
println!("cargo:rerun-if-env-changed=SGLANG_SOURCE");
Ok(())
}
96 changes: 96 additions & 0 deletions lib/sglang-sidecar/scripts/fetch_sglang_proto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""Fetch the pinned SGLang gRPC contract used by the sidecar.

Normal builds download the immutable file from the SGLang commit below. Set
``SGLANG_PROTO_PATH`` to an exact local proto, or ``SGLANG_SOURCE`` to the root
of an SGLang checkout, when building without network access. Every source is
checked against the pinned digest before it is passed to ``tonic-build``.
"""

from __future__ import annotations

import argparse
import hashlib
import os
from pathlib import Path
import sys
from urllib.error import URLError
from urllib.request import Request, urlopen


SGLANG_REVISION = "cf2aab82a3c5342f6bc89b7bae47a078ace1149c"
SGLANG_PROTO_RELATIVE_PATH = "proto/sglang/runtime/v1/sglang.proto"
SGLANG_PROTO_SHA256 = (
"a2e14952ddb2b34b6e22cbbc4e76d76d70c44f2dbf087cb9918aed3399d9ef42"
)
SGLANG_PROTO_URL = (
"https://raw.githubusercontent.com/sgl-project/sglang/"
f"{SGLANG_REVISION}/{SGLANG_PROTO_RELATIVE_PATH}"
)


def _read_proto() -> tuple[bytes, str]:
if local_proto := os.environ.get("SGLANG_PROTO_PATH"):
path = Path(local_proto).expanduser().resolve()
try:
return path.read_bytes(), str(path)
except OSError as err:
raise SystemExit(f"failed to read SGLANG_PROTO_PATH {path}: {err}") from err

if sglang_source := os.environ.get("SGLANG_SOURCE"):
path = (
Path(sglang_source).expanduser().resolve() / SGLANG_PROTO_RELATIVE_PATH
)
try:
return path.read_bytes(), str(path)
except OSError as err:
raise SystemExit(f"failed to read SGLANG_SOURCE proto {path}: {err}") from err

request = Request(SGLANG_PROTO_URL, headers={"User-Agent": "dynamo-build"})
try:
with urlopen(request, timeout=30) as response:
return response.read(), SGLANG_PROTO_URL
except (OSError, URLError) as err:
raise SystemExit(
f"failed to download SGLang proto from {SGLANG_PROTO_URL}: {err}\n"
"For an offline build, set SGLANG_PROTO_PATH to the proto file or "
"SGLANG_SOURCE to a checkout at the pinned revision."
) from err


def _verify_proto(contents: bytes, source: str) -> None:
actual = hashlib.sha256(contents).hexdigest()
if actual != SGLANG_PROTO_SHA256:
raise SystemExit(
f"SGLang proto checksum mismatch for {source}: "
f"expected {SGLANG_PROTO_SHA256}, got {actual}. "
"Update the pinned revision and checksum together."
)


def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("output", type=Path, help="destination for the verified proto")
args = parser.parse_args()

contents, source = _read_proto()
_verify_proto(contents, source)

output = args.output.resolve()
output.parent.mkdir(parents=True, exist_ok=True)
if output.exists() and output.read_bytes() == contents:
return

temporary = output.with_name(f".{output.name}.tmp")
temporary.write_bytes(contents)
os.replace(temporary, output)


if __name__ == "__main__":
try:
main()
except BrokenPipeError:
sys.exit(1)
Loading
Loading