--- title: "How to Support New Diffusion Models" metatags: description: "This document explains how to add support for new diffusion models in SGLang Diffusion." --- This document explains how to add support for new diffusion models in SGLang Diffusion. ## Architecture Overview SGLang Diffusion is engineered for both performance and flexibility, built upon a pipeline architecture. This design allows developers to construct pipelines for various diffusion models while keeping the core generation loop standardized for optimization. At its core, the architecture revolves around two key concepts, as highlighted in our [blog post](https://lmsys.org/blog/2025-11-07-sglang-diffusion/#architecture): - **`ComposedPipeline`**: This class orchestrates a series of `PipelineStage`s to define the complete generation process for a specific model. It acts as the main entry point for a model and manages the data flow between the different stages of the diffusion process. - **`PipelineStage`**: Each stage is a modular component that encapsulates a function within the diffusion process. Examples include prompt encoding, the denoising loop, or VAE decoding. ### Two Pipeline Styles SGLang Diffusion supports two pipeline composition styles. Both are valid; choose the one that best fits your model. #### Style A: Hybrid Monolithic Pipeline (Recommended Default) The recommended default for most new models. Uses a three-stage structure: ``` BeforeDenoisingStage (model-specific) → DenoisingStage (standard) → DecodingStage (standard) ```
| Stage | Ownership | Responsibility |
|---|---|---|
{Model}BeforeDenoisingStage |
Model-specific | All pre-processing: input validation, text/image encoding, latent preparation, timestep computation |
DenoisingStage |
Framework-standard | The denoising loop (DiT/UNet forward passes), shared across all models |
DecodingStage |
Framework-standard | VAE decoding from latent space to pixel space, shared across all models |
| Situation | Recommended Style |
|---|---|
| Model has unique/complex pre-processing (VLM captioning, AR token generation, custom latent packing, etc.) | Hybrid — consolidate into a BeforeDenoisingStage |
| Model fits neatly into standard text-to-image or text+image-to-image pattern | Modular — use add_standard_t2i_stages() / add_standard_ti2i_stages() |
| Porting a Diffusers pipeline with many custom steps | Hybrid — copy the __call__ logic into a single stage |
| Adding a variant of an existing model that shares most logic | Modular — reuse existing stages, customize via PipelineConfig callbacks |
| A specific pre-processing step needs special parallelism or profiling isolation | Modular — extract that step as a dedicated stage |
| Stage Class | Description |
|---|---|
DenoisingStage |
Executes the main denoising loop, iteratively applying the model (DiT/UNet) to refine the latents. |
DecodingStage |
Decodes the final latent tensor back into pixel space using the VAE. |
DmdDenoisingStage |
A specialized denoising stage for DMD model architectures. |
CausalDMDDenoisingStage |
A specialized causal denoising stage for specific video models. |
| Stage Class | Description |
|---|---|
InputValidationStage |
Validates user-provided SamplingParams. |
TextEncodingStage |
Encodes text prompts into embeddings using one or more text encoders. |
ImageEncodingStage |
Encodes input images into embeddings, often used in image-to-image tasks. |
ImageVAEEncodingStage |
Encodes an input image into latent space using the VAE. |
TimestepPreparationStage |
Prepares the scheduler's timesteps for the diffusion process. |
LatentPreparationStage |
Creates the initial noisy latent tensor that will be denoised. |
| Field | Type | Description |
|---|---|---|
batch.latents |
torch.Tensor |
Initial noisy latent tensor |
batch.timesteps |
torch.Tensor |
Timestep schedule |
batch.num_inference_steps |
int |
Number of denoising steps |
batch.sigmas |
list[float] |
Sigma schedule (must be a Python list, not numpy) |
batch.prompt_embeds |
list[torch.Tensor] |
Positive prompt embeddings (wrapped in a list) |
batch.negative_prompt_embeds |
list[torch.Tensor] |
Negative prompt embeddings (wrapped in a list) |
batch.generator |
torch.Generator |
RNG generator for reproducibility |
batch.raw_latent_shape |
tuple |
Original latent shape before any packing |
| Model | Pipeline | BeforeDenoisingStage | PipelineConfig |
|---|---|---|---|
| GLM-Image | runtime/pipelines/glm_image.py |
stages/model_specific_stages/glm_image.py |
configs/pipeline_configs/glm_image.py |
| Qwen-Image-Layered | runtime/pipelines/qwen_image.py |
stages/model_specific_stages/qwen_image_layered.py |
configs/pipeline_configs/qwen_image.py |
| Model | Pipeline | Notes |
|---|---|---|
| Qwen-Image (T2I) | runtime/pipelines/qwen_image.py |
Uses add_standard_t2i_stages() |
| Qwen-Image-Edit | runtime/pipelines/qwen_image.py |
Uses add_standard_ti2i_stages() |
| Flux | runtime/pipelines/flux.py |
Uses add_standard_t2i_stages() with custom prepare_mu |
| Wan | runtime/pipelines/wan_pipeline.py |
Uses add_standard_ti2v_stages() |