|
| 1 | +/** |
| 2 | + * bench_kv_overhead.cpp -- KV cache quantization time microbenchmark |
| 3 | + * |
| 4 | + * Measures wall-clock time for: |
| 5 | + * - uniform_4b quantize per vector |
| 6 | + * - turbo_kv_3b quantize per vector |
| 7 | + * - turbo_kv_1b quantize per vector |
| 8 | + * - turbo_kv_1b attention per key |
| 9 | + * |
| 10 | + * Reports ns/vector for each operation. |
| 11 | + */ |
| 12 | + |
| 13 | +#include <cmath> |
| 14 | +#include <cstdio> |
| 15 | +#include <cstring> |
| 16 | +#include <random> |
| 17 | +#include <vector> |
| 18 | +#include <chrono> |
| 19 | + |
| 20 | +extern "C" { |
| 21 | +#include "turboquant/turboquant.h" |
| 22 | + |
| 23 | +void tq_uniform_4b_quantize_ref(const float* src, void* dst, int n); |
| 24 | +void tq_turbo_kv_3b_quantize_ref(const float* src, void* dst, int n); |
| 25 | +void tq_turbo_kv_1b_quantize_ref(const float* src, void* dst, int n); |
| 26 | +void tq_turbo_kv_1b_attention_ref(const float* query, const void* kv, |
| 27 | + float* scores, int seq_len, int head_dim); |
| 28 | +void tq_turbo_kv_3b_attention_ref(const float* query, const void* kv, |
| 29 | + float* scores, int seq_len, int head_dim); |
| 30 | +} |
| 31 | + |
| 32 | +static const int DIM = 128; |
| 33 | +static const int N_VECTORS = 10000; |
| 34 | +static const int N_WARMUP = 100; |
| 35 | + |
| 36 | +int main(void) { |
| 37 | + printf("=== TurboQuant KV Cache Quantization Overhead ===\n"); |
| 38 | + printf("dim=%d, vectors=%d\n\n", DIM, N_VECTORS); |
| 39 | + |
| 40 | + /* Generate random input vectors */ |
| 41 | + std::mt19937 rng(42); |
| 42 | + std::normal_distribution<float> dist(0.0f, 1.0f); |
| 43 | + |
| 44 | + std::vector<std::vector<float>> vectors(N_VECTORS); |
| 45 | + for (int i = 0; i < N_VECTORS; i++) { |
| 46 | + vectors[i].resize(DIM); |
| 47 | + for (int d = 0; d < DIM; d++) vectors[i][d] = dist(rng); |
| 48 | + } |
| 49 | + |
| 50 | + std::vector<float> query(DIM); |
| 51 | + for (int d = 0; d < DIM; d++) query[d] = dist(rng); |
| 52 | + |
| 53 | + /* === Uniform 4-bit quantize === */ |
| 54 | + { |
| 55 | + std::vector<block_tq_uniform_4b> blocks(N_VECTORS); |
| 56 | + |
| 57 | + /* Warmup */ |
| 58 | + for (int i = 0; i < N_WARMUP; i++) { |
| 59 | + tq_uniform_4b_quantize_ref(vectors[i].data(), &blocks[i], DIM); |
| 60 | + } |
| 61 | + |
| 62 | + auto t0 = std::chrono::high_resolution_clock::now(); |
| 63 | + for (int i = 0; i < N_VECTORS; i++) { |
| 64 | + tq_uniform_4b_quantize_ref(vectors[i].data(), &blocks[i], DIM); |
| 65 | + } |
| 66 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 67 | + |
| 68 | + double ns = std::chrono::duration<double, std::nano>(t1 - t0).count(); |
| 69 | + printf(" uniform_4b quantize: %8.1f ns/vector\n", ns / N_VECTORS); |
| 70 | + } |
| 71 | + |
| 72 | + /* === TurboKV 3-bit quantize === */ |
| 73 | + { |
| 74 | + std::vector<block_tq_turbo_kv_3b> blocks(N_VECTORS); |
| 75 | + |
| 76 | + for (int i = 0; i < N_WARMUP; i++) { |
| 77 | + tq_turbo_kv_3b_quantize_ref(vectors[i].data(), &blocks[i], DIM); |
| 78 | + } |
| 79 | + |
| 80 | + auto t0 = std::chrono::high_resolution_clock::now(); |
| 81 | + for (int i = 0; i < N_VECTORS; i++) { |
| 82 | + tq_turbo_kv_3b_quantize_ref(vectors[i].data(), &blocks[i], DIM); |
| 83 | + } |
| 84 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 85 | + |
| 86 | + double ns = std::chrono::duration<double, std::nano>(t1 - t0).count(); |
| 87 | + printf(" turbo_kv_3b quantize: %8.1f ns/vector\n", ns / N_VECTORS); |
| 88 | + } |
| 89 | + |
| 90 | + /* === TurboKV 1-bit quantize === */ |
| 91 | + { |
| 92 | + std::vector<block_tq_turbo_kv_1b> blocks(N_VECTORS); |
| 93 | + |
| 94 | + for (int i = 0; i < N_WARMUP; i++) { |
| 95 | + tq_turbo_kv_1b_quantize_ref(vectors[i].data(), &blocks[i], DIM); |
| 96 | + } |
| 97 | + |
| 98 | + auto t0 = std::chrono::high_resolution_clock::now(); |
| 99 | + for (int i = 0; i < N_VECTORS; i++) { |
| 100 | + tq_turbo_kv_1b_quantize_ref(vectors[i].data(), &blocks[i], DIM); |
| 101 | + } |
| 102 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 103 | + |
| 104 | + double ns = std::chrono::duration<double, std::nano>(t1 - t0).count(); |
| 105 | + printf(" turbo_kv_1b quantize: %8.1f ns/vector\n", ns / N_VECTORS); |
| 106 | + } |
| 107 | + |
| 108 | + /* === TurboKV 1-bit attention per key === */ |
| 109 | + { |
| 110 | + /* Pre-quantize all keys */ |
| 111 | + std::vector<block_tq_turbo_kv_1b> blocks(N_VECTORS); |
| 112 | + for (int i = 0; i < N_VECTORS; i++) { |
| 113 | + tq_turbo_kv_1b_quantize_ref(vectors[i].data(), &blocks[i], DIM); |
| 114 | + } |
| 115 | + |
| 116 | + std::vector<float> scores(N_VECTORS); |
| 117 | + |
| 118 | + /* Warmup */ |
| 119 | + tq_turbo_kv_1b_attention_ref(query.data(), blocks.data(), |
| 120 | + scores.data(), N_WARMUP, DIM); |
| 121 | + |
| 122 | + auto t0 = std::chrono::high_resolution_clock::now(); |
| 123 | + tq_turbo_kv_1b_attention_ref(query.data(), blocks.data(), |
| 124 | + scores.data(), N_VECTORS, DIM); |
| 125 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 126 | + |
| 127 | + double ns = std::chrono::duration<double, std::nano>(t1 - t0).count(); |
| 128 | + printf(" turbo_kv_1b attention: %8.1f ns/key\n", ns / N_VECTORS); |
| 129 | + } |
| 130 | + |
| 131 | + /* === TurboKV 3-bit attention per key === */ |
| 132 | + { |
| 133 | + std::vector<block_tq_turbo_kv_3b> blocks(N_VECTORS); |
| 134 | + for (int i = 0; i < N_VECTORS; i++) { |
| 135 | + tq_turbo_kv_3b_quantize_ref(vectors[i].data(), &blocks[i], DIM); |
| 136 | + } |
| 137 | + |
| 138 | + std::vector<float> scores(N_VECTORS); |
| 139 | + |
| 140 | + tq_turbo_kv_3b_attention_ref(query.data(), blocks.data(), |
| 141 | + scores.data(), N_WARMUP, DIM); |
| 142 | + |
| 143 | + auto t0 = std::chrono::high_resolution_clock::now(); |
| 144 | + tq_turbo_kv_3b_attention_ref(query.data(), blocks.data(), |
| 145 | + scores.data(), N_VECTORS, DIM); |
| 146 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 147 | + |
| 148 | + double ns = std::chrono::duration<double, std::nano>(t1 - t0).count(); |
| 149 | + printf(" turbo_kv_3b attention: %8.1f ns/key\n", ns / N_VECTORS); |
| 150 | + } |
| 151 | + |
| 152 | + /* === RHT overhead (isolated) === */ |
| 153 | + { |
| 154 | + std::vector<float> buf(DIM); |
| 155 | + |
| 156 | + /* Warmup */ |
| 157 | + for (int i = 0; i < N_WARMUP; i++) { |
| 158 | + std::copy(vectors[i].begin(), vectors[i].end(), buf.begin()); |
| 159 | + tq_rht_transform(buf.data(), DIM, 0x12345678u); |
| 160 | + } |
| 161 | + |
| 162 | + auto t0 = std::chrono::high_resolution_clock::now(); |
| 163 | + for (int i = 0; i < N_VECTORS; i++) { |
| 164 | + std::copy(vectors[i % 1000].begin(), vectors[i % 1000].end(), buf.begin()); |
| 165 | + tq_rht_transform(buf.data(), DIM, 0x12345678u); |
| 166 | + } |
| 167 | + auto t1 = std::chrono::high_resolution_clock::now(); |
| 168 | + |
| 169 | + double ns = std::chrono::duration<double, std::nano>(t1 - t0).count(); |
| 170 | + printf(" RHT transform: %8.1f ns/vector (dim=%d)\n", ns / N_VECTORS, DIM); |
| 171 | + } |
| 172 | + |
| 173 | + printf("\nAll measurements include function call overhead.\n"); |
| 174 | + printf("RHT is O(d log d) per vector; matmul is ~O(d^2) per layer.\n"); |
| 175 | + |
| 176 | + return 0; |
| 177 | +} |
0 commit comments