Fix tensor lifetime issue#4228
Open
SandSnip3r wants to merge 1 commit intopytorch:mainfrom
Open
Conversation
SandSnip3r
commented
May 1, 2026
|
|
||
| auto dims = core::util::toVec(out_shape); | ||
| auto type = util::TRTDataTypeToScalarType(compiled_engine->exec_ctx->getEngine().getTensorDataType(name.c_str())); | ||
| outputs[pyt_idx] = std::move(at::empty(dims, {at::kCUDA}).to(type).contiguous()); |
Contributor
Author
There was a problem hiding this comment.
By the way, a separate cleanup should be done where this line is instead
outputs[pyt_idx] = at::empty(dims, at::TensorOptions().device(at::kCUDA).dtype(type));
This would improve from two allocations & a dtype-conversion kernel to just a single allocation.
Collaborator
There was a problem hiding this comment.
I think this is the same line Shane identified as well.
narendasan
approved these changes
May 1, 2026
Collaborator
narendasan
left a comment
There was a problem hiding this comment.
This looks good to me
|
|
||
| auto dims = core::util::toVec(out_shape); | ||
| auto type = util::TRTDataTypeToScalarType(compiled_engine->exec_ctx->getEngine().getTensorDataType(name.c_str())); | ||
| outputs[pyt_idx] = std::move(at::empty(dims, {at::kCUDA}).to(type).contiguous()); |
Collaborator
There was a problem hiding this comment.
I think this is the same line Shane identified as well.
3b6cdb3 to
66c5a42
Compare
66c5a42 to
61b3003
Compare
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.
Description
This change fixes a correctness issue that I and others were seeing when running the FLUX2 diffusion model. The model, when compiled with either TensorRT or TensorRT-RTX was producing garbage images.
The issue was that the input tensor's lifetime was incorrect. The input tensor's ref count dropped to 0 before the engine ran with
enqueueV3(). In this specific case, it was a bit of a perfect storm with an output having the same size and shape and also there being a fp32->bf16 cast. Another tensor was being allocated (the output tensor) and that was given the address of the input tensor.Type of change
Checklist: