Agent Wikis

wikis / SGLang / wiki / concepts / diffusion-serving.md view as markdown report a mistake

type: conceptconfidence: mediumupdated: 2026-08-24sglang_version: v0.5.18sources: 13

Definition

SGLang Diffusion (the sglang.multimodal_gen module, branded "SGLang-Diffusion") is a high-performance inference framework for image and video generation, shipped alongside the LLM-serving core described in sglang overview. It provides native SGLang pipelines, diffusers-backend support for models without a native implementation, an OpenAI-compatible HTTP server, and an optimized kernel stack built on precompiled sgl-kernel operators plus JIT kernels (raw/github_doc-docs-docs-sglang-diffusion-index-mdx.md). It supports broad model families — Wan, Hunyuan, Qwen-Image, FLUX, Z-Image, GLM-Image, and more — across NVIDIA, AMD, Intel XPU, Ascend, Apple Silicon, and Moore Threads platforms.

How It Works

Installation

The standard install already bundles the optimized kernel stack (raw/github_doc-docs-docs-sglang-diffusion-installation-mdx.md):

pip install --upgrade pip
pip install uv
uv pip install "sglang[diffusion]" --prerelease=allow

From source: pip install -e "python[diffusion]" (or the uv equivalent) after cloning the repo. Docker images are published at lmsysorg/sglang (tag :dev for the standard path, ROCm-specific tags such as :v0.5.5.post2-rocm700-mi30x for AMD Instinct GPUs). Platform-specific paths exist for ROCm (AMD), MUSA (Moore Threads — requires swapping in python/pyproject_other.toml and installing the all_musa extra), Intel XPU, Ascend NPU, and Apple MPS (brew install ffmpeg uv, then uv pip install -e "python[all_mps]"; diffusion always runs on PyTorch MPS, and the all_mps extra's SRT MLX backend dependencies are unrelated — SGLANG_USE_MLX has no effect on diffusion).

CLI: generate and serve

Two entry points cover one-off jobs and persistent serving (raw/github_doc-docs-docs-sglang-diffusion-api-cli-mdx.md):

sglang generate --model-path Qwen/Qwen-Image --prompt "A beautiful sunset over the mountains" --save-output
sglang serve --model-path Wan-AI/Wan2.1-T2V-1.3B-Diffusers --num-gpus 4 --ulysses-degree 2 --ring-degree 2 --port 30010

sglang generate runs one generation job and exits; HTTP-server-only arguments are ignored. sglang serve starts the HTTP server and keeps the model resident. Both accept --config config.yaml (or JSON) for structured configuration, with CLI flags overriding file values. Non-diffusers checkpoints can resolve through a self-hosted "overlay repo" registry (SGLANG_DIFFUSION_MODEL_OVERLAY_REGISTRY, a dev/debug override) that materializes a local componentized copy under ~/.cache/sgl_diffusion/materialized_models/ on first load.

Component overrides use --<component>-path / --component-paths.<component> (replace a component's config+weights) or --<component>-weights-path / --component-weights-paths.<component> (replace only weights, keep the base config) — the component key must match model_index.json or a native pipeline's registered module name. --component-residency COMPONENT=MODE assigns resident, component-offload, or layerwise-offload per component (group selectors dit, text_encoder, image_encoder, vae, and all are also available; an exact key overrides a matching group, and a group overrides all). Legacy per-component flags (--dit-cpu-offload, --layerwise-offload-components, etc.) remain supported and compose with the new selectors. Use --backend diffusers to force vanilla diffusers pipelines when no native implementation exists or a model needs a custom pipeline class (--trust-remote-code for custom classes).

Health endpoints separate liveness from readiness: GET /liveness returns 200 as soon as the HTTP server accepts requests (even during warmup); GET /health returns 503 during server-based warmup and 200 once ready; GET /health_generate is a compatibility alias that does not itself issue a generation request. Cloud storage for generated outputs (S3-compatible, including MinIO) is configured via SGLANG_CLOUD_STORAGE_TYPE=s3 plus bucket/endpoint/credential env vars — see Environment Variables below.

OpenAI-compatible API

The HTTP server implements OpenAI-compatible image and video endpoints under /v1 (raw/github_doc-docs-docs-sglang-diffusion-api-openai-api-mdx.md):

  • Images: POST /v1/images/generations (text-to-image; quality selects a model-owned sampling level — lossless or high — see diffusion optimization — but this only takes effect when the target model advertises such a level; omitting quality, or sending OpenAI's auto, keeps the runtime default instead), POST /v1/images/edits (multipart, image + prompt), GET /v1/images/{image_id}/content (when response_format=url and no cloud storage is configured, a relative URL is returned instead of an absolute one).
  • Videos: POST /v1/videos (text-to-video, or image-to-video via multipart input_reference or a reference_url JSON field), GET /v1/videos (list/poll status), GET /v1/videos/{video_id}/content.
  • Discovery: GET /v1/models and GET /v1/models/{name} return the public model name plus diffusion runtime info (num_gpus, task_type, precision, pipeline class); GET /server_info also reports served_model_name. The resolved public name follows --served-model-name, then --model-id, then --model-path--model-id selects a registered model configuration for checkpoints whose local path can't be identified, it is not a free-form deployment alias.
  • LoRA management: POST /v1/set_lora (load/activate one or more adapters by nickname, path, target transformer, strength, and merge_mode), POST /v1/merge_lora_weights, POST /v1/unmerge_lora_weights (must precede switching to a different LoRA), GET /v1/list_loras. Regular weights statically merge by default; FSDP-sharded weights use dynamic LoRA to avoid full-gather memory peaks.
  • Output quality: output-quality (maximum/high/medium/low/default) actually defaults to the string "default", which auto-resolves to a compression value of 50 for video and 75 for image; output-compression (0–100, takes precedence when set) directly overrides the compression level. PNG ignores both. Note: the cited source itself is inconsistent about this field's own spelling — it's called output_quality (underscore) in the request-quality discussion elsewhere on the same page but output-quality/output-compression (hyphenated) in the parameter reference — verify the actual spelling against the API schema/code before relying on it.

Post-processing

Optional steps run after generation and can be combined — frame interpolation runs first (raising frame count), then upscaling runs on every frame (raising resolution) (raw/github_doc-docs-docs-sglang-diffusion-api-post-processing-mdx.md). Frame interpolation (--enable-frame-interpolation) uses only RIFE 4.22.lite (elfgum/RIFE-4.22.lite, auto-downloaded); output frame count follows (N-1) × 2^exp + 1 where exp is --frame-interpolation-exp (default 1). Upscaling (--enable-upscaling) uses Real-ESRGAN with architecture auto-detected from checkpoint keys (RRDBNet for quality, SRVGGNetCompact — the default RealESRGAN_x4.pth — for speed); --upscaling-scale (default 4) beyond the native 4× applies a bicubic resize after the network output.

Models with autoregressive or prompt-enhancement stages

GLM-Image, Qwen Image Layered, and LongCat-Image ship a bundled autoregressive (AR) stage (raw/github_doc-docs-docs-sglang-diffusion-models-with-ar-mdx.md). Qwen Image Layered and LongCat-Image run the native Qwen2.5-VL component in-process; GLM-Image can instead delegate AR inference to a separately launched SGLang server via --srt-encoder-url (with --srt-encoder-timeout / --srt-encoder-connection-timeout for long-running or flaky links) — the diffusion server sends one HTTP request per AR step, so co-locating both servers on a fast local network is recommended, and startup fails fast if the AR host is offline. ERNIE-Image similarly supports built-in prompt enhancement (native Ministral3 implementation) or an external --pe-server-url SGLang PE server; --layerwise-offload-components pe streams the in-process PE decoder's layers for memory-constrained deployments (does not apply when --pe-server-url is set). Ascend NPU deployments running both servers on the same NPU group need distinct HCCL_IF_BASE_PORT / HCCL_HOST_SOCKET_PORT_RANGE / HCCL_NPU_SOCKET_PORT_RANGE ranges per process.

Realtime and causal video models

Two execution modes generate video incrementally and reuse state across chunks, unlike offline pipelines that denoise one bounded sequence and release state at completion (raw/github_doc-docs-docs-sglang-diffusion-realtime-models-mdx.md): realtime sessions (state persists until disconnect, served over the /v1/realtime_video/generate WebSocket — e.g. LingBot World, SANA-WM realtime) and request-based causal generation (state reused across chunks within one request, then released — e.g. LongLive 2.0, batch-streaming SANA-WM). A causal DiT is not automatically a realtime session model; the pipeline must also register a realtime adapter and implement the WebSocket lifecycle. Requests can override realtime_causal_sink_size and realtime_causal_kv_cache_num_frames. For LingBot World, --kv-cache-quant {off,int4,int2} compresses completed causal KV-cache chunks (lossy, disabled by default) — see diffusion optimization for the underlying Quant-VideoGen mechanism.

Spectrum acceleration

Spectrum forecasts DiT features to skip selected denoising steps (raw/github_doc-docs-docs-sglang-diffusion-spectrum-mdx.md). It is scoped narrowly: available only on native FLUX.1, Wan, HunyuanVideo, and SD3 implementation paths (not --backend diffusers, not FLUX.2 yet), reachable only through sglang generate and Python sampling parameters (not sglang serve or the OpenAI server yet), and mutually exclusive with --enable-teacache.

sglang generate --model-path Wan-AI/Wan2.1-T2V-1.3B-Diffusers \
  --prompt "A paper boat floating through a misty mountain lake" \
  --enable-spectrum --save-output

Advanced controls (--spectrum-window-size default 2.0, --spectrum-flex-window 0.75, --spectrum-warmup-steps 5, --spectrum-m 4, --spectrum-lam 0.1, --spectrum-tau-num-steps 50, --history-size 100, --taylor-order 1, --w 1.0) trade speed against fidelity; providing any override implicitly enables Spectrum, but explicit --enable-spectrum is preferred for new commands. --debug adds shadow-prediction validation work and is not representative of normal latency.

Supported models and the compatibility matrix

Pass a Hugging Face model ID (or local directory) to --model-path for sglang generate/sglang serve, or the equivalent Python loading helper (raw/github_doc-docs-docs-sglang-diffusion-compatibility-matrix-mdx.md — verified against the main-branch docs fetched 2026-08-24, not a release-tag-pinned snapshot). Broad families include, on the image side, FLUX (1/2, including klein variants and NVFP4), Z-Image, Qwen-Image (incl. Edit/Layered), LongCat-Image, SD3/3.5, SANA, FireRed-Image, JoyAI-Image, and GLM-Image/Hunyuan3D-2/ERNIE-Image/ideogram-4 as long-tail entries; on the video side, FastWan, SANA-Video, LingBot Video MoE, Wan2.1/2.2 (and TurboWan variants), LongLive 2.0, HunyuanVideo/FastHunyuan, MOVA, MiniMax-H3, Helios, LTX-2/2.5, and Cosmos3; and for realtime/world models, LingBotWorld and SANA-WM. See supported models for SGLang's non-diffusion model coverage. The detailed video optimization matrix cross-references model against TeaCache/Sliding-Tile/Sage/VSA/SLA/SageSLA/SVG2/Laser/BSA/Rain-Fusion support (✅ full, ❌ none, ⭕ not applicable) — see diffusion optimization for what each abbreviation does. A missing checkpoint alias in the matrix does not imply the family is unsupported: the runtime registry also accepts detector-based aliases and local directories matching the same family.

Deployment and performance-mode presets

--performance-mode (manual/auto/speed/memory, alias --mode; default auto) applies safe residency/parallelism presets without overriding explicit flags (raw/github_doc-docs-docs-sglang-diffusion-deployment-cookbook-mdx.md). auto checks the least-available GPU memory across selected devices: for image workloads with ≥45 GiB per GPU it keeps the DiT resident and layerwise-offloads large auxiliary encoders, below that it keeps the DiT offloaded; it may also enable FSDP+CFG parallelism on validated multi-GPU deployments, and CFG parallelism when the model defaults to CFG and no explicit parallelism policy is set. speed favors GPU-resident execution and disables CPU offload by default (may OOM); manual keeps every performance flag under explicit control. The quick decision rule: resident + no FSDP for the fastest single-GPU run that fits; component-then-layerwise offload for lower single-GPU memory; FSDP + CFG parallelism + resident sharded components for faster multi-GPU Qwen/Wan CFG generation; SP/Ulysses/Ring for sequence-length scaling; explicit TP for compatibility rather than as a default latency lever. --served-model-name decouples the public API identity from the checkpoint mount path (useful across replicas or hosts) — see the OpenAI API section above for resolution order. Startup and readiness probes should point at /health with a large failure budget (model loading and compilation can legitimately take minutes); liveness probes should use /liveness instead. Benchmark takeaways from the docs: Z-Image and Qwen-Image were faster single-GPU/no-FSDP than FSDP/SP in tested settings; Wan benefited from FSDP replacing DiT offload on validated multi-GPU workloads; component offload mainly helped memory, not latency. Always re-benchmark on the target resolution, frame count, step count, and GPU. See diffusion optimization for the parallelism, caching, and kernel mechanics these modes are built on.

Supporting new models

Adding a model is a triage flow, not a fixed template (raw/github_doc-docs-docs-sglang-diffusion-support-new-models-mdx.md). Read the request path in dependency order: registry.pyconfigs/pipeline_configs/{model}.pyruntime/pipelines/{model}.pyruntime/pipelines_core/stages/runtime/models/ (only when the architecture can't be reused). Decide the smallest applicable change: a new checkpoint of an existing family needs only a registry entry and maybe a SamplingParams/PipelineConfig variant; a new native architecture needs a native pipeline and missing components; a long-tail model can start on the diffusers backend for compatibility first. Prefer native stages directly, then subclassing the narrowest native stage, then a custom single-purpose stage, and only as a last resort an aggregated BeforeDenoisingStage (hides multiple responsibilities and bypasses shared offload/profiling/disaggregation/batching hooks). Out-of-tree models can register without touching SGLang's source: call ModelRegistry.register_model and register_pipeline in an installed package's __init__.py, then set SGLANG_EXTERNAL_MODEL_PACKAGE=<package> before launching (each process imports it once). Complete native support (not just single-GPU parity) also requires encoder/DiT TP+SP, ParallelTiledVAE-based parallel decode, and LayerwiseOffloadableModuleMixin layer declarations — see diffusion optimization for what these integrate with.

Environment variables

Runtime configuration is largely environment-driven (raw/github_doc-docs-docs-sglang-diffusion-environment-variables-mdx.md): SGLANG_DIFFUSION_TARGET_DEVICE (default cuda; also rocm/xpu/npu/musa/mps/cpu), SGLANG_DIFFUSION_ATTENTION_BACKEND and SGLANG_DIFFUSION_ATTENTION_CONFIG (two separate env-level attention overrides — the former overrides the backend selection, the latter points at a JSON/YAML backend-config file — see diffusion optimization), SGLANG_DIFFUSION_STAGE_LOGGING, SGLANG_DIFFUSION_TORCH_PROFILER_DIR, SGLANG_DIFFUSION_CACHE_ROOT (default ~/.cache/sgl_diffusion), SGLANG_DIFFUSION_CONFIG_ROOT, SGLANG_DIFFUSION_WORKER_MULTIPROC_METHOD (fork/spawn), SGLANG_DIFFUSION_IPC_A2A (CUDA-IPC all-to-all for eligible 2-GPU Ulysses groups, default on; 0 forces NCCL) with _TIMEOUT_MS (default 10000) and _MAX_BUFFERS (default 16), and SGLANG_USE_RUNAI_MODEL_STREAMER. Platform-specific variables cover Apple MPS (SGLANG_USE_MLX, SRT-only, no diffusion effect), ROCm (SGLANG_USE_ROCM_VAE, SGLANG_USE_ROCM_CUDNN_BENCHMARK), and quantization (SGLANG_DIFFUSION_FLASHINFER_FP4_GEMM_BACKEND, SGLANG_DIFFUSION_ENABLE_W8A8_FP8_GEMM). Cloud storage uses SGLANG_CLOUD_STORAGE_TYPE=s3 plus SGLANG_S3_BUCKET_NAME/_ENDPOINT_URL/_REGION_NAME/_ACCESS_KEY_ID/_SECRET_ACCESS_KEY. CUDA crash debugging exposes SGLANG_KERNEL_API_LOGLEVEL (0/1/3/5/10), _LOGDEST, _DUMP_DIR, _DUMP_INCLUDE/_EXCLUDE. The SGLANG_CACHE_DIT_* family (enable flag, Fn/Bn/warmup/RDT/max-continuous, TaylorSeer, SCM preset/policy/bins, plus _SECONDARY_* variants for dual-transformer models) configures Cache-DiT server-wide defaults — see diffusion optimization for the caching mechanics they control.

Key Parameters

  • --model-path / --served-model-name / --model-id / --model-variant — checkpoint location, public API name, registry hint, and semantic weight-partition selector, respectively.
  • --component-paths.<component> / --<component>-path, --component-weights-paths.<component> / --<component>-weights-path — replace a component's config+weights or weights-only.
  • --component-residency COMPONENT=MODE (resident/component-offload/layerwise-offload), plus legacy --dit-cpu-offload, --layerwise-offload-components, --dit-offload-prefetch-size, --dit-layerwise-resident-layers, --dit-layerwise-residency-policy.
  • --performance-mode / --mode (manual/auto/speed/memory).
  • --num-gpus, --tp-size, --sp-degree, --ulysses-degree, --ring-degree, --dp-size (see diffusion optimization for the parallelism math).
  • --quality {lossless,high} — request-level exactness vs. validated-accelerated tradeoff (fused kernels, described in diffusion optimization).
  • --prompt, --negative-prompt, --image-path, --num-inference-steps, --seed, --num-outputs-per-prompt, --height/--width/--num-frames/--fps, --output-path/--output-file-name/--save-output.
  • --enable-frame-interpolation / --frame-interpolation-exp / --frame-interpolation-scale / --frame-interpolation-model-path; --enable-upscaling / --upscaling-scale / --upscaling-model-path.
  • --srt-encoder-url / --srt-encoder-timeout / --srt-encoder-connection-timeout (AR delegation); --pe-server-url (prompt enhancement delegation).
  • --enable-spectrum and the --spectrum-* tuning flags.
  • --log-requests, --log-requests-level {0-3}, --log-requests-format {text,json}, --log-requests-target.
  • SGLANG_DIFFUSION_MODEL_OVERLAY_REGISTRY, SGLANG_EXTERNAL_MODEL_PACKAGE, SGLANG_CLOUD_STORAGE_TYPE + SGLANG_S3_*.

When To Use

Use SGLang Diffusion when you need to self-host image/video generation behind an OpenAI-compatible Images API and a documented subset of the OpenAI Videos API (not a blanket drop-in guarantee for video clients), with the same operational shape as SGLang's LLM serving (see sglang overview). Reach for sglang generate for one-off/batch jobs and scripted benchmarking, and sglang serve for a persistent multi-request server. Use the AR/PE delegation paths (--srt-encoder-url, --pe-server-url) when GLM-Image's or ERNIE-Image's auxiliary stage needs independent scaling or resources. Use realtime sessions for interactive world models needing live control (camera actions, prompt updates) and request-based causal generation for long videos that stream chunk-by-chunk without needing a persistent WebSocket. Use Spectrum only after validating quality/latency on the exact model, shape, hardware, and sampling settings you plan to deploy — it is explicitly approximate.

Risks & Pitfalls

  • The quality field in a video response body is unrelated to the quality sampling parameter — it is fixed Sora-compatible metadata always reported as "standard", not a reflection of what actually ran.
  • /health is not a liveness probe: a long server-based warmup can legitimately hold it at 503 for minutes; use /liveness for that purpose instead.
  • --model-id is not a free-form deployment alias — it selects a registry configuration; use --served-model-name to control the name clients see.
  • The AR delegation path (GLM-Image --srt-encoder-url) issues one HTTP request per AR step; cross-region encoder/diffusion placement measurably degrades latency, and the diffusion server refuses to start if the AR host is unreachable at startup.
  • Ascend NPU deployments running two servers (AR + diffusion) on one NPU group must assign non-overlapping HCCL_* port ranges or the servers will conflict.
  • Spectrum is approximate, scoped to only FLUX.1/Wan/HunyuanVideo/SD3 native paths, unavailable via sglang serve/the OpenAI server, and mutually exclusive with TeaCache — do not assume it composes with an arbitrary deployment.
  • SGLANG_DIFFUSION_MODEL_OVERLAY_REGISTRY is documented as a dev/debug override, not a production configuration mechanism.
  • SGLANG_EXTERNAL_MODEL_PACKAGE must be set before process startup; each process imports the package exactly once, and overwrite=True is required to intentionally replace a built-in pipeline.
  • A missing model ID in the compatibility matrix does not mean unsupported — check for detector-based aliases or a local directory match before concluding a family is unavailable.
  • HunyuanVideo/FastHunyuan default to tiled VAE decode for a reason: overriding --vae-config.parallel-decode-mode to spatial/spatial_shard at their documented shapes can request hundreds of GiB per rank for the causal mask — only use those modes at smaller validated shapes.

Related Concepts

  • diffusion optimization — the performance/acceleration stack (caching, attention backends, kernels, parallelism, disaggregation, batching, quantization, profiling) that the deployment and performance-mode levers described here build on.
  • sglang overview — the LLM-serving core SGLang Diffusion ships alongside.
  • supported models — SGLang's non-diffusion (LLM/multimodal/embedding/reward) model coverage.
  • quantization — SGLang's LLM-side quantization support, contrasted with diffusion checkpoint quantization in diffusion optimization.
  • parallelism and disaggregation — SGLang's LLM-side TP/PP/EP/DP and PD disaggregation, contrasted with the diffusion-specific CFG/TP/SP parallelism and encoder/denoiser/decoder disaggregation in diffusion optimization.
  • server apis — the LLM-serving OpenAI/native/Anthropic/Ollama API surface, alongside which the diffusion /v1/images and /v1/videos endpoints are served.

Sources

  • raw/github_doc-docs-docs-sglang-diffusion-index-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-installation-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-api-cli-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-api-openai-api-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-api-post-processing-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-models-with-ar-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-models-with-pe-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-realtime-models-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-spectrum-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-compatibility-matrix-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-deployment-cookbook-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-support-new-models-mdx.md
  • raw/github_doc-docs-docs-sglang-diffusion-environment-variables-mdx.md