-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathquantizer.cpp
More file actions
64 lines (51 loc) · 1.6 KB
/
Copy pathquantizer.cpp
File metadata and controls
64 lines (51 loc) · 1.6 KB
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
#include <exception>
#include <iostream>
#include "rabitqlib/quantization/rabitq.hpp"
#include "rabitqlib/utils/rotator.hpp"
int run() {
// generate random data
size_t dim = 128;
size_t bit = 4;
float* data = new float[dim];
for (size_t i = 0; i < dim; i++) {
data[i] = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
// choose rotator
rabitqlib::Rotator<float>* rotator = rabitqlib::choose_rotator<float>(dim);
size_t padded_dim = rotator->size();
float* rotated_data = new float[padded_dim];
rotator->rotate(data, rotated_data);
// print rotated_data
for (size_t i = 0; i < padded_dim; i++) {
std::cout << rotated_data[i] << " ";
}
std::cout << '\n';
// quantize
uint8_t* code = new uint8_t[padded_dim];
float delta = 0;
float vl = 0;
rabitqlib::quant::quantize_scalar(rotated_data, padded_dim, bit, code, delta, vl);
// [Note: we don't need to store vl as vl = - delta * (2^bit - 1) / 2]
// reconstruct
float* reconstructed_data = new float[padded_dim];
rabitqlib::quant::reconstruct_vec(code, delta, vl, padded_dim, reconstructed_data);
// print reconstructed_data
for (size_t i = 0; i < padded_dim; i++) {
std::cout << reconstructed_data[i] << " ";
}
std::cout << '\n';
delete rotator;
delete[] data;
delete[] rotated_data;
delete[] code;
delete[] reconstructed_data;
return 0;
}
int main() {
try {
return run();
} catch (const std::exception& error) {
std::cerr << "Error: " << error.what() << '\n';
return 1;
}
}