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
Converting a torchscript with torch-tensorRT shows large memory leaks. A simple way to reproduce the leaks is provided in this script:
Running this script prints the used memory for each loop, which increases steadily by 35MB - 45MB per loop, where it should not increase, since all objects from the loop are deleted.
Running the small reproduction script with Valgrind, using the following command:
shows the following pretty important (42MB) possible losses:
This points to two places,
torch_tensorrt::core::conversion::ConversionCtx::SerializeEngine
(defined incore/conversion/conversionctx/ConversionCtx.cpp
)torch::class_<torch_tensorrt::core::runtime::TRTEngine>
(defined incore/runtime/register_jit_hooks.cpp
)In these two places, pointers to TensorRT objects are obtained:
IHostMemory*
raw pointer is returned bynvinfer1::IBuilder::buildSerializedNetwork(...)
IHostMemory*
raw pointer is returned bynvinfer1::ICudaEngine::serialize()
This PR adds missing wrapping of raw pointers in smart pointers, so that the destructors of the underlying TensorRT objects are called properly, thus effectively releasing that host memory.
Type of change
Please delete options that are not relevant and/or add your own.
Checklist: