diff --git a/docs/cookbook/diffusion/Qwen-Image/Qwen-Image-2.1.mdx b/docs/cookbook/diffusion/Qwen-Image/Qwen-Image-2.1.mdx index 302e853717..6f2b51b33a 100644 --- a/docs/cookbook/diffusion/Qwen-Image/Qwen-Image-2.1.mdx +++ b/docs/cookbook/diffusion/Qwen-Image/Qwen-Image-2.1.mdx @@ -1,6 +1,6 @@ --- -title: Qwen-Image 2.1 -description: "Run Qwen-Image 2.1 text-to-image and image-conditioned generation with SGLang Diffusion." +title: Qwen-Image 2.1 and Turbo +description: "Run Qwen-Image 2.1 and its eight-step Turbo variant with SGLang Diffusion: text-to-image, reference-image editing, and transparent RGBA output." tag: NEW --- @@ -14,9 +14,10 @@ import { config } from '/src/snippets/configs/Qwen/qwen-image-2.1.jsx'; Use a nightly Docker image containing this integration, or install from source, following the [SGLang Diffusion installation guide](/docs/sglang-diffusion/installation). -Run the commands below inside that environment. The picker uses `Qwen/Qwen-Image-2.1`; -you can also set a local checkpoint directory under **Variables**. The recipes -target NVIDIA CUDA on Linux; the picker marks which single-GPU workloads have +Run the commands below inside that environment. Select **Checkpoint weights** for +`Qwen/Qwen-Image-2.1` or `Qwen/Qwen-Image-2.1-Turbo`; you can also set a local +checkpoint directory under **Variables**. The recipes +target NVIDIA CUDA on Linux; the picker marks which workloads have been verified with the full checkpoint. @@ -63,7 +64,7 @@ batch size one. Explicit placement and attention overrides preserve each recipe. | RTX 4090 24GB | DiT and VAE resident, encoder layerwise offload / FlashAttention | 18.68 s | 21.68 s | 22.7 GiB | | DGX Spark 128GB unified | Resident / Torch SDPA | 35.36 s | 42.23 s | — (unified) | -Measured on 2026-09-20 at 1024×1024, 40 steps, CFG 1, and one RGBA PNG per +Measured with the original checkpoint on 2026-09-20 at 1024×1024, 40 steps, CFG 1, and one RGBA PNG per request. Times are median HTTP latency after warmup, including PNG serialization and excluding startup; VRAM is the sampled request-phase peak. Prompts and software versions affect both latency and memory use. @@ -111,10 +112,43 @@ output pixels, even with the same seed. See [Inference batching](/docs/sglang-diffusion/dynamic_batching) for admission rules and metrics. +### Turbo sampling + +Select **Qwen-Image 2.1 Turbo** under **Setup → Checkpoint weights** and restart +with the generated Server command. Requests use the checkpoint's preset eight-step +sigma grid automatically; omit `num_inference_steps`. Setting that field to 8 +on the original checkpoint does not reproduce Turbo. + +For offline generation: + +```bash Command +sglang generate --model-path Qwen/Qwen-Image-2.1-Turbo \ + --prompt "A ceramic teapot on a wooden table in soft morning light" +``` + +The preset comes from `sample_sigmas` in `model_index.json`, with scheduler +settings loaded from `scheduler/scheduler_config.json`. Preserve both when copying +or exporting the checkpoint. Changing only `num_inference_steps` does not override +the preset. Custom grids are experimental and can reduce quality; the picker keeps +Turbo on its published schedule. The default CFG scale is 1 for both checkpoints. +See the [Turbo model card](https://huggingface.co/Qwen/Qwen-Image-2.1-Turbo). + +Turbo was verified at 1024×1024 with native precision and eager execution on +H200: single-GPU generation, editing, transparent generation and editing, +multi-image editing, and two outputs; TP×2 generation and editing also passed. +The picker distinguishes these from untested Turbo hardware and combinations. + +The hardware latency table above describes the original 40-step checkpoint. +Turbo has the same architecture and weight-memory footprint; fewer denoising steps +do not eliminate encoder or VAE costs. Existing quantization and acceleration +results for the base checkpoint do not establish Turbo quality or speed. + ## 2. Model capabilities Qwen-Image 2.1 supports text-to-image generation, single- and multi-image editing, -and RGBA output. Use one checkpoint for all modes. +and RGBA output. Both the original and eight-step Turbo checkpoints support all +these modes. Choose Turbo for shorter denoising runs; compare the two checkpoints +on your prompts when fidelity is the priority. For multi-round editing, send the previous output as the next reference image. The server does not retain conversation state. Repeated prompts and reference @@ -149,7 +183,8 @@ including partly transparent edges, without thresholding or background removal. ## 4. Offline requests -Defaults are 1024×1024, 40 steps, CFG 1, and seed 42; output saving is enabled. +Defaults are 1024×1024, CFG 1, and seed 42; output saving is enabled. +The original checkpoint defaults to 40 steps; Turbo uses its preset 8-step grid. For GPUs that need offload, also pass the placement flags from the picker. ### Text-to-image @@ -177,7 +212,8 @@ noise seeds and independent prefix caches. ## 5. Runtime features -The default is 40 Euler flow-matching steps with CFG disabled. For CFG, provide +Both checkpoints use Euler flow matching with CFG disabled by default: 40 steps +for the original, or the preset 8-step grid for Turbo. For CFG, provide `--negative-prompt` and `--guidance-scale` greater than one. The API requires a text prompt; precomputed embeddings alone are insufficient. @@ -204,6 +240,8 @@ for shared runtime options. The picker marks exact tested HTTP deployments, not every combination of the features below. Functional checks do not establish quality for lossy settings. +The feature checks below cover the original checkpoint; see +[Turbo sampling](#turbo-sampling) for the Turbo validation scope. - **Parallelism and offload:** H200/B200 full-checkpoint checks cover TP, Ulysses, Ring with FlashAttention, CFG parallelism, encoder folding, and diff --git a/docs/src/snippets/_deployment.jsx b/docs/src/snippets/_deployment.jsx index a495f609b9..efa03268bf 100644 --- a/docs/src/snippets/_deployment.jsx +++ b/docs/src/snippets/_deployment.jsx @@ -41,7 +41,9 @@ // command rendering. Shape: // {defaultSelection, resource: {limits, verifiedRecipes, // autoTopology(sel), validateTopology(sel)}, -// resolveDeployment(sel)}. The resolver returns a cell plus +// resolveDeployment(sel)}. Recipes may use verifiedWhen(sel) +// to qualify the recommendation badge by checkpoint or mode. +// The resolver returns a cell plus // `builder` metadata (topologySummary, errors, warnings, // verification, resolvedSettings). UI-only scope/expand and // local head-address/rank state never enter the URL hash. @@ -1881,6 +1883,8 @@ export const Deployment = ({ config, benchmarks, agenticLink }) => { || sel[dim.id] || "—"; const recommendedRecipe = recommendedBuilderRecipe(sel.hw); + const recommendedIsVerified = !!recommendedRecipe && !recommendedRecipe.unverified + && (typeof recommendedRecipe.verifiedWhen !== "function" || recommendedRecipe.verifiedWhen(sel)); const recommendedInUse = !!recommendedRecipe && Number(sel.nodes) === recommendedRecipe.nodes && Number(sel.gpus_per_node) === recommendedRecipe.gpus_per_node @@ -2004,7 +2008,7 @@ export const Deployment = ({ config, benchmarks, agenticLink }) => { {/* This is the verified operating point, not sizing advice — a hardware whose validation ran on 8 GPUs is not "recommending" 8 over a smaller deployment. */} - {recommendedRecipe.unverified ? "Derived recipe" : "Verified recipe"} · {sel.hw.toUpperCase()} + {recommendedIsVerified ? "Verified recipe" : "Derived recipe"} · {sel.hw.toUpperCase()} {[ `${recommendedRecipe.nodes * recommendedRecipe.gpus_per_node} GPUs`, @@ -2016,10 +2020,10 @@ export const Deployment = ({ config, benchmarks, agenticLink }) => {
- {renderStatus(recommendedRecipe.unverified ? "unverified" : "verified")} + {renderStatus(recommendedIsVerified ? "verified" : "unverified")} {recommendedInUse ? In use - : } + : }
)} diff --git a/docs/src/snippets/configs/Qwen/qwen-image-2.1.jsx b/docs/src/snippets/configs/Qwen/qwen-image-2.1.jsx index 45c5729061..631c7c5bb8 100644 --- a/docs/src/snippets/configs/Qwen/qwen-image-2.1.jsx +++ b/docs/src/snippets/configs/Qwen/qwen-image-2.1.jsx @@ -4,7 +4,7 @@ const platformAttention = (s) => sm12Hardware.includes(s.hw) ? "sdpa" : "fa"; const effectiveAttention = (s) => s.attention === "platform" || (sm12Hardware.includes(s.hw) && s.attention === "fa") ? platformAttention(s) : s.attention; const config = { - modelName: "Qwen-Image 2.1", + modelName: "Qwen-Image 2.1 / Turbo", supportedHardware: ["h200", "b200", "rtxpro6000", "rtx5090", "rtx4090", "dgx-spark"], hardware: [ { id: "rtxpro6000", label: "RTX PRO 6000", vram: "96GB", vendor: "consumer" }, @@ -19,9 +19,12 @@ const config = { id: "weights", title: "Checkpoint weights", scope: "base", - description: "One checkpoint serves generation and editing. Set its Hugging Face repository or local path under Variables.", + description: "Both checkpoints serve generation and editing. Turbo uses its preset eight-step sigma grid. Restart the server when changing weights.", default: "default", - options: [{ id: "default", label: "Qwen-Image 2.1", flags: [] }], + options: [ + { id: "default", label: "Qwen-Image 2.1", description: "Original checkpoint with a configurable step count; defaults to 40 steps." }, + { id: "turbo", label: "Qwen-Image 2.1 Turbo", description: "Accelerated checkpoint with a fixed eight-step schedule, loaded automatically from model_index.json." }, + ], }, { id: "mode", @@ -263,9 +266,19 @@ const config = { id: "steps", title: "Denoising steps", scope: "request", + showWhen: (s) => s.weights !== "turbo", description: "40 is the checkpoint default. Fewer steps trade detail for latency.", kind: "number", min: 1, max: 100, unit: "steps", default: 40, options: [], }, + { + id: "schedule", + title: "Denoising schedule", + scope: "request", + showWhen: (s) => s.weights === "turbo", + description: "Turbo loads the checkpoint's eight-step sigma grid. A step-count override does not replace this grid.", + default: "preset", + options: [{ id: "preset", label: "8 steps (checkpoint preset)", recommended: true }], + }, { id: "outputs", title: "Outputs", @@ -284,6 +297,7 @@ const config = { limits: { nodes: { min: 1, max: 1 }, gpus_per_node: { min: 1, max: 4 } }, verifiedRecipes: [ { id: "h200-1-resident", hw: "h200", nodes: 1, gpus_per_node: 1, placement: "resident", tp_size: 1, ulysses_degree: 1, ring_degree: 1, encoder: "auto", attentions: ["fa", "sdpa"], batchSizes: [1, 2, 4], batchAttentions: ["fa"], default: true }, + { id: "h200-2-tp-turbo", weights: "turbo", hw: "h200", nodes: 1, gpus_per_node: 2, placement: "resident", tp_size: 2, ulysses_degree: 1, ring_degree: 1, encoder: "auto", attentions: ["fa"], verifiedWhen: (s) => s.weights === "turbo" && (!s.batching || s.batching === "off") }, { id: "b200-1-resident", hw: "b200", nodes: 1, gpus_per_node: 1, placement: "resident", tp_size: 1, ulysses_degree: 1, ring_degree: 1, encoder: "auto", attentions: ["fa", "sdpa"], batchSizes: [1, 2, 4], batchAttentions: ["fa"], default: true }, { id: "b200-2-tp", hw: "b200", nodes: 1, gpus_per_node: 2, placement: "resident", tp_size: 2, ulysses_degree: 1, ring_degree: 1, encoder: "auto", attentions: ["fa"], batchSizes: [1, 2] }, { id: "b200-2-ulysses", hw: "b200", nodes: 1, gpus_per_node: 2, placement: "resident", tp_size: 1, ulysses_degree: 2, ring_degree: 1, encoder: "auto", attentions: ["fa"], batchSizes: [1, 2] }, @@ -293,7 +307,9 @@ const config = { { id: "rtx4090-1-offload", hw: "rtx4090", nodes: 1, gpus_per_node: 1, placement: "offload", tp_size: 1, ulysses_degree: 1, ring_degree: 1, encoder: "auto", attentions: ["fa", "sdpa"], batchSizes: [1, 2], batchAttentions: ["fa"], default: true }, { id: "dgx-spark-1-resident", hw: "dgx-spark", nodes: 1, gpus_per_node: 1, placement: "resident", tp_size: 1, ulysses_degree: 1, ring_degree: 1, encoder: "auto", attentions: ["sdpa"], batchSizes: [1], default: true }, ], - autoTopology: (s) => ({ tp_size: 1, ulysses_degree: Number(s.gpus_per_node), ring_degree: 1 }), + autoTopology: (s) => s.weights === "turbo" && s.hw === "h200" && Number(s.gpus_per_node) === 2 + ? { tp_size: 2, ulysses_degree: 1, ring_degree: 1 } + : { tp_size: 1, ulysses_degree: Number(s.gpus_per_node), ring_degree: 1 }, validateTopology: (s, topology) => { const errors = []; const nodes = Number(s.nodes); @@ -322,20 +338,27 @@ const config = { && entry.placement === s.placement && entry.tp_size === topology.tp_size && entry.ulysses_degree === topology.ulysses_degree && entry.ring_degree === topology.ring_degree); const serveVerified = !!recipe && !recipe.unverified && errors.length === 0 && s.encoder === "auto" - && recipe.attentions.includes(effectiveAttention(s)) && s.precision === "native" + && recipe.verifiedWhen(s) && recipe.attentions.includes(effectiveAttention(s)) && s.precision === "native" && s.execution === "eager" && s.vae === "full" && (!s.batching || s.batching === "off" || ((recipe.batchSizes || [1]).includes(Number(s.batching)) && (recipe.batchAttentions || recipe.attentions).includes(effectiveAttention(s)))); // Exact HTTP workloads from the validation matrix, not blanket quality coverage. - const requestVerified = serveVerified - && ((recipe.batchSizes && (Number(s.outputs) === 1 || (recipe.batchAttentions || recipe.attentions).includes(effectiveAttention(s))) + const turboRequestVerified = s.resolution === "1024" + && (Number(s.gpus_per_node) === 1 + ? ((Number(s.outputs) === 1 && (s.mode === "text" + || (s.mode === "edit" && (s.background === "scene" || effectiveAttention(s) === "fa")) + || (s.mode === "multi" && s.background === "scene" && effectiveAttention(s) === "fa"))) + || (Number(s.outputs) === 2 && s.mode === "text" && s.background === "scene" && effectiveAttention(s) === "fa")) + : Number(s.outputs) === 1 && ["text", "edit"].includes(s.mode) && s.background === "scene"); + const requestVerified = serveVerified && (s.weights === "turbo" ? turboRequestVerified + : ((recipe.batchSizes && (Number(s.outputs) === 1 || (recipe.batchAttentions || recipe.attentions).includes(effectiveAttention(s))) && ["text", "edit"].includes(s.mode) && s.resolution === "1024" && Number(s.steps) === 40 && recipe.batchSizes.includes(Number(s.outputs))) || (["text", "edit"].includes(s.mode) && s.resolution === "1024" && Number(s.steps) === 40 && Number(s.outputs) === 1 && (["h200", "rtxpro6000"].includes(s.hw) || s.mode === "text" || s.background === "scene")) || (s.hw === "h200" && s.background === "scene" && s.mode === "text" && s.resolution === "512" && Number(s.steps) === 4 && Number(s.outputs) === 2) - || (s.hw === "h200" && s.background === "scene" && s.mode === "multi" && s.resolution === "512" && Number(s.steps) === 4 && Number(s.outputs) === 1)); + || (s.hw === "h200" && s.background === "scene" && s.mode === "multi" && s.resolution === "512" && Number(s.steps) === 4 && Number(s.outputs) === 1))); const world = Number(s.nodes) * Number(s.gpus_per_node); - const flags = ['--model-path "{{MODEL_PATH}}"']; + const flags = [s.weights === "turbo" ? '--model-path "{{TURBO_MODEL_PATH}}"' : '--model-path "{{MODEL_PATH}}"']; if (world > 1) flags.push(`--num-gpus ${world}`); if (topology.tp_size > 1) flags.push(`--tp-size ${topology.tp_size}`); if (world > 1) flags.push(`--ulysses-degree ${topology.ulysses_degree}`); @@ -370,7 +393,8 @@ const config = { modelNames: { default: "Qwen-Image-2.1" }, placeholders: { - MODEL_PATH: { target: "command", label: "Checkpoint repository or directory", default: "Qwen/Qwen-Image-2.1" }, + MODEL_PATH: { target: "command", label: "Base checkpoint repository or directory", default: "Qwen/Qwen-Image-2.1" }, + TURBO_MODEL_PATH: { target: "command", label: "Turbo checkpoint repository or directory", default: "Qwen/Qwen-Image-2.1-Turbo" }, FP8_DIT_PATH: { target: "command", label: "Serialized FP8 DiT directory", default: "/models/qwen-image-2.1-fp8/transformer" }, FP8_ENCODER_PATH: { target: "command", label: "Serialized FP8 encoder directory", default: "/models/qwen-image-2.1-fp8/text_encoder" }, GGUF_DIT_PATH: { target: "command", label: "GGUF DiT file", default: "/models/qwen-image-2.1-gguf/transformer-Q4_0.gguf" }, @@ -403,7 +427,7 @@ const config = { }; if (Number(s.outputs) !== 1) request.n = Number(s.outputs); if (s.resolution !== "1024") request.size = `${s.resolution}x${s.resolution}`; - if (Number(s.steps) !== 40) request.num_inference_steps = Number(s.steps); + if (s.weights !== "turbo" && Number(s.steps) !== 40) request.num_inference_steps = Number(s.steps); if (transparent) request.background = "transparent"; if (s.mode === "text") { return `curl -sS --fail-with-body http://{{CURL_HOST}}:{{CURL_PORT}}/v1/images/generations \\ @@ -422,5 +446,11 @@ ${fields.join(" \\\n")}`; cells: [], }; +// Turbo verification is limited to the H200 runs; other base recipes remain unchanged. +for (const recipe of config.commandBuilder.resource.verifiedRecipes) { + recipe.verifiedWhen ??= (s) => s.weights !== "turbo" + || (recipe.id === "h200-1-resident" && (!s.batching || s.batching === "off")); +} + return config; })(); diff --git a/docs/src/snippets/diffusion/model-catalog.jsx b/docs/src/snippets/diffusion/model-catalog.jsx index c35d0ee06b..3cc3cb8f78 100644 --- a/docs/src/snippets/diffusion/model-catalog.jsx +++ b/docs/src/snippets/diffusion/model-catalog.jsx @@ -36,8 +36,8 @@ export const DiffusionModelCatalog = ({ category }) => { cookbook: "/cookbook/diffusion/Qwen-Image/Qwen-Image", }, { - name: "Qwen-Image 2.1", - modelIds: ["Qwen/Qwen-Image-2.1"], + name: "Qwen-Image 2.1 / Turbo", + modelIds: ["Qwen/Qwen-Image-2.1", "Qwen/Qwen-Image-2.1-Turbo"], cookbook: "/cookbook/diffusion/Qwen-Image/Qwen-Image-2.1", }, { diff --git a/python/sglang/multimodal_gen/README.md b/python/sglang/multimodal_gen/README.md index d4f8aac550..6c74f3f004 100644 --- a/python/sglang/multimodal_gen/README.md +++ b/python/sglang/multimodal_gen/README.md @@ -9,7 +9,7 @@ SGLang diffusion features an end-to-end unified pipeline for accelerating diffus ## Key Features SGLang Diffusion has the following features: - - Broad model support: Wan, FastWan, FLUX, Qwen-Image / Qwen-Image 2.1, LongCat-Image, Z-Image, Anima, Ideogram 4, Krea-2, Cosmos3, LTX-2/LTX-2.3/LTX-2.5, MiniMax-H3, FastH3, VDN-H3, LingBot Video MoE, LingBot World, SANA-Video/SANA-WM, JoyEcho, MOVA, GLM-Image, ERNIE-Image, Hunyuan3D, and more + - Broad model support: Wan, FastWan, FLUX, Qwen-Image / Qwen-Image 2.1 / Qwen-Image 2.1 Turbo, LongCat-Image, Z-Image, Anima, Ideogram 4, Krea-2, Cosmos3, LTX-2/LTX-2.3/LTX-2.5, MiniMax-H3, FastH3, VDN-H3, LingBot Video MoE, LingBot World, SANA-Video/SANA-WM, JoyEcho, MOVA, GLM-Image, ERNIE-Image, Hunyuan3D, and more - Fast inference speed: empowered by optimized `sgl-kernel` kernels, scheduler/runtime improvements, caching acceleration, and native diffusion hot-path optimizations - Ease of use: OpenAI-compatible api, CLI, python sdk, and a [ComfyUI plugin](apps/ComfyUI_SGLDiffusion/README.md) - [Conditioning reuse](https://docs.sglang.io/docs/sglang-diffusion/caching-acceleration#conditioning-cache): bounded CPU caching for repeated text/image inputs and VAE posteriors, with `--disable-conditioning-cache` to opt out diff --git a/python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image21.py b/python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image21.py index 9ed66e033b..ec2e18b14d 100644 --- a/python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image21.py +++ b/python/sglang/multimodal_gen/configs/pipeline_configs/qwen_image21.py @@ -33,6 +33,7 @@ class QwenImage21PipelineConfig(ImagePipelineConfig): vae_config: QwenImage21VAEConfig = field(default_factory=QwenImage21VAEConfig) text_encoder_configs: tuple = field(default_factory=lambda: (Qwen3VLConfig(),)) text_encoder_precisions: tuple[str, ...] = ("bf16",) + sample_sigmas: list[float] | None = None def validate_server_args(self, server_args: Any) -> None: super().validate_server_args(server_args) @@ -48,7 +49,9 @@ class QwenImage21PipelineConfig(ImagePipelineConfig): return True def prepare_sigmas(self, sigmas, num_inference_steps): - return self._prepare_sigmas(sigmas, num_inference_steps) + if sigmas is None: + sigmas = self.sample_sigmas + return list(self._prepare_sigmas(sigmas, num_inference_steps)) def get_classifier_free_guidance_scale(self, batch, guidance_scale): return ( @@ -113,6 +116,6 @@ def register(): register_configs( sampling_param_cls=QwenImage21SamplingParams, pipeline_config_cls=QwenImage21PipelineConfig, - hf_model_paths=["Qwen/Qwen-Image-2.1"], + hf_model_paths=["Qwen/Qwen-Image-2.1", "Qwen/Qwen-Image-2.1-Turbo"], model_detectors=[lambda hf_id: "qwen-image-2.1" in hf_id.lower()], ) diff --git a/python/sglang/multimodal_gen/runtime/pipelines/qwen_image21.py b/python/sglang/multimodal_gen/runtime/pipelines/qwen_image21.py index d69904e6c3..c74eee4139 100644 --- a/python/sglang/multimodal_gen/runtime/pipelines/qwen_image21.py +++ b/python/sglang/multimodal_gen/runtime/pipelines/qwen_image21.py @@ -22,6 +22,13 @@ class QwenImage21Pipeline(LoRAPipeline, ComposedPipelineBase): "scheduler", ] + def _load_config(self): + model_index = super()._load_config() + sample_sigmas = model_index.pop("sample_sigmas", None) + if self.server_args.pipeline_config.sample_sigmas is None: + self.server_args.pipeline_config.sample_sigmas = sample_sigmas + return model_index + def create_pipeline_stages(self, server_args): self.add_stage(QwenImage21InputValidationStage()) self.add_stage_factory( diff --git a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/qwen_image21.py b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/qwen_image21.py index db178e10fe..c491469f13 100644 --- a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/qwen_image21.py +++ b/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/qwen_image21.py @@ -55,6 +55,13 @@ class QwenImage21InputValidationStage(InputValidationStage): batch = super().forward(batch, server_args) if batch.height % 32 or batch.width % 32: raise ValueError("Qwen-Image 2.1 height and width must be divisible by 32") + batch.sigmas = server_args.pipeline_config.prepare_sigmas( + batch.sigmas, batch.num_inference_steps + ) + if not batch.sigmas: + raise ValueError("Qwen-Image 2.1 requires a non-empty sigma schedule") + # the preset or explicit sigma grid determines the denoising step count + batch.num_inference_steps = len(batch.sigmas) return batch diff --git a/python/sglang/multimodal_gen/test/unit/test_qwen_image21.py b/python/sglang/multimodal_gen/test/unit/test_qwen_image21.py index 435ecba0e0..df61bbbf82 100644 --- a/python/sglang/multimodal_gen/test/unit/test_qwen_image21.py +++ b/python/sglang/multimodal_gen/test/unit/test_qwen_image21.py @@ -6,8 +6,10 @@ from contextlib import nullcontext from types import SimpleNamespace from unittest.mock import Mock +import numpy as np import pytest import torch +from diffusers import FlowMatchEulerDiscreteScheduler as ReferenceFlowMatchScheduler from diffusers.image_processor import VaeImageProcessor from PIL import Image from transformers import BatchFeature @@ -45,6 +47,9 @@ from sglang.multimodal_gen.runtime.models.encoders.base import ( from sglang.multimodal_gen.runtime.models.encoders.qwen3vl_vision import ( Qwen3VLVisionRotaryEmbedding, ) +from sglang.multimodal_gen.runtime.models.schedulers.scheduling_flow_match_euler_discrete import ( + FlowMatchEulerDiscreteScheduler, +) from sglang.multimodal_gen.runtime.models.vaes.autoencoder_kl_qwenimage21 import ( AutoencoderKLQwenImage21, QwenImage21RMS_norm, @@ -52,6 +57,7 @@ from sglang.multimodal_gen.runtime.models.vaes.autoencoder_kl_qwenimage21 import _patchify, _unpatchify, ) +from sglang.multimodal_gen.runtime.pipelines.qwen_image21 import QwenImage21Pipeline from sglang.multimodal_gen.runtime.pipelines_core.schedule_batch import OutputBatch, Req from sglang.multimodal_gen.runtime.pipelines_core.stages.decoding import DecodingStage from sglang.multimodal_gen.runtime.pipelines_core.stages.input_validation import ( @@ -61,11 +67,99 @@ from sglang.multimodal_gen.runtime.pipelines_core.stages.model_specific_stages.q QwenImage21EncodingStage, QwenImage21InputValidationStage, collapse_image_slots, + prepare_qwen21_mu, +) +from sglang.multimodal_gen.runtime.pipelines_core.stages.timestep_preparation import ( + TimestepPreparationStage, ) from sglang.multimodal_gen.runtime.platforms.rocm import RocmPlatform from sglang.multimodal_gen.runtime.server_args import ServerArgs +@pytest.mark.parametrize("steps", [2, 40]) +@pytest.mark.parametrize("preset", [False, True]) +@pytest.mark.parametrize("explicit_sigmas", [None, [1.0, 0.7, 0.3]]) +@pytest.mark.parametrize("dynamic_shifting", [False, True]) +def test_checkpoint_sigma_grid_reaches_request_scheduler( + tmp_path, monkeypatch, steps, preset, explicit_sigmas, dynamic_shifting +): + sample_sigmas = [ + 1.0, + 0.978453, + 0.95418, + 0.926626, + 0.89508, + 0.845148, + 0.704534, + 0.414568, + ] + model_index = { + "_class_name": "QwenImage21Pipeline", + "_diffusers_version": "0.41.0.dev0", + "scheduler": ["diffusers", "FlowMatchEulerDiscreteScheduler"], + } + if preset: + model_index["sample_sigmas"] = sample_sigmas + (tmp_path / "model_index.json").write_text(json.dumps(model_index)) + (tmp_path / "scheduler").mkdir() + config = QwenImage21PipelineConfig() + args = SimpleNamespace( + pipeline_config=config, + model_subfolder=None, + revision=None, + enable_cfg_parallel=False, + ) + pipeline = object.__new__(QwenImage21Pipeline) + pipeline.model_path = str(tmp_path) + pipeline.server_args = args + loaded_config = pipeline._load_config() + assert "sample_sigmas" not in loaded_config + assert config.sample_sigmas == (sample_sigmas if preset else None) + + batch = Req( + sampling_params=QwenImage21SamplingParams( + prompt="a ceramic teapot", + height=1024, + width=1024, + num_inference_steps=steps, + ), + sigmas=explicit_sigmas, + ) + QwenImage21InputValidationStage().forward(batch, args) + batch.extra["qwen21_mu"] = 0.7 + scheduler_kwargs = dict( + use_dynamic_shifting=dynamic_shifting, + shift=1.0, + shift_terminal=0.02 if dynamic_shifting else None, + ) + scheduler = FlowMatchEulerDiscreteScheduler(**scheduler_kwargs) + monkeypatch.setattr( + "sglang.multimodal_gen.runtime.pipelines_core.stages.timestep_preparation.get_local_torch_device", + lambda: torch.device("cpu"), + ) + TimestepPreparationStage(scheduler, [prepare_qwen21_mu]).forward(batch, args) + expected_sigmas = explicit_sigmas + if expected_sigmas is None: + expected_sigmas = sample_sigmas if preset else np.linspace(1, 1 / steps, steps) + # preserve the native scheduler's existing dynamic-shift rounding; Turbo's + # unshifted grid must also match the Diffusers scheduler bit for bit + scheduler_cls = ( + FlowMatchEulerDiscreteScheduler + if dynamic_shifting + else ReferenceFlowMatchScheduler + ) + reference = scheduler_cls(**scheduler_kwargs) + reference.set_timesteps(sigmas=expected_sigmas, mu=0.7, device="cpu") + torch.testing.assert_close(batch.timesteps, reference.timesteps, atol=0, rtol=0) + torch.testing.assert_close(batch.scheduler.sigmas, reference.sigmas, atol=0, rtol=0) + assert batch.num_inference_steps == len(expected_sigmas) + assert batch.scheduler.num_inference_steps == len(expected_sigmas) + assert isinstance(batch.sigmas, list) + # request-local schedules must not mutate the checkpoint's preset + batch.sigmas[0] = 0.9 + assert config.sample_sigmas == (sample_sigmas if preset else None) + + @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16]) @pytest.mark.parametrize("layout", ["contiguous", "channels_last", "transposed"]) @pytest.mark.parametrize("device", ["cpu", "cuda"]) @@ -582,6 +676,10 @@ def test_architecture_derived_dimensions(): def test_registry_routes_local_checkpoint_and_preserves_legacy(): + assert ( + _get_config_info("Qwen/Qwen-Image-2.1-Turbo").pipeline_config_cls + is QwenImage21PipelineConfig + ) assert ( _get_config_info("Qwen/Qwen-Image-2.1").pipeline_config_cls is QwenImage21PipelineConfig