Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 30 additions & 22 deletions bindings/python/src/smg/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,23 @@ def gpu_env(self, args: argparse.Namespace, dp_rank: int, env: dict | None = Non
return env

def _filter_backend_args(self, backend_args: list[str], filter_args: list[str]) -> list[str]:
"""Filter backend_args to only include those relevant for vLLM's grpc_server."""
filtered_backend_args = []
"""Filter out args from backend_args that are already set by the launcher.

Handles both ``--key value`` and ``--key=value`` syntax.
"""
filtered = []
skip_next = False
for arg in backend_args:
if skip_next:
skip_next = False
continue
if arg in filter_args:
skip_next = True # Assume all valid args take a value
key = arg.split("=", 1)[0]
if key in filter_args:
if "=" not in arg:
skip_next = True # value is the next token
continue
filtered_backend_args.append(arg)

return filtered_backend_args
filtered.append(arg)
Comment thread
slin1237 marked this conversation as resolved.
Resolved
return filtered

def launch(
self, args: argparse.Namespace, backend_args: list[str], host: str, port: int, env: dict
Expand Down Expand Up @@ -124,7 +128,7 @@ def build_command(


class VllmWorkerLauncher(WorkerLauncher):
"""Launcher for vLLM inference workers (gRPC mode only)."""
"""Launcher for vLLM inference workers."""

def _get_tp_size(self, args: argparse.Namespace) -> int:
return getattr(args, "tensor_parallel_size", 1)
Expand Down Expand Up @@ -165,6 +169,10 @@ def _get_tp_size(self, args: argparse.Namespace) -> int:
"""Get tensor parallel size from args or config file.

Priority: args.tp_size > args.tensor_parallel_size > config file > default(1)

Raises:
FileNotFoundError: If --config path does not exist.
yaml.YAMLError: If --config file contains invalid YAML.
"""
# Try --tp-size argument first
tp_size = getattr(args, "tp_size", None)
Expand All @@ -176,22 +184,22 @@ def _get_tp_size(self, args: argparse.Namespace) -> int:
if tp_size is not None:
return tp_size

# Try reading from config YAML file
# Try reading from config YAML file — let I/O and parse errors propagate
# so misconfigurations are caught early instead of silently using tp=1.
config_path = getattr(args, "config", None)
if config_path:
try:
import yaml

with open(config_path) as f:
config = yaml.safe_load(f)
if config and "tensor_parallel_size" in config:
return int(config["tensor_parallel_size"])
if config and "tp_size" in config:
return int(config["tp_size"])
except Exception as e:
logger.warning(
"Failed to read tensor_parallel_size from config %s: %s", config_path, e
)
import yaml

with open(config_path) as f:
config = yaml.safe_load(f)
if config and "tensor_parallel_size" in config:
return int(config["tensor_parallel_size"])
if config and "tp_size" in config:
return int(config["tp_size"])
logger.warning(
"Config %s does not contain tensor_parallel_size or tp_size, defaulting to 1",
config_path,
)

return 1

Expand Down
66 changes: 54 additions & 12 deletions bindings/python/tests/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,37 @@ def test_gpu_env_none_copies_os_environ(self):
assert env["CUDA_VISIBLE_DEVICES"] == "0"


class TestFilterBackendArgs:
"""Test _filter_backend_args handles both --key value and --key=value."""

def test_filters_key_value_pair(self):
launcher = SglangWorkerLauncher()
backend_args = ["--model-path", "/tmp/m", "--trust-remote-code"]
result = launcher._filter_backend_args(backend_args, ["--model-path"])
assert result == ["--trust-remote-code"]

def test_filters_key_equals_value(self):
launcher = SglangWorkerLauncher()
backend_args = ["--model-path=/tmp/m", "--trust-remote-code"]
result = launcher._filter_backend_args(backend_args, ["--model-path"])
assert result == ["--trust-remote-code"]

def test_filters_multiple_keys(self):
launcher = SglangWorkerLauncher()
backend_args = ["--model", "/tmp/m", "--host", "0.0.0.0", "--extra-flag"]
result = launcher._filter_backend_args(backend_args, ["--model", "--host"])
assert result == ["--extra-flag"]

def test_empty_backend_args(self):
launcher = SglangWorkerLauncher()
assert launcher._filter_backend_args([], ["--model"]) == []

def test_no_filter_args(self):
launcher = SglangWorkerLauncher()
backend_args = ["--foo", "bar"]
assert launcher._filter_backend_args(backend_args, []) == ["--foo", "bar"]


class TestSglangWorkerLauncher:
"""Test SglangWorkerLauncher.build_command()."""

Expand Down Expand Up @@ -485,7 +516,6 @@ def test_build_command(self):
assert "0.0.0.0" in cmd
assert "--port" in cmd
assert "50051" in cmd
print(cmd)
for arg in backend_args:
assert arg in cmd

Expand Down Expand Up @@ -533,26 +563,38 @@ def test_get_tp_size_from_config_tensor_parallel_size_takes_precedence(self, tmp
args = argparse.Namespace(config=str(config_file))
assert launcher._get_tp_size(args) == 8

def test_get_tp_size_from_config_read_fails_returns_default(self):
"""When config file read fails, log warning and return default 1."""
def test_get_tp_size_from_config_read_fails_raises(self):
"""When config file does not exist, FileNotFoundError propagates."""
launcher = TrtllmWorkerLauncher()
args = argparse.Namespace(config="/nonexistent/config.yaml")
with pytest.raises(FileNotFoundError):
launcher._get_tp_size(args)

def test_get_tp_size_from_config_empty_or_no_keys_returns_default(self, tmp_path):
"""When config has no tp keys or is empty, warn and return default 1."""
config_file = tmp_path / "config.yaml"
config_file.write_text("other_key: 42\n")
launcher = TrtllmWorkerLauncher()
args = argparse.Namespace(config=str(config_file))
with patch("smg.serve.logger") as mock_logger:
result = launcher._get_tp_size(args)
assert result == 1
mock_logger.warning.assert_called_once()
call_args = mock_logger.warning.call_args[0]
assert "Failed to read tensor_parallel_size from config" in call_args[0]
assert "/nonexistent/config.yaml" in call_args[1]
assert call_args[2] is not None # exception message or object
assert (
"does not contain tensor_parallel_size or tp_size"
in mock_logger.warning.call_args[0][0]
)

def test_get_tp_size_from_config_empty_or_no_keys_returns_default(self, tmp_path):
"""When config has no tp keys or is empty, return default 1."""
config_file = tmp_path / "config.yaml"
config_file.write_text("other_key: 42\n")
def test_get_tp_size_from_config_malformed_yaml_raises(self, tmp_path):
"""When config file contains invalid YAML, error propagates."""
config_file = tmp_path / "bad.yaml"
config_file.write_text("{{invalid yaml")
launcher = TrtllmWorkerLauncher()
args = argparse.Namespace(config=str(config_file))
assert launcher._get_tp_size(args) == 1
import yaml

with pytest.raises(yaml.YAMLError):
launcher._get_tp_size(args)


# ---------------------------------------------------------------------------
Expand Down