-
Notifications
You must be signed in to change notification settings - Fork 1
/
quant_gpu.cu
301 lines (244 loc) · 8.49 KB
/
quant_gpu.cu
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "quant.h"
#include "quant_gpu.h"
#include "cudalloyds.h"
#include "test_compress_common.h"
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include "test_compress_gpu.h"
#define QUANTIZE_BLOCK_SIZE 1024
#define QUANTIZE_BLOCK_COUNT 64
// Using constant memory for codebook quant and deqaunt made it slower
// #define MAX_CODEBOOK_SIZE (1024 * 8)
// __constant__ float constCodebookArray[MAX_CODEBOOK_SIZE];
struct QuantLogFunctor {
QuantLog uni;
__host__ __device__ QuantLogFunctor(int binCount, float threshold,
float maxVal) {
uni.init(binCount, threshold, maxVal);
}
__host__ __device__ int operator() (float x) const {
return uni.quant(x);
}
};
struct DequantLogFunctor {
QuantLog uni;
__host__ __device__ DequantLogFunctor(int binCount, float threshold,
float maxVal) {
uni.init(binCount, threshold, maxVal);
}
__host__ __device__ float operator() (int x) const {
return uni.dequant(x);
}
};
struct QuantUniformFunctor {
QuantUniform uni;
__host__ __device__ QuantUniformFunctor(int binCount, float threshold,
float maxVal) {
uni.init(binCount, threshold, maxVal);
}
__host__ __device__ int operator() (float x) const {
return uni.quant(x);
}
};
struct DequantUniformFunctor {
QuantUniform uni;
__host__ __device__ DequantUniformFunctor(int binCount, float threshold,
float maxVal) {
uni.init(binCount, threshold, maxVal);
}
__host__ __device__ float operator() (int x) const {
return uni.dequant(x);
}
};
struct QuantCodebookFunctor {
int boundaryCount;
const float *boundaries_dev;
static float *copyVectorToGPU(const vector<float> &v) {
float *v_dev = NULL;
size_t bytes = v.size()*sizeof(float);
// CUCHECK(cudaMemcpyToSymbol(constCodebookArray, v.data(), bytes, 0, cudaMemcpyHostToDevice));
CUCHECK(cudaMalloc((void**)&v_dev, bytes));
CUCHECK(cudaMemcpy(v_dev, v.data(), bytes, cudaMemcpyHostToDevice));
return v_dev;
}
__host__ __device__ QuantCodebookFunctor(int boundaryCount_,
const float *boundaries_dev_) {
boundaryCount = boundaryCount_;
boundaries_dev = boundaries_dev_;
}
__device__ int operator() (float x) const {
return QuantCodebook::quant(x, boundaryCount, boundaries_dev);
// return QuantCodebook::quant(x, boundaryCount, constCodebookArray);
}
};
struct DequantCodebookFunctor {
int binCount;
const float *binValues;
__host__ __device__ DequantCodebookFunctor(int binCount_,
const float *binValues_) {
binCount = binCount_;
binValues = binValues_;
}
__device__ float operator() (int x) const {
unsigned ux = x;
if (ux >= binCount) {
return 0.0f;
} else {
return binValues[ux];
// return constCodebookArray[ux];
}
}
};
bool quantizeGPU(int *outputData, const float *inputData, int count,
WaveletCompressionParam ¶m,
const float *nonzeroData_dev, int nonzeroCount,
float maxAbsVal, float minValue, float maxValue,
CudaTimer &quantizeTimer, int *zeroBin) {
thrust::device_ptr<const float> inputStart(inputData),
inputEnd(inputData+count);
thrust::device_ptr<int> outputStart(outputData);
switch (param.quantAlg) {
case QUANT_ALG_UNIFORM:
{
param.maxValue = maxAbsVal;
quantizeTimer.start();
QuantUniformFunctor q(param.binCount, param.thresholdValue,
param.maxValue);
thrust::transform(inputStart, inputEnd, outputStart, q);
quantizeTimer.end();
break;
}
case QUANT_ALG_LOG:
{
param.maxValue = maxAbsVal;
quantizeTimer.start();
QuantLogFunctor q(param.binCount, param.thresholdValue, param.maxValue);
thrust::transform(inputStart, inputEnd, outputStart, q);
quantizeTimer.end();
break;
}
case QUANT_ALG_LLOYD:
{
computeLloydQuantizationGPU(nonzeroData_dev, nonzeroCount, param.binCount,
minValue, maxValue, param.thresholdValue,
param.binBoundaries, param.binValues);
quantizeTimer.start();
float *boundaries_dev =
QuantCodebookFunctor::copyVectorToGPU(param.binBoundaries);
QuantCodebookFunctor q(param.binBoundaries.size(), boundaries_dev);
thrust::transform(inputStart, inputEnd, outputStart, q);
CUCHECK(cudaFree(boundaries_dev));
quantizeTimer.end();
break;
}
default:
fprintf(stderr, "Quantization algorithm %d not found.\n",
(int)param.quantAlg);
return false;
}
// compute the bin number to which zero values map
if (zeroBin) {
Quantizer *quantizer = createQuantizer(param);
*zeroBin = quantizer->quant(0);
delete quantizer;
}
return true;
}
void computeLloydQuantizationGPU(const float *inputData, int count,
int binCount, float minVal, float maxVal,
float thresholdValue,
std::vector<float> &quantBinBoundaries,
std::vector<float> &quantBinValues) {
// Make 'codebookSize' entries on either size of 0
int codebookSize = (binCount-1) / 2;
quantBinBoundaries.clear();
quantBinValues.clear();
// inputData is sorted, use it to get minVal and maxVal
// Skip the last entry in inputData[] because it is often much larger than
// the rest and skews the results
float maxAbsVal;
CUCHECK(cudaMemcpy(&maxAbsVal, inputData+count-2, sizeof(float),
cudaMemcpyDeviceToHost));
assert(maxAbsVal > 0);
vector<float> codebook;
initialLloydCodebook(codebook, codebookSize, thresholdValue, maxAbsVal);
/*
printf("Before Lloyd\n");
for (int i=0; i < codebookSize; i++) printf("%d) %f\n", i, codebook[i]);
*/
// fine-tune the codebook and bin boundaries using Lloyd's algorithm.
// This also applies the quantization to each value, writing the values
// to quantizedData[].
int lloydIters = 0;
CudaTimer lloydTimer;
lloydTimer.start();
cudaLloyd(inputData, count-1, codebook.data(), (int)codebook.size(),
DEFAULT_LLOYD_STOP_CRITERIA, true, &lloydIters);
lloydTimer.end();
lloydTimer.sync();
if (!QUIET)
printf("GPU lloyd %d iterations, %.3f ms\n", lloydIters, lloydTimer.time());
/*
printf("After Lloyd\n");
for (int i=0; i < codebookSize; i++) printf("%d) %f\n", i, codebook[i]);
*/
setBinsFromCodebook(quantBinValues, quantBinBoundaries, binCount,
codebook, thresholdValue, minVal, maxVal);
}
// change data_dev from int[] to float[] in place
bool dequantizeGPU(float *outputData, const int *inputData,
int count, const WaveletCompressionParam ¶m) {
CudaTimer timer("Dequantize");
thrust::device_ptr<const int> inputStart(inputData),
inputEnd(inputData+count);
thrust::device_ptr<float> outputStart(outputData);
if (inputData == NULL)
inputData = (const int*) outputData;
timer.start();
switch (param.quantAlg) {
case QUANT_ALG_UNIFORM:
{
DequantUniformFunctor q(param.binCount, param.thresholdValue,
param.maxValue);
thrust::transform(inputStart, inputEnd, outputStart, q);
break;
}
case QUANT_ALG_LOG:
{
DequantLogFunctor q(param.binCount, param.thresholdValue, param.maxValue);
thrust::transform(inputStart, inputEnd, outputStart, q);
break;
}
case QUANT_ALG_LLOYD:
{
float *values_dev =
QuantCodebookFunctor::copyVectorToGPU(param.binValues);
DequantCodebookFunctor q(param.binCount, values_dev);
thrust::transform(inputStart, inputEnd, outputStart, q);
break;
}
default:
fprintf(stderr, "Quantization algorithm %d not found.\n",
(int)param.quantAlg);
return false;
}
timer.end();
if (!QUIET) {
timer.sync();
timer.print();
}
// copy the data to the CPU and print it all
/*
int *input = new int[count];
float *output = new float[count];
CUCHECK(cudaMemcpy(input, inputData, count*sizeof(int),
cudaMemcpyDeviceToHost));
CUCHECK(cudaMemcpy(output, outputData, count*sizeof(float),
cudaMemcpyDeviceToHost));
for (int i=0; i < count; i++)
printf("%d) %d -> %f\n", i, input[i], output[i]);
delete[] input;
delete[] output;
*/
return true;
}