MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / setUpInference

Function setUpInference

samples/common/sampleInference.cpp:211–437  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

209}
210
211bool setUpInference(InferenceEnvironment& iEnv, InferenceOptions const& inference, SystemOptions const& system)
212{
213 int32_t device{};
214 cudaCheck(cudaGetDevice(&device));
215
216 cudaDeviceProp properties;
217 cudaCheck(cudaGetDeviceProperties(&properties, device));
218 // Use managed memory on integrated devices when transfers are skipped
219 // and when it is explicitly requested on the commandline.
220 bool useManagedMemory{(inference.skipTransfers && properties.integrated) || inference.useManaged};
221 using FillSafeBindings = FillBindingClosure<nvinfer1::safe::ICudaEngine, nvinfer1::safe::IExecutionContext>;
222 if (iEnv.safe)
223 {
224 ASSERT(sample::hasSafeRuntime());
225
226 auto* safeEngine = iEnv.engine.getSafe();
227 SMP_RETVAL_IF_FALSE(safeEngine != nullptr, "Got invalid safeEngine!", false, sample::gLogError);
228
229 // Release serialized blob to save memory space.
230 iEnv.engine.releaseBlob();
231
232 for (int32_t s = 0; s < inference.infStreams; ++s)
233 {
234 auto ec = safeEngine->createExecutionContext();
235 if (ec == nullptr)
236 {
237 sample::gLogError << "Unable to create execution context for stream " << s << "." << std::endl;
238 return false;
239 }
240 iEnv.safeContexts.emplace_back(ec);
241 iEnv.bindings.emplace_back(new Bindings(useManagedMemory));
242 }
243 int32_t const nbBindings = safeEngine->getNbBindings();
244 auto const* safeContext = iEnv.safeContexts.front().get();
245 // batch is set to 1 because safety only support explicit batch.
246 return FillSafeBindings(safeEngine, safeContext, inference.inputs, iEnv.bindings, 1, nbBindings)();
247 }
248
249 using FillStdBindings = FillBindingClosure<nvinfer1::ICudaEngine, nvinfer1::IExecutionContext>;
250
251 auto* engine = iEnv.engine.get();
252 SMP_RETVAL_IF_FALSE(engine != nullptr, "Got invalid engine!", false, sample::gLogError);
253
254 bool const hasDLA = system.DLACore >= 0;
255 if (engine->hasImplicitBatchDimension() && hasDLA && inference.batch != engine->getMaxBatchSize())
256 {
257 sample::gLogError << "When using DLA with an implicit batch engine, the inference batch size must be the same "
258 "as the engine's maximum batch size. Please specify the batch size by adding: '--batch="
259 << engine->getMaxBatchSize() << "' to your command." << std::endl;
260 return false;
261 }
262
263 // Release serialized blob to save memory space.
264 iEnv.engine.releaseBlob();
265
266 for (int32_t s = 0; s < inference.infStreams; ++s)
267 {
268 auto ec = engine->createExecutionContext();

Callers 2

mainFunction · 0.85

Calls 15

cudaCheckFunction · 0.85
hasSafeRuntimeFunction · 0.85
validateTensorNamesFunction · 0.85
loadFromFileFunction · 0.85
getSafeMethod · 0.80
releaseBlobMethod · 0.80
getNbBindingsMethod · 0.80
setNvtxVerbosityMethod · 0.80

Tested by

no test coverage detected