78 fprintf(stderr,
"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
123 BASE_GPU_CHECK(cudaSetDevice(
gpu_id_));
124 BASE_GPU_CHECK(cudaStreamCreate(&stream_));
131 if (engine_ ==
nullptr) {
132 AERROR <<
"Fail to load obstacle ONNX model";
137 context_ = engine_->createExecutionContext();
138 if (context_ ==
nullptr) {
139 AERROR <<
"Fail to create Exceution Context";
142 for (
const auto& name : output_names_) {
143 auto iter = shapes.find(name);
144 if (iter != shapes.end()) {
145 auto blob = std::make_shared<Blob<float>>(iter->second);
146 blobs_.emplace(name, blob);
150 for (
const auto& name : input_names_) {
151 auto iter = shapes.find(name);
152 if (iter != shapes.end()) {
153 auto blob = std::make_shared<Blob<float>>(iter->second);
154 blobs_.emplace(name, blob);
157 buffers_.resize(input_names_.size() + output_names_.size());
166 nvinfer1::IOptimizationProfile* profile,
170 for (
int i = 0; i < network_->getNbInputs(); ++i) {
171 nvinfer1::ITensor* nv_input = network_->getInput(i);
172 const char* nv_input_name = nv_input->getName();
173 nvinfer1::Dims nv_input_dims = nv_input->getDimensions();
174 auto it = input_shapes.find(nv_input_name);
175 if (it != input_shapes.end()) {
176 CHECK_EQ(nv_input_dims.nbDims, input_shapes.at(nv_input_name)->num_axes());
178 AINFO <<
"nv_input_name: " << nv_input_name;
179 AINFO <<
"nv_input_dims: ";
180 AINFO <<
"\t" << nv_input_dims.d[0];
181 if (nv_input_dims.d[0] > 0) {
185 for (
int j = 1; j < nv_input_dims.nbDims; ++j) {
186 AINFO <<
"\t" << nv_input_dims.d[j];
187 if (it != input_shapes.end()) {
190 CHECK(nv_input_dims.d[j] < 0 || nv_input_dims.d[j] == it->second->shape(j));
191 nv_input_dims.d[j] = it->second->shape(j);
194 CHECK(nv_input_dims.d[j] > 0);
198 nv_input_dims.d[0] = 1;
199 profile->setDimensions(nv_input_name, nvinfer1::OptProfileSelector::kMIN, nv_input_dims);
200 nv_input_dims.d[0] = std::max(1,
static_cast<int>(
max_batch_size_ / 2));
201 profile->setDimensions(nv_input_name, nvinfer1::OptProfileSelector::kOPT, nv_input_dims);
203 profile->setDimensions(nv_input_name, nvinfer1::OptProfileSelector::kMAX, nv_input_dims);
209 AERROR <<
"must use gpu mode";
214 network_ = builder_->createNetworkV2(
215 1U <<
static_cast<int>(nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH));
220 const char* onnx_filename = model_file.c_str();
222 if (!parser->parseFromFile(onnx_filename, verbosity)) {
223 AERROR <<
"Failure while parsing ONNX file: " << onnx_filename;
229 nvinfer1::IBuilderConfig* config = builder_->createBuilderConfig();
230 config->setMaxWorkspaceSize(1 << 30);
231 bool use_fp16 = builder_->platformHasFastFp16();
233 config->setFlag(nvinfer1::BuilderFlag::kFP16);
235 config->setFlag(nvinfer1::BuilderFlag::kDEBUG);
237 auto trt_cache_path = model_file +
".trt.engine";
240 nvinfer1::IOptimizationProfile* profile = builder_->createOptimizationProfile();
242 config->addOptimizationProfile(profile);
244 engine_ = builder_->buildEngineWithConfig(*network_, *config);
245 nvinfer1::IHostMemory* ser_mem = engine_->serialize();
248 nvinfer1::IHostMemory* serialized_model(ser_mem);
249 std::ofstream
f(trt_cache_path, std::ios::binary);
251 f.write(
reinterpret_cast<char*
>(serialized_model->data()), serialized_model->size());
252 serialized_model->destroy();
255 AINFO <<
"Loading TensorRT engine from serialized model file...";
256 std::ifstream planFile(trt_cache_path);
258 if (!planFile.is_open()) {
259 AERROR <<
"Could not open serialized model";
264 AINFO <<
"success open serialized model";
265 std::stringstream planBuffer;
266 planBuffer << planFile.rdbuf();
267 std::string plan = planBuffer.str();
268 nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(
onnx_gLogger);
270 engine_ = runtime->deserializeCudaEngine(
static_cast<const void*
>(plan.data()), plan.size(),
nullptr);
296 BASE_GPU_CHECK(cudaSetDevice(
gpu_id_));
297 BASE_GPU_CHECK(cudaStreamSynchronize(stream_));
298 CHECK(input_names_.size() > 0 && blobs_.size() > 0);
299 auto input_shape = blobs_[input_names_[0]]->shape();
300 kBatchSize = input_shape[0];
301 if (kBatchSize == 0) {
302 AINFO <<
"batch size is zero.";
309 for (
auto name : input_names_) {
311 if (blob !=
nullptr) {
312 int32_t index = engine_->getBindingIndex(name.c_str());
315 batch_size = blob->shape(0);
317 buffers_[index] =
const_cast<float*
>(blob->gpu_data());
320 nvinfer1::Dims dims = network_->getInput(index)->getDimensions();
321 dims.d[0] = batch_size;
322 context_->setOptimizationProfile(0);
323 context_->setBindingDimensions(index, dims);
328 for (
auto name : output_names_) {
330 if (blob !=
nullptr) {
331 blob->mutable_gpu_data();
332 int32_t index = engine_->getBindingIndex(name.c_str());
333 buffers_[index] = blob->mutable_gpu_data();
338 std::lock_guard<std::mutex> lock(mutex_);
339 context_->enqueueV2(&buffers_[0], stream_,
nullptr);
342 BASE_GPU_CHECK(cudaStreamSynchronize(stream_));
343 for (
auto name : output_names_) {
345 if (blob !=
nullptr) {
346 blob->mutable_gpu_data();