-
Notifications
You must be signed in to change notification settings - Fork 9k
[diffusion] Enable breakable CUDA graph (BCG) for diffusion DiTs #27436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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
15b836e
BCG prompt-invariant capture + cosmos3 / SANA / LTX-2
b7a8c16
[diffusion] BCG: support Helios + Wan, harden runner for CPU inputs
4cef688
[diffusion] BCG: scope wiring to SANA / Cosmos3 / Wan2.x
9184375
[diffusion] Make Z-Image BCG capture-safe
BBuf 2d5a267
[diffusion] Route DMD denoising through BCG runner
BBuf 9745aa8
[diffusion] Make video rotary inputs BCG capture-safe
BBuf edd2bf2
[diffusion] Make GLM-Image BCG capture-safe
BBuf 0aeb321
[diffusion] Make LTX-2 BCG capture-safe
BBuf 92aa9cd
[diffusion] Make Hunyuan3D shape BCG capture-safe
BBuf dbf8bbf
[diffusion] Make LTX-2.3 warmup BCG-safe
BBuf 2317c23
[diffusion] Make LTX-2.3 perturbation masks BCG-safe
BBuf abbbe6f
[diffusion] Make Helios BCG capture-safe
BBuf b3ba566
[diffusion] Make MOVA dual-tower BCG-safe
BBuf 9159a38
[diffusion] Skip edit-model warmup BCG capture
BBuf bb82c40
Fix diffusion BCG prompt bucketing lint
BBuf b9ce17e
Bucket masked diffusion prompts for BCG
BBuf 93b460b
Fix Z-Image BCG prompt padding
BBuf 86ceb95
Fix GLM Image BCG prompt reuse
BBuf aa71ebf
Fix edit-model BCG warmup capture
BBuf 6383c81
Fix Hunyuan3D BCG warmup capture
BBuf d705077
Disable HunyuanVideo BCG replay for prompt correctness
BBuf 9c032e9
Default missing diffusion flags to disabled in tests
BBuf 6def28b
Fix Helios BCG warmup capture
BBuf 2be15b7
Fix LTX2.3 BCG warmup capture
BBuf b632a71
Fix diffusion BCG lint issues
BBuf 88ab259
Add diffusion BCG service validation helper
BBuf 1315427
Fix Qwen diffusion BCG varlen replay
BBuf 1b1261e
Use fused Qwen modulation ops under diffusion BCG
BBuf 7fa0e35
Use fused Qwen select modulation under diffusion BCG
BBuf f9594bd
[diffusion] Add explicit BCG resolution + text-bucket server args
a1fb29f
[diffusion] Extract BCG prompt-padding helpers into bcg_utils + model…
ec2da16
[diffusion] Add BaseBreakableCudaGraphRunner with capture/replay API
47c2372
[diffusion] Capture all BCG graphs at warmup so serving never recaptures
12b6bce
[diffusion] Force server-based warmup under BCG so capture happens at…
a9ec07a
[diffusion] Harden BCG re-capture campaign driver (free-port probe + …
68a6f8d
[diffusion] BCG runner: capture-on-first-use + restore visible captur…
6aab6b9
[diffusion] Remove temporary BCG PR-validation helper scripts
e166e99
[diffusion] Fix unreachable --bcg-text-buckets validation
934627e
[diffusion] Allow BCG captures beyond reserved memory threshold
BBuf 44769e1
Fix Cohere2Moe config import
BBuf 1446f2d
Fix Cohere2Moe config startup compatibility
BBuf fdc2510
Restrict diffusion BCG support
BBuf 6321189
Limit diffusion BCG to Qwen Image models
BBuf 7905812
Scope diffusion BCG to Qwen Image
BBuf 67110cb
Drop server args BCG unit changes
BBuf c021e25
Document diffusion replay token use
BBuf e3d4e57
Clarify diffusion BCG padding utilities docstring
BBuf 6882e67
Add Z-Image diffusion BCG support
BBuf 37beef8
Enable BCG for GLM Image and FLUX2 Klein
BBuf 789f238
Remove FLUX2 Klein from diffusion BCG allowlist
BBuf 41d5e39
Fix diffusion BCG lint formatting
BBuf 63cc964
Disable GLM Image diffusion BCG support
BBuf 26d1302
Restore GLM Image BCG support
BBuf 6c531c7
Refactor diffusion BCG helpers
BBuf 0ec34f2
Add Ideogram diffusion BCG support
BBuf 5acfcfe
Revert "Add Ideogram diffusion BCG support"
BBuf 7bb08a6
Revert "Revert "Add Ideogram diffusion BCG support""
BBuf 83eb826
Fix diffusion BCG lint formatting
BBuf c2c07cb
Merge remote-tracking branch 'origin/main' into diffusion/breakable-c…
BBuf 33ea232
[diffusion] Fix BCG-vs-main integration breakage after merge
BBuf 1549091
Merge remote-tracking branch 'origin/main' into HEAD
BBuf 01581fd
Fix BCG failure hint re-export after main merge
BBuf 361b779
Fix BCG backend graph construction after API split
BBuf f6fa91c
Merge remote-tracking branch 'origin/main' into HEAD
BBuf 2873e2a
Stabilize DSV4 HiCache CI memory margin
BBuf 7e6c099
Re-export breakable CUDA graph copy helper
BBuf d909c7d
Merge remote-tracking branch 'origin/main' into HEAD
BBuf 8fc27c4
Increase DeepSeek V3 CP test memory fraction
BBuf aa921fc
Fix Z-Image eager path for BCG prompt padding
BBuf a6306ee
Merge remote-tracking branch 'origin/main' into HEAD
BBuf f797d06
Merge remote-tracking branch 'origin/main' into HEAD
BBuf 2e15d7a
Merge remote-tracking branch 'origin/main' into HEAD
BBuf 12caf89
Fix MOVA SP merge lint
BBuf 7b2a549
Fix diffusion BCG CI failures
BBuf 98586b7
Relax Qwen NPU consistency threshold
BBuf 7e94827
Relax NPU W4A4 throughput guard
BBuf 3d312f5
Merge main and fix legacy accessor ratchet
BBuf 47e274e
Update ZImage 2-GPU denoise baseline
BBuf 8b3fdde
Retry transient diffusion GT download misses
BBuf 4ff2c07
Relax NPU W4A4 throughput guard again
BBuf dacb13c
Stabilize AMD diffusion update-weights CI
BBuf 19d8735
Merge remote-tracking branch 'origin/main' into diffusion/breakable-c…
BBuf bad8cd8
Revert unrelated CI and non-diffusion test tweaks
BBuf b2a57ab
Merge origin/main into diffusion/breakable-cuda-graph
BBuf 28c1c28
Use Z-Image caption mask for BCG padding
BBuf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
python/sglang/multimodal_gen/runtime/breakable_cuda_graph/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Diffusion breakable CUDA graph runtime helpers.""" |
1 change: 1 addition & 0 deletions
1
python/sglang/multimodal_gen/runtime/breakable_cuda_graph/model_padders/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Model-specific prompt padders for diffusion breakable CUDA graph.""" |
131 changes: 131 additions & 0 deletions
131
python/sglang/multimodal_gen/runtime/breakable_cuda_graph/model_padders/ideogram.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
102 changes: 102 additions & 0 deletions
102
python/sglang/multimodal_gen/runtime/breakable_cuda_graph/model_padders/qwen_image.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.