NVIDIA DeepStream SDK API Reference

9.2 Release
sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/infer/engine_builder.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef ENGINE_BUILDER_H
19 #define ENGINE_BUILDER_H
20 
21 #include <string>
22 #include <fstream>
23 #include <iostream>
24 #include <memory>
25 #include <NvInfer.h>
26 #include <NvOnnxParser.h>
27 #include <cuda_runtime.h>
28 #include "../utils/log_utils.h"
29 
30 
31 // Define TensorRT 10.3 constants if needed
32 #define TRT_MAJOR_VERSION 10
33 #define TRT_MINOR_VERSION 3
34 #define TRT_PATCH_VERSION 0
35 
36 // Smart pointer types for TensorRT objects to ensure proper cleanup
37 struct TRTDeleter {
38  template <typename T>
39  void operator()(T* obj) const {
40  if (obj) {
41  delete obj;
42  }
43  }
44 };
45 
46 template <typename T>
47 using TRTUniquePtr = std::unique_ptr<T, TRTDeleter>;
48 
49 // Logger for TensorRT with verbosity control
50 class Logger : public nvinfer1::ILogger {
51 public:
52  // Default constructor sets verbosity to ERROR (only show errors and fatals)
53  Logger(Severity verbosity = Severity::kERROR) : mVerbosity(verbosity) {}
54 
55  void log(Severity severity, const char* msg) noexcept override {
56  // Only log messages at or above the configured verbosity level
57  if (severity <= mVerbosity) {
58  // Add a prefix to identify the source of the message
59  const char* severityStr = "";
60  switch (severity) {
61  case Severity::kINTERNAL_ERROR: severityStr = "[F] "; break;
62  case Severity::kERROR: severityStr = "[E] "; break;
63  case Severity::kWARNING: severityStr = "[W] "; break;
64  case Severity::kINFO: severityStr = "[I] "; break;
65  case Severity::kVERBOSE: severityStr = "[V] "; break;
66  }
67  std::cerr << severityStr << msg << std::endl;
68  }
69  }
70 
71 private:
72  Severity mVerbosity;
73 } static logger;
74 
88  const std::string& onnxFilePath,
89  const std::string& engineFilePath,
90  int batchSize,
91  int numSensors,
92  bool fp16Mode,
93  PrettyLogger* logger = nullptr
94 );
95 
96 #endif // ENGINE_BUILDER_H
PrettyLogger
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/log_utils.h:57
TRTUniquePtr
std::unique_ptr< T, TRTDeleter > TRTUniquePtr
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/infer/engine_builder.h:47
TRTDeleter::operator()
void operator()(T *obj) const
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/infer/engine_builder.h:39
buildEngineFromOnnx
bool buildEngineFromOnnx(const std::string &onnxFilePath, const std::string &engineFilePath, int batchSize, int numSensors, bool fp16Mode, PrettyLogger *logger=nullptr)
Builds a TensorRT engine from an ONNX model and saves it to the specified file path.
logger
Logger logger
logs::Severity
Severity
Severity Levels assigned to log events, based on Log Data Model, with the addition of kInvalid (mappe...
Definition: includes/opentelemetry/logs/severity.h:22
Logger::Logger
Logger(Severity verbosity=Severity::kERROR)
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/infer/engine_builder.h:53
Logger
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/infer/engine_builder.h:50
TRTDeleter
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/infer/engine_builder.h:37
Logger::log
void log(Severity severity, const char *msg) noexcept override
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/infer/engine_builder.h:55