[Bugfix] Fix PyTorch H2D input lifetime across CUDA streams - #4792
Open
grimoire wants to merge 1 commit into
Open
[Bugfix] Fix PyTorch H2D input lifetime across CUDA streams#4792grimoire wants to merge 1 commit into
grimoire wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a CUDA caching-allocator lifetime hazard in the PyTorch model agent by registering the forward CUDA stream against H2D-transferred device allocations after waiting on the H2D event, preventing premature reuse across streams.
Changes:
- Introduces
_record_forward_input_stream()and integrates it into the background loop afterwait_event()and before model execution. - Adds
record_stream()implementations across model/vision/delta/sampling inputs and strategy extra inputs/stopping criteria. - Adds unit tests to validate stream-recording behavior and a CUDA allocator regression test.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/pytorch/test_model_inputs.py | Adds tests verifying ModelInputs/VisionModelInputs and ModelInputsDelta stream recording. |
| tests/pytorch/engine/test_model_agent.py | Adds tests for _record_forward_input_stream() protocol behavior and an allocator-lifetime regression test. |
| tests/pytorch/engine/test_logits_process.py | Adds a test for SamplingInputs.record_stream() behavior. |
| lmdeploy/pytorch/strategies/base/model_agent.py | Adds default record_stream() to ExtraInputs and StoppingCriteria base classes. |
| lmdeploy/pytorch/multimodal/data_type.py | Adds MultiModalData.record_stream() for multimodal payload ownership tracking. |
| lmdeploy/pytorch/model_inputs.py | Adds record_stream() to VisionModelInputs, ModelInputsDelta, and ModelInputs. |
| lmdeploy/pytorch/engine/model_agent/agent.py | Adds _record_forward_input_stream() and uses it after H2D event wait; centralizes H2D key list. |
| lmdeploy/pytorch/engine/logits_process.py | Adds SamplingInputs.record_stream() for forward-stream registration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+109
to
+114
| for value in (self.meta or {}).values(): | ||
| if isinstance(value, Tensor): | ||
| if value.is_cuda: | ||
| value.record_stream(stream) | ||
| elif hasattr(value, 'record_stream'): | ||
| value.record_stream(stream) |
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.
Motivation
The PyTorch model agent transfers forward inputs on the H2D stream and
consumes the resulting tensors on the forward stream after waiting for a
CUDA event.
The event orders execution, but does not register the forward stream with
PyTorch's caching allocator. After Python releases the tensor references,
the allocations could therefore be reused on the H2D stream while
forward-stream kernels were still consuming them.
_H2DTransfer.refsprotects CPU source objects until the non-blocking H2Dcopies complete, but does not protect the lifetime of the resulting device
allocations.
Modification
forward-stream lifetime registration.
the H2D event and before model execution.
record_stream()methods for model, delta, vision,multimodal, sampling, stopping, and strategy extra inputs.
protocol.
This preserves the existing asynchronous H2D/forward overlap and does not
introduce a stream synchronization.