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

Method infer

samples/sampleCharRNN/sampleCharRNN.cpp:834–915  ·  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

832//! allocates the buffer, sets inputs, executes the engine, and verifies the output.
833//!
834bool SampleCharRNNBase::infer()
835{
836 // Create RAII buffer manager object
837 samplesCommon::BufferManager buffers(mEngine, mParams.useILoop ? 0 : mParams.batchSize);
838
839 auto context = SampleUniquePtr<nvinfer1::IExecutionContext>(mEngine->createExecutionContext());
840
841 if (!context)
842 {
843 return false;
844 }
845
846 // Select a random seed string.
847 srand(unsigned(time(nullptr)));
848 int sentenceIndex = rand() % mParams.inputSentences.size();
849 std::string inputSentence = mParams.inputSentences[sentenceIndex];
850 std::string expected = mParams.outputSentences[sentenceIndex];
851 std::string genstr;
852
853 sample::gLogInfo << "RNN warmup sentence: " << inputSentence << std::endl;
854 sample::gLogInfo << "Expected output: " << expected << std::endl;
855
856 // create stream for trt execution
857 cudaStream_t stream;
858 CHECK(cudaStreamCreate(&stream));
859
860 // Set sequence lengths to maximum
861 int* sequenceLengthIn
862 = reinterpret_cast<int32_t*>(buffers.getHostBuffer(mParams.bindingNames.SEQ_LEN_IN_BLOB_NAME));
863 auto sequenceLengthTensorSize = buffers.size(mParams.bindingNames.SEQ_LEN_IN_BLOB_NAME);
864 std::fill_n(sequenceLengthIn, sequenceLengthTensorSize / sizeof(mParams.seqSize), mParams.seqSize);
865
866 // Initialize hiddenIn and cellIn tensors to zero before seeding
867 void* hiddenIn = buffers.getHostBuffer(mParams.bindingNames.HIDDEN_IN_BLOB_NAME);
868 auto hiddenTensorSize = buffers.size(mParams.bindingNames.HIDDEN_IN_BLOB_NAME);
869
870 void* cellIn = buffers.getHostBuffer(mParams.bindingNames.CELL_IN_BLOB_NAME);
871 auto cellTensorSize = buffers.size(mParams.bindingNames.CELL_IN_BLOB_NAME);
872
873 std::memset(hiddenIn, 0, hiddenTensorSize);
874 std::memset(cellIn, 0, cellTensorSize);
875
876 // Seed the RNN with the input sentence.
877 for (auto& a : inputSentence)
878 {
879 SampleCharRNNBase::copyEmbeddingToInput(buffers, a);
880
881 if (!SampleCharRNNBase::stepOnce(buffers, context, stream))
882 {
883 return false;
884 }
885
886 SampleCharRNNBase::copyRNNOutputsToInputs(buffers);
887 genstr.push_back(a);
888 }
889
890 // Extract first predicted character
891 uint32_t predIdx = *reinterpret_cast<uint32_t*>(buffers.getHostBuffer(mParams.bindingNames.OUTPUT_BLOB_NAME));

Callers 1

mainFunction · 0.45

Calls 3

sizeMethod · 0.45
getHostBufferMethod · 0.45

Tested by

no test coverage detected