Skip to main content

qualia_core_db/inference/lab/
operator_compare.rs

1//! Canonical comparison execution with host-specific checkpoint discovery tests.
2pub use etalon_inference_lab::operator_compare::*;
3
4#[cfg(test)]
5mod tests {
6    use super::*;
7    #[test]
8    fn test_real_model_checkpoint_or_not_measured() {
9        let candidates = [
10            std::env::var("QUALIA_TEST_MODEL_PATH").unwrap_or_default(),
11            r"E:\LLM_Models\lmstudio-community\granite-4.0-h-tiny-GGUF\granite-4.0-h-tiny-Q4_K_M.gguf".to_string(),
12            r"E:\LLM_Models\lmstudio-community\gemma-4-26B-A4B-it-GGUF\gemma-4-26B-A4B-it-Q4_K_M.gguf".to_string(),
13            r"E:\LLM_Models\lmstudio-community\Qwen3.6-35B-A3B-GGUF\Qwen3.6-35B-A3B-Q4_K_M.gguf".to_string(),
14            "models/SmolLM2-360M-Instruct-Q4_K_M.gguf".to_string(),
15        ];
16
17        let mut found_path = None;
18        for c in &candidates {
19            if !c.is_empty() && std::path::Path::new(c).exists() {
20                found_path = Some(c.clone());
21                break;
22            }
23        }
24
25        let path_str = match found_path {
26            Some(p) => p,
27            None => {
28                println!("CARGO_TEST_STATUS: not measured: No candidate GGUF model found");
29                return;
30            }
31        };
32
33        println!("CARGO_TEST_STATUS: evaluating real model at {}", path_str);
34        let file = match std::fs::File::open(&path_str) {
35            Ok(f) => f,
36            Err(e) => {
37                println!(
38                    "CARGO_TEST_STATUS: not measured: failed to open {}: {}",
39                    path_str, e
40                );
41                return;
42            }
43        };
44        let mmap = match unsafe { memmap2::MmapOptions::new().map(&file) } {
45            Ok(m) => m,
46            Err(e) => {
47                println!(
48                    "CARGO_TEST_STATUS: not measured: failed to mmap {}: {}",
49                    path_str, e
50                );
51                return;
52            }
53        };
54        let index = crate::gguf_sharder::GgufTensorIndex::from_gguf(&mmap);
55        if index.entries.is_empty() {
56            println!(
57                "CARGO_TEST_STATUS: not measured: empty tensor index for {}",
58                path_str
59            );
60            return;
61        }
62
63        // Find any 2D Q4_K weight matrix in the model
64        let mut target_info = None;
65        for (_h, info) in &index.entries {
66            if info.ggml_type == crate::ggml_quants::GGML_TYPE_Q4_K
67                && info.n_dims == 2
68                && info.dims[0] >= 256
69                && info.dims[1] >= 256
70                && info.dims[0] % 256 == 0
71            {
72                target_info = Some(*info);
73                break;
74            }
75        }
76
77        let target_info = match target_info {
78            Some(info) => info,
79            None => {
80                println!(
81                    "CARGO_TEST_STATUS: not measured: no 2D Q4_K tensor found in {}",
82                    path_str
83                );
84                return;
85            }
86        };
87
88        let in_features = target_info.dims[0] as usize;
89        let out_features = target_info.dims[1] as usize;
90        let raw_bytes = match crate::ggml_quants::fetch_tensor_bytes(
91            &mmap,
92            index.tensor_data_start,
93            &target_info,
94        ) {
95            Ok(b) => b,
96            Err(e) => {
97                println!(
98                    "CARGO_TEST_STATUS: not measured: failed to fetch tensor bytes: {:?}",
99                    e
100                );
101                return;
102            }
103        };
104
105        let mut input = vec![0.0f32; in_features];
106        for (i, v) in input.iter_mut().enumerate() {
107            *v = ((i % 128) as f32 * 0.02).sin();
108        }
109
110        let receipt = compare_single_tensor(raw_bytes, in_features, out_features, &input);
111        println!(
112            "CARGO_TEST_STATUS: real tensor compare [in={}, out={}]: max_err={:e}, mean_err={:e}, existing_ns={}, lookup_ns={}",
113            in_features, out_features, receipt.numerical_max_error, receipt.numerical_mean_error, receipt.lut_setup_ns, receipt.warm_latency_ns
114        );
115        assert!(
116            receipt.fidelity_satisfied,
117            "real tensor fidelity must be satisfied"
118        );
119        assert!(
120            receipt.numerical_max_error < 1e-3,
121            "real tensor max error < 1e-3"
122        );
123    }
124}