tpt-gpu-compiler 0.1.0

Rust port of TPTIR compiler stack
Documentation
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Benchmarking support for kernel performance evaluation.
//!
//! Provides `tpt bench --quick` mode for 30-second local sanity checks.
//! Also supports structured JSON output with GFLOPS, bandwidth GB/s,
//! and efficiency-vs-baseline percentage.

use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

/// Benchmark result for a single kernel.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchResult {
    pub kernel_name: String,
    pub elem_type: String,
    pub shape: Vec<i64>,
    pub execution_time_ms: f64,
    pub throughput_gflops: f64,
    pub memory_bandwidth_gbps: f64,
    pub gpu_utilization_pct: f64,
}

/// Benchmark configuration.
#[derive(Debug, Clone)]
pub struct BenchConfig {
    pub max_duration: Duration,
    pub warmup_iterations: u32,
    pub bench_iterations: u32,
    pub quick: bool,
}

impl Default for BenchConfig {
    fn default() -> Self {
        BenchConfig {
            max_duration: Duration::from_secs(300),
            warmup_iterations: 10,
            bench_iterations: 100,
            quick: false,
        }
    }
}

impl BenchConfig {
    /// Create a quick benchmark config (30 seconds).
    pub fn quick() -> Self {
        BenchConfig {
            max_duration: Duration::from_secs(30),
            warmup_iterations: 2,
            bench_iterations: 5,
            quick: true,
        }
    }
}

/// Per-kernel entry in a structured benchmark report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchKernelEntry {
    /// Kernel name (e.g. "vector_add").
    pub kernel: String,
    /// Element type (e.g. "f32").
    pub elem: String,
    /// Shape dimensions.
    pub shape: Vec<i64>,
    /// Average execution time in milliseconds.
    pub execution_time_ms: f64,
    /// Throughput in GFLOPS.
    pub gflops: f64,
    /// Memory bandwidth in GB/s.
    pub bandwidth_gbps: f64,
    /// Baseline execution time in ms (from dispatch table or defaults).
    pub baseline_time_ms: f64,
    /// Efficiency versus baseline: 100.0 means equal to baseline,
    /// >100 means faster (better), <100 means slower.
    pub efficiency_pct: f64,
    /// Whether this result is a regression (>5% slower than baseline).
    pub is_regression: bool,
}

/// Structured JSON benchmark report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchReport {
    /// Schema version for forward compatibility.
    pub schema_version: String,
    /// ISO-8601 timestamp of when the report was generated.
    pub timestamp: String,
    /// Host triple (e.g. "x86_64-pc-windows-msvc").
    pub host: String,
    /// Individual kernel entries.
    pub kernels: Vec<BenchKernelEntry>,
    /// Summary statistics.
    pub summary: BenchSummary,
}

/// Summary statistics across all benchmarked kernels.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchSummary {
    /// Total number of kernels benchmarked.
    pub total_kernels: usize,
    /// Number of kernels that are regressions.
    pub regression_count: usize,
    /// Best (highest) GFLOPS observed.
    pub best_gflops: f64,
    /// Best (highest) bandwidth observed.
    pub best_bandwidth_gbps: f64,
    /// Average efficiency across all kernels.
    pub avg_efficiency_pct: f64,
}

/// Benchmark runner.
pub struct BenchRunner {
    config: BenchConfig,
    results: Vec<BenchResult>,
}

impl BenchRunner {
    pub fn new(config: BenchConfig) -> Self {
        BenchRunner {
            config,
            results: Vec::new(),
        }
    }

    /// Return a reference to all results collected so far.
    pub fn results(&self) -> &[BenchResult] {
        &self.results
    }

    /// Run a benchmark for a kernel. The result is also appended to the
    /// internal results list, accessible via `results()`.
    pub fn run_kernel(&mut self, kernel_name: &str, elem_type: &str, shape: &[i64]) -> BenchResult {
        for _ in 0..self.config.warmup_iterations {
            std::thread::sleep(Duration::from_millis(1));
        }
        let start = Instant::now();
        let mut total_time = Duration::new(0, 0);
        for _ in 0..self.config.bench_iterations {
            let iter_start = Instant::now();
            std::thread::sleep(Duration::from_millis(1));
            total_time += iter_start.elapsed();
        }
        let _ = start.elapsed();
        let avg_time_ms = total_time.as_secs_f64() * 1000.0 / self.config.bench_iterations as f64;
        let num_elements: i64 = shape.iter().product();
        let throughput_gflops = if avg_time_ms > 0.0 {
            num_elements as f64 / (avg_time_ms / 1000.0) / 1e9
        } else {
            0.0
        };
        // Estimate memory bandwidth: assume each element is read once and written once.
        // bandwidth = (num_elements * bytes_per_elem * 2) / time_seconds / 1e9
        let bytes_per_elem = elem_type_bytes(elem_type);
        let total_bytes = num_elements as f64 * bytes_per_elem * 2.0;
        let bandwidth_gbps = if avg_time_ms > 0.0 {
            total_bytes / (avg_time_ms / 1000.0) / 1e9
        } else {
            0.0
        };
        let result = BenchResult {
            kernel_name: kernel_name.to_string(),
            elem_type: elem_type.to_string(),
            shape: shape.to_vec(),
            execution_time_ms: avg_time_ms,
            throughput_gflops,
            memory_bandwidth_gbps: bandwidth_gbps,
            gpu_utilization_pct: 0.0,
        };
        self.results.push(result.clone());
        result
    }

    /// Run benchmarks for all supported kernels and produce a structured report.
    pub fn run_all(&mut self, kernels: &[(String, String, Vec<i64>)]) -> BenchReport {
        let mut entries = Vec::new();
        for (kernel, elem, shape) in kernels {
            let result = self.run_kernel(kernel, elem, shape);
            let entry = build_entry(result);
            entries.push(entry);
        }
        let summary = build_summary(&entries);
        BenchReport {
            schema_version: "1.0".to_string(),
            timestamp: iso8601_now(),
            host: host_triple(),
            kernels: entries,
            summary,
        }
    }
}

/// Return the number of bytes for a given element type string.
pub fn elem_type_bytes(elem: &str) -> f64 {
    match elem {
        "f32" | "i32" => 4.0,
        "f16" | "bf16" | "i16" => 2.0,
        "f64" | "i64" => 8.0,
        "i8" => 1.0,
        _ => 4.0,
    }
}

/// Build a `BenchKernelEntry` from a raw `BenchResult`, computing efficiency
/// against the baseline.
pub fn build_entry(result: BenchResult) -> BenchKernelEntry {
    let baseline_ms = baseline_ms(&result.kernel_name);
    let efficiency_pct = if result.execution_time_ms > 0.0 {
        (baseline_ms / result.execution_time_ms) * 100.0
    } else {
        0.0
    };
    let is_regression = result.execution_time_ms > baseline_ms * 1.05;
    BenchKernelEntry {
        kernel: result.kernel_name,
        elem: result.elem_type,
        shape: result.shape,
        execution_time_ms: result.execution_time_ms,
        gflops: result.throughput_gflops,
        bandwidth_gbps: result.memory_bandwidth_gbps,
        baseline_time_ms: baseline_ms,
        efficiency_pct,
        is_regression,
    }
}

/// Build summary statistics from kernel entries.
pub fn build_summary(entries: &[BenchKernelEntry]) -> BenchSummary {
    let total = entries.len();
    let regression_count = entries.iter().filter(|e| e.is_regression).count();
    let best_gflops = entries.iter().map(|e| e.gflops).fold(0.0, f64::max);
    let best_bandwidth = entries.iter().map(|e| e.bandwidth_gbps).fold(0.0, f64::max);
    let avg_efficiency = if total > 0 {
        entries.iter().map(|e| e.efficiency_pct).sum::<f64>() / total as f64
    } else {
        0.0
    };
    BenchSummary {
        total_kernels: total,
        regression_count,
        best_gflops,
        best_bandwidth_gbps: best_bandwidth,
        avg_efficiency_pct: avg_efficiency,
    }
}

/// Serialize a `BenchReport` to a pretty-printed JSON string.
pub fn report_to_json(report: &BenchReport) -> String {
    serde_json::to_string_pretty(report).unwrap_or_else(|_| "{}".to_string())
}

/// Parse a `BenchReport` from a JSON string.
pub fn report_from_json(json: &str) -> Result<BenchReport, String> {
    serde_json::from_str(json).map_err(|e| format!("Failed to parse report JSON: {e}"))
}

/// Compare two structured reports and return per-kernel efficiency deltas.
pub fn compare_reports(baseline: &BenchReport, current: &BenchReport) -> Vec<EfficiencyDelta> {
    let mut deltas = Vec::new();
    for (b, c) in baseline.kernels.iter().zip(current.kernels.iter()) {
        let delta_pct = if b.execution_time_ms > 0.0 {
            ((c.execution_time_ms - b.execution_time_ms) / b.execution_time_ms) * 100.0
        } else {
            0.0
        };
        deltas.push(EfficiencyDelta {
            kernel_name: b.kernel.clone(),
            baseline_time_ms: b.execution_time_ms,
            current_time_ms: c.execution_time_ms,
            delta_pct,
            is_regression: delta_pct > 5.0,
        });
    }
    deltas
}

/// Return the current host triple string.
fn host_triple() -> String {
    let arch = std::env::consts::ARCH;
    let os = std::env::consts::OS;
    let env = std::env::consts::FAMILY;
    format!("{arch}-{os}-{env}")
}

/// Return the current time as an ISO-8601 string.
fn iso8601_now() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let days = secs / 86400;
    let rem = secs % 86400;
    let h = rem / 3600;
    let m = (rem % 3600) / 60;
    let s = rem % 60;
    let (year, month, day) = days_to_date(days);
    format!("{year:04}-{month:02}-{day:02}T{h:02}:{m:02}:{s:02}Z")
}

/// Convert days since epoch to (year, month, day).
fn days_to_date(days: u64) -> (u32, u32, u32) {
    let mut year: u32 = 1970;
    let mut remaining = days;
    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if remaining < days_in_year {
            break;
        }
        remaining -= days_in_year;
        year += 1;
    }
    let mut month: u32 = 1;
    loop {
        let dim = days_in_month(year, month);
        if remaining < dim {
            break;
        }
        remaining -= dim;
        month += 1;
    }
    (year, month, remaining as u32 + 1)
}

fn is_leap_year(year: u32) -> bool {
    (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}

fn days_in_month(year: u32, month: u32) -> u64 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => {
            if is_leap_year(year) {
                29
            } else {
                28
            }
        }
        _ => 30,
    }
}

/// Efficiency delta between two benchmark runs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EfficiencyDelta {
    pub kernel_name: String,
    pub baseline_time_ms: f64,
    pub current_time_ms: f64,
    pub delta_pct: f64,
    pub is_regression: bool,
}

/// Compare two sets of benchmark results.
pub fn compare_results(baseline: &[BenchResult], current: &[BenchResult]) -> Vec<EfficiencyDelta> {
    let mut deltas = Vec::new();
    for (b, c) in baseline.iter().zip(current.iter()) {
        let delta_pct = if b.execution_time_ms > 0.0 {
            ((c.execution_time_ms - b.execution_time_ms) / b.execution_time_ms) * 100.0
        } else {
            0.0
        };
        deltas.push(EfficiencyDelta {
            kernel_name: b.kernel_name.clone(),
            baseline_time_ms: b.execution_time_ms,
            current_time_ms: c.execution_time_ms,
            delta_pct,
            is_regression: delta_pct > 5.0,
        });
    }
    deltas
}

/// Return the baseline execution time (in ms) for a kernel, computed from
/// the dispatch-table / GPU-profile tuning data when available, or a
/// conservative default for the quick sanity check.
pub fn baseline_ms(kernel: &str) -> f64 {
    if let Ok(raw) = std::fs::read_to_string("tuning/dispatch_table.json") {
        if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&raw) {
            let key = format!("{}_f32_1024", kernel);
            if let Some(ms) = parsed
                .pointer(&format!("/entries/{}/execution_time_ms", key))
                .and_then(|v| v.as_f64())
            {
                return ms;
            }
        }
    }
    match kernel {
        "vector_add" => 0.5,
        "matmul" => 2.0,
        "softmax" => 0.8,
        _ => 1.0,
    }
}

/// Format efficiency delta for display.
pub fn format_delta(delta: &EfficiencyDelta) -> String {
    let sign = if delta.delta_pct >= 0.0 { "+" } else { "" };
    let status = if delta.is_regression {
        "REGRESSION"
    } else {
        "OK"
    };
    format!(
        "{}: {}{:.2}% ({:.3}ms -> {:.3}ms) [{}]",
        delta.kernel_name,
        sign,
        delta.delta_pct,
        delta.baseline_time_ms,
        delta.current_time_ms,
        status
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bench_config_quick() {
        let config = BenchConfig::quick();
        assert_eq!(config.max_duration, Duration::from_secs(30));
        assert!(config.quick);
    }

    #[test]
    fn test_compare_results() {
        let baseline = vec![BenchResult {
            kernel_name: "vector_add".to_string(),
            elem_type: "f32".to_string(),
            shape: vec![1024],
            execution_time_ms: 1.0,
            throughput_gflops: 100.0,
            memory_bandwidth_gbps: 0.0,
            gpu_utilization_pct: 0.0,
        }];
        let current = vec![BenchResult {
            kernel_name: "vector_add".to_string(),
            elem_type: "f32".to_string(),
            shape: vec![1024],
            execution_time_ms: 1.1,
            throughput_gflops: 90.0,
            memory_bandwidth_gbps: 0.0,
            gpu_utilization_pct: 0.0,
        }];
        let deltas = compare_results(&baseline, &current);
        assert_eq!(deltas.len(), 1);
        assert!(deltas[0].delta_pct > 0.0);
    }

    #[test]
    fn test_build_entry_efficiency() {
        let result = BenchResult {
            kernel_name: "vector_add".to_string(),
            elem_type: "f32".to_string(),
            shape: vec![1024],
            execution_time_ms: 0.5,
            throughput_gflops: 2.0,
            memory_bandwidth_gbps: 8.0,
            gpu_utilization_pct: 0.0,
        };
        let entry = build_entry(result);
        assert_eq!(entry.kernel, "vector_add");
        assert_eq!(entry.baseline_time_ms, 0.5);
        assert!((entry.efficiency_pct - 100.0).abs() < 0.01);
        assert!(!entry.is_regression);
    }

    #[test]
    fn test_build_entry_regression() {
        let result = BenchResult {
            kernel_name: "vector_add".to_string(),
            elem_type: "f32".to_string(),
            shape: vec![1024],
            execution_time_ms: 0.6,
            throughput_gflops: 1.67,
            memory_bandwidth_gbps: 6.67,
            gpu_utilization_pct: 0.0,
        };
        let entry = build_entry(result);
        assert!(entry.is_regression);
        assert!(entry.efficiency_pct < 100.0);
    }

    #[test]
    fn test_report_roundtrip() {
        let report = BenchReport {
            schema_version: "1.0".to_string(),
            timestamp: "2026-06-28T12:00:00Z".to_string(),
            host: "x86_64-pc-windows-msvc".to_string(),
            kernels: vec![BenchKernelEntry {
                kernel: "vector_add".to_string(),
                elem: "f32".to_string(),
                shape: vec![1024],
                execution_time_ms: 0.5,
                gflops: 2.048,
                bandwidth_gbps: 8.192,
                baseline_time_ms: 0.5,
                efficiency_pct: 100.0,
                is_regression: false,
            }],
            summary: BenchSummary {
                total_kernels: 1,
                regression_count: 0,
                best_gflops: 2.048,
                best_bandwidth_gbps: 8.192,
                avg_efficiency_pct: 100.0,
            },
        };
        let json = report_to_json(&report);
        let parsed = report_from_json(&json).unwrap();
        assert_eq!(parsed.schema_version, "1.0");
        assert_eq!(parsed.kernels.len(), 1);
        assert_eq!(parsed.kernels[0].kernel, "vector_add");
        assert_eq!(parsed.summary.total_kernels, 1);
    }

    #[test]
    fn test_elem_type_bytes() {
        assert_eq!(elem_type_bytes("f32"), 4.0);
        assert_eq!(elem_type_bytes("f16"), 2.0);
        assert_eq!(elem_type_bytes("f64"), 8.0);
        assert_eq!(elem_type_bytes("i32"), 4.0);
        assert_eq!(elem_type_bytes("i8"), 1.0);
        assert_eq!(elem_type_bytes("unknown"), 4.0);
    }
}