Skip to main content

qualia_core_db/inference/lab/
device_roof.rs

1//! Device peak calibration — real roofline anchors (plan L1.2).
2
3use std::time::Instant;
4
5use crate::device_benchmark::benchmark_devices;
6
7pub use etalon_inference_lab::device_roof::DeviceRoof;
8
9/// Calibrate using the existing multi-circuit GEMV passport bench + derived balance point.
10pub fn calibrate_device_roof(gemv_n: usize) -> DeviceRoof {
11    let n = gemv_n.max(256).min(4096);
12    let matrix = benchmark_devices(n);
13    let best = matrix.best();
14    let (label, backend, ms, gflops, up) = match best {
15        Some(c) => (
16            c.label.clone(),
17            c.backend.clone(),
18            c.ms_per_gemv,
19            c.gflops,
20            c.upload_gbps,
21        ),
22        None => ("none".into(), "none".into(), f64::INFINITY, 0.0, 0.0),
23    };
24    // Rough balance: if we measured G FLOP/s and U GB/s, balance ≈ G / U (FLOP/byte).
25    // upload_gbps is host→device; for in-pool compute use gflops as primary signal.
26    let balance = if up.is_finite() && up > 0.1 {
27        // GFLOP/s / (GB/s) = FLOP/byte
28        (gflops.max(0.01)) / up
29    } else {
30        // CPU in-pool: treat as compute-leaning default
31        20.0
32    };
33    DeviceRoof {
34        gemv_n: n,
35        best_label: label,
36        best_backend: backend,
37        gemv_ms: ms,
38        gemv_gflops: gflops,
39        upload_gbps: up,
40        balance_flop_per_byte: balance,
41        notes: format!(
42            "from benchmark_devices({n}); use balance_flop_per_byte for schedule classification"
43        ),
44    }
45}
46
47/// Quick CPU Q4 dequant·dot micro for intensity reference (no GPU).
48pub fn cpu_q4_intensity_probe(n_in: usize, n_out: usize) -> (f64, f64) {
49    use crate::ggml_quants::{
50        dequantize_row_into, q4k_block_to_soa, BLOCK_Q4K_SOA_BYTES, GGML_TYPE_Q4_K_SOA,
51    };
52    let n_in = n_in.max(256) & !255; // multiple of 256
53    let n_out = n_out.max(1).min(64);
54    let mut stock = [0u8; 144];
55    stock[0] = 0x00;
56    stock[1] = 0x3c;
57    for i in 4..144 {
58        stock[i] = (i as u8).wrapping_mul(17);
59    }
60    let mut soa = [0u8; BLOCK_Q4K_SOA_BYTES];
61    let _ = q4k_block_to_soa(&stock, &mut soa);
62    let n_blocks = n_in / 256;
63    let mut weight = Vec::with_capacity(n_out * n_blocks * BLOCK_Q4K_SOA_BYTES);
64    for _ in 0..n_out {
65        for _ in 0..n_blocks {
66            weight.extend_from_slice(&soa);
67        }
68    }
69    let x: Vec<f32> = (0..n_in).map(|i| (i as f32) * 0.001).collect();
70    let mut row = vec![0.0f32; n_in];
71    let t0 = Instant::now();
72    let mut acc = 0.0f64;
73    for r in 0..n_out {
74        let off = r * n_blocks * BLOCK_Q4K_SOA_BYTES;
75        dequantize_row_into(
76            &weight[off..off + n_blocks * BLOCK_Q4K_SOA_BYTES],
77            GGML_TYPE_Q4_K_SOA,
78            n_in,
79            &mut row,
80        )
81        .ok();
82        acc += row
83            .iter()
84            .zip(x.iter())
85            .map(|(a, b)| (*a as f64) * (*b as f64))
86            .sum::<f64>();
87    }
88    let secs = t0.elapsed().as_secs_f64().max(1e-9);
89    let flops = (n_in as f64) * (n_out as f64) * 2.0; // mul+add
90    let bytes = (weight.len() + n_in * 4 + n_out * 4) as f64;
91    let gflops = (flops / secs) / 1e9;
92    let intensity = flops / bytes;
93    let _ = acc;
94    (gflops, intensity)
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn cpu_intensity_positive() {
103        let (g, i) = cpu_q4_intensity_probe(256, 4);
104        assert!(g > 0.0);
105        assert!(i > 0.0);
106    }
107}