batch size > 1
This commit is contained in:
@@ -62,6 +62,7 @@ public:
|
|||||||
dataDim_t getOutputDim();
|
dataDim_t getOutputDim();
|
||||||
|
|
||||||
bool fp16, dla, int8;
|
bool fp16, dla, int8;
|
||||||
|
int maxBatchSize;
|
||||||
bool dontLoadWeights;
|
bool dontLoadWeights;
|
||||||
std::string fileImgList;
|
std::string fileImgList;
|
||||||
std::string fileLabelList;
|
std::string fileLabelList;
|
||||||
|
|||||||
@@ -74,11 +74,18 @@ public:
|
|||||||
NetworkRT(Network *net, const char *name);
|
NetworkRT(Network *net, const char *name);
|
||||||
virtual ~NetworkRT();
|
virtual ~NetworkRT();
|
||||||
|
|
||||||
|
int getMaxBatchSize() {
|
||||||
|
if(engineRT != nullptr)
|
||||||
|
return engineRT->getMaxBatchSize();
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Do inferece
|
Do inferece
|
||||||
*/
|
*/
|
||||||
dnnType* infer(dataDim_t &dim, dnnType* data);
|
dnnType* infer(dataDim_t &dim, dnnType* data);
|
||||||
void enqueue();
|
void enqueue(int batchSize = 1);
|
||||||
|
|
||||||
nvinfer1::ILayer* convert_layer(nvinfer1::ITensor *input, Layer *l);
|
nvinfer1::ILayer* convert_layer(nvinfer1::ITensor *input, Layer *l);
|
||||||
nvinfer1::ILayer* convert_layer(nvinfer1::ITensor *input, Conv2d *l);
|
nvinfer1::ILayer* convert_layer(nvinfer1::ITensor *input, Conv2d *l);
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public:
|
|||||||
|
|
||||||
virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override {
|
virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override {
|
||||||
|
|
||||||
std::cout<<this->n<<" "<<this->c<<" "<<this->h<<" "<<this->w<<" "<<this->stride_H<<" "<<this->stride_W<<" "<<this->winSize<<" "<<this->padding<<std::endl;
|
//std::cout<<this->n<<" "<<this->c<<" "<<this->h<<" "<<this->w<<" "<<this->stride_H<<" "<<this->stride_W<<" "<<this->winSize<<" "<<this->padding<<std::endl;
|
||||||
dnnType *srcData = (dnnType*)reinterpret_cast<const dnnType*>(inputs[0]);
|
dnnType *srcData = (dnnType*)reinterpret_cast<const dnnType*>(inputs[0]);
|
||||||
dnnType *dstData = reinterpret_cast<dnnType*>(outputs[0]);
|
dnnType *dstData = reinterpret_cast<dnnType*>(outputs[0]);
|
||||||
MaxPoolingForward(srcData, dstData, this->n, this->c, this->h, this->w, this->stride_H, this->stride_W, this->winSize, this->padding);
|
MaxPoolingForward(srcData, dstData, this->n, this->c, this->h, this->w, this->stride_H, this->stride_W, this->winSize, this->padding);
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ Network::Network(dataDim_t input_dim) {
|
|||||||
int8 = true;
|
int8 = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
maxBatchSize = 1;
|
||||||
|
if(const char* env_p = std::getenv("TKDNN_BATCHSIZE")) {
|
||||||
|
maxBatchSize = atoi(env_p);
|
||||||
|
}
|
||||||
if(const char* env_p = std::getenv("TKDNN_CALIB_IMG_PATH"))
|
if(const char* env_p = std::getenv("TKDNN_CALIB_IMG_PATH"))
|
||||||
fileImgList = env_p;
|
fileImgList = env_p;
|
||||||
|
|
||||||
|
|||||||
+15
-9
@@ -58,7 +58,7 @@ NetworkRT::NetworkRT(Network *net, const char *name) {
|
|||||||
dataDim_t dim = net->layers[0]->input_dim;
|
dataDim_t dim = net->layers[0]->input_dim;
|
||||||
dtRT = DataType::kFLOAT;
|
dtRT = DataType::kFLOAT;
|
||||||
|
|
||||||
builderRT->setMaxBatchSize(1);
|
builderRT->setMaxBatchSize(net->maxBatchSize);
|
||||||
builderRT->setMaxWorkspaceSize(1 << 30);
|
builderRT->setMaxWorkspaceSize(1 << 30);
|
||||||
|
|
||||||
if(net->fp16 && builderRT->platformHasFastFp16()) {
|
if(net->fp16 && builderRT->platformHasFastFp16()) {
|
||||||
@@ -133,6 +133,7 @@ NetworkRT::NetworkRT(Network *net, const char *name) {
|
|||||||
input->setName("out");
|
input->setName("out");
|
||||||
networkRT->markOutput(*input);
|
networkRT->markOutput(*input);
|
||||||
|
|
||||||
|
std::cout<<"Selected maxBatchSize: "<<builderRT->getMaxBatchSize()<<"\n";
|
||||||
std::cout<<"Building tensorRT cuda engine...\n";
|
std::cout<<"Building tensorRT cuda engine...\n";
|
||||||
#if NV_TENSORRT_MAJOR >= 6
|
#if NV_TENSORRT_MAJOR >= 6
|
||||||
engineRT = builderRT->buildEngineWithConfig(*networkRT, *configRT);
|
engineRT = builderRT->buildEngineWithConfig(*networkRT, *configRT);
|
||||||
@@ -181,9 +182,9 @@ NetworkRT::NetworkRT(Network *net, const char *name) {
|
|||||||
// create GPU buffers and a stream
|
// create GPU buffers and a stream
|
||||||
for(int i=0; i<engineRT->getNbBindings(); i++) {
|
for(int i=0; i<engineRT->getNbBindings(); i++) {
|
||||||
Dims dim = engineRT->getBindingDimensions(i);
|
Dims dim = engineRT->getBindingDimensions(i);
|
||||||
checkCuda(cudaMalloc(&buffersRT[i], dim.d[0]*dim.d[1]*dim.d[2]*sizeof(dnnType)));
|
checkCuda(cudaMalloc(&buffersRT[i], engineRT->getMaxBatchSize()*dim.d[0]*dim.d[1]*dim.d[2]*sizeof(dnnType)));
|
||||||
}
|
}
|
||||||
checkCuda(cudaMalloc(&output, output_dim.tot()*sizeof(dnnType)));
|
checkCuda(cudaMalloc(&output, engineRT->getMaxBatchSize()*output_dim.tot()*sizeof(dnnType)));
|
||||||
checkCuda(cudaStreamCreate(&stream));
|
checkCuda(cudaStreamCreate(&stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,19 +193,24 @@ NetworkRT::~NetworkRT() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dnnType* NetworkRT::infer(dataDim_t &dim, dnnType* data) {
|
dnnType* NetworkRT::infer(dataDim_t &dim, dnnType* data) {
|
||||||
|
int batches = dim.n;
|
||||||
|
if(batches > getMaxBatchSize()) {
|
||||||
|
FatalError("input batch size too large");
|
||||||
|
}
|
||||||
|
|
||||||
checkCuda(cudaMemcpyAsync(buffersRT[buf_input_idx], data, input_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToDevice, stream));
|
checkCuda(cudaMemcpyAsync(buffersRT[buf_input_idx], data, batches*input_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToDevice, stream));
|
||||||
contextRT->enqueue(1, buffersRT, stream, nullptr);
|
contextRT->enqueue(batches, buffersRT, stream, nullptr);
|
||||||
checkCuda(cudaMemcpyAsync(output, buffersRT[buf_output_idx], output_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToDevice, stream));
|
checkCuda(cudaMemcpyAsync(output, buffersRT[buf_output_idx], batches*output_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToDevice, stream));
|
||||||
cudaStreamSynchronize(stream);
|
checkCuda(cudaStreamSynchronize(stream));
|
||||||
|
|
||||||
dim = output_dim;
|
dim = output_dim;
|
||||||
|
dim.n = batches;
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkRT::enqueue() {
|
void NetworkRT::enqueue(int batchSize) {
|
||||||
contextRT->enqueue(1, buffersRT, stream, nullptr);
|
contextRT->enqueue(batchSize, buffersRT, stream, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ILayer* NetworkRT::convert_layer(ITensor *input, Layer *l) {
|
ILayer* NetworkRT::convert_layer(ITensor *input, Layer *l) {
|
||||||
|
|||||||
@@ -2,31 +2,42 @@
|
|||||||
#include "tkdnn.h"
|
#include "tkdnn.h"
|
||||||
#include <stdlib.h> /* srand, rand */
|
#include <stdlib.h> /* srand, rand */
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if(argc < 2 || !fileExist(argv[1]))
|
if(argc < 2 || !fileExist(argv[1]))
|
||||||
FatalError("unable to read serialRT file");
|
FatalError("unable to read serialRT file");
|
||||||
|
|
||||||
|
int BATCH_SIZE = 1;
|
||||||
|
if(argc >2)
|
||||||
|
BATCH_SIZE = atoi(argv[2]);
|
||||||
|
|
||||||
//always same test
|
//always same test
|
||||||
srand (0);
|
srand (0);
|
||||||
|
|
||||||
//convert network to tensorRT
|
//convert network to tensorRT
|
||||||
tk::dnn::NetworkRT netRT(NULL, argv[1]);
|
tk::dnn::NetworkRT netRT(NULL, argv[1]);
|
||||||
|
|
||||||
|
tk::dnn::dataDim_t idim = netRT.input_dim;
|
||||||
|
tk::dnn::dataDim_t odim = netRT.output_dim;
|
||||||
|
idim.n = BATCH_SIZE;
|
||||||
|
odim.n = BATCH_SIZE;
|
||||||
|
dnnType *input = new float[idim.tot()];
|
||||||
|
dnnType *output = new float[odim.tot()];
|
||||||
|
dnnType *input_d;
|
||||||
|
checkCuda( cudaMalloc(&input_d, idim.tot()*sizeof(dnnType)));
|
||||||
|
|
||||||
dnnType *input = new float[netRT.input_dim.tot()];
|
std::cout<<"Testing with batchsize: "<<BATCH_SIZE<<"\n";
|
||||||
dnnType *output = new float[netRT.input_dim.tot()];
|
|
||||||
|
|
||||||
printCenteredTitle(" TENSORRT inference ", '=', 30);
|
printCenteredTitle(" TENSORRT inference ", '=', 30);
|
||||||
for(int i=0; i<100; i++) {
|
for(int i=0; i<10; i++) {
|
||||||
for(int j=0; j<netRT.input_dim.tot(); j++)
|
for(int j=0; j<idim.tot(); j++) {
|
||||||
input[j] = ((float) rand() / (RAND_MAX));
|
input[j] = ((float) rand() / (RAND_MAX));
|
||||||
|
}
|
||||||
|
checkCuda(cudaMemcpy(input_d, input, idim.tot()*sizeof(dnnType), cudaMemcpyHostToDevice));
|
||||||
|
|
||||||
|
tk::dnn::dataDim_t dim = idim;
|
||||||
TIMER_START
|
TIMER_START
|
||||||
checkCuda( cudaMemcpyAsync(netRT.buffersRT[netRT.buf_input_idx], input,
|
netRT.infer(dim, input_d);
|
||||||
netRT.input_dim.tot()*sizeof(float), cudaMemcpyHostToDevice, netRT.stream));
|
|
||||||
netRT.enqueue();
|
|
||||||
checkCuda( cudaMemcpyAsync(output, netRT.buffersRT[netRT.buf_output_idx],
|
|
||||||
netRT.output_dim.tot()*sizeof(float), cudaMemcpyDeviceToHost, netRT.stream));
|
|
||||||
cudaStreamSynchronize(netRT.stream);
|
|
||||||
TIMER_STOP
|
TIMER_STOP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user