better rt inference

This commit is contained in:
Francesco Gatti
2017-08-21 12:10:17 +00:00
parent c63ac6b590
commit 0aa9de4ce8
3 changed files with 22 additions and 8 deletions
+2 -1
View File
@@ -32,6 +32,7 @@ public:
Do inferece
*/
dnnType* infer(dataDim_t &dim, dnnType* data);
void enqueue();
nvinfer1::ILayer* convert_layer(nvinfer1::ITensor *input, Layer *l);
nvinfer1::ILayer* convert_layer(nvinfer1::ITensor *input, Conv2d *l);
@@ -62,4 +63,4 @@ template<typename T> T readBUF(const char*& buffer)
}
}
#endif //NETWORKRT_H
#endif //NETWORKRT_H
+4
View File
@@ -139,6 +139,10 @@ dnnType* NetworkRT::infer(dataDim_t &dim, dnnType* data) {
return output;
}
void NetworkRT::enqueue() {
contextRT->enqueue(1, buffersRT, stream, nullptr);
}
ILayer* NetworkRT::convert_layer(ITensor *input, Layer *l) {
layerType_t type = l->getLayerType();
+16 -7
View File
@@ -1,24 +1,33 @@
#include<iostream>
#include "tkdnn.h"
#include <stdlib.h> /* srand, rand */
int main(int argc, char *argv[]) {
if(argc < 2 || !fileExist(argv[1]))
FatalError("unable to read serialRT file");
//always same test
srand (0);
//convert network to tensorRT
tkDNN::NetworkRT netRT(NULL, argv[1]);
tkDNN::dataDim_t dim = netRT.input_dim;
dnnType *data;
checkCuda(cudaMalloc(&data, dim.tot()*sizeof(dnnType)));
dnnType *input = new float[netRT.input_dim.tot()];
dnnType *output = new float[netRT.input_dim.tot()];
printCenteredTitle(" TENSORRT inference ", '=', 30); {
dim.print();
printCenteredTitle(" TENSORRT inference ", '=', 30);
for(int i=0; i<100; i++) {
for(int j=0; j<netRT.input_dim.tot(); j++)
input[j] = ((float) rand() / (RAND_MAX));
TIMER_START
data = netRT.infer(dim, data);
checkCuda( cudaMemcpyAsync(netRT.buffersRT[netRT.buf_input_idx], input,
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
dim.print();
}
return 0;