Skip to content

The platform for continuously improving AI agents.

2.7 KB
Raw Blame
"""SGLang launch for Qwen4 MoE. The shared builder's fa3 and EAGLE flags do not load it."""

from __future__ import annotations

import os
import secrets

# This image 403s /load_lora_adapter unless the server and the caller share a key.
# Generated per process so it never sits in the repo or the logged argv.
_ADMIN_API_KEY = secrets.token_urlsafe(24)


def build_args(
    *,
    model_path: str,
    served_name: str,
    port: int,
    draft_model: str = "",
    draft_tokens: int = 0,
    mtp: tuple[int, int, int] | None = None,
    tool_call_parser: str = "",
    reasoning_parser: str = "",
    gpu_type: str = "",
    model: str = "",
    enable_lora: bool = False,
    max_lora_rank: int = 16,
    lora_target_modules: str = "",
    mem_fraction: str = "0.90",
    max_model_len: int | None = None,
    max_running_requests: int | None = None,
) -> list[str]:
    del draft_model, draft_tokens, gpu_type, model, max_running_requests
    cmd = [
        "python3",
        "-m",
        "sglang.launch_server",
        "--model-path",
        model_path,
        "--served-model-name",
        served_name,
        "--tp-size",
        "1",
        "--mem-fraction-static",
        mem_fraction,
        "--dtype",
        "bfloat16",
        "--trust-remote-code",
        "--qsa-indexer-dtype",
        "fp8_e4m3",
        "--linear-attn-prefill-backend",
        "flashinfer",
        "--linear-attn-decode-backend",
        "flashinfer",
        # NEXTN verify rejects a bfloat16 mamba state. Leave the dtype at the default.
        "--host",
        "0.0.0.0",
        "--port",
        str(port),
    ]
    if max_model_len:
        cmd += ["--context-length", str(max_model_len)]
    if mtp:
        steps, topk, tokens = mtp
        cmd += [
            "--speculative-algorithm",
            "NEXTN",
            "--speculative-num-steps",
            str(steps),
            "--speculative-eagle-topk",
            str(topk),
            "--speculative-num-draft-tokens",
            str(tokens),
        ]
    if enable_lora:
        modules = [
            part.strip() for part in (lora_target_modules or "all").split(",") if part.strip()
        ]
        cmd += [
            "--enable-lora",
            "--max-lora-rank",
            str(max_lora_rank),
            "--lora-target-modules",
            *modules,
        ]
    cmd += [
        "--tool-call-parser",
        tool_call_parser or "qwen4",
        "--reasoning-parser",
        reasoning_parser or "qwen4",
    ]
    return cmd


def process_env() -> dict[str, str]:
    env = os.environ.copy()
    env["SGLANG_ADMIN_API_KEY"] = _ADMIN_API_KEY
    return env


def admin_headers() -> dict[str, str]:
    return {"Authorization": f"Bearer {_ADMIN_API_KEY}"}