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

Method constructNetwork

samples/sampleCharRNN/sampleCharRNN.cpp:773–826  ·  view source on GitHub ↗

\brief Create full model using the TensorRT network definition API and build the engine. \param weightMap Map that contains all the weights required by the model. \param modelStream The stream within which the engine is serialized once built.

Source from the content-addressed store, hash-verified

771//! \param modelStream The stream within which the engine is serialized once built.
772//!
773void SampleCharRNNBase::constructNetwork(SampleUniquePtr<nvinfer1::IBuilder>& builder,
774 SampleUniquePtr<nvinfer1::INetworkDefinition>& network, SampleUniquePtr<nvinfer1::IBuilderConfig>& config)
775{
776 // add RNNv2 layer and set its parameters
777 auto rnn = addLSTMLayers(network);
778
779 // Transpose FC weights since TensorFlow's weights are transposed when compared to TensorRT
780 ASSERT(utils::transposeSubBuffers((void*) mWeightMap[mParams.weightNames.FCW_NAME].values,
781 nvinfer1::DataType::kFLOAT, 1, mParams.hiddenSize, mParams.vocabSize));
782
783 // add Constant layers for fully connected weights
784 auto fcwts = network->addConstant(
785 nvinfer1::Dims2(mParams.vocabSize, mParams.hiddenSize), mWeightMap[mParams.weightNames.FCW_NAME]);
786
787 // Add matrix multiplication layer for multiplying rnn output with FC weights
788 auto matrixMultLayer = network->addMatrixMultiply(
789 *fcwts->getOutput(0), MatrixOperation::kNONE, *rnn->getOutput(0), MatrixOperation::kTRANSPOSE);
790 ASSERT(matrixMultLayer != nullptr);
791 matrixMultLayer->getOutput(0)->setName("Matrix Multiplicaton output");
792
793 // Add elementwise layer for adding bias
794 auto fcbias = network->addConstant(nvinfer1::Dims2(mParams.vocabSize, 1), mWeightMap[mParams.weightNames.FCB_NAME]);
795 auto addBiasLayer = network->addElementWise(
796 *matrixMultLayer->getOutput(0), *fcbias->getOutput(0), nvinfer1::ElementWiseOperation::kSUM);
797 ASSERT(addBiasLayer != nullptr);
798 addBiasLayer->getOutput(0)->setName("Add Bias output");
799
800 // Add TopK layer to determine which character has highest probability.
801 int reduceAxis = 0x1; // reduce across vocab axis
802 auto pred = network->addTopK(*addBiasLayer->getOutput(0), nvinfer1::TopKOperation::kMAX, 1, reduceAxis);
803 ASSERT(pred != nullptr);
804 pred->getOutput(1)->setName(mParams.bindingNames.OUTPUT_BLOB_NAME);
805
806 // Mark the outputs for the network
807 network->markOutput(*pred->getOutput(1));
808 pred->getOutput(1)->setType(nvinfer1::DataType::kINT32);
809
810 sample::gLogInfo << "Done constructing network..." << std::endl;
811
812 SampleUniquePtr<IHostMemory> plan{builder->buildSerializedNetwork(*network, *config)};
813 if (!plan)
814 {
815 return;
816 }
817
818 mRuntime = std::shared_ptr<nvinfer1::IRuntime>(createInferRuntime(sample::gLogger.getTRTLogger()));
819 if (!mRuntime)
820 {
821 return;
822 }
823
824 mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(
825 mRuntime->deserializeCudaEngine(plan->data(), plan->size()), samplesCommon::InferDeleter());
826}
827
828//!
829//! \brief Runs the TensorRT inference engine for this sample

Callers

nothing calls this directly

Calls 15

Dims2Class · 0.85
createInferRuntimeFunction · 0.85
addConstantMethod · 0.80
addMatrixMultiplyMethod · 0.80
addElementWiseMethod · 0.80
addTopKMethod · 0.80
markOutputMethod · 0.80
setTypeMethod · 0.80
deserializeCudaEngineMethod · 0.80
InferDeleterClass · 0.50
getOutputMethod · 0.45

Tested by

no test coverage detected