TensorRT: 🐛 [Bug] Torch Tensor RT crash when trying to compile a script module on Windows (C++)
Bug Description
I can’t compile a script module with TorchTensorRT. This is my code:
#include <iostream>
#include <vector>
#include <ATen/Context.h>
#include <torch/torch.h>
#include <torch/script.h>
#include "torch_tensorrt/torch_tensorrt.h"
void compile(std::string model_path) {
const torch::Device device = torch::Device(torch::kCUDA, 0);
torch::jit::script::Module model;
std::cout << "Trying to load the model" << std::endl;
try {
model = torch::jit::load(model_path, device);
model.to(device);
model.eval();
std::cout << "AI model loaded successfully." << std::endl;
}
catch (const c10::Error& e) {
std::cerr << e.what() << std::endl;
}
auto input = torch_tensorrt::Input(std::vector<int64_t>{ 1, 3, 512, 512 });
std::cout << "Creating compile settings" << std::endl;
auto compile_settings = torch_tensorrt::ts::CompileSpec({ input });
// Compile module
std::cout << "Compiling..." << std::endl;
auto trt_mod = torch_tensorrt::ts::compile(model, compile_settings); <-- CRASHES HERE.
// Run like normal
std::cout << "Create tensor" << std::endl;
auto in = torch::randn({ 1, 3, 512, 512 }, device);
std::cout << "Forward pass..." << std::endl;
auto results = trt_mod.forward({ in });
// Save module for later
trt_mod.save("output/model/path.ts");
}
int main() {
compile("path/to/traced_script_module.pt");
return 0;
}
This is the error I get:

First a WARNING get printed “WARNING: [Torch-TensorRT] - Interpolation layer will be run through ATen, not TensorRT. Performance may be lower than expected
”,
and then, as you can see from the screenshot, I got an exception “read access violation. creator was nullptr
.” when running the following lines:
auto creator = getPluginRegistry()->getPluginCreator("Interpolate", "1", "torch_tensorrt");
auto interpolate_plugin = creator->createPlugin(name, &fc);
The file interpolate.cpp
is located at path/to/Torch-TensorRT/core/conversion/converters/impl
.
What am I doing wrong?
This is my CMakeLists.txt
:
cmake_minimum_required (VERSION 3.8)
project(example-app)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(example-app create_trt_module.cpp)
target_include_directories(example-app PRIVATE "path/to/Torch-TensorRT/cpp/include")
target_link_libraries(example-app "${TORCH_LIBRARIES}")
target_link_libraries(example-app path/to/Torch-TensorRT/out/build/x64-Release/lib/torchtrt.lib)
target_link_libraries(example-app path/to/Torch-TensorRT/out/build/x64-Release/lib/torchtrt_plugins.lib)
I exported the traced script module with the following code:
# Import model
# ...
model.to("cuda")
model.eval()
# Create dummy data for tracing and benchmarking purposes.
shape = (1, 3, 512, 512)
input_data = torch.randn(shape).to("cuda")
# Convert model to script module
print("Tracing PyTorch model...")
traced_script_module = torch.jit.trace(model, input_data)
torch.jit.save(traced_script_module, "traced_script_module.pt")
Environment
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 31 (5 by maintainers)
Thank you soo much @andreabonvini. It is working now
Great that you had it working @andreabonvini ! 😃
In my opinion, that issue is independent from the CMake support in itself. What I mean by that is:
Still, that problem puzzled me and I might have a proposal. I was wondering how that mechanism works in pytorch/vision (aka TorchVision) since there is at least one situation that works similarly to torchtrt_plugins: the library provides additional operator to pytorch that needs to be registered and potentially nothing else (no symbols to be used by the consumer of the library). From my understanding they rely on “linker” pragmas:
So basically, by just including that header (as documented in the README), the library is linked since one symbol is required.
So that could easily be done in torch-tensorRT as well. Moreover that would be consistent with torchvision. What do you think @narendasan ?
That’s a very good point! If my understanding is correct, on Linux, torch-tensorRT relies on static initialisation to register the Engine class with torch (see here). That works if you force linking to that library, even when no symbol are used (that’s always the case for torchtrt_runtime), so that it’s automatically loaded by the consumer executable/library. Not sure what’s the equivalent on windows (or even if there is an equivalent, or if you have to “manually” dlopen the library).
Anyway, a possible (ugly) workaround is to link to torchtrt (and not torchtrt_runtime) and use at least one symbol from that lib (just instentiate a
CompileSpec
or anInput
).Not sure what the proper design will be, but I guess this will need to be addressed at some point, if the windows support becomes a bit more official, right @narendasan ?
The release notes for version 1.1.0 indicates
I cannot guarantee it is related, but I would try with TensorRT 8.2.