-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathbench_kernel.cpp
More file actions
382 lines (334 loc) · 13.6 KB
/
Copy pathbench_kernel.cpp
File metadata and controls
382 lines (334 loc) · 13.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
* quant.cpp -- Individual Kernel Performance Benchmark
*
* Times each operation (quantize, dequantize, attention) separately for
* all 7 quantization types. Reports both machine-readable metrics and a
* human-readable comparison table.
*
* Output (machine-readable):
* kernel_quantize_<type>=XXXXX (elements/ms)
* kernel_dequantize_<type>=XXXXX (elements/ms)
* kernel_attention_<type>=XXXXX (queries/sec, seq=512)
*
* Build:
* cmake -B build -DTQ_BUILD_BENCH=ON
* cmake --build build --target bench_kernel
*
* Run:
* ./build/bench_kernel
*/
extern "C" {
#include "turboquant/turboquant.h"
/* Reference implementations */
void tq_polar_quantize_ref(const float* src, void* dst, int n);
void tq_polar_dequantize_ref(const void* src, float* dst, int n);
void tq_polar_attention_ref(const float* query, const void* kv,
float* scores, int seq_len, int head_dim);
void tq_qjl_quantize_ref(const float* src, void* dst, int n);
void tq_qjl_dequantize_ref(const void* src, float* dst, int n);
void tq_qjl_attention_ref(const float* query, const void* kv,
float* scores, int seq_len, int head_dim);
void tq_turbo_quantize_ref(const float* src, void* dst, int n);
void tq_turbo_dequantize_ref(const void* src, float* dst, int n);
void tq_turbo_attention_ref(const float* query, const void* kv,
float* scores, int seq_len, int head_dim);
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_2b_quantize_ref(const float* src, void* dst, int n);
void tq_uniform_2b_dequantize_ref(const void* src, float* dst, int n);
#ifdef __ARM_NEON
void tq_uniform_4b_quantize_neon(const float* src, void* dst, int n);
void tq_uniform_4b_dequantize_neon(const void* src, float* dst, int n);
void tq_polar_quantize_neon(const float* src, void* dst, int n);
void tq_polar_dequantize_neon(const void* src, float* dst, int n);
void tq_qjl_quantize_neon(const float* src, void* dst, int n);
void tq_qjl_attention_neon(const float* query, const void* kv,
float* scores, int seq_len, int head_dim);
#endif
}
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
/* ============================================================
* Timing utilities
* ============================================================ */
static double now_sec() {
auto t = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(t.time_since_epoch()).count();
}
/**
* Run fn() for n_warmup + n_iters times, return median elapsed seconds
* for a single iteration.
*/
template <typename Fn>
static double bench_median_sec(Fn fn, int n_warmup = 3, int n_iters = 7) {
for (int i = 0; i < n_warmup; i++) fn();
std::vector<double> times(n_iters);
for (int i = 0; i < n_iters; i++) {
double t0 = now_sec();
fn();
double t1 = now_sec();
times[i] = t1 - t0;
}
std::sort(times.begin(), times.end());
return times[n_iters / 2];
}
/* ============================================================
* PRNG
* ============================================================ */
static uint32_t rng_state = 7777;
static float rand_float() {
rng_state = rng_state * 1664525u + 1013904223u;
return ((float)(rng_state >> 8) / (float)(1 << 24)) * 4.0f - 2.0f;
}
/* ============================================================
* Kernel descriptor
* ============================================================ */
struct KernelType {
const char* name;
const char* backend; /* "ref" or "neon" or "avx2" */
tq_type type;
tq_quantize_fn quantize;
tq_dequantize_fn dequantize;
tq_attention_fn attention; /* NULL if unavailable */
size_t block_bytes;
int block_elems;
};
/* ============================================================
* Build the kernel list (ref + NEON where available)
* ============================================================ */
static std::vector<KernelType> build_kernel_list() {
std::vector<KernelType> kernels;
/* Reference implementations for all 7 types */
kernels.push_back({
"polar_3b", "ref", TQ_TYPE_POLAR_3B,
tq_polar_quantize_ref, tq_polar_dequantize_ref, tq_polar_attention_ref,
sizeof(block_tq_polar), TQ_BK,
});
kernels.push_back({
"polar_4b", "ref", TQ_TYPE_POLAR_4B,
tq_polar_quantize_ref, tq_polar_dequantize_ref, tq_polar_attention_ref,
sizeof(block_tq_polar), TQ_BK,
});
kernels.push_back({
"qjl_1b", "ref", TQ_TYPE_QJL_1B,
tq_qjl_quantize_ref, tq_qjl_dequantize_ref, tq_qjl_attention_ref,
sizeof(block_tq_qjl), TQ_BK_QJL,
});
kernels.push_back({
"turbo_3b", "ref", TQ_TYPE_TURBO_3B,
tq_turbo_quantize_ref, tq_turbo_dequantize_ref, tq_turbo_attention_ref,
sizeof(block_tq_turbo), TQ_BK,
});
kernels.push_back({
"turbo_4b", "ref", TQ_TYPE_TURBO_4B,
tq_turbo_quantize_ref, tq_turbo_dequantize_ref, tq_turbo_attention_ref,
sizeof(block_tq_turbo), TQ_BK,
});
kernels.push_back({
"uniform_4b", "ref", TQ_TYPE_UNIFORM_4B,
tq_uniform_4b_quantize_ref, tq_uniform_4b_dequantize_ref, nullptr,
sizeof(block_tq_uniform_4b), TQ_BK,
});
kernels.push_back({
"uniform_2b", "ref", TQ_TYPE_UNIFORM_2B,
tq_uniform_2b_quantize_ref, tq_uniform_2b_dequantize_ref, nullptr,
sizeof(block_tq_uniform_2b), TQ_BK,
});
#ifdef __ARM_NEON
/* NEON-accelerated variants */
kernels.push_back({
"polar_3b", "neon", TQ_TYPE_POLAR_3B,
tq_polar_quantize_neon, tq_polar_dequantize_neon, tq_polar_attention_ref,
sizeof(block_tq_polar), TQ_BK,
});
kernels.push_back({
"polar_4b", "neon", TQ_TYPE_POLAR_4B,
tq_polar_quantize_neon, tq_polar_dequantize_neon, tq_polar_attention_ref,
sizeof(block_tq_polar), TQ_BK,
});
kernels.push_back({
"qjl_1b", "neon", TQ_TYPE_QJL_1B,
tq_qjl_quantize_neon, tq_qjl_dequantize_ref, tq_qjl_attention_neon,
sizeof(block_tq_qjl), TQ_BK_QJL,
});
kernels.push_back({
"uniform_4b", "neon", TQ_TYPE_UNIFORM_4B,
tq_uniform_4b_quantize_neon, tq_uniform_4b_dequantize_neon, nullptr,
sizeof(block_tq_uniform_4b), TQ_BK,
});
#endif
return kernels;
}
/* ============================================================
* Main benchmark
* ============================================================ */
int main() {
const int HEAD_DIM = 128;
const int N_VECTORS = 10000;
const int SEQ_LEN = 512;
printf("# quant.cpp Kernel Performance Benchmark\n");
printf("# HEAD_DIM=%d, N_VECTORS=%d (quant/dequant), SEQ_LEN=%d (attention)\n",
HEAD_DIM, N_VECTORS, SEQ_LEN);
#ifdef __ARM_NEON
printf("# NEON: enabled\n");
#else
printf("# NEON: disabled\n");
#endif
printf("\n");
/* Generate input data */
std::vector<float> input_data(N_VECTORS * HEAD_DIM);
for (size_t i = 0; i < input_data.size(); i++) input_data[i] = rand_float();
std::vector<float> query(HEAD_DIM);
for (int i = 0; i < HEAD_DIM; i++) query[i] = rand_float();
auto kernels = build_kernel_list();
/* Storage for results */
struct KernelResult {
const char* name;
const char* backend;
double quant_elem_per_ms;
double dequant_elem_per_ms;
double attn_queries_per_sec;
bool has_attention;
};
std::vector<KernelResult> results;
for (const auto& kt : kernels) {
KernelResult res;
res.name = kt.name;
res.backend = kt.backend;
res.has_attention = (kt.attention != nullptr);
/* --- Quantize benchmark --- */
{
size_t buf_size = kt.block_bytes * N_VECTORS;
std::vector<uint8_t> quant_buf(buf_size, 0);
double dt = bench_median_sec([&]() {
for (int v = 0; v < N_VECTORS; v++) {
kt.quantize(input_data.data() + v * HEAD_DIM,
quant_buf.data() + (size_t)v * kt.block_bytes,
HEAD_DIM);
}
});
double total_elems = (double)N_VECTORS * HEAD_DIM;
res.quant_elem_per_ms = total_elems / (dt * 1000.0);
}
/* --- Dequantize benchmark --- */
{
/* First, quantize data so we have valid blocks */
size_t buf_size = kt.block_bytes * N_VECTORS;
std::vector<uint8_t> quant_buf(buf_size, 0);
for (int v = 0; v < N_VECTORS; v++) {
kt.quantize(input_data.data() + v * HEAD_DIM,
quant_buf.data() + (size_t)v * kt.block_bytes,
HEAD_DIM);
}
std::vector<float> deq_buf(N_VECTORS * HEAD_DIM, 0.0f);
double dt = bench_median_sec([&]() {
for (int v = 0; v < N_VECTORS; v++) {
kt.dequantize(quant_buf.data() + (size_t)v * kt.block_bytes,
deq_buf.data() + v * HEAD_DIM,
HEAD_DIM);
}
});
double total_elems = (double)N_VECTORS * HEAD_DIM;
res.dequant_elem_per_ms = total_elems / (dt * 1000.0);
}
/* --- Attention benchmark --- */
if (kt.attention != nullptr) {
/* Build KV cache of SEQ_LEN blocks */
size_t cache_size = kt.block_bytes * SEQ_LEN;
std::vector<uint8_t> kv_cache(cache_size, 0);
for (int s = 0; s < SEQ_LEN; s++) {
int idx = s % N_VECTORS;
kt.quantize(input_data.data() + idx * HEAD_DIM,
kv_cache.data() + (size_t)s * kt.block_bytes,
HEAD_DIM);
}
std::vector<float> scores(SEQ_LEN, 0.0f);
double dt = bench_median_sec([&]() {
kt.attention(query.data(), kv_cache.data(),
scores.data(), SEQ_LEN, HEAD_DIM);
});
res.attn_queries_per_sec = 1.0 / dt;
} else {
res.attn_queries_per_sec = 0.0;
}
results.push_back(res);
}
/* --- Machine-readable output --- */
for (const auto& r : results) {
const char* suffix = (strcmp(r.backend, "ref") == 0) ? "" : r.backend;
if (strlen(suffix) > 0) {
printf("kernel_quantize_%s_%s=%.0f\n", r.name, suffix, r.quant_elem_per_ms);
printf("kernel_dequantize_%s_%s=%.0f\n", r.name, suffix, r.dequant_elem_per_ms);
if (r.has_attention) {
printf("kernel_attention_%s_%s=%.0f\n", r.name, suffix, r.attn_queries_per_sec);
}
} else {
printf("kernel_quantize_%s=%.0f\n", r.name, r.quant_elem_per_ms);
printf("kernel_dequantize_%s=%.0f\n", r.name, r.dequant_elem_per_ms);
if (r.has_attention) {
printf("kernel_attention_%s=%.0f\n", r.name, r.attn_queries_per_sec);
}
}
}
/* --- Human-readable comparison table --- */
printf("\n# ============================================================\n");
printf("# Kernel Performance Comparison\n");
printf("# ============================================================\n\n");
printf("%-14s %-6s %14s %14s %14s\n",
"Type", "Backend", "Quant (elem/ms)", "Deq (elem/ms)", "Attn (q/sec)");
printf("%-14s %-6s %14s %14s %14s\n",
"--------------", "------", "--------------", "--------------", "--------------");
for (const auto& r : results) {
if (r.has_attention) {
printf("%-14s %-6s %14.0f %14.0f %14.0f\n",
r.name, r.backend,
r.quant_elem_per_ms, r.dequant_elem_per_ms,
r.attn_queries_per_sec);
} else {
printf("%-14s %-6s %14.0f %14.0f %14s\n",
r.name, r.backend,
r.quant_elem_per_ms, r.dequant_elem_per_ms,
"N/A");
}
}
/* --- SIMD speedup summary (if NEON entries exist) --- */
#ifdef __ARM_NEON
printf("\n# SIMD Speedup (NEON / Reference)\n");
printf("%-14s %14s %14s %14s\n",
"Type", "Quant Speedup", "Deq Speedup", "Attn Speedup");
printf("%-14s %14s %14s %14s\n",
"--------------", "--------------", "--------------", "--------------");
for (const auto& neon_r : results) {
if (strcmp(neon_r.backend, "neon") != 0) continue;
/* Find matching ref entry */
const KernelResult* ref_r = nullptr;
for (const auto& rr : results) {
if (strcmp(rr.backend, "ref") == 0 &&
strcmp(rr.name, neon_r.name) == 0) {
ref_r = &rr;
break;
}
}
if (!ref_r) continue;
double q_speedup = (ref_r->quant_elem_per_ms > 0)
? neon_r.quant_elem_per_ms / ref_r->quant_elem_per_ms : 0.0;
double d_speedup = (ref_r->dequant_elem_per_ms > 0)
? neon_r.dequant_elem_per_ms / ref_r->dequant_elem_per_ms : 0.0;
if (neon_r.has_attention && ref_r->has_attention &&
ref_r->attn_queries_per_sec > 0) {
double a_speedup = neon_r.attn_queries_per_sec / ref_r->attn_queries_per_sec;
printf("%-14s %13.2fx %13.2fx %13.2fx\n",
neon_r.name, q_speedup, d_speedup, a_speedup);
} else {
printf("%-14s %13.2fx %13.2fx %14s\n",
neon_r.name, q_speedup, d_speedup, "N/A");
}
}
#endif
return 0;
}