-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantize_bench.rs
More file actions
248 lines (196 loc) · 8.43 KB
/
quantize_bench.rs
File metadata and controls
248 lines (196 loc) · 8.43 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
//! Criterion benchmarks for TurboQuant quantization, dequantization,
//! QJL inner-product estimation, and attention operations.
// qual:allow(BP-010) — criterion::bench_with_input closure signatures are mandated by the library and cannot be refactored
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use turboquant::attention::QuantizedKVCache;
use turboquant::packed::TurboQuantConfig;
use turboquant::qjl::{estimate_inner_product, precompute_query_projections, quantize_with_qjl};
use turboquant::quantize::{dequantize_vec, quantize_vec};
use turboquant::test_utils::pseudo_random_vec;
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const DIM_64: usize = 64;
const DIM_128: usize = 128;
const DIM_256: usize = 256;
const BITS_TQ3: u8 = 3;
const BITS_TQ4: u8 = 4;
const ROTATION_SEED: u64 = 42;
const QJL_SEED: u64 = 12345;
const CACHE_SEQ_LEN: usize = 1024;
const BENCH_NUM_LAYERS: usize = 1;
const BENCH_LAYER: usize = 0;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
fn make_config(bits: u8, dim: usize) -> TurboQuantConfig {
TurboQuantConfig::new(bits, dim)
.unwrap()
.with_seed(ROTATION_SEED)
}
// ---------------------------------------------------------------------------
// Benchmark: quantize_vec
// ---------------------------------------------------------------------------
// qual:allow(BP-010) — criterion benchmark_group idiom
fn bench_quantize(c: &mut Criterion) {
let mut group = c.benchmark_group("quantize_vec");
for &(bits, dim) in &[
(BITS_TQ3, DIM_64),
(BITS_TQ3, DIM_128),
(BITS_TQ3, DIM_256),
(BITS_TQ4, DIM_128),
] {
let config = make_config(bits, dim);
let data = pseudo_random_vec(dim, 1000);
// qual:allow(BP-010) — criterion idiom: `format!` label + bench_with_input closure is mandated by the library
let label = format!("tq{bits}_d{dim}");
group.bench_with_input(BenchmarkId::new("polarquant", &label), &data, |b, data| {
b.iter(|| quantize_vec(black_box(&config), black_box(data)))
});
}
group.finish();
}
// ---------------------------------------------------------------------------
// Benchmark: dequantize_vec
// ---------------------------------------------------------------------------
fn bench_dequantize(c: &mut Criterion) {
let mut group = c.benchmark_group("dequantize_vec");
for &(bits, dim) in &[
(BITS_TQ3, DIM_64),
(BITS_TQ3, DIM_128),
(BITS_TQ3, DIM_256),
(BITS_TQ4, DIM_128),
] {
let config = make_config(bits, dim);
let data = pseudo_random_vec(dim, 2000);
let block = quantize_vec(&config, &data).unwrap();
let label = format!("tq{bits}_d{dim}");
group.bench_with_input(
BenchmarkId::new("polarquant", &label),
&block,
|b, block| b.iter(|| dequantize_vec(black_box(&config), black_box(block))),
);
}
group.finish();
}
// ---------------------------------------------------------------------------
// Benchmark: quantize_with_qjl
// ---------------------------------------------------------------------------
fn bench_quantize_qjl(c: &mut Criterion) {
let mut group = c.benchmark_group("quantize_with_qjl");
for &(bits, dim) in &[(BITS_TQ3, DIM_128), (BITS_TQ4, DIM_128)] {
let config = make_config(bits, dim);
let data = pseudo_random_vec(dim, 3000);
let label = format!("tq{bits}_d{dim}");
group.bench_with_input(BenchmarkId::new("qjl", &label), &data, |b, data| {
b.iter(|| quantize_with_qjl(black_box(&config), black_box(data), QJL_SEED))
});
}
group.finish();
}
// ---------------------------------------------------------------------------
// Benchmark: estimate_inner_product (pre-computed projections + SIMD)
// ---------------------------------------------------------------------------
fn bench_inner_product(c: &mut Criterion) {
let mut group = c.benchmark_group("estimate_inner_product");
for &(bits, dim) in &[(BITS_TQ3, DIM_128), (BITS_TQ4, DIM_128)] {
let config = make_config(bits, dim);
let key = pseudo_random_vec(dim, 4000);
let query = pseudo_random_vec(dim, 5000);
let block = quantize_with_qjl(&config, &key, QJL_SEED).unwrap();
let r_query = precompute_query_projections(&query, dim, QJL_SEED);
let label = format!("tq{bits}_d{dim}");
group.bench_with_input(
BenchmarkId::new("qjl", &label),
&(&query, &r_query, &block),
|b, &(query, r_query, block)| {
b.iter(|| {
estimate_inner_product(
black_box(query),
black_box(r_query),
black_box(block),
&config,
)
})
},
);
}
group.finish();
}
// ---------------------------------------------------------------------------
// Benchmark: precompute_query_projections
// ---------------------------------------------------------------------------
fn bench_precompute_projections(c: &mut Criterion) {
let mut group = c.benchmark_group("precompute_query_projections");
for &dim in &[DIM_64, DIM_128, DIM_256] {
let query = pseudo_random_vec(dim, 5000);
let label = format!("d{dim}");
group.bench_with_input(
BenchmarkId::new("hash_rademacher", &label),
&query,
|b, query| b.iter(|| precompute_query_projections(black_box(query), dim, QJL_SEED)),
);
}
group.finish();
}
// ---------------------------------------------------------------------------
// Benchmark: attention_scores over 1024 keys
// ---------------------------------------------------------------------------
fn bench_attention_scores(c: &mut Criterion) {
let mut group = c.benchmark_group("attention_scores_1024");
let config = make_config(BITS_TQ3, DIM_128);
let mut cache = QuantizedKVCache::new(config, BENCH_NUM_LAYERS, QJL_SEED);
for i in 0..CACHE_SEQ_LEN {
let key = pseudo_random_vec(DIM_128, i as u64 * 100);
let value = pseudo_random_vec(DIM_128, i as u64 * 100 + 50);
let _ = cache.push(BENCH_LAYER, &key, &value);
}
let query = pseudo_random_vec(DIM_128, 99999);
group.bench_function("tq3_d128", |b| {
b.iter(|| cache.attention_scores(black_box(BENCH_LAYER), black_box(&query)))
});
group.finish();
}
// ---------------------------------------------------------------------------
// Benchmark: memory comparison
// ---------------------------------------------------------------------------
fn bench_memory_comparison(c: &mut Criterion) {
let mut group = c.benchmark_group("memory_comparison");
let config = make_config(BITS_TQ3, DIM_128);
let mut cache = QuantizedKVCache::new(config, BENCH_NUM_LAYERS, QJL_SEED);
let entry_counts = [64, 256, 1024];
for &count in &entry_counts {
// Fill cache to desired count (reuse previous entries)
while cache.entry_count(BENCH_LAYER) < count {
let i = cache.entry_count(BENCH_LAYER);
let key = pseudo_random_vec(DIM_128, i as u64 * 200);
let value = pseudo_random_vec(DIM_128, i as u64 * 200 + 100);
let _ = cache.push(BENCH_LAYER, &key, &value);
}
let tq3_bytes = cache.memory_usage();
let fp16_bytes = cache.fp16_equivalent_memory();
let ratio = fp16_bytes as f64 / tq3_bytes as f64;
group.bench_function(BenchmarkId::new("memory_usage", count), |b| {
b.iter(|| black_box(cache.memory_usage()))
});
// Print compression ratio (visible in benchmark output)
println!(
" entries={count}: TQ3={tq3_bytes} bytes, FP16={fp16_bytes} bytes, compression={ratio:.1}x"
);
}
group.finish();
}
// ---------------------------------------------------------------------------
// Groups
// ---------------------------------------------------------------------------
criterion_group!(
benches,
bench_quantize,
bench_dequantize,
bench_quantize_qjl,
bench_inner_product,
bench_precompute_projections,
bench_attention_scores,
bench_memory_comparison,
);
criterion_main!(benches);