|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +pub const OutputFormat = enum { JSON, Markdown, CSV }; |
| 4 | + |
| 5 | +pub const BenchmarkConfig = struct { |
| 6 | + name: []const u8, |
| 7 | + warmup_iterations: usize = 10, |
| 8 | + benchmark_iterations: usize = 100, |
| 9 | + output_format: OutputFormat = .JSON, |
| 10 | +}; |
| 11 | + |
| 12 | +pub const BenchmarkResult = struct { |
| 13 | + name: []const u8, |
| 14 | + iterations: usize, |
| 15 | + total_time_ns: u64, |
| 16 | + min_time_ns: u64, |
| 17 | + max_time_ns: u64, |
| 18 | + mean_time_ns: u64, |
| 19 | + std_dev_f: f64, |
| 20 | + ops_per_second: f64, |
| 21 | +}; |
| 22 | + |
| 23 | +pub const BenchmarkSuite = struct { |
| 24 | + allocator: std.mem.Allocator, |
| 25 | + results: std.ArrayList(BenchmarkResult), |
| 26 | + config: BenchmarkConfig, |
| 27 | + |
| 28 | + const Self = @This(); |
| 29 | + |
| 30 | + pub fn init(allocator: std.mem.Allocator, config: BenchmarkConfig) Self { |
| 31 | + return Self{ |
| 32 | + .allocator = allocator, |
| 33 | + .results = std.ArrayList(BenchmarkResult).init(allocator), |
| 34 | + .config = config, |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + pub fn deinit(self: *Self) void { |
| 39 | + self.results.deinit(); |
| 40 | + } |
| 41 | + |
| 42 | + pub fn runAll(self: *Self) !void { |
| 43 | + std.log.info("Running Trinity S³AI Benchmark Suite", .{}); |
| 44 | + try self.benchmarkVSABind(); |
| 45 | + try self.benchmarkVSABundle(); |
| 46 | + try self.benchmarkVSACosine(); |
| 47 | + try self.generateReport(); |
| 48 | + } |
| 49 | + |
| 50 | + fn benchmarkVSABind(self: *Self) !void { |
| 51 | + const dim: usize = 1024; |
| 52 | + const iterations = self.config.benchmark_iterations; |
| 53 | + const warmup = self.config.warmup_iterations; |
| 54 | + |
| 55 | + const a = try self.allocator.alloc(i8, dim); |
| 56 | + defer self.allocator.free(a); |
| 57 | + const b = try self.allocator.alloc(i8, dim); |
| 58 | + defer self.allocator.free(b); |
| 59 | + const result = try self.allocator.alloc(i8, dim); |
| 60 | + defer self.allocator.free(result); |
| 61 | + |
| 62 | + var prng = std.Random.DefaultPrng.init(0x52455539); |
| 63 | + const rng = prng.random(); |
| 64 | + for (0..dim) |i| { |
| 65 | + a[i] = rng.intRangeAtMost(i8, -1, 2); |
| 66 | + b[i] = rng.intRangeAtMost(i8, -1, 2); |
| 67 | + } |
| 68 | + |
| 69 | + var i: usize = 0; |
| 70 | + while (i < warmup) : (i += 1) { |
| 71 | + _ = self.bindOp(a, b, result); |
| 72 | + } |
| 73 | + |
| 74 | + const times = try self.allocator.alloc(u64, iterations); |
| 75 | + defer self.allocator.free(times); |
| 76 | + |
| 77 | + i = 0; |
| 78 | + while (i < iterations) : (i += 1) { |
| 79 | + const start = std.time.nanoTimestamp(); |
| 80 | + _ = self.bindOp(a, b, result); |
| 81 | + const end = std.time.nanoTimestamp(); |
| 82 | + times[i] = @as(u64, @intCast(end - start)); |
| 83 | + } |
| 84 | + |
| 85 | + try self.addResult("VSA Bind", dim, iterations, times); |
| 86 | + } |
| 87 | + |
| 88 | + fn benchmarkVSABundle(self: *Self) !void { |
| 89 | + const dim: usize = 1024; |
| 90 | + const iterations = self.config.benchmark_iterations; |
| 91 | + const warmup = self.config.warmup_iterations; |
| 92 | + |
| 93 | + const a = try self.allocator.alloc(i8, dim); |
| 94 | + defer self.allocator.free(a); |
| 95 | + const b = try self.allocator.alloc(i8, dim); |
| 96 | + defer self.allocator.free(b); |
| 97 | + const c = try self.allocator.alloc(i8, dim); |
| 98 | + defer self.allocator.free(c); |
| 99 | + const result = try self.allocator.alloc(i8, dim); |
| 100 | + defer self.allocator.free(result); |
| 101 | + |
| 102 | + var prng = std.Random.DefaultPrng.init(0x52455539); |
| 103 | + const rng = prng.random(); |
| 104 | + for (0..dim) |i| { |
| 105 | + a[i] = rng.intRangeAtMost(i8, -1, 2); |
| 106 | + b[i] = rng.intRangeAtMost(i8, -1, 2); |
| 107 | + c[i] = rng.intRangeAtMost(i8, -1, 2); |
| 108 | + } |
| 109 | + |
| 110 | + var i: usize = 0; |
| 111 | + while (i < warmup) : (i += 1) { |
| 112 | + _ = self.bundle3Op(a, b, c, result); |
| 113 | + } |
| 114 | + |
| 115 | + const times = try self.allocator.alloc(u64, iterations); |
| 116 | + defer self.allocator.free(times); |
| 117 | + |
| 118 | + i = 0; |
| 119 | + while (i < iterations) : (i += 1) { |
| 120 | + const start = std.time.nanoTimestamp(); |
| 121 | + _ = self.bundle3Op(a, b, c, result); |
| 122 | + const end = std.time.nanoTimestamp(); |
| 123 | + times[i] = @as(u64, @intCast(end - start)); |
| 124 | + } |
| 125 | + |
| 126 | + try self.addResult("VSA Bundle3", dim, iterations, times); |
| 127 | + } |
| 128 | + |
| 129 | + fn benchmarkVSACosine(self: *Self) !void { |
| 130 | + const dim: usize = 1024; |
| 131 | + const iterations = self.config.benchmark_iterations; |
| 132 | + const warmup = self.config.warmup_iterations; |
| 133 | + |
| 134 | + const a = try self.allocator.alloc(i8, dim); |
| 135 | + defer self.allocator.free(a); |
| 136 | + const b = try self.allocator.alloc(i8, dim); |
| 137 | + defer self.allocator.free(b); |
| 138 | + |
| 139 | + var prng = std.Random.DefaultPrng.init(0x52455539); |
| 140 | + const rng = prng.random(); |
| 141 | + for (0..dim) |i| { |
| 142 | + a[i] = rng.intRangeAtMost(i8, -1, 2); |
| 143 | + b[i] = rng.intRangeAtMost(i8, -1, 2); |
| 144 | + } |
| 145 | + |
| 146 | + var i: usize = 0; |
| 147 | + while (i < warmup) : (i += 1) { |
| 148 | + _ = self.cosineSimilarityOp(a, b); |
| 149 | + } |
| 150 | + |
| 151 | + const times = try self.allocator.alloc(u64, iterations); |
| 152 | + defer self.allocator.free(times); |
| 153 | + |
| 154 | + i = 0; |
| 155 | + while (i < iterations) : (i += 1) { |
| 156 | + const start = std.time.nanoTimestamp(); |
| 157 | + _ = self.cosineSimilarityOp(a, b); |
| 158 | + const end = std.time.nanoTimestamp(); |
| 159 | + times[i] = @as(u64, @intCast(end - start)); |
| 160 | + } |
| 161 | + |
| 162 | + try self.addResult("VSA Cosine Similarity", dim, iterations, times); |
| 163 | + } |
| 164 | + |
| 165 | + fn addResult(self: *Self, name: []const u8, ops_per_iter: usize, iterations: usize, times: []u64) !void { |
| 166 | + var total_time: u64 = 0; |
| 167 | + for (times) |t| { |
| 168 | + total_time += t; |
| 169 | + } |
| 170 | + const mean_time = total_time / iterations; |
| 171 | + |
| 172 | + var min_time: u64 = times[0]; |
| 173 | + var max_time: u64 = times[0]; |
| 174 | + for (times) |t| { |
| 175 | + if (t < min_time) min_time = t; |
| 176 | + if (t > max_time) max_time = t; |
| 177 | + } |
| 178 | + |
| 179 | + var variance_f: f64 = 0; |
| 180 | + for (times) |t| { |
| 181 | + const diff_f = @as(f64, @floatFromInt(t)) - @as(f64, @floatFromInt(mean_time)); |
| 182 | + variance_f += diff_f * diff_f; |
| 183 | + } |
| 184 | + variance_f /= @as(f64, @floatFromInt(iterations)); |
| 185 | + const std_dev = @sqrt(variance_f); |
| 186 | + |
| 187 | + const ops_per_sec = @as(f64, @floatFromInt(ops_per_iter * iterations)) / |
| 188 | + @as(f64, @floatFromInt(total_time)) * 1_000_000_000; |
| 189 | + |
| 190 | + try self.results.append(self.allocator, BenchmarkResult{ |
| 191 | + .name = name, |
| 192 | + .iterations = iterations, |
| 193 | + .total_time_ns = total_time, |
| 194 | + .min_time_ns = min_time, |
| 195 | + .max_time_ns = max_time, |
| 196 | + .mean_time_ns = mean_time, |
| 197 | + .std_dev_f = std_dev, |
| 198 | + .ops_per_second = ops_per_sec, |
| 199 | + }); |
| 200 | + |
| 201 | + std.log.info("{s}: {d:.2} ops/sec ({d:.0} ns/op)", .{ name, ops_per_sec, @as(f64, @floatFromInt(mean_time)) }); |
| 202 | + } |
| 203 | + |
| 204 | + fn generateReport(self: *Self) !void { |
| 205 | + const stdout = std.io.getStdOut().writer(); |
| 206 | + |
| 207 | + switch (self.config.output_format) { |
| 208 | + .JSON => try self.generateJSONReport(stdout), |
| 209 | + .Markdown => try self.generateMarkdownReport(stdout), |
| 210 | + .CSV => try self.generateCSVReport(stdout), |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + fn generateJSONReport(self: *Self, writer: anytype) !void { |
| 215 | + try writer.writeAll("{\"benchmarks\":[\n"); |
| 216 | + for (self.results.items, 0..) |result, i| { |
| 217 | + if (i > 0) try writer.writeAll(",\n"); |
| 218 | + try writer.print( |
| 219 | + \\{{"name":"{s}","iterations":{d},"ops_per_second":{d:.2},"mean_ns":{d},"min_ns":{d},"max_ns":{d},"std_dev_f":{d:.2}}} |
| 220 | + , .{ result.name, result.iterations, result.ops_per_second, result.mean_time_ns, result.min_time_ns, result.max_time_ns, result.std_dev_f }); |
| 221 | + } |
| 222 | + try writer.writeAll("\n],\"summary\":{\"total_benchmarks\":{d}}}\n", .{self.results.items.len}); |
| 223 | + } |
| 224 | + |
| 225 | + fn generateMarkdownReport(self: *Self, writer: anytype) !void { |
| 226 | + try writer.writeAll("\n# Benchmark Results\n\n"); |
| 227 | + try writer.writeAll("| Benchmark | Ops/sec | Mean (ns) | Min (ns) | Max (ns) | Std Dev |\n"); |
| 228 | + try writer.writeAll("|-----------|---------|----------|----------|----------|--------|\n"); |
| 229 | + |
| 230 | + for (self.results.items) |result| { |
| 231 | + try writer.print("| {s} | {d:.2} | {d} | {d} | {d} | {d:.2} |\n", .{ result.name, result.ops_per_second, result.mean_time_ns, result.min_time_ns, result.max_time_ns, result.std_dev_f }); |
| 232 | + } |
| 233 | + try writer.writeAll("\n## Summary\n\n"); |
| 234 | + try writer.print("- Total Benchmarks: {d}\n", .{self.results.items.len}); |
| 235 | + try writer.writeAll("- Status: PASSED\n\n"); |
| 236 | + } |
| 237 | + |
| 238 | + fn generateCSVReport(self: *Self, writer: anytype) !void { |
| 239 | + try writer.writeAll("name,ops_per_second,mean_ns,min_ns,max_ns,std_dev_f\n"); |
| 240 | + |
| 241 | + for (self.results.items) |result| { |
| 242 | + try writer.print("{s},{d:.2},{d},{d},{d},{d:.2}\n", .{ result.name, result.ops_per_second, result.mean_time_ns, result.min_time_ns, result.max_time_ns, result.std_dev_f }); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + fn bindOp(self: *Self, a: []const i8, b: []const i8, result: []i8) void { |
| 247 | + _ = self; |
| 248 | + for (0..a.len) |i| { |
| 249 | + result[i] = a[i] * b[i]; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + fn bundle3Op(self: *Self, a: []const i8, b: []const i8, c: []const i8, result: []i8) void { |
| 254 | + _ = self; |
| 255 | + for (0..a.len) |i| { |
| 256 | + const sum = a[i] + b[i] + c[i]; |
| 257 | + result[i] = if (sum > 0) 1 else if (sum < 0) -1 else 0; |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + fn cosineSimilarityOp(self: *Self, a: []const i8, b: []const i8) f64 { |
| 262 | + _ = self; |
| 263 | + var dot: i64 = 0; |
| 264 | + var norm_a: i64 = 0; |
| 265 | + var norm_b: i64 = 0; |
| 266 | + |
| 267 | + for (0..a.len) |i| { |
| 268 | + dot += @as(i64, a[i]) * @as(i64, b[i]); |
| 269 | + norm_a += @as(i64, a[i]) * @as(i64, a[i]); |
| 270 | + norm_b += @as(i64, b[i]) * @as(i64, b[i]); |
| 271 | + } |
| 272 | + |
| 273 | + const norm_a_f = @sqrt(@as(f64, @floatFromInt(norm_a))); |
| 274 | + const norm_b_f = @sqrt(@as(f64, @floatFromInt(norm_b))); |
| 275 | + |
| 276 | + if (norm_a_f == 0 or norm_b_f == 0) return 0.0; |
| 277 | + return @as(f64, @floatFromInt(dot)) / (norm_a_f * norm_b_f); |
| 278 | + } |
| 279 | +}; |
| 280 | + |
| 281 | +pub fn main() !void { |
| 282 | + const allocator = std.heap.page_allocator; |
| 283 | + |
| 284 | + var config = BenchmarkConfig{ |
| 285 | + .name = "Trinity S³AI Benchmark Suite", |
| 286 | + .warmup_iterations = 10, |
| 287 | + .benchmark_iterations = 100, |
| 288 | + .output_format = .JSON, |
| 289 | + }; |
| 290 | + |
| 291 | + var suite = BenchmarkSuite.init(allocator, config); |
| 292 | + defer suite.deinit(); |
| 293 | + |
| 294 | + try suite.runAll(); |
| 295 | +} |
| 296 | + |
| 297 | +test "vsa_bind_operation" { |
| 298 | + const allocator = std.testing.allocator; |
| 299 | + const a = try allocator.alloc(i8, 4); |
| 300 | + defer allocator.free(a); |
| 301 | + const b = try allocator.alloc(i8, 4); |
| 302 | + defer allocator.free(b); |
| 303 | + const result = try allocator.alloc(i8, 4); |
| 304 | + defer allocator.free(result); |
| 305 | + |
| 306 | + a[0] = 1; |
| 307 | + a[1] = -1; |
| 308 | + a[2] = 0; |
| 309 | + a[3] = 1; |
| 310 | + |
| 311 | + b[0] = 1; |
| 312 | + b[1] = 1; |
| 313 | + b[2] = -1; |
| 314 | + b[3] = 0; |
| 315 | + |
| 316 | + var suite = BenchmarkSuite.init(allocator, BenchmarkConfig{ |
| 317 | + .name = "test", |
| 318 | + .warmup_iterations = 1, |
| 319 | + .benchmark_iterations = 1, |
| 320 | + .output_format = .JSON, |
| 321 | + }); |
| 322 | + defer suite.deinit(); |
| 323 | + |
| 324 | + suite.bindOp(a, b, result); |
| 325 | + |
| 326 | + try std.testing.expectEqual(@as(i8, 1), result[0]); |
| 327 | + try std.testing.expectEqual(@as(i8, -1), result[1]); |
| 328 | + try std.testing.expectEqual(@as(i8, 0), result[2]); |
| 329 | + try std.testing.expectEqual(@as(i8, 0), result[3]); |
| 330 | +} |
| 331 | + |
| 332 | +test "vsa_bundle3_operation" { |
| 333 | + const allocator = std.testing.allocator; |
| 334 | + const a = try allocator.alloc(i8, 3); |
| 335 | + defer allocator.free(a); |
| 336 | + const b = try allocator.alloc(i8, 3); |
| 337 | + defer allocator.free(b); |
| 338 | + const c = try allocator.alloc(i8, 3); |
| 339 | + defer allocator.free(c); |
| 340 | + const result = try allocator.alloc(i8, 3); |
| 341 | + defer allocator.free(result); |
| 342 | + |
| 343 | + a[0] = 1; |
| 344 | + a[1] = -1; |
| 345 | + a[2] = 1; |
| 346 | + |
| 347 | + b[0] = 1; |
| 348 | + b[1] = 1; |
| 349 | + b[2] = 0; |
| 350 | + |
| 351 | + c[0] = -1; |
| 352 | + c[1] = -1; |
| 353 | + c[2] = 0; |
| 354 | + |
| 355 | + var suite = BenchmarkSuite.init(allocator, BenchmarkConfig{ |
| 356 | + .name = "test", |
| 357 | + .warmup_iterations = 1, |
| 358 | + .benchmark_iterations = 1, |
| 359 | + .output_format = .JSON, |
| 360 | + }); |
| 361 | + defer suite.deinit(); |
| 362 | + |
| 363 | + suite.bundle3Op(a, b, c, result); |
| 364 | + |
| 365 | + try std.testing.expectEqual(@as(i8, 1), result[0]); |
| 366 | + try std.testing.expectEqual(@as(i8, -1), result[1]); |
| 367 | + try std.testing.expectEqual(@as(i8, 0), result[2]); |
| 368 | +} |
0 commit comments