| 253 | |
| 254 | template <typename T> |
| 255 | bool Engine<T>::loadNetwork(std::string trtModelPath, const std::array<float, 3> &subVals, const std::array<float, 3> &divVals, |
| 256 | bool normalize) { |
| 257 | m_subVals = subVals; |
| 258 | m_divVals = divVals; |
| 259 | m_normalize = normalize; |
| 260 | |
| 261 | // Read the serialized model from disk |
| 262 | if (!Util::doesFileExist(trtModelPath)) { |
| 263 | std::cout << "Error, unable to read TensorRT model at path: " + trtModelPath << std::endl; |
| 264 | return false; |
| 265 | } else { |
| 266 | std::cout << "Loading TensorRT engine file at path: " << trtModelPath << std::endl; |
| 267 | } |
| 268 | |
| 269 | std::ifstream file(trtModelPath, std::ios::binary | std::ios::ate); |
| 270 | std::streamsize size = file.tellg(); |
| 271 | file.seekg(0, std::ios::beg); |
| 272 | |
| 273 | std::vector<char> buffer(size); |
| 274 | if (!file.read(buffer.data(), size)) { |
| 275 | throw std::runtime_error("Unable to read engine file"); |
| 276 | } |
| 277 | |
| 278 | // Create a runtime to deserialize the engine file. |
| 279 | m_runtime = std::unique_ptr<nvinfer1::IRuntime>{nvinfer1::createInferRuntime(m_logger)}; |
| 280 | if (!m_runtime) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | // Set the device index |
| 285 | auto ret = cudaSetDevice(m_options.deviceIndex); |
| 286 | if (ret != 0) { |
| 287 | int numGPUs; |
| 288 | cudaGetDeviceCount(&numGPUs); |
| 289 | auto errMsg = "Unable to set GPU device index to: " + std::to_string(m_options.deviceIndex) + ". Note, your device has " + |
| 290 | std::to_string(numGPUs) + " CUDA-capable GPU(s)."; |
| 291 | throw std::runtime_error(errMsg); |
| 292 | } |
| 293 | |
| 294 | // Create an engine, a representation of the optimized model. |
| 295 | m_engine = std::unique_ptr<nvinfer1::ICudaEngine>(m_runtime->deserializeCudaEngine(buffer.data(), buffer.size())); |
| 296 | if (!m_engine) { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | // The execution context contains all of the state associated with a |
| 301 | // particular invocation |
| 302 | m_context = std::unique_ptr<nvinfer1::IExecutionContext>(m_engine->createExecutionContext()); |
| 303 | if (!m_context) { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | // Storage for holding the input and output buffers |
| 308 | // This will be passed to TensorRT for inference |
| 309 | clearGpuBuffers(); |
| 310 | m_buffers.resize(m_engine->getNbIOTensors()); |
| 311 | |
| 312 | m_outputLengths.clear(); |
no test coverage detected