94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
253 lines
8.8 KiB
Python
253 lines
8.8 KiB
Python
import time
|
|
from typing import Any, List, Optional, Union
|
|
|
|
from fastapi import APIRouter, Body, HTTPException
|
|
from pydantic import BaseModel, Field
|
|
|
|
from sglang.multimodal_gen.registry import get_model_info
|
|
from sglang.multimodal_gen.runtime.entrypoints.utils import (
|
|
ListLorasReq,
|
|
MergeLoraWeightsReq,
|
|
SetLoraReq,
|
|
UnmergeLoraWeightsReq,
|
|
format_lora_message,
|
|
)
|
|
from sglang.multimodal_gen.runtime.pipelines_core.schedule_batch import OutputBatch
|
|
from sglang.multimodal_gen.runtime.scheduler_client import async_scheduler_client
|
|
from sglang.multimodal_gen.runtime.server_args import get_global_server_args
|
|
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
|
from sglang.srt.utils.json_response import orjson_response
|
|
|
|
router = APIRouter(prefix="/v1")
|
|
logger = init_logger(__name__)
|
|
|
|
|
|
class ModelCard(BaseModel):
|
|
"""Model cards."""
|
|
|
|
id: str
|
|
object: str = "model"
|
|
created: int = Field(default_factory=lambda: int(time.time()))
|
|
owned_by: str = "sglang"
|
|
root: Optional[str] = None
|
|
parent: Optional[str] = None
|
|
max_model_len: Optional[int] = None
|
|
|
|
|
|
class DiffusionModelCard(ModelCard):
|
|
"""Extended ModelCard with diffusion-specific fields."""
|
|
|
|
num_gpus: Optional[int] = None
|
|
task_type: Optional[str] = None
|
|
dit_precision: Optional[str] = None
|
|
vae_precision: Optional[str] = None
|
|
pipeline_name: Optional[str] = None
|
|
pipeline_class: Optional[str] = None
|
|
|
|
|
|
async def _handle_lora_request(req: Any, success_msg: str, failure_msg: str):
|
|
try:
|
|
output: OutputBatch = await async_scheduler_client.forward(req)
|
|
if output.error is None:
|
|
return {"status": "ok", "message": success_msg}
|
|
else:
|
|
error_msg = output.error
|
|
raise HTTPException(status_code=500, detail=f"{failure_msg}: {error_msg}")
|
|
except Exception as e:
|
|
if isinstance(e, HTTPException):
|
|
raise
|
|
logger.error(f"Error during '{failure_msg}': {e}", exc_info=True)
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.post("/set_lora")
|
|
async def set_lora(
|
|
lora_nickname: Union[str, List[str]] = Body(..., embed=True),
|
|
lora_path: Optional[Union[str, List[Optional[str]]]] = Body(None, embed=True),
|
|
target: Union[str, List[str]] = Body("all", embed=True),
|
|
strength: Union[float, List[float]] = Body(1.0, embed=True),
|
|
merge_mode: Optional[str] = Body(None, embed=True),
|
|
):
|
|
"""
|
|
Set LoRA adapter(s) for the specified transformer(s).
|
|
Supports both single LoRA (backward compatible) and multiple LoRA adapters.
|
|
|
|
Args:
|
|
lora_nickname: The nickname(s) of the adapter(s). Can be a string or a list of strings.
|
|
lora_path: Path(s) to the LoRA adapter(s) (local path or HF repo id).
|
|
Can be a string, None, or a list of strings/None. Must match the length of lora_nickname.
|
|
target: Which transformer(s) to apply the LoRA to. Can be a string or a list of strings.
|
|
If a list, must match the length of lora_nickname. Valid values:
|
|
- "all": Apply to all transformers (default)
|
|
- "transformer": Apply only to the primary transformer (high noise for Wan2.2)
|
|
- "transformer_2": Apply only to transformer_2 (low noise for Wan2.2)
|
|
- "critic": Apply only to the critic model
|
|
strength: LoRA strength(s) for merge, default 1.0. Can be a float or a list of floats.
|
|
If a list, must match the length of lora_nickname. Values < 1.0 reduce the effect,
|
|
values > 1.0 amplify the effect.
|
|
merge_mode: Optional LoRA merge mode: "auto", "merge", or "dynamic".
|
|
"""
|
|
req = SetLoraReq(
|
|
lora_nickname=lora_nickname,
|
|
lora_path=lora_path,
|
|
target=target,
|
|
strength=strength,
|
|
merge_mode=merge_mode,
|
|
)
|
|
nickname_str, target_str, strength_str = format_lora_message(
|
|
lora_nickname, target, strength
|
|
)
|
|
|
|
return await _handle_lora_request(
|
|
req,
|
|
f"Successfully set LoRA adapter(s): {nickname_str} (target: {target_str}, strength: {strength_str})",
|
|
"Failed to set LoRA adapter",
|
|
)
|
|
|
|
|
|
@router.post("/merge_lora_weights")
|
|
async def merge_lora_weights(
|
|
target: str = Body("all", embed=True),
|
|
strength: float = Body(1.0, embed=True),
|
|
):
|
|
"""
|
|
Merge LoRA weights into the base model.
|
|
|
|
Args:
|
|
target: Which transformer(s) to merge. One of "all", "transformer",
|
|
"transformer_2", "critic".
|
|
strength: LoRA strength for merge, default 1.0. Values < 1.0 reduce the effect,
|
|
values > 1.0 amplify the effect.
|
|
"""
|
|
req = MergeLoraWeightsReq(target=target, strength=strength)
|
|
return await _handle_lora_request(
|
|
req,
|
|
f"Successfully merged LoRA weights (target: {target}, strength: {strength})",
|
|
"Failed to merge LoRA weights",
|
|
)
|
|
|
|
|
|
@router.post("/unmerge_lora_weights")
|
|
async def unmerge_lora_weights(
|
|
target: str = Body("all", embed=True),
|
|
):
|
|
"""
|
|
Unmerge LoRA weights from the base model.
|
|
|
|
Args:
|
|
target: Which transformer(s) to unmerge. One of "all", "transformer",
|
|
"transformer_2", "critic".
|
|
"""
|
|
req = UnmergeLoraWeightsReq(target=target)
|
|
return await _handle_lora_request(
|
|
req,
|
|
f"Successfully unmerged LoRA weights (target: {target})",
|
|
"Failed to unmerge LoRA weights",
|
|
)
|
|
|
|
|
|
@router.get("/model_info")
|
|
async def model_info():
|
|
"""Get the model information."""
|
|
server_args = get_global_server_args()
|
|
if not server_args:
|
|
raise HTTPException(status_code=500, detail="Server args not initialized")
|
|
|
|
result = {
|
|
"model_path": server_args.model_path,
|
|
}
|
|
return result
|
|
|
|
|
|
@router.get("/list_loras")
|
|
async def list_loras():
|
|
"""List loaded LoRA adapters and current application status per module."""
|
|
try:
|
|
req = ListLorasReq()
|
|
output: OutputBatch = await async_scheduler_client.forward(req)
|
|
if output.error is None:
|
|
return output.output or {}
|
|
else:
|
|
raise HTTPException(status_code=500, detail=output.error)
|
|
except Exception as e:
|
|
if isinstance(e, HTTPException):
|
|
raise
|
|
logger.error(f"Error during 'list_loras': {e}", exc_info=True)
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.get("/models")
|
|
async def available_models():
|
|
"""Show available models. OpenAI-compatible endpoint with extended diffusion info."""
|
|
server_args = get_global_server_args()
|
|
if not server_args:
|
|
raise HTTPException(status_code=500, detail="Server args not initialized")
|
|
|
|
model_info = get_model_info(
|
|
server_args.model_path,
|
|
backend=server_args.backend,
|
|
model_id=server_args.model_id,
|
|
)
|
|
|
|
card_kwargs = {
|
|
"id": server_args.model_path,
|
|
"root": server_args.model_path,
|
|
# Extended diffusion-specific fields
|
|
"num_gpus": server_args.num_gpus,
|
|
"task_type": server_args.pipeline_config.task_type.name,
|
|
"dit_precision": server_args.pipeline_config.dit_precision,
|
|
"vae_precision": server_args.pipeline_config.vae_precision,
|
|
}
|
|
|
|
if model_info:
|
|
card_kwargs["pipeline_name"] = model_info.pipeline_cls.pipeline_name
|
|
card_kwargs["pipeline_class"] = model_info.pipeline_cls.__name__
|
|
|
|
model_card = DiffusionModelCard(**card_kwargs)
|
|
|
|
# Return dict directly to preserve extended fields (ModelList strips them)
|
|
return {"object": "list", "data": [model_card.model_dump()]}
|
|
|
|
|
|
@router.get("/models/{model:path}")
|
|
async def retrieve_model(model: str):
|
|
"""Retrieve a model instance. OpenAI-compatible endpoint with extended diffusion info."""
|
|
server_args = get_global_server_args()
|
|
if not server_args:
|
|
raise HTTPException(status_code=500, detail="Server args not initialized")
|
|
|
|
if model != server_args.model_path:
|
|
return orjson_response(
|
|
{
|
|
"error": {
|
|
"message": f"The model '{model}' does not exist",
|
|
"type": "invalid_request_error",
|
|
"param": "model",
|
|
"code": "model_not_found",
|
|
}
|
|
},
|
|
status_code=404,
|
|
)
|
|
|
|
model_info = get_model_info(
|
|
server_args.model_path,
|
|
backend=server_args.backend,
|
|
model_id=server_args.model_id,
|
|
)
|
|
|
|
card_kwargs = {
|
|
"id": model,
|
|
"root": model,
|
|
"num_gpus": server_args.num_gpus,
|
|
"task_type": server_args.pipeline_config.task_type.name,
|
|
"dit_precision": server_args.pipeline_config.dit_precision,
|
|
"vae_precision": server_args.pipeline_config.vae_precision,
|
|
}
|
|
|
|
if model_info:
|
|
card_kwargs["pipeline_name"] = model_info.pipeline_cls.pipeline_name
|
|
card_kwargs["pipeline_class"] = model_info.pipeline_cls.__name__
|
|
|
|
# Return dict to preserve extended fields
|
|
return DiffusionModelCard(**card_kwargs).model_dump()
|