Repository navigation
fix(runtime): keep the output structure under whole-graph CUDA graphs - #4906
Open
narendasan wants to merge 1 commit into
Open
narendasan wants to merge 1 commit into
narendasan wants to merge 1 commit into
Conversation
CudaGraphsTorchTensorRTModule cloned its outputs out of the static buffers as a flat list: [output.clone() for output in output_buffers]. A module whose output is a pytree, such as an exported Hugging Face model returning a ModelOutput, failed on every call once a graph break put it under whole-graph CUDA graphs: AttributeError: 'CausalLMOutputWithPast' object has no attribute 'clone' Clone the tensor leaves with tree_map instead, which also returns tuples as tuples and single-element sequences as such, as the module itself does without CUDA graphs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 21, 2026
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Stacked on #4905. The base is
narendasan/cudagraphs-kwargs-order.Summary
CudaGraphsTorchTensorRTModule(whole-graph CUDA graphs, used when a compiled module has PyTorch subgraphs) cloned its outputs out of the static buffers as a flat list:[output.clone() for output in output_buffers]. That assumes a tensor or a flat sequence of tensors.ModelOutput(a pytree), so with any graph break every call failed:tree_map, keeping the output structure.Why it matters
Graph breaks cost the most when every engine boundary is a separate launch, and CUDA graphs are the remedy. For example, TensorRT 11.3's attention at head_dim 256 is several times slower than PyTorch's FlashAttention-2, so Gemma runs faster with its attention left in PyTorch, despite 26 graph breaks. For a 256-token forward of Gemma-3-1B (bf16, RTX 5090):
torch.compile(mode="max-autotune").Gemma-2-2B gets 8.69 ms, against 8.51 ms. Without this fix,
enable_cudagraphscannot run such a module at all.Tests
test_cudagraphs_whole_graph_nested_outputs_cpp(a dict-with-tuple output, a graph break forced withtorch_executed_ops) fails without the change with the error above.pytest tests/py/dynamo/runtime/test_002_cudagraphs_{cpp,py}.py test_aliased_io_cudagraphs.py test_empty_output.py test_input_lifetime.py test_output_allocator.py test_004_weight_streaming.py: 49 passed.Stack
3 of 4. Base: #4905. Next: #4907.
🤖 Generated with Claude Code