-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtest_uniform.cpp
More file actions
289 lines (241 loc) · 10.1 KB
/
Copy pathtest_uniform.cpp
File metadata and controls
289 lines (241 loc) · 10.1 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
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
#include <gtest/gtest.h>
extern "C" {
#include "turboquant/turboquant.h"
void tq_uniform_4b_quantize_ref(const float* src, void* dst, int n);
void tq_uniform_4b_dequantize_ref(const void* src, float* dst, int n);
void tq_uniform_4b_attention_ref(const float* query, const void* kv,
float* scores, int seq_len, int head_dim);
void tq_uniform_2b_quantize_ref(const float* src, void* dst, int n);
void tq_uniform_2b_dequantize_ref(const void* src, float* dst, int n);
void tq_uniform_2b_attention_ref(const float* query, const void* kv,
float* scores, int seq_len, int head_dim);
void tq_uniform_3b_quantize_ref(const float* src, void* dst, int n);
void tq_uniform_3b_dequantize_ref(const void* src, float* dst, int n);
void tq_uniform_3b_attention_ref(const float* query, const void* kv,
float* scores, int seq_len, int head_dim);
}
#include <cmath>
#include <vector>
TEST(Uniform4B, RoundtripBasic) {
std::vector<float> input(TQ_BK);
for (int i = 0; i < TQ_BK; i++) input[i] = sinf(i * 0.1f);
block_tq_uniform_4b block;
tq_uniform_4b_quantize_ref(input.data(), &block, TQ_BK);
std::vector<float> output(TQ_BK);
tq_uniform_4b_dequantize_ref(&block, output.data(), TQ_BK);
// 4-bit uniform should have low MSE
double mse = 0;
for (int i = 0; i < TQ_BK; i++) {
double d = input[i] - output[i];
mse += d * d;
}
mse /= TQ_BK;
EXPECT_LT(mse, 0.01); // Very low MSE for 4-bit on [-1, 1] range
}
TEST(Uniform4B, ExtremeValues) {
std::vector<float> input(TQ_BK);
for (int i = 0; i < TQ_BK; i++) input[i] = (float)i / TQ_BK * 100.0f - 50.0f;
block_tq_uniform_4b block;
tq_uniform_4b_quantize_ref(input.data(), &block, TQ_BK);
std::vector<float> output(TQ_BK);
tq_uniform_4b_dequantize_ref(&block, output.data(), TQ_BK);
// MSE scales with range^2 / (16^2); range=100 -> step~6.67 -> MSE~3.7
double mse = 0;
for (int i = 0; i < TQ_BK; i++) {
double d = input[i] - output[i];
mse += d * d;
}
mse /= TQ_BK;
EXPECT_LT(mse, 5.0); // Wider range = higher MSE but still bounded
}
TEST(Uniform4B, BlockSize) {
EXPECT_EQ(sizeof(block_tq_uniform_4b), 4u + TQ_BK / 2);
}
TEST(Uniform2B, RoundtripBasic) {
std::vector<float> input(TQ_BK);
for (int i = 0; i < TQ_BK; i++) input[i] = sinf(i * 0.1f);
block_tq_uniform_2b block;
tq_uniform_2b_quantize_ref(input.data(), &block, TQ_BK);
std::vector<float> output(TQ_BK);
tq_uniform_2b_dequantize_ref(&block, output.data(), TQ_BK);
// 2-bit is more lossy than 4-bit
double mse = 0;
for (int i = 0; i < TQ_BK; i++) {
double d = input[i] - output[i];
mse += d * d;
}
mse /= TQ_BK;
EXPECT_LT(mse, 0.15); // Higher MSE for 2-bit, but still bounded
}
TEST(Uniform2B, BlockSize) {
EXPECT_EQ(sizeof(block_tq_uniform_2b), 4u * TQ_2B_NSUB + TQ_BK / 4);
}
TEST(Uniform4B, ConstantInput) {
// All same value should roundtrip perfectly
std::vector<float> input(TQ_BK, 3.14f);
block_tq_uniform_4b block;
tq_uniform_4b_quantize_ref(input.data(), &block, TQ_BK);
std::vector<float> output(TQ_BK);
tq_uniform_4b_dequantize_ref(&block, output.data(), TQ_BK);
for (int i = 0; i < TQ_BK; i++) {
EXPECT_NEAR(output[i], 3.14f, 0.1f);
}
}
TEST(Uniform4B, Attention) {
const int head_dim = TQ_BK;
const int seq_len = 4;
// Create query vector
std::vector<float> query(head_dim);
for (int i = 0; i < head_dim; i++) query[i] = cosf(i * 0.05f);
// Create key vectors and quantize them
std::vector<block_tq_uniform_4b> blocks(seq_len);
std::vector<std::vector<float>> keys(seq_len, std::vector<float>(head_dim));
for (int s = 0; s < seq_len; s++) {
for (int d = 0; d < head_dim; d++)
keys[s][d] = sinf(s * 1.0f + d * 0.1f);
tq_uniform_4b_quantize_ref(keys[s].data(), &blocks[s], head_dim);
}
// Compute attention scores via the new function
std::vector<float> scores(seq_len);
tq_uniform_4b_attention_ref(query.data(), blocks.data(), scores.data(),
seq_len, head_dim);
// Compare with FP32 dot product on dequantized keys
for (int s = 0; s < seq_len; s++) {
std::vector<float> deq(head_dim);
tq_uniform_4b_dequantize_ref(&blocks[s], deq.data(), head_dim);
float fp32_dot = 0;
for (int d = 0; d < head_dim; d++) fp32_dot += query[d] * deq[d];
EXPECT_NEAR(scores[s], fp32_dot, 1e-4f);
}
}
TEST(Uniform2B, Attention) {
const int head_dim = TQ_BK;
const int seq_len = 4;
// Create query vector
std::vector<float> query(head_dim);
for (int i = 0; i < head_dim; i++) query[i] = cosf(i * 0.05f);
// Create key vectors and quantize them
std::vector<block_tq_uniform_2b> blocks(seq_len);
std::vector<std::vector<float>> keys(seq_len, std::vector<float>(head_dim));
for (int s = 0; s < seq_len; s++) {
for (int d = 0; d < head_dim; d++)
keys[s][d] = sinf(s * 1.0f + d * 0.1f);
tq_uniform_2b_quantize_ref(keys[s].data(), &blocks[s], head_dim);
}
// Compute attention scores via the new function
std::vector<float> scores(seq_len);
tq_uniform_2b_attention_ref(query.data(), blocks.data(), scores.data(),
seq_len, head_dim);
// Compare with FP32 dot product on dequantized keys
for (int s = 0; s < seq_len; s++) {
std::vector<float> deq(head_dim);
tq_uniform_2b_dequantize_ref(&blocks[s], deq.data(), head_dim);
float fp32_dot = 0;
for (int d = 0; d < head_dim; d++) fp32_dot += query[d] * deq[d];
EXPECT_NEAR(scores[s], fp32_dot, 1e-4f);
}
}
/* ====================================================================
* Uniform 3-bit with sub-block scales tests
* ==================================================================== */
TEST(Uniform3B, RoundtripBasic) {
std::vector<float> input(TQ_BK);
for (int i = 0; i < TQ_BK; i++) input[i] = sinf(i * 0.1f);
block_tq_uniform_3b block;
tq_uniform_3b_quantize_ref(input.data(), &block, TQ_BK);
std::vector<float> output(TQ_BK);
tq_uniform_3b_dequantize_ref(&block, output.data(), TQ_BK);
// 3-bit with sub-block scales should have moderate MSE
double mse = 0;
for (int i = 0; i < TQ_BK; i++) {
double d = input[i] - output[i];
mse += d * d;
}
mse /= TQ_BK;
// Sub-block 3-bit should be better than global 3-bit, targeting < 0.05
EXPECT_LT(mse, 0.05);
}
TEST(Uniform3B, ExtremeValues) {
std::vector<float> input(TQ_BK);
for (int i = 0; i < TQ_BK; i++) input[i] = (float)i / TQ_BK * 100.0f - 50.0f;
block_tq_uniform_3b block;
tq_uniform_3b_quantize_ref(input.data(), &block, TQ_BK);
std::vector<float> output(TQ_BK);
tq_uniform_3b_dequantize_ref(&block, output.data(), TQ_BK);
// With wide range, MSE will be higher but sub-blocks help
double mse = 0;
for (int i = 0; i < TQ_BK; i++) {
double d = input[i] - output[i];
mse += d * d;
}
mse /= TQ_BK;
// 8 levels over 25-unit sub-range: step~3.6, MSE~1.1
EXPECT_LT(mse, 20.0);
}
TEST(Uniform3B, BlockSize) {
// 8 (sub_scale) + 8 (sub_min) + 48 (qs) = 64 bytes
EXPECT_EQ(sizeof(block_tq_uniform_3b), 64u);
}
TEST(Uniform3B, BitsPerElement) {
float bpe = tq_type_bpe(TQ_TYPE_UNIFORM_3B);
// 64 * 8 / 128 = 4.0
EXPECT_NEAR(bpe, 4.0f, 0.01f);
}
TEST(Uniform3B, Attention) {
const int head_dim = TQ_BK;
const int seq_len = 4;
std::vector<float> query(head_dim);
for (int i = 0; i < head_dim; i++) query[i] = cosf(i * 0.05f);
std::vector<block_tq_uniform_3b> blocks(seq_len);
std::vector<std::vector<float>> keys(seq_len, std::vector<float>(head_dim));
for (int s = 0; s < seq_len; s++) {
for (int d = 0; d < head_dim; d++)
keys[s][d] = sinf(s * 1.0f + d * 0.1f);
tq_uniform_3b_quantize_ref(keys[s].data(), &blocks[s], head_dim);
}
std::vector<float> scores(seq_len);
tq_uniform_3b_attention_ref(query.data(), blocks.data(), scores.data(),
seq_len, head_dim);
// Compare with FP32 dot product on dequantized keys
for (int s = 0; s < seq_len; s++) {
std::vector<float> deq(head_dim);
tq_uniform_3b_dequantize_ref(&blocks[s], deq.data(), head_dim);
float fp32_dot = 0;
for (int d = 0; d < head_dim; d++) fp32_dot += query[d] * deq[d];
EXPECT_NEAR(scores[s], fp32_dot, 1e-4f);
}
}
TEST(Uniform3B, SubBlockBenefit) {
// Verify sub-block scales handle heterogeneous distributions well
// First 32 elements in [-1, 1], next 32 in [-100, 100], etc.
std::vector<float> input(TQ_BK);
for (int i = 0; i < 32; i++) input[i] = sinf(i * 0.1f); // [-1, 1]
for (int i = 32; i < 64; i++) input[i] = (float)(i - 48) * 3.0f; // [-48, 48]
for (int i = 64; i < 96; i++) input[i] = sinf(i * 0.05f) * 0.1f; // [-0.1, 0.1]
for (int i = 96; i < 128; i++) input[i] = (float)(i - 112) * 0.5f; // [-8, 8]
block_tq_uniform_3b block;
tq_uniform_3b_quantize_ref(input.data(), &block, TQ_BK);
std::vector<float> output(TQ_BK);
tq_uniform_3b_dequantize_ref(&block, output.data(), TQ_BK);
// Check per-sub-block MSE: each should be reasonable for its own range
for (int sb = 0; sb < 4; sb++) {
double mse = 0;
for (int i = sb * 32; i < (sb + 1) * 32; i++) {
double d = input[i] - output[i];
mse += d * d;
}
mse /= 32;
// Each sub-block's MSE should be proportional to its own range, not the global range
float range = 0;
for (int i = sb * 32; i < (sb + 1) * 32; i++) {
float a = fabsf(input[i]);
if (a > range) range = a;
}
// MSE should be small relative to the sub-block's range^2
// 3-bit (8 levels): step = range/3.5, MSE ~ step^2/12 ~ range^2/147
float expected_max_mse = (range * range) / 10.0f; // generous bound
if (expected_max_mse < 0.01f) expected_max_mse = 0.01f;
EXPECT_LT(mse, expected_max_mse)
<< "Sub-block " << sb << " MSE too high relative to its range";
}
}