Repository navigation
fix: fix vlm cuda graph shape stability #30868
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
mickqian
merged 8 commits into
sgl-project:main
from
mickqian:codex/vlm-graph-shape-stability
Jul 19, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07beb9c
fix VLM CUDA graph shape stability
mickqian 310f56a
address VLM CUDA graph review feedback
mickqian 7ebaf72
test: make VLM CI tests runnable directly
mickqian 33f7091
Merge branch 'main' into codex/vlm-graph-shape-stability
mickqian a67123d
Merge branch 'main' into codex/vlm-graph-shape-stability
mickqian 3fa765b
Merge branch 'main' into codex/vlm-graph-shape-stability
mickqian bd4b0c6
test: initialize prefill graph capture buckets
mickqian 2777135
test: initialize prefill graph backend state
mickqian 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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -544,6 +544,71 @@ def _run_dummy_forward(self, num_tokens: int) -> None: | |
| attn_backend.init_forward_metadata(fb) | ||
| self._run_forward(fb, num_tokens) | ||
|
|
||
| def run_dummy_multimodal_deepstack_forward( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks hacky.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| self, language_model: torch.nn.Module, num_tokens: int | ||
| ) -> bool: | ||
| """Warm the tensor-valued deepstack branch before serving requests. | ||
|
|
||
| The regular PCG dummy is text-only. Qwen3-VL only provides | ||
| ``input_deepstack_embeds`` after visual encoding, so leaving this | ||
| branch cold makes the first image request synchronously recompile the | ||
| language model. The model/signature checks keep this a no-op for | ||
| non-deepstack architectures. | ||
| """ | ||
| if ( | ||
| "input_deepstack_embeds" | ||
| not in inspect.signature(language_model.forward).parameters | ||
| ): | ||
| return False | ||
|
|
||
| num_deepstack = getattr(self.model_runner.model, "num_deepstack_embeddings", 0) | ||
| if num_deepstack <= 0: | ||
| return False | ||
|
|
||
| hidden_size = ( | ||
| getattr(getattr(language_model, "config", None), "hidden_size", None) | ||
| or self.model_runner.model_config.hidden_size | ||
| ) | ||
| fb, attn_backend = self.capture_prepare(num_tokens) | ||
| attn_backend.init_forward_metadata(fb) | ||
| deepstack_embeds = torch.zeros( | ||
| (num_tokens, hidden_size * num_deepstack), | ||
| dtype=self.model_runner.dtype, | ||
| device=self.device, | ||
| ) | ||
| torch._dynamo.maybe_mark_dynamic(deepstack_embeds, 0) | ||
|
|
||
| fb.dp_local_start_pos = fb.dp_local_num_tokens = None | ||
| set_dp_buffer_len( | ||
| fb.global_dp_buffer_len, | ||
| num_tokens, | ||
| fb.dp_padding_mode.is_max_len(), | ||
| fb.global_num_tokens_cpu, | ||
| ) | ||
| set_is_extend_in_batch(False) | ||
|
|
||
| with ( | ||
| forward_context( | ||
| ForwardContext(attn_backend=self.model_runner.attn_backend) | ||
| ), | ||
| set_tc_piecewise_forward_context( | ||
| fb, | ||
| self.attention_layers, | ||
| self.quant_config, | ||
| self.moe_layers, | ||
| self.moe_fusions, | ||
| dsa_indexers=self.dsa_indexers, | ||
| ), | ||
| ): | ||
| language_model.forward( | ||
| fb.input_ids, | ||
| self._get_layer_model_positions(fb), | ||
| fb, | ||
| input_embeds=fb.input_embeds, | ||
| input_deepstack_embeds=deepstack_embeds, | ||
| ) | ||
| return True | ||
|
|
||
| def _has_inactive_dp_rank(self, forward_batch: ForwardBatch) -> bool: | ||
| # DSV4 DP attention / DeepEP collectives need every DP rank to enter | ||
| # the same replay path. Sparse-DP batches (one or more ranks with | ||
|
|
||
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
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
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,56 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from sglang.test.ci.ci_register import register_cuda_ci | ||
|
|
||
| register_cuda_ci(est_time=5, stage="base-b", runner_config="1-gpu-small") | ||
|
|
||
| if not torch.cuda.is_available(): | ||
| pytest.skip("CUDA piecewise backend requires CUDA", allow_module_level=True) | ||
|
|
||
| import sglang.srt.compilation.cuda_piecewise_backend as cuda_backend | ||
| from sglang.srt.compilation.cuda_piecewise_backend import ( | ||
| ConcreteSizeEntry, | ||
| CUDAPiecewiseBackend, | ||
| ) | ||
|
|
||
|
|
||
| def test_runtime_recompile_without_capture_stream_falls_back(monkeypatch): | ||
| """A Dynamo replacement backend cannot capture outside a PCG session.""" | ||
| compile_config = MagicMock() | ||
| compile_config.get_capture_sizes.return_value = [] | ||
| backend = CUDAPiecewiseBackend( | ||
| graph=MagicMock(), | ||
| compile_config=compile_config, | ||
| inductor_config={}, | ||
| graph_pool=None, | ||
| piecewise_compile_index=0, | ||
| total_piecewise_compiles=1, | ||
| sym_shape_indices=[0], | ||
| compiled_graph_for_general_shape=MagicMock(), | ||
| sglang_backend=MagicMock(), | ||
| ) | ||
| backend.first_run_finished = True | ||
| fallback = MagicMock(return_value="fallback-result") | ||
| backend.concrete_size_entries = { | ||
| 4: ConcreteSizeEntry( | ||
| runtime_shape=4, | ||
| need_to_compile=False, | ||
| use_cudagraph=True, | ||
| runnable=fallback, | ||
| num_finished_warmup=1, | ||
| ) | ||
| } | ||
|
|
||
| monkeypatch.setattr(cuda_backend, "get_pcg_capture_stream", lambda: None) | ||
| monkeypatch.setattr(cuda_backend, "is_in_torch_compile_warmup", lambda: False) | ||
| monkeypatch.setattr(cuda_backend, "print_warning_once", lambda _message: None) | ||
|
|
||
| assert backend(4) == "fallback-result" | ||
| fallback.assert_called_once_with(4) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(pytest.main([__file__, "-v"])) |
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.