Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
3954bd7
[diffusion] Enable breakable CUDA graph (BCG) for diffusion DiTs
Jun 6, 2026
15b836e
BCG prompt-invariant capture + cosmos3 / SANA / LTX-2
Jun 8, 2026
b7a8c16
[diffusion] BCG: support Helios + Wan, harden runner for CPU inputs
Jun 10, 2026
4cef688
[diffusion] BCG: scope wiring to SANA / Cosmos3 / Wan2.x
Jun 11, 2026
9184375
[diffusion] Make Z-Image BCG capture-safe
BBuf Jun 13, 2026
2d5a267
[diffusion] Route DMD denoising through BCG runner
BBuf Jun 13, 2026
9745aa8
[diffusion] Make video rotary inputs BCG capture-safe
BBuf Jun 13, 2026
edd2bf2
[diffusion] Make GLM-Image BCG capture-safe
BBuf Jun 13, 2026
0aeb321
[diffusion] Make LTX-2 BCG capture-safe
BBuf Jun 13, 2026
92aa9cd
[diffusion] Make Hunyuan3D shape BCG capture-safe
BBuf Jun 13, 2026
dbf8bbf
[diffusion] Make LTX-2.3 warmup BCG-safe
BBuf Jun 13, 2026
2317c23
[diffusion] Make LTX-2.3 perturbation masks BCG-safe
BBuf Jun 13, 2026
abbbe6f
[diffusion] Make Helios BCG capture-safe
BBuf Jun 13, 2026
b3ba566
[diffusion] Make MOVA dual-tower BCG-safe
BBuf Jun 13, 2026
9159a38
[diffusion] Skip edit-model warmup BCG capture
BBuf Jun 13, 2026
bb82c40
Fix diffusion BCG prompt bucketing lint
BBuf Jun 14, 2026
b9ce17e
Bucket masked diffusion prompts for BCG
BBuf Jun 15, 2026
93b460b
Fix Z-Image BCG prompt padding
BBuf Jun 15, 2026
86ceb95
Fix GLM Image BCG prompt reuse
BBuf Jun 15, 2026
aa71ebf
Fix edit-model BCG warmup capture
BBuf Jun 15, 2026
6383c81
Fix Hunyuan3D BCG warmup capture
BBuf Jun 15, 2026
d705077
Disable HunyuanVideo BCG replay for prompt correctness
BBuf Jun 15, 2026
9c032e9
Default missing diffusion flags to disabled in tests
BBuf Jun 15, 2026
6def28b
Fix Helios BCG warmup capture
BBuf Jun 15, 2026
2be15b7
Fix LTX2.3 BCG warmup capture
BBuf Jun 15, 2026
b632a71
Fix diffusion BCG lint issues
BBuf Jun 15, 2026
88ab259
Add diffusion BCG service validation helper
BBuf Jun 15, 2026
1315427
Fix Qwen diffusion BCG varlen replay
BBuf Jun 16, 2026
1b1261e
Use fused Qwen modulation ops under diffusion BCG
BBuf Jun 16, 2026
7fa0e35
Use fused Qwen select modulation under diffusion BCG
BBuf Jun 16, 2026
f9594bd
[diffusion] Add explicit BCG resolution + text-bucket server args
Jun 16, 2026
a1fb29f
[diffusion] Extract BCG prompt-padding helpers into bcg_utils + model…
Jun 16, 2026
ec2da16
[diffusion] Add BaseBreakableCudaGraphRunner with capture/replay API
Jun 16, 2026
47c2372
[diffusion] Capture all BCG graphs at warmup so serving never recaptures
Jun 16, 2026
12b6bce
[diffusion] Force server-based warmup under BCG so capture happens at…
Jun 16, 2026
a9ec07a
[diffusion] Harden BCG re-capture campaign driver (free-port probe + …
Jun 16, 2026
68a6f8d
[diffusion] BCG runner: capture-on-first-use + restore visible captur…
Jun 16, 2026
6aab6b9
[diffusion] Remove temporary BCG PR-validation helper scripts
Jun 16, 2026
e166e99
[diffusion] Fix unreachable --bcg-text-buckets validation
Jun 17, 2026
934627e
[diffusion] Allow BCG captures beyond reserved memory threshold
BBuf Jun 17, 2026
44769e1
Fix Cohere2Moe config import
BBuf Jun 17, 2026
1446f2d
Fix Cohere2Moe config startup compatibility
BBuf Jun 17, 2026
fdc2510
Restrict diffusion BCG support
BBuf Jun 17, 2026
6321189
Limit diffusion BCG to Qwen Image models
BBuf Jun 18, 2026
7905812
Scope diffusion BCG to Qwen Image
BBuf Jun 18, 2026
67110cb
Drop server args BCG unit changes
BBuf Jun 18, 2026
c021e25
Document diffusion replay token use
BBuf Jun 18, 2026
e3d4e57
Clarify diffusion BCG padding utilities docstring
BBuf Jun 18, 2026
6882e67
Add Z-Image diffusion BCG support
BBuf Jun 18, 2026
37beef8
Enable BCG for GLM Image and FLUX2 Klein
BBuf Jun 18, 2026
789f238
Remove FLUX2 Klein from diffusion BCG allowlist
BBuf Jun 18, 2026
41d5e39
Fix diffusion BCG lint formatting
BBuf Jun 18, 2026
63cc964
Disable GLM Image diffusion BCG support
BBuf Jun 19, 2026
26d1302
Restore GLM Image BCG support
BBuf Jun 19, 2026
6c531c7
Refactor diffusion BCG helpers
BBuf Jun 21, 2026
0ec34f2
Add Ideogram diffusion BCG support
BBuf Jun 21, 2026
5acfcfe
Revert "Add Ideogram diffusion BCG support"
BBuf Jun 21, 2026
7bb08a6
Revert "Revert "Add Ideogram diffusion BCG support""
BBuf Jun 21, 2026
83eb826
Fix diffusion BCG lint formatting
BBuf Jul 3, 2026
c2c07cb
Merge remote-tracking branch 'origin/main' into diffusion/breakable-c…
BBuf Jul 3, 2026
33ea232
[diffusion] Fix BCG-vs-main integration breakage after merge
BBuf Jul 4, 2026
1549091
Merge remote-tracking branch 'origin/main' into HEAD
BBuf Jul 4, 2026
01581fd
Fix BCG failure hint re-export after main merge
BBuf Jul 4, 2026
361b779
Fix BCG backend graph construction after API split
BBuf Jul 4, 2026
f6fa91c
Merge remote-tracking branch 'origin/main' into HEAD
BBuf Jul 4, 2026
2873e2a
Stabilize DSV4 HiCache CI memory margin
BBuf Jul 4, 2026
7e6c099
Re-export breakable CUDA graph copy helper
BBuf Jul 4, 2026
d909c7d
Merge remote-tracking branch 'origin/main' into HEAD
BBuf Jul 4, 2026
8fc27c4
Increase DeepSeek V3 CP test memory fraction
BBuf Jul 4, 2026
aa921fc
Fix Z-Image eager path for BCG prompt padding
BBuf Jul 4, 2026
a6306ee
Merge remote-tracking branch 'origin/main' into HEAD
BBuf Jul 4, 2026
f797d06
Merge remote-tracking branch 'origin/main' into HEAD
BBuf Jul 4, 2026
2e15d7a
Merge remote-tracking branch 'origin/main' into HEAD
BBuf Jul 4, 2026
12caf89
Fix MOVA SP merge lint
BBuf Jul 4, 2026
7b2a549
Fix diffusion BCG CI failures
BBuf Jul 5, 2026
98586b7
Relax Qwen NPU consistency threshold
BBuf Jul 5, 2026
7e94827
Relax NPU W4A4 throughput guard
BBuf Jul 5, 2026
3d312f5
Merge main and fix legacy accessor ratchet
BBuf Jul 5, 2026
47e274e
Update ZImage 2-GPU denoise baseline
BBuf Jul 5, 2026
8b3fdde
Retry transient diffusion GT download misses
BBuf Jul 5, 2026
4ff2c07
Relax NPU W4A4 throughput guard again
BBuf Jul 5, 2026
dacb13c
Stabilize AMD diffusion update-weights CI
BBuf Jul 5, 2026
19d8735
Merge remote-tracking branch 'origin/main' into diffusion/breakable-c…
BBuf Jul 5, 2026
bad8cd8
Revert unrelated CI and non-diffusion test tweaks
BBuf Jul 5, 2026
b2a57ab
Merge origin/main into diffusion/breakable-cuda-graph
BBuf Jul 8, 2026
28c1c28
Use Z-Image caption mask for BCG padding
BBuf Jul 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,30 @@ def get_freqs_cis(self, batch, device, rotary_emb, dtype):
return cos, sin

def prepare_pos_cond_kwargs(self, batch, device, rotary_emb, dtype):
return {
kwargs = {
"prior_token_id": batch.prior_token_id,
"prior_token_drop": batch.prior_token_drop_cond,
"crop_coords": batch.crop_coords,
"target_size": batch.target_size,
"kv_caches": batch.kv_caches,
"kv_caches_mode": "read",
"freqs_cis": self.get_freqs_cis(batch, device, rotary_emb, dtype),
}
if getattr(batch, "prior_token_image_ids", None) is not None:
kwargs["kv_caches"] = batch.kv_caches
kwargs["kv_caches_mode"] = "read"
return kwargs

def prepare_neg_cond_kwargs(self, batch, device, rotary_emb, dtype):
return {
kwargs = {
"prior_token_id": batch.prior_token_id,
"prior_token_drop": batch.prior_token_drop_uncond,
"crop_coords": batch.crop_coords,
"target_size": batch.target_size,
"kv_caches": batch.kv_caches,
"kv_caches_mode": "skip",
"freqs_cis": self.get_freqs_cis(batch, device, rotary_emb, dtype),
}
if getattr(batch, "prior_token_image_ids", None) is not None:
kwargs["kv_caches"] = batch.kv_caches
kwargs["kv_caches_mode"] = "skip"
return kwargs

def get_decode_scale_and_shift(self, device, dtype, vae):
latents_mean = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Diffusion breakable CUDA graph runtime helpers."""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Model-specific prompt padders for diffusion breakable CUDA graph."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Copyright 2023-2026 SGLang Team
# Licensed under the Apache License, Version 2.0
# ==============================================================================
"""Ideogram-4 breakable CUDA graph (BCG) prompt padding."""

from __future__ import annotations

from typing import Any

import torch

from sglang.multimodal_gen.runtime.breakable_cuda_graph import (
prompt_padding as bcg_utils,
)
from sglang.multimodal_gen.runtime.layers.attention import DynamicVarlenMaskMeta

_SEQUENCE_PADDING_INDICATOR = -1
_OUTPUT_IMAGE_INDICATOR = 2
_LLM_TOKEN_INDICATOR = 3
_DYNAMIC_MASK_META_ATTR = "_sglang_bcg_ideogram_attn_mask_meta"


def is_ideogram_transformer(current_model: Any, call_kwargs: dict) -> bool:
return (
bcg_utils.transformer_class_name_matches(current_model, "ideogram")
and "llm_features" in call_kwargs
and "x" in call_kwargs
and "indicator" in call_kwargs
and "position_ids" in call_kwargs
)


def _unwrap_model(current_model: Any) -> Any:
for attr in ("module", "_orig_mod"):
wrapped = getattr(current_model, attr, None)
if wrapped is not None:
current_model = wrapped
return current_model


def _dynamic_mask_meta(current_model: Any) -> DynamicVarlenMaskMeta:
model = _unwrap_model(current_model)
meta = getattr(model, _DYNAMIC_MASK_META_ATTR, None)
if not isinstance(meta, DynamicVarlenMaskMeta):
meta = DynamicVarlenMaskMeta()
setattr(model, _DYNAMIC_MASK_META_ATTR, meta)
return meta


def _first_indicator(call_kwargs: dict) -> torch.Tensor | None:
indicator = bcg_utils.first_tensor(call_kwargs.get("indicator"))
if not torch.is_tensor(indicator) or indicator.dim() < 2:
return None
return indicator


def _text_and_image_lengths(indicator: torch.Tensor) -> tuple[int, int] | None:
row = indicator[0]
if not torch.any(row == _LLM_TOKEN_INDICATOR):
return None
image_positions = (row == _OUTPUT_IMAGE_INDICATOR).nonzero(as_tuple=False)
if image_positions.numel() == 0:
return None
text_seq = int(image_positions[0].item())
if text_seq <= 0:
return None
image_seq = int(row.numel()) - text_seq
if image_seq <= 0:
return None
return text_seq, image_seq


def _pad_total_dim(obj: Any, *, source: int, target: int, value: float = 0) -> Any:
return bcg_utils.pad_nested_dim(
obj, dim=1, source=source, target=target, value=value
)


def pad_ideogram_prompt_kwargs(
call_kwargs: dict, current_model: Any, buckets: tuple[int, ...]
) -> dict:
indicator = _first_indicator(call_kwargs)
if indicator is None:
return call_kwargs

lengths = _text_and_image_lengths(indicator)
if lengths is None:
return call_kwargs
text_seq, image_seq = lengths

bucket = bcg_utils.select_text_bucket(text_seq, buckets)
if bucket is None:
return call_kwargs

source_total = text_seq + image_seq
target_total = bucket + image_seq
out = dict(call_kwargs)

if source_total < target_total:
for key in ("llm_features", "x"):
if key in out and out[key] is not None:
out[key] = _pad_total_dim(
out[key], source=source_total, target=target_total
)
if out.get("position_ids") is not None:
out["position_ids"] = _pad_total_dim(
out["position_ids"], source=source_total, target=target_total
)
if out.get("segment_ids") is not None:
out["segment_ids"] = _pad_total_dim(
out["segment_ids"],
source=source_total,
target=target_total,
value=_SEQUENCE_PADDING_INDICATOR,
)
if out.get("indicator") is not None:
out["indicator"] = _pad_total_dim(
out["indicator"], source=source_total, target=target_total
)
if out.get("attn_mask") is not None:
out["attn_mask"] = _pad_total_dim(
out["attn_mask"], source=source_total, target=target_total
)

if out.get("attn_mask") is not None:
out["attn_mask_meta"] = _dynamic_mask_meta(current_model)

return out


bcg_utils.register_prompt_padder(is_ideogram_transformer, pad_ideogram_prompt_kwargs)
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright 2023-2026 SGLang Team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Qwen-Image breakable CUDA graph (BCG) prompt padding.

Qwen-Image / Qwen-Image-Edit carry text length on dim 1 of
``encoder_hidden_states`` and a separate ``freqs_cis`` text-rope cache plus
``txt_seq_lens``; they may not pass an explicit prompt mask, so this padder
synthesizes one. Registered with the base denoising stage's padder registry.
"""

from __future__ import annotations

from typing import Any

import torch

from sglang.multimodal_gen.runtime.breakable_cuda_graph import (
prompt_padding as bcg_utils,
)


def is_qwen_transformer(current_model: Any, call_kwargs: dict) -> bool:
return (
bcg_utils.transformer_class_name_matches(current_model, "qwen")
and "txt_seq_lens" in call_kwargs
and "freqs_cis" in call_kwargs
)


def pad_qwen_prompt_kwargs(
call_kwargs: dict, current_model: Any, buckets: tuple[int, ...]
) -> dict:
ehs = call_kwargs.get("encoder_hidden_states")
ehs_tensor = bcg_utils.first_tensor(ehs)
if not torch.is_tensor(ehs_tensor) or ehs_tensor.dim() < 2:
return call_kwargs

seq = ehs_tensor.shape[1]
bucket = bcg_utils.select_text_bucket(seq, buckets)
if bucket is None:
return call_kwargs

out = dict(call_kwargs)
if seq < bucket:
out["encoder_hidden_states"] = bcg_utils.pad_nested_dim(
ehs, dim=1, source=seq, target=bucket
)
if (
"encoder_hidden_states_2" in out
and out["encoder_hidden_states_2"] is not None
):
out["encoder_hidden_states_2"] = bcg_utils.pad_nested_dim(
out["encoder_hidden_states_2"], dim=1, source=seq, target=bucket
)

mask = out.get("encoder_hidden_states_mask")
if mask is None:
mask = torch.ones(
ehs_tensor.shape[:2],
device=ehs_tensor.device,
dtype=torch.bool,
)
if mask is not None:
out["encoder_hidden_states_mask"] = bcg_utils.pad_nested_dim(
mask, dim=1, source=seq, target=bucket
)

if "encoder_attention_mask" in out and out["encoder_attention_mask"] is not None:
out["encoder_attention_mask"] = bcg_utils.pad_nested_dim(
out["encoder_attention_mask"], dim=1, source=seq, target=bucket
)

freqs_cis = out.get("freqs_cis")
if isinstance(freqs_cis, tuple) and len(freqs_cis) == 2:
img_cache, txt_cache = freqs_cis
txt_cache = bcg_utils.pad_nested_dim(
txt_cache, dim=0, source=seq, target=bucket
)
out["freqs_cis"] = (img_cache, txt_cache)
elif isinstance(freqs_cis, list) and len(freqs_cis) == 2:
img_cache, txt_cache = freqs_cis
txt_cache = bcg_utils.pad_nested_dim(
txt_cache, dim=0, source=seq, target=bucket
)
out["freqs_cis"] = [img_cache, txt_cache]

out["txt_seq_lens"] = bcg_utils.bucket_txt_seq_lens(out.get("txt_seq_lens"), bucket)
return out


bcg_utils.register_prompt_padder(is_qwen_transformer, pad_qwen_prompt_kwargs)
Comment thread
BBuf marked this conversation as resolved.
Loading
Loading