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

Method infer

samples/sampleAlgorithmSelector/sampleAlgorithmSelector.cpp:631–680  ·  view source on GitHub ↗

\brief Runs the TensorRT inference engine for this sample. \details This function is the main execution function of the sample. It allocates the buffer, sets inputs, executes the engine, and verifies the output.

Source from the content-addressed store, hash-verified

629//! the buffer, sets inputs, executes the engine, and verifies the output.
630//!
631bool SampleAlgorithmSelector::infer()
632{
633 // Create RAII buffer manager object.
634 samplesCommon::BufferManager buffers(mEngine);
635
636 auto context = SampleUniquePtr<nvinfer1::IExecutionContext>(mEngine->createExecutionContext());
637 if (!context)
638 {
639 return false;
640 }
641
642 // Pick a random digit to try to infer.
643 srand(time(NULL));
644 int32_t const digit = rand() % 10;
645
646 // Read the input data into the managed buffers.
647 // There should be just 1 input tensor.
648 ASSERT(mParams.inputTensorNames.size() == 1);
649
650 if (!processInput(buffers, mParams.inputTensorNames[0], digit))
651 {
652 return false;
653 }
654 // Create CUDA stream for the execution of this inference.
655 cudaStream_t stream;
656 CHECK(cudaStreamCreate(&stream));
657
658 // Asynchronously copy data from host input buffers to device input buffers
659 buffers.copyInputToDeviceAsync(stream);
660
661 // Asynchronously enqueue the inference work
662 if (!context->enqueueV2(buffers.getDeviceBindings().data(), stream, nullptr))
663 {
664 return false;
665 }
666 // Asynchronously copy data from device output buffers to host output buffers.
667 buffers.copyOutputToHostAsync(stream);
668
669 // Wait for the work in the stream to complete.
670 CHECK(cudaStreamSynchronize(stream));
671
672 // Release stream.
673 CHECK(cudaStreamDestroy(stream));
674
675 // Check and print the output of the inference.
676 // There should be just one output tensor.
677 ASSERT(mParams.outputTensorNames.size() == 1);
678 bool outputCorrect = verifyOutput(buffers, mParams.outputTensorNames[0], digit);
679 return outputCorrect;
680}
681
682//!
683//! \brief Initializes members of the params struct using the command line args

Callers 1

mainFunction · 0.45

Calls 6

enqueueV2Method · 0.80
copyOutputToHostAsyncMethod · 0.80
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected