-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtest_multiblock.cpp
More file actions
286 lines (243 loc) · 10.8 KB
/
Copy pathtest_multiblock.cpp
File metadata and controls
286 lines (243 loc) · 10.8 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
/**
* Tests for multi-block attention (head_dim > TQ_BK).
*
* Validates that attention functions correctly handle key vectors
* spanning multiple quantization blocks.
*/
#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_4b_attention_int_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_attention_ref(const float* query, const void* kv,
float* scores, int seq_len, int head_dim);
}
#include <cmath>
#include <vector>
static float cosine_similarity(const float* a, const float* b, int n) {
double dot = 0, na = 0, nb = 0;
for (int i = 0; i < n; i++) {
dot += (double)a[i] * b[i];
na += (double)a[i] * a[i];
nb += (double)b[i] * b[i];
}
if (na < 1e-20 || nb < 1e-20) return 0.0f;
return (float)(dot / (sqrt(na) * sqrt(nb)));
}
/* Helper: quantize keys using per-block quantization (matching multi-block layout) */
static void quantize_multiblock_4b(const float* key, block_tq_uniform_4b* blocks,
int head_dim) {
int blocks_per_key = (head_dim + TQ_BK - 1) / TQ_BK;
for (int b = 0; b < blocks_per_key; b++) {
int offset = b * TQ_BK;
int chunk = (head_dim - offset > TQ_BK) ? TQ_BK : (head_dim - offset);
tq_uniform_4b_quantize_ref(key + offset, &blocks[b], chunk);
}
}
static void quantize_multiblock_2b(const float* key, block_tq_uniform_2b* blocks,
int head_dim) {
int blocks_per_key = (head_dim + TQ_BK - 1) / TQ_BK;
for (int b = 0; b < blocks_per_key; b++) {
int offset = b * TQ_BK;
int chunk = (head_dim - offset > TQ_BK) ? TQ_BK : (head_dim - offset);
tq_uniform_2b_quantize_ref(key + offset, &blocks[b], chunk);
}
}
/* ================================================================
* Uniform 4-bit multi-block tests
* ================================================================ */
TEST(MultiBlock, Attention256) {
const int head_dim = 256; /* 2 blocks */
const int seq_len = 16;
const int blocks_per_key = (head_dim + TQ_BK - 1) / TQ_BK;
/* Create query */
std::vector<float> query(head_dim);
for (int i = 0; i < head_dim; i++) query[i] = cosf(i * 0.05f);
/* Create and quantize keys (multi-block layout) */
std::vector<block_tq_uniform_4b> blocks(seq_len * blocks_per_key);
std::vector<std::vector<float>> keys(seq_len);
for (int s = 0; s < seq_len; s++) {
keys[s].resize(head_dim);
for (int d = 0; d < head_dim; d++)
keys[s][d] = sinf(s * 1.0f + d * 0.1f);
quantize_multiblock_4b(keys[s].data(), &blocks[s * blocks_per_key], head_dim);
}
/* Compute FP32 reference (dequantize per block, dot product) */
std::vector<float> ref_scores(seq_len);
for (int s = 0; s < seq_len; s++) {
float dot = 0;
for (int b = 0; b < blocks_per_key; b++) {
int offset = b * TQ_BK;
int chunk = (head_dim - offset > TQ_BK) ? TQ_BK : (head_dim - offset);
float deq[TQ_BK];
tq_uniform_4b_dequantize_ref(&blocks[s * blocks_per_key + b], deq, chunk);
for (int d = 0; d < chunk; d++)
dot += query[offset + d] * deq[d];
}
ref_scores[s] = dot;
}
/* Compute via attention function */
std::vector<float> attn_scores(seq_len);
tq_uniform_4b_attention_ref(query.data(), blocks.data(),
attn_scores.data(), seq_len, head_dim);
/* Should match reference exactly (same code path) */
for (int s = 0; s < seq_len; s++) {
EXPECT_FLOAT_EQ(attn_scores[s], ref_scores[s])
<< "Score mismatch at position " << s;
}
/* Cosine similarity with original FP32 dot products should be high */
std::vector<float> fp32_scores(seq_len);
for (int s = 0; s < seq_len; s++) {
float dot = 0;
for (int d = 0; d < head_dim; d++)
dot += query[d] * keys[s][d];
fp32_scores[s] = dot;
}
float cos_sim = cosine_similarity(fp32_scores.data(), attn_scores.data(), seq_len);
EXPECT_GT(cos_sim, 0.95f) << "Cosine similarity too low: " << cos_sim;
}
TEST(MultiBlock, Attention384) {
const int head_dim = 384; /* 3 blocks */
const int seq_len = 8;
const int blocks_per_key = (head_dim + TQ_BK - 1) / TQ_BK;
std::vector<float> query(head_dim);
for (int i = 0; i < head_dim; i++) query[i] = cosf(i * 0.03f);
std::vector<block_tq_uniform_4b> blocks(seq_len * blocks_per_key);
std::vector<std::vector<float>> keys(seq_len);
for (int s = 0; s < seq_len; s++) {
keys[s].resize(head_dim);
for (int d = 0; d < head_dim; d++)
keys[s][d] = sinf(s * 0.7f + d * 0.08f) * (1.0f + s * 0.01f);
quantize_multiblock_4b(keys[s].data(), &blocks[s * blocks_per_key], head_dim);
}
/* FP32 reference */
std::vector<float> fp32_scores(seq_len);
for (int s = 0; s < seq_len; s++) {
float dot = 0;
for (int d = 0; d < head_dim; d++)
dot += query[d] * keys[s][d];
fp32_scores[s] = dot;
}
std::vector<float> attn_scores(seq_len);
tq_uniform_4b_attention_ref(query.data(), blocks.data(),
attn_scores.data(), seq_len, head_dim);
float cos_sim = cosine_similarity(fp32_scores.data(), attn_scores.data(), seq_len);
EXPECT_GT(cos_sim, 0.95f) << "Cosine similarity too low: " << cos_sim;
}
TEST(MultiBlock, IntegerAttention256) {
const int head_dim = 256; /* 2 blocks */
const int seq_len = 16;
const int blocks_per_key = (head_dim + TQ_BK - 1) / TQ_BK;
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_4b> blocks(seq_len * blocks_per_key);
std::vector<std::vector<float>> keys(seq_len);
for (int s = 0; s < seq_len; s++) {
keys[s].resize(head_dim);
for (int d = 0; d < head_dim; d++)
keys[s][d] = sinf(s * 1.0f + d * 0.1f);
quantize_multiblock_4b(keys[s].data(), &blocks[s * blocks_per_key], head_dim);
}
/* FP32 dequant+dot reference */
std::vector<float> fp32_scores(seq_len);
for (int s = 0; s < seq_len; s++) {
float dot = 0;
for (int b = 0; b < blocks_per_key; b++) {
int offset = b * TQ_BK;
int chunk = (head_dim - offset > TQ_BK) ? TQ_BK : (head_dim - offset);
float deq[TQ_BK];
tq_uniform_4b_dequantize_ref(&blocks[s * blocks_per_key + b], deq, chunk);
for (int d = 0; d < chunk; d++)
dot += query[offset + d] * deq[d];
}
fp32_scores[s] = dot;
}
/* Integer-domain attention */
std::vector<float> int_scores(seq_len);
tq_uniform_4b_attention_int_ref(query.data(), blocks.data(),
int_scores.data(), seq_len, head_dim);
float cos_sim = cosine_similarity(fp32_scores.data(), int_scores.data(), seq_len);
EXPECT_GT(cos_sim, 0.99f) << "Integer attention cosine similarity too low: " << cos_sim;
/* Absolute tolerance check */
float max_mag = 0;
for (int s = 0; s < seq_len; s++) {
float m = fabsf(fp32_scores[s]);
if (m > max_mag) max_mag = m;
}
float tol = max_mag * 0.05f;
for (int s = 0; s < seq_len; s++) {
EXPECT_LT(fabsf(int_scores[s] - fp32_scores[s]), tol)
<< "Score " << s << ": fp32=" << fp32_scores[s]
<< " int=" << int_scores[s];
}
}
TEST(MultiBlock, Uniform2b256) {
const int head_dim = 256;
const int seq_len = 8;
const int blocks_per_key = (head_dim + TQ_BK - 1) / TQ_BK;
std::vector<float> query(head_dim);
for (int i = 0; i < head_dim; i++) query[i] = cosf(i * 0.04f);
std::vector<block_tq_uniform_2b> blocks(seq_len * blocks_per_key);
std::vector<std::vector<float>> keys(seq_len);
for (int s = 0; s < seq_len; s++) {
keys[s].resize(head_dim);
for (int d = 0; d < head_dim; d++)
keys[s][d] = sinf(s * 0.5f + d * 0.07f);
quantize_multiblock_2b(keys[s].data(), &blocks[s * blocks_per_key], head_dim);
}
/* FP32 reference */
std::vector<float> fp32_scores(seq_len);
for (int s = 0; s < seq_len; s++) {
float dot = 0;
for (int d = 0; d < head_dim; d++)
dot += query[d] * keys[s][d];
fp32_scores[s] = dot;
}
std::vector<float> attn_scores(seq_len);
tq_uniform_2b_attention_ref(query.data(), blocks.data(),
attn_scores.data(), seq_len, head_dim);
float cos_sim = cosine_similarity(fp32_scores.data(), attn_scores.data(), seq_len);
EXPECT_GT(cos_sim, 0.90f) << "2-bit attention cosine similarity too low: " << cos_sim;
}
/* ================================================================
* Context API multi-block test (end-to-end)
* ================================================================ */
TEST(MultiBlock, ContextAPI256) {
const int head_dim = 256;
const int seq_len = 4;
tq_context_t* ctx = nullptr;
ASSERT_EQ(tq_init(&ctx, TQ_BACKEND_CPU), TQ_OK);
std::vector<float> query(head_dim);
for (int i = 0; i < head_dim; i++) query[i] = cosf(i * 0.05f);
/* Create keys */
std::vector<float> keys(seq_len * head_dim);
for (int s = 0; s < seq_len; s++)
for (int d = 0; d < head_dim; d++)
keys[s * head_dim + d] = sinf(s * 1.0f + d * 0.1f);
/* Quantize via context API */
size_t kv_size = tq_quantize_keys_size(seq_len, head_dim, TQ_TYPE_UNIFORM_4B);
ASSERT_GT(kv_size, 0u);
std::vector<uint8_t> kv_buf(kv_size);
ASSERT_EQ(tq_quantize_keys(ctx, keys.data(), seq_len, head_dim,
TQ_TYPE_UNIFORM_4B, kv_buf.data(), kv_size), TQ_OK);
/* Attention via context API */
std::vector<float> scores(seq_len);
ASSERT_EQ(tq_attention(ctx, query.data(), kv_buf.data(), seq_len, head_dim,
TQ_TYPE_UNIFORM_4B, scores.data()), TQ_OK);
/* FP32 reference */
std::vector<float> fp32_scores(seq_len);
for (int s = 0; s < seq_len; s++) {
float dot = 0;
for (int d = 0; d < head_dim; d++)
dot += query[d] * keys[s * head_dim + d];
fp32_scores[s] = dot;
}
float cos_sim = cosine_similarity(fp32_scores.data(), scores.data(), seq_len);
EXPECT_GT(cos_sim, 0.95f) << "Context API multi-block cosine similarity: " << cos_sim;
tq_free(ctx);
}