The platform for continuously improving AI agents.
Speed serverless SGLang boot and give BF16 vLLM one MTP token (#318)
Rohit Gupta4de3c5551b8e
diff --git a/modal_shared/images/serve.py b/modal_shared/images/serve.pyindex 664c923f..99789cb1 100644--- a/modal_shared/images/serve.py+++ b/modal_shared/images/serve.py@@ -64,7 +64,9 @@ dedicated_sglang_image = attach_serving_args(modal.Image.from_registry(f"lmsysorg/sglang:{_DEDICATED_SGLANG_TAG}",setup_dockerfile_commands=["RUN ln -sf $(command -v python3) /usr/local/bin/python"],- ).entrypoint([])+ )+ .entrypoint([])+ .pip_install("runai-model-streamer>=0.15.7"))# LFM3 conv layers are absent from v0.5.22 and from vLLM 0.33. Support is the# untagged image built from sgl-project/sglang#46858. Do not point other models here.@@ -72,7 +74,9 @@ dedicated_sglang_lfm3_image = attach_serving_args(modal.Image.from_registry("lmsysorg/sglang:dev-lfm3",setup_dockerfile_commands=["RUN ln -sf $(command -v python3) /usr/local/bin/python"],- ).entrypoint([])+ )+ .entrypoint([])+ .pip_install("runai-model-streamer>=0.15.7"))SERVE_IMAGES: dict[str, modal.Image] = {diff --git a/modal_shared/serving/sglang.py b/modal_shared/serving/sglang.pyindex 5b3df398..c09ca71e 100644--- a/modal_shared/serving/sglang.py+++ b/modal_shared/serving/sglang.py@@ -2,6 +2,11 @@from __future__ import annotations+import json+from pathlib import Path++from modal_shared.serving.args import CUDAGRAPH_CAPTURE_SIZES+# vLLM's parser names that SGLang spells differently. Other names match._TOOL_PARSER_NAMES = {"openai": "gpt-oss",@@ -25,6 +30,25 @@ def sglang_reasoning_parser(name: str) -> str:return _REASONING_PARSER_NAMES.get(name, name)+def _position_limit(model_path: str) -> int | None:+ path = Path(model_path) / "config.json"+ if not path.is_file():+ return None+ try:+ config = json.loads(path.read_text())+ except (OSError, json.JSONDecodeError):+ return None+ sources = [config]+ text = config.get("text_config")+ if isinstance(text, dict):+ sources.append(text)+ for source in sources:+ limit = source.get("max_position_embeddings")+ if type(limit) is int and limit > 0:+ return limit+ return None++def attention_backend(gpu_type: str, *, model: str = "") -> str:# fa3 is the Hopper kernel. On Ada and Ampere it dies during graph capture.lowered = model.lower()@@ -59,6 +83,8 @@ def build_sglang_args(max_lora_rank: int = 16,lora_target_modules: str = "",mem_fraction: str = "0.75",+ max_model_len: int | None = None,+ max_running_requests: int | None = None,) -> list[str]:cmd = ["python3",@@ -85,6 +111,27 @@ def build_sglang_args("--port",str(port),]+ # Runtime LoRA load rejects this format (the LFM3 image has no RUNAI_STREAMER+ # loader). A folded checkpoint has no adapter load, so it can stream.+ if not enable_lora:+ cmd += ["--load-format", "runai_streamer"]+ if max_model_len:+ limit = _position_limit(model_path)+ cmd += ["--context-length", str(min(max_model_len, limit) if limit else max_model_len)]+ # Triton dequantizes FP8 KV before attention. fa3 and flashinfer fuse it.+ if backend in {"fa3", "flashinfer"}:+ cmd += ["--kv-cache-dtype", "fp8_e4m3"]+ # 0.88 filled an L4 and the EAGLE draft capture was killed. 0.75 leaves+ # that headroom. Prefill graphs are the long boot; decode graphs stay.+ cmd.append("--disable-prefill-cuda-graph")+ # The short capture list drops speculative acceptance. Cap the width instead.+ if mtp or draft_model:+ cmd += ["--cuda-graph-max-bs-decode", "32"]+ else:+ sizes = [size for size in CUDAGRAPH_CAPTURE_SIZES if size <= (max_running_requests or size)]+ cmd += ["--cuda-graph-bs-decode", *(str(size) for size in sizes)]+ if max_running_requests:+ cmd += ["--max-running-requests", str(max_running_requests)]if mtp:steps, topk, tokens = mtpcmd += [diff --git a/modal_shared/serving/speculative.py b/modal_shared/serving/speculative.pyindex a531ba0d..bafe352b 100644--- a/modal_shared/serving/speculative.py+++ b/modal_shared/serving/speculative.py@@ -49,6 +49,10 @@ def mtp_for(repo: str) -> tuple[int, int, int] | None:return _MTP.get(repo)+# vLLM's hybrid cache accepts one MTP token. The tuple above is the SGLang EAGLE recipe.+VLLM_MTP_TOKENS = 1++# 15-token DFlash on this hybrid leaves 2.9 GiB of KV on an 80GB GPU, and the# configured 16384 context needs 3.35. H200 (141) and larger still take the draft._DRAFT_MIN_VRAM_GB = {diff --git a/overbae/modal/modal_vllm_worker.py b/overbae/modal/modal_vllm_worker.pyindex 9373f67a..6e4e4734 100644--- a/overbae/modal/modal_vllm_worker.py+++ b/overbae/modal/modal_vllm_worker.py@@ -439,7 +439,12 @@ class _BaseVLLMWorker:multimodal_text_only_args,)from modal_shared.serving.capacity import GPU_VRAM_GB, sequence_cap_for_checkpoint- from modal_shared.serving.speculative import draft_for, draft_volume_path, mtp_for+ from modal_shared.serving.speculative import (+ VLLM_MTP_TOKENS,+ draft_for,+ draft_volume_path,+ mtp_for,+ )spec = resolve(base_model or self.model_name)draft_model = ""@@ -488,6 +493,8 @@ class _BaseVLLMWorker:enable_lora=self.enable_lora,max_lora_rank=self.max_lora_rank,lora_target_modules=lora_targets,+ max_model_len=self.max_model_len,+ max_running_requests=max_num_seqs,)else:cmd = (@@ -507,6 +514,7 @@ class _BaseVLLMWorker:gpu_type=getattr(self, "gpu_type", ""),draft_model=draft_model,draft_tokens=draft_tokens,+ mtp_tokens=VLLM_MTP_TOKENS if mtp else 0,),spec,)diff --git a/tests/test_serving_args.py b/tests/test_serving_args.pyindex 27d79717..d57460a7 100644--- a/tests/test_serving_args.py+++ b/tests/test_serving_args.py@@ -219,6 +219,7 @@ def test_sglang_dflash_argv_and_engine_choice() -> None:max_lora_rank=16,)assert lora_cmd[lora_cmd.index("--max-lora-rank") + 1] == "16"+ assert "--load-format" not in lora_cmdassert lora_cmd[lora_cmd.index("--lora-target-modules") + 1] == "all"named = build_sglang_args(model_path="/tmp/base",@@ -241,10 +242,63 @@ def test_sglang_dflash_argv_and_engine_choice() -> None:model="LiquidAI/LFM3-1.7B",)assert "--attention-backend" not in lfm3+ assert ada[ada.index("--mem-fraction-static") + 1] == "0.75"+ assert ada[ada.index("--load-format") + 1] == "runai_streamer"+ assert "--disable-prefill-cuda-graph" in ada+ assert "--model-loader-extra-config" not in ada+ assert ada[ada.index("--kv-cache-dtype") + 1] == "fp8_e4m3"+ assert cmd[cmd.index("--kv-cache-dtype") + 1] == "fp8_e4m3"+ assert "--kv-cache-dtype" not in oss+ assert "--kv-cache-dtype" not in gemma+ assert "--kv-cache-dtype" not in lfm3+ assert "--cuda-graph-bs-decode" not in cmd+ assert "--cuda-graph-bs-decode" not in mtp_cmd+ assert mtp_cmd[mtp_cmd.index("--cuda-graph-max-bs-decode") + 1] == "32"+ graph = ada.index("--cuda-graph-bs-decode") + 1+ assert ada[graph : graph + len(CUDAGRAPH_CAPTURE_SIZES)] == [+ str(size) for size in CUDAGRAPH_CAPTURE_SIZES+ ]+++def test_sglang_context_clamps_to_the_checkpoint(tmp_path: Path) -> None:+ from modal_shared.serving.sglang import build_sglang_args++ checkpoint = tmp_path / "weights"+ checkpoint.mkdir()+ (checkpoint / "config.json").write_text(json.dumps({"max_position_embeddings": 4096}))+ cmd = build_sglang_args(+ model_path=str(checkpoint),+ served_name="deploy-1",+ port=8000,+ gpu_type="L4",+ max_model_len=8192,+ max_running_requests=4,+ )+ assert cmd[cmd.index("--context-length") + 1] == "4096"+ assert cmd[cmd.index("--max-running-requests") + 1] == "4"+ graph = cmd.index("--cuda-graph-bs-decode") + 1+ assert cmd[graph : graph + 3] == ["1", "2", "4"]+ nested = tmp_path / "nested"+ nested.mkdir()+ (nested / "config.json").write_text(+ json.dumps({"text_config": {"max_position_embeddings": 2048}})+ )+ nested_cmd = build_sglang_args(+ model_path=str(nested),+ served_name="deploy-1",+ port=8000,+ max_model_len=8192,+ )+ assert nested_cmd[nested_cmd.index("--context-length") + 1] == "2048"def test_dflash_argv_for_the_three_bases(tmp_path: Path) -> None:- from modal_shared.serving.speculative import draft_for, draft_volume_path, mtp_for+ from modal_shared.serving.speculative import (+ VLLM_MTP_TOKENS,+ draft_for,+ draft_volume_path,+ mtp_for,+ )cmd = _args(_checkpoint(tmp_path),@@ -259,10 +313,11 @@ def test_dflash_argv_for_the_three_bases(tmp_path: Path) -> None:"num_speculative_tokens": 15,}assert "--cudagraph-capture-sizes" not in cmd- mtp = _args(_checkpoint(tmp_path), base_model="Qwen/Qwen3.5-4B", mtp_tokens=1)+ mtp = _args(_checkpoint(tmp_path), base_model="Qwen/Qwen3.6-27B", mtp_tokens=VLLM_MTP_TOKENS)mtp_payload = json.loads(mtp[mtp.index("--speculative-config") + 1])assert mtp_payload == {"method": "mtp", "num_speculative_tokens": 1}assert "--cudagraph-capture-sizes" not in mtp+ assert "--speculative-config" not in _args(_checkpoint(tmp_path), base_model="Qwen/Qwen3.6-27B")for repo in ("Qwen/Qwen3.5-4B","unsloth/Qwen3.5-4B",