|
| 1 | +/** |
| 2 | + * @file bench_tritnet_gemm.cpp |
| 3 | + * @brief Benchmark TritNet GEMM performance vs BitNet |
| 4 | + * |
| 5 | + * Measures performance of naive vs AVX2 implementations and compares |
| 6 | + * against BitNet's expected performance. |
| 7 | + * |
| 8 | + * @date 2025-11-23 |
| 9 | + */ |
| 10 | + |
| 11 | +#include "../include/tritnet_gemm.h" |
| 12 | +#include <iostream> |
| 13 | +#include <vector> |
| 14 | +#include <chrono> |
| 15 | +#include <iomanip> |
| 16 | +#include <cstdlib> |
| 17 | +#include <cmath> |
| 18 | + |
| 19 | +using namespace std::chrono; |
| 20 | + |
| 21 | +/** |
| 22 | + * @brief Benchmark configuration for different matrix sizes |
| 23 | + */ |
| 24 | +struct BenchConfig { |
| 25 | + int M, N, K; |
| 26 | + const char* name; |
| 27 | + const char* use_case; |
| 28 | +}; |
| 29 | + |
| 30 | +// Benchmark configurations matching BitNet model sizes |
| 31 | +static const BenchConfig BENCH_CONFIGS[] = { |
| 32 | + // Tiny (for debugging) |
| 33 | + {8, 8, 160, "Tiny", "Debug"}, |
| 34 | + |
| 35 | + // Small (similar to attention heads) |
| 36 | + {32, 64, 512, "Small", "Attention head"}, |
| 37 | + |
| 38 | + // Medium (similar to MLP layers in 2B model) |
| 39 | + {1024, 2048, 4096, "Medium-2B", "MLP layer 2B model"}, |
| 40 | + |
| 41 | + // Large (similar to MLP layers in 7B model) |
| 42 | + {2048, 8192, 8192, "Large-7B", "MLP layer 7B model"}, |
| 43 | + |
| 44 | + // Huge (similar to 100B model) |
| 45 | + {4096, 16384, 16384, "Huge-100B", "MLP layer 100B model"}, |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * @brief Run GEMM and measure time |
| 50 | + */ |
| 51 | +double benchmark_gemm( |
| 52 | + int M, int N, int K, |
| 53 | + const float* A, |
| 54 | + const uint8_t* B, |
| 55 | + float* C, |
| 56 | + int num_runs = 10 |
| 57 | +) { |
| 58 | + // Warm-up run |
| 59 | + tritnet_gemm_f32(M, N, K, A, B, C); |
| 60 | + |
| 61 | + // Timed runs |
| 62 | + auto start = high_resolution_clock::now(); |
| 63 | + |
| 64 | + for (int run = 0; run < num_runs; run++) { |
| 65 | + tritnet_gemm_f32(M, N, K, A, B, C); |
| 66 | + } |
| 67 | + |
| 68 | + auto end = high_resolution_clock::now(); |
| 69 | + auto duration = duration_cast<microseconds>(end - start); |
| 70 | + |
| 71 | + return duration.count() / 1000.0 / num_runs; // Average ms per run |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @brief Calculate GFLOPS for matrix multiply |
| 76 | + */ |
| 77 | +double calculate_gops(int M, int N, int K, double time_ms) { |
| 78 | + // GEMM operations: 2*M*N*K (multiply + add for each element) |
| 79 | + double ops = 2.0 * M * N * K; |
| 80 | + double gops = ops / (time_ms * 1e6); // Convert to Gops/s |
| 81 | + return gops; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief Run full benchmark suite |
| 86 | + */ |
| 87 | +void run_benchmarks() { |
| 88 | + std::cout << "========================================================\n"; |
| 89 | + std::cout << " TritNet GEMM Benchmark Suite\n"; |
| 90 | + std::cout << "========================================================\n\n"; |
| 91 | + |
| 92 | + std::cout << std::setw(12) << "Config" |
| 93 | + << std::setw(15) << "Dimensions" |
| 94 | + << std::setw(12) << "Time (ms)" |
| 95 | + << std::setw(12) << "Gops/s" |
| 96 | + << std::setw(15) << "Memory (MB)" |
| 97 | + << "\n"; |
| 98 | + std::cout << std::string(66, '-') << "\n"; |
| 99 | + |
| 100 | + for (const auto& config : BENCH_CONFIGS) { |
| 101 | + int M = config.M; |
| 102 | + int N = config.N; |
| 103 | + int K = config.K; |
| 104 | + |
| 105 | + // Allocate matrices |
| 106 | + std::vector<float> A(M * K); |
| 107 | + std::vector<uint8_t> B((K / 5) * N); |
| 108 | + std::vector<float> C(M * N); |
| 109 | + |
| 110 | + // Initialize with random data |
| 111 | + for (auto& val : A) { |
| 112 | + val = (float)(rand() % 200 - 100) / 100.0f; |
| 113 | + } |
| 114 | + for (auto& val : B) { |
| 115 | + val = (uint8_t)(rand() % 243); |
| 116 | + } |
| 117 | + |
| 118 | + // Run benchmark |
| 119 | + int num_runs = (M * N * K < 1000000) ? 100 : 10; // More runs for small sizes |
| 120 | + double time_ms = benchmark_gemm(M, N, K, A.data(), B.data(), C.data(), num_runs); |
| 121 | + |
| 122 | + // Calculate metrics |
| 123 | + double gops = calculate_gops(M, N, K, time_ms); |
| 124 | + double memory_mb = (M * K * 4 + (K / 5) * N + M * N * 4) / (1024.0 * 1024.0); |
| 125 | + |
| 126 | + // Print results |
| 127 | + std::cout << std::setw(12) << config.name |
| 128 | + << std::setw(8) << M << "×" << N << "×" << K |
| 129 | + << std::setw(12) << std::fixed << std::setprecision(3) << time_ms |
| 130 | + << std::setw(12) << std::fixed << std::setprecision(2) << gops |
| 131 | + << std::setw(15) << std::fixed << std::setprecision(1) << memory_mb |
| 132 | + << "\n"; |
| 133 | + } |
| 134 | + |
| 135 | + std::cout << "\n========================================================\n"; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * @brief Compare with BitNet expected performance |
| 140 | + */ |
| 141 | +void compare_with_bitnet() { |
| 142 | + std::cout << "\n========================================================\n"; |
| 143 | + std::cout << " Performance Comparison: TritNet vs BitNet\n"; |
| 144 | + std::cout << "========================================================\n\n"; |
| 145 | + |
| 146 | + // Medium config (2B model MLP layer) |
| 147 | + int M = 1024, N = 2048, K = 4096; |
| 148 | + |
| 149 | + std::vector<float> A(M * K); |
| 150 | + std::vector<uint8_t> B((K / 5) * N); |
| 151 | + std::vector<float> C(M * N); |
| 152 | + |
| 153 | + // Initialize |
| 154 | + for (auto& val : A) val = (float)(rand() % 100) / 100.0f; |
| 155 | + for (auto& val : B) val = (uint8_t)(rand() % 243); |
| 156 | + |
| 157 | + // Benchmark TritNet (naive) |
| 158 | + double tritnet_time = benchmark_gemm(M, N, K, A.data(), B.data(), C.data(), 10); |
| 159 | + double tritnet_gops = calculate_gops(M, N, K, tritnet_time); |
| 160 | + |
| 161 | + // BitNet TL2 expected performance (from their benchmarks) |
| 162 | + // On Intel i7, they report 2.37-6.17× speedup over INT8 |
| 163 | + // INT8 GEMM on i7 ~= 50-100 Gops/s |
| 164 | + // BitNet TL2 ~= 118-617 Gops/s (let's estimate ~200 Gops/s for this size) |
| 165 | + double bitnet_estimated_gops = 200.0; // Conservative estimate |
| 166 | + |
| 167 | + // Our target (2-3× faster than BitNet) |
| 168 | + double target_gops = bitnet_estimated_gops * 2.5; // 500 Gops/s |
| 169 | + |
| 170 | + std::cout << std::setw(25) << "Implementation" |
| 171 | + << std::setw(15) << "Gops/s" |
| 172 | + << std::setw(15) << "Speedup" |
| 173 | + << "\n"; |
| 174 | + std::cout << std::string(55, '-') << "\n"; |
| 175 | + |
| 176 | + std::cout << std::setw(25) << "TritNet (Naive)" |
| 177 | + << std::setw(15) << std::fixed << std::setprecision(2) << tritnet_gops |
| 178 | + << std::setw(15) << "1.0×" |
| 179 | + << "\n"; |
| 180 | + |
| 181 | + std::cout << std::setw(25) << "BitNet TL2 (estimated)" |
| 182 | + << std::setw(15) << bitnet_estimated_gops |
| 183 | + << std::setw(15) << std::fixed << std::setprecision(1) |
| 184 | + << (bitnet_estimated_gops / tritnet_gops) << "×" |
| 185 | + << "\n"; |
| 186 | + |
| 187 | + std::cout << std::setw(25) << "TritNet Target (AVX2)" |
| 188 | + << std::setw(15) << target_gops |
| 189 | + << std::setw(15) << (target_gops / tritnet_gops) << "×" |
| 190 | + << "\n"; |
| 191 | + |
| 192 | + std::cout << "\n"; |
| 193 | + |
| 194 | + // Status |
| 195 | + if (tritnet_gops >= target_gops) { |
| 196 | + std::cout << "✅ TARGET ACHIEVED! TritNet is " << (tritnet_gops / bitnet_estimated_gops) |
| 197 | + << "× faster than BitNet\n"; |
| 198 | + } else { |
| 199 | + double gap = (target_gops / tritnet_gops); |
| 200 | + std::cout << "⚠️ Need " << gap << "× speedup to reach target\n"; |
| 201 | + std::cout << " Current: " << tritnet_gops << " Gops/s\n"; |
| 202 | + std::cout << " Target: " << target_gops << " Gops/s\n"; |
| 203 | + std::cout << " Next: Implement AVX2 SIMD optimization\n"; |
| 204 | + } |
| 205 | + |
| 206 | + std::cout << "\n========================================================\n"; |
| 207 | +} |
| 208 | + |
| 209 | +/** |
| 210 | + * @brief Memory bandwidth analysis |
| 211 | + */ |
| 212 | +void analyze_memory_bandwidth() { |
| 213 | + std::cout << "\n========================================================\n"; |
| 214 | + std::cout << " Memory Bandwidth Analysis\n"; |
| 215 | + std::cout << "========================================================\n\n"; |
| 216 | + |
| 217 | + // Medium config |
| 218 | + int M = 1024, N = 2048, K = 4096; |
| 219 | + |
| 220 | + // Calculate memory traffic |
| 221 | + double A_mb = (M * K * 4) / (1024.0 * 1024.0); // FP32 activations |
| 222 | + double B_mb = ((K / 5) * N) / (1024.0 * 1024.0); // Dense243 weights |
| 223 | + double C_mb = (M * N * 4) / (1024.0 * 1024.0); // FP32 output |
| 224 | + |
| 225 | + double total_read = A_mb + B_mb; |
| 226 | + double total_write = C_mb; |
| 227 | + double total_traffic = total_read + total_write; |
| 228 | + |
| 229 | + std::cout << "Matrix dimensions: " << M << "×" << N << "×" << K << "\n\n"; |
| 230 | + |
| 231 | + std::cout << "Memory traffic:\n"; |
| 232 | + std::cout << " A (activations): " << std::fixed << std::setprecision(2) << A_mb << " MB\n"; |
| 233 | + std::cout << " B (weights): " << B_mb << " MB\n"; |
| 234 | + std::cout << " C (output): " << C_mb << " MB\n"; |
| 235 | + std::cout << " Total: " << total_traffic << " MB\n\n"; |
| 236 | + |
| 237 | + // Bandwidth comparison |
| 238 | + std::cout << "Weight format comparison:\n"; |
| 239 | + double bitnet_2bit_mb = (K * N * 2 / 8) / (1024.0 * 1024.0); // 2 bits per weight |
| 240 | + double dense243_mb = B_mb; |
| 241 | + double savings = (1.0 - dense243_mb / bitnet_2bit_mb) * 100.0; |
| 242 | + |
| 243 | + std::cout << " BitNet 2-bit: " << bitnet_2bit_mb << " MB\n"; |
| 244 | + std::cout << " Dense243: " << dense243_mb << " MB\n"; |
| 245 | + std::cout << " Savings: " << savings << "%\n"; |
| 246 | + |
| 247 | + std::cout << "\n========================================================\n"; |
| 248 | +} |
| 249 | + |
| 250 | +/** |
| 251 | + * @brief Main benchmark runner |
| 252 | + */ |
| 253 | +int main(int argc, char** argv) { |
| 254 | + // Set random seed for reproducibility |
| 255 | + srand(42); |
| 256 | + |
| 257 | + std::cout << "\nTritNet Direct Ternary GEMM - Performance Benchmark\n"; |
| 258 | + std::cout << "====================================================\n"; |
| 259 | + std::cout << "Platform: x86-64 AVX2\n"; |
| 260 | + std::cout << "Precision: FP32 activations, Ternary weights\n"; |
| 261 | + std::cout << "Packing: Dense243 (5 trits/byte)\n\n"; |
| 262 | + |
| 263 | + // Run benchmarks |
| 264 | + run_benchmarks(); |
| 265 | + |
| 266 | + // Compare with BitNet |
| 267 | + compare_with_bitnet(); |
| 268 | + |
| 269 | + // Analyze memory bandwidth |
| 270 | + analyze_memory_bandwidth(); |
| 271 | + |
| 272 | + return 0; |
| 273 | +} |
0 commit comments