Skip to content

Commit dd0f6d5

Browse files
committed
Backwards compatibility for TRT 7.x
Signed-off-by: Boris Fomitchev <[email protected]>
1 parent 744b417 commit dd0f6d5

File tree

13 files changed

+60
-120
lines changed

13 files changed

+60
-120
lines changed

core/conversion/conversionctx/ConversionCtx.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ ConversionCtx::ConversionCtx(BuilderSettings build_settings)
5454
cudaSetDevice(settings.device.gpu_id) == cudaSuccess, "Unable to set gpu id: " << settings.device.gpu_id);
5555
}
5656

57-
builder = nvinfer1::createInferBuilder(logger);
58-
net = builder->createNetworkV2(1U << static_cast<uint32_t>(nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH));
57+
builder = make_trt(nvinfer1::createInferBuilder(logger));
58+
net = make_trt(builder->createNetworkV2(1U << static_cast<uint32_t>(nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH)));
5959

6060
LOG_DEBUG(build_settings);
61-
cfg = builder->createBuilderConfig();
61+
cfg = make_trt(builder->createBuilderConfig());
6262

6363
for (auto p = settings.enabled_precisions.begin(); p != settings.enabled_precisions.end(); ++p) {
6464
switch (*p) {
@@ -91,11 +91,11 @@ ConversionCtx::ConversionCtx(BuilderSettings build_settings)
9191
if (settings.disable_tf32) {
9292
cfg->clearFlag(nvinfer1::BuilderFlag::kTF32);
9393
}
94-
94+
#if NV_TENSORRT_MAJOR > 7
9595
if (settings.sparse_weights) {
9696
cfg->setFlag(nvinfer1::BuilderFlag::kSPARSE_WEIGHTS);
9797
}
98-
98+
#endif
9999
if (settings.refit) {
100100
cfg->setFlag(nvinfer1::BuilderFlag::kREFIT);
101101
}
@@ -136,9 +136,6 @@ ConversionCtx::ConversionCtx(BuilderSettings build_settings)
136136
}
137137

138138
ConversionCtx::~ConversionCtx() {
139-
delete builder;
140-
delete net;
141-
delete cfg;
142139
for (auto ptr : builder_resources) {
143140
free(ptr);
144141
}
@@ -156,10 +153,19 @@ torch::jit::IValue* ConversionCtx::AssociateValueAndIValue(const torch::jit::Val
156153
}
157154

158155
std::string ConversionCtx::SerializeEngine() {
156+
#if NV_TENSORRT_MAJOR > 7
159157
auto serialized_network = builder->buildSerializedNetwork(*net, *cfg);
160158
if (!serialized_network) {
161159
TRTORCH_THROW_ERROR("Building serialized network failed in TensorRT");
162160
}
161+
#else
162+
auto engine = builder->buildEngineWithConfig(*net, *cfg);
163+
if (!engine) {
164+
TRTORCH_THROW_ERROR("Building TensorRT engine failed");
165+
}
166+
auto serialized_network = engine->serialize();
167+
engine->destroy();
168+
#endif
163169
auto engine_str = std::string((const char*)serialized_network->data(), serialized_network->size());
164170
return engine_str;
165171
}

core/conversion/conversionctx/ConversionCtx.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct BuilderSettings {
3232
bool strict_types = false;
3333
bool truncate_long_and_double = false;
3434
Device device;
35-
nvinfer1::EngineCapability capability = nvinfer1::EngineCapability::kSTANDARD;
35+
nvinfer1::EngineCapability capability = nvinfer1::EngineCapability::kDEFAULT;
3636
nvinfer1::IInt8Calibrator* calibrator = nullptr;
3737
uint64_t num_min_timing_iters = 2;
3838
uint64_t num_avg_timing_iters = 1;
@@ -56,9 +56,9 @@ struct ConversionCtx {
5656
uint64_t num_inputs = 0;
5757
uint64_t num_outputs = 0;
5858
bool input_is_dynamic = false;
59-
nvinfer1::IBuilder* builder;
60-
nvinfer1::INetworkDefinition* net;
61-
nvinfer1::IBuilderConfig* cfg;
59+
std::shared_ptr<nvinfer1::IBuilder> builder;
60+
std::shared_ptr<nvinfer1::INetworkDefinition> net;
61+
std::shared_ptr<nvinfer1::IBuilderConfig> cfg;
6262
std::set<nvinfer1::DataType> enabled_precisions;
6363
BuilderSettings settings;
6464
util::logging::TRTorchLogger logger;

core/conversion/converters/Weights.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include "core/conversion/conversionctx/ConversionCtx.h"
43
#include "core/util/prelude.h"
4+
#include "core/conversion/conversionctx/ConversionCtx.h"
55

66
namespace trtorch {
77
namespace core {
@@ -25,4 +25,4 @@ struct Weights {
2525
} // namespace converters
2626
} // namespace conversion
2727
} // namespace core
28-
} // namespace trtorch
28+
} // namespace trtorch

core/conversion/converters/impl/interpolate.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@ void resize_layer_size(
108108

109109
resize_layer->setResizeMode(mode);
110110
resize_layer->setName(util::node_info(n).c_str());
111-
111+
#if NV_TENSORRT_MAJOR < 8
112+
resize_layer->setAlignCorners(align_corners);
113+
#else
112114
if (align_corners) {
113115
resize_layer->setCoordinateTransformation(nvinfer1::ResizeCoordinateTransformation::kALIGN_CORNERS);
114116
}
117+
#endif
115118
auto layer_output = ctx->AssociateValueAndTensor(n->outputs()[0], resize_layer->getOutput(0));
116119

117120
LOG_DEBUG("Output tensor shape: " << layer_output->getDimensions());

core/conversion/converters/impl/quantization.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ namespace conversion {
88
namespace converters {
99
namespace impl {
1010
namespace {
11-
11+
12+
#if NV_TENSORRT_MAJOR > 7
1213
// clang-format off
1314
auto quantization_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns()
1415
.pattern({"aten::fake_quantize_per_tensor_affine(Tensor self, float scale, int zero_point, int quant_min, int quant_max) -> (Tensor)",
@@ -53,6 +54,7 @@ auto quantization_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns(
5354
return true;
5455
}});
5556
// clang-format on
57+
#endif
5658
} // namespace
5759
} // namespace impl
5860
} // namespace converters

core/runtime/TRTEngine.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ TRTEngine::TRTEngine(std::string mod_name, std::string serialized_engine, CudaDe
4141
device_info = cuda_device;
4242
set_cuda_device(device_info);
4343

44-
rt = std::shared_ptr<nvinfer1::IRuntime>(nvinfer1::createInferRuntime(util::logging::get_logger()));
44+
rt = make_trt(nvinfer1::createInferRuntime(util::logging::get_logger()));
4545

4646
name = slugify(mod_name);
4747

48-
cuda_engine = std::shared_ptr<nvinfer1::ICudaEngine>(
49-
rt->deserializeCudaEngine(serialized_engine.c_str(), serialized_engine.size()));
50-
TRTORCH_CHECK((cuda_engine != nullptr), "Unable to deserialize the TensorRT engine");
48+
cuda_engine = make_trt(rt->deserializeCudaEngine(serialized_engine.c_str(), serialized_engine.size()));
49+
TRTORCH_CHECK((cuda_engine.get() != nullptr), "Unable to deserialize the TensorRT engine");
5150

52-
exec_ctx = std::shared_ptr<nvinfer1::IExecutionContext>(cuda_engine->createExecutionContext());
51+
exec_ctx = make_trt(cuda_engine->createExecutionContext());
5352

5453
uint64_t inputs = 0;
5554
uint64_t outputs = 0;

core/runtime/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class DeviceList {
7474
std::string dump_list();
7575
};
7676

77+
7778
DeviceList get_available_device_list();
7879
const std::unordered_map<std::string, std::string>& get_dla_supported_SMs();
7980

core/util/trt_util.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88

99
namespace nvinfer1 {
1010

11+
#if NV_TENSORRT_MAJOR < 8
12+
template <class T>
13+
std::shared_ptr<T> make_trt(T* p) {
14+
return std::shared_ptr<T>(p, [](T* p){p->destroy();});
15+
}
16+
#else
17+
template <class T>
18+
std::shared_ptr<T> make_trt(T* p) {
19+
return std::shared_ptr<T>(p);
20+
}
21+
#endif
22+
1123
inline std::ostream& operator<<(std::ostream& os, const nvinfer1::TensorFormat& format) {
1224
switch (format) {
1325
case nvinfer1::TensorFormat::kLINEAR:
@@ -87,11 +99,11 @@ inline std::ostream& operator<<(std::ostream& stream, const nvinfer1::DeviceType
8799

88100
inline std::ostream& operator<<(std::ostream& stream, const nvinfer1::EngineCapability& cap) {
89101
switch (cap) {
90-
case nvinfer1::EngineCapability::kSTANDARD:
102+
case nvinfer1::EngineCapability::kDEFAULT:
91103
return stream << "standard";
92-
case nvinfer1::EngineCapability::kSAFETY:
104+
case nvinfer1::EngineCapability::kSAFE_GPU:
93105
return stream << "safety";
94-
case nvinfer1::EngineCapability::kDLA_STANDALONE:
106+
case nvinfer1::EngineCapability::kSAFE_DLA:
95107
return stream << "DLA standalone";
96108
default:
97109
return stream << "Unknown Engine Capability Setting";

cpp/api/src/compile_spec.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,14 @@ core::CompileSpec to_internal_compile_spec(CompileSpec external) {
387387

388388
switch (external.capability) {
389389
case CompileSpec::EngineCapability::kSAFETY:
390-
internal.convert_info.engine_settings.capability = nvinfer1::EngineCapability::kSAFETY;
390+
internal.convert_info.engine_settings.capability = nvinfer1::EngineCapability::kSAFE_GPU;
391391
break;
392392
case CompileSpec::EngineCapability::kDLA_STANDALONE:
393-
internal.convert_info.engine_settings.capability = nvinfer1::EngineCapability::kDLA_STANDALONE;
393+
internal.convert_info.engine_settings.capability = nvinfer1::EngineCapability::kSAFE_DLA;
394394
break;
395395
case CompileSpec::EngineCapability::kSTANDARD:
396396
default:
397-
internal.convert_info.engine_settings.capability = nvinfer1::EngineCapability::kSTANDARD;
397+
internal.convert_info.engine_settings.capability = nvinfer1::EngineCapability::kDEFAULT;
398398
}
399399

400400
internal.convert_info.engine_settings.device.gpu_id = external.device.gpu_id;

docker/Dockerfile.20.06

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)