forked from zerollzeng/tiny-tensorrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInt8EntropyCalibrator.cpp
81 lines (65 loc) · 2.59 KB
/
Int8EntropyCalibrator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* @Description: int8 entrophy calibrator 2
* @Author: zengren
* @Date: 2019-08-21 16:52:06
* @LastEditTime: 2019-08-22 17:04:49
* @LastEditors: Please set LastEditors
*/
#include "Int8EntropyCalibrator.h"
#include <fstream>
#include <iterator>
#include <cassert>
#include <string.h>
#include <algorithm>
Int8EntropyCalibrator::Int8EntropyCalibrator(int BatchSize,const std::vector<std::vector<float>>& data,
const std::string& CalibDataName /*= ""*/,bool readCache /*= true*/)
: mCalibDataName(CalibDataName),mBatchSize(BatchSize),mReadCache(readCache)
{
mDatas.reserve(data.size());
mDatas = data;
mInputCount = BatchSize * data[0].size();
mCurBatchData = new float[mInputCount];
mCurBatchIdx = 0;
CUDA_CHECK(cudaMalloc(&mDeviceInput, mInputCount * sizeof(float)));
}
Int8EntropyCalibrator::~Int8EntropyCalibrator()
{
CUDA_CHECK(cudaFree(mDeviceInput));
if(mCurBatchData)
delete[] mCurBatchData;
}
bool Int8EntropyCalibrator::getBatch(void* bindings[], const char* names[], int nbBindings)
{
std::cout << "name: " << names[0] << "nbBindings: " << nbBindings << std::endl;
if (mCurBatchIdx + mBatchSize > int(mDatas.size()))
return false;
float* ptr = mCurBatchData;
size_t imgSize = mInputCount / mBatchSize;
auto iter = mDatas.begin() + mCurBatchIdx;
std::for_each(iter, iter + mBatchSize, [=,&ptr](std::vector<float>& val){
assert(imgSize == val.size());
memcpy(ptr,val.data(),imgSize*sizeof(float));
ptr += imgSize;
});
CUDA_CHECK(cudaMemcpy(mDeviceInput, mCurBatchData, mInputCount * sizeof(float), cudaMemcpyHostToDevice));
//std::cout << "input name " << names[0] << std::endl;
bindings[0] = mDeviceInput;
std::cout << "load batch " << mCurBatchIdx << " to " << mCurBatchIdx + mBatchSize - 1 << std::endl;
mCurBatchIdx += mBatchSize;
return true;
}
const void* Int8EntropyCalibrator::readCalibrationCache(size_t& length)
{
mCalibrationCache.clear();
std::ifstream input(mCalibDataName+".calib", std::ios::binary);
input >> std::noskipws;
if (mReadCache && input.good())
std::copy(std::istream_iterator<char>(input), std::istream_iterator<char>(), std::back_inserter(mCalibrationCache));
length = mCalibrationCache.size();
return length ? &mCalibrationCache[0] : nullptr;
}
void Int8EntropyCalibrator::writeCalibrationCache(const void* cache, size_t length)
{
std::ofstream output(mCalibDataName+".calib", std::ios::binary);
output.write(reinterpret_cast<const char*>(cache), length);
}