Repository navigation
Expand file tree
/
Copy pathtrt_engine.cpp
More file actions
174 lines (150 loc) · 5.14 KB
/
Copy pathtrt_engine.cpp
File metadata and controls
174 lines (150 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "vrt/trt_engine.h"
#include <NvInferVersion.h>
#include <cstdio>
#include <fstream>
#include <iterator>
#include <stdexcept>
#include <utility>
#include "vrt/cuda_check.h"
namespace vrt {
namespace {
class Logger : public nvinfer1::ILogger {
public:
void log(Severity severity, const char* msg) noexcept override {
if (severity <= Severity::kWARNING) std::fprintf(stderr, "[TensorRT] %s\n", msg);
}
};
Logger& logger() {
static Logger instance;
return instance;
}
std::vector<char> read_file(const std::string& path) {
std::ifstream f(path, std::ios::binary);
if (!f) throw std::runtime_error("cannot open " + path);
return {std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()};
}
} // namespace
std::size_t dtype_size(nvinfer1::DataType t) {
using nvinfer1::DataType;
switch (t) {
case DataType::kFLOAT:
return 4;
case DataType::kHALF:
return 2;
case DataType::kBF16:
return 2;
case DataType::kINT8:
return 1;
case DataType::kUINT8:
return 1;
case DataType::kINT32:
return 4;
case DataType::kINT64:
return 8;
case DataType::kBOOL:
return 1;
default:
throw std::runtime_error("unsupported tensor data type");
}
}
const char* dtype_name(nvinfer1::DataType t) {
using nvinfer1::DataType;
switch (t) {
case DataType::kFLOAT:
return "float32";
case DataType::kHALF:
return "float16";
case DataType::kBF16:
return "bfloat16";
case DataType::kINT8:
return "int8";
case DataType::kUINT8:
return "uint8";
case DataType::kINT32:
return "int32";
case DataType::kINT64:
return "int64";
case DataType::kBOOL:
return "bool";
default:
return "other";
}
}
std::string tensorrt_version() {
return std::to_string(NV_TENSORRT_MAJOR) + "." + std::to_string(NV_TENSORRT_MINOR) + "." +
std::to_string(NV_TENSORRT_PATCH);
}
std::shared_ptr<TrtModel> TrtModel::load(const std::string& engine_path) {
std::shared_ptr<TrtModel> m(new TrtModel());
m->runtime_.reset(nvinfer1::createInferRuntime(logger()));
if (!m->runtime_) throw std::runtime_error("createInferRuntime failed");
const std::vector<char> blob = read_file(engine_path);
m->engine_.reset(m->runtime_->deserializeCudaEngine(blob.data(), blob.size()));
if (!m->engine_) throw std::runtime_error("failed to deserialize " + engine_path);
nvinfer1::ICudaEngine& e = *m->engine_;
for (int i = 0; i < e.getNbIOTensors(); ++i) {
TensorInfo t;
t.name = e.getIOTensorName(i);
t.dtype = e.getTensorDataType(t.name.c_str());
t.dims = e.getTensorShape(t.name.c_str());
t.is_input = e.getTensorIOMode(t.name.c_str()) == nvinfer1::TensorIOMode::kINPUT;
t.count = 1;
for (int d = 0; d < t.dims.nbDims; ++d) {
if (t.dims.d[d] < 0)
throw std::runtime_error("dynamic shape on " + t.name + " not supported");
t.count *= static_cast<std::size_t>(t.dims.d[d]);
}
t.bytes = t.count * dtype_size(t.dtype);
m->tensors_.push_back(t);
}
return m;
}
TrtEngine::TrtEngine(const std::string& engine_path) : TrtEngine(TrtModel::load(engine_path)) {}
TrtEngine::TrtEngine(std::shared_ptr<TrtModel> model) : model_(std::move(model)) {
context_.reset(model_->engine().createExecutionContext());
if (!context_) throw std::runtime_error("createExecutionContext failed");
}
TrtEngine::~TrtEngine() {
if (graph_exec_) cudaGraphExecDestroy(graph_exec_);
if (graph_) cudaGraphDestroy(graph_);
}
const TensorInfo& TrtEngine::input() const {
for (const auto& t : tensors())
if (t.is_input) return t;
throw std::runtime_error("engine has no input");
}
const TensorInfo& TrtEngine::output() const {
for (const auto& t : tensors())
if (!t.is_input) return t;
throw std::runtime_error("engine has no output");
}
void TrtEngine::bind(const std::string& name, void* device_ptr) {
if (!context_->setTensorAddress(name.c_str(), device_ptr))
throw std::runtime_error("setTensorAddress failed for " + name);
}
void TrtEngine::enqueue(cudaStream_t stream) {
if (graph_exec_) {
VRT_CUDA_CHECK(cudaGraphLaunch(graph_exec_, stream));
} else if (!context_->enqueueV3(stream)) {
throw std::runtime_error("enqueueV3 failed");
}
}
void TrtEngine::capture_graph(cudaStream_t stream) {
if (graph_exec_) return;
// One ordinary run first: TensorRT does lazy setup on the first enqueue (allocations,
// stream/event creation) that is not allowed inside a capture.
if (!context_->enqueueV3(stream)) throw std::runtime_error("enqueueV3 failed");
VRT_CUDA_CHECK(cudaStreamSynchronize(stream));
VRT_CUDA_CHECK(cudaStreamBeginCapture(stream, cudaStreamCaptureModeThreadLocal));
const bool ok = context_->enqueueV3(stream);
cudaGraph_t graph = nullptr;
const cudaError_t end = cudaStreamEndCapture(stream, &graph);
if (!ok || end != cudaSuccess) {
if (graph) cudaGraphDestroy(graph);
throw std::runtime_error(std::string("capturing enqueueV3 into a CUDA graph failed: ") +
(ok ? cudaGetErrorString(end) : "enqueueV3 returned false"));
}
graph_ = graph;
VRT_CUDA_CHECK(cudaGraphInstantiate(&graph_exec_, graph_, 0));
}
} // namespace vrt