Skip to main content

qualia_core_db/inference/lab/
audit_path.rs

1//! Hot-path wiring auditor — makes unfinished integration visible.
2
3use crate::inference_modes::{
4    active_inference_mode, post_turn_verify_enabled, prefer_tensor_core_gemm,
5    quant_graph_grounding_enabled, sentinel_mid_decode_enabled,
6};
7use crate::llm_bench::{
8    attention_o_fuse_enabled, attention_preproject_enabled, coop_gemv_enabled, ffn_fusion_enabled,
9    ffn_fusion_in_resident, kv_int8_enabled, resident_decode_enabled, resident_prefill_enabled,
10    resident_weights_enabled,
11};
12
13#[derive(Debug, Clone)]
14pub struct HotPathAudit {
15    pub resident_decode: bool,
16    pub resident_prefill: bool,
17    pub resident_weights: bool,
18    pub coop_gemv: bool,
19    pub ffn_fusion_flag: bool,
20    pub ffn_fusion_in_resident_decode: bool,
21    pub kv_int8: bool,
22    pub attention_preproject: bool,
23    pub attention_o_fuse: bool,
24    pub prefer_cuda_gemm: bool,
25    pub cuda_caps: bool,
26    pub mode: String,
27    pub post_turn_verify: bool,
28    pub sentinel_mid: bool,
29    pub quant_graph: bool,
30    pub timestamps_supported: bool,
31    pub gpu_profile_env: bool,
32    pub notes: Vec<String>,
33}
34
35/// Static/dynamic audit of claimed features vs what decode is configured to use.
36pub fn audit_hot_path() -> HotPathAudit {
37    let mut notes = Vec::new();
38    let ffn_flag = ffn_fusion_enabled();
39    // Set true when a resident plan was built with fused_ffn bind groups (T-A1).
40    // False until first successful plan build, or when quant is Q4_K_SOA/F16 (fallback).
41    let ffn_in_resident = ffn_fusion_in_resident();
42    if ffn_flag && !ffn_in_resident {
43        notes.push(
44            "ffn_fusion flag ON but last resident plan did not fuse (unsupported quant or plan not built yet) — T-A1 partial"
45                .into(),
46        );
47    }
48    if ffn_in_resident {
49        notes.push(
50            "T-A1/T-A1b: fused_ffn in resident (coop entry when QUALIA_LLM_COOP_GEMV on)".into(),
51        );
52    }
53
54    let cuda_caps = {
55        #[cfg(feature = "wgsl-forge")]
56        {
57            crate::wgsl_forge::dispatch::caps().cuda
58        }
59        #[cfg(not(feature = "wgsl-forge"))]
60        {
61            false
62        }
63    };
64    if prefer_tensor_core_gemm() && !cuda_caps {
65        notes.push("mode prefers CUDA GEMM but cuda caps=false".into());
66    }
67    if prefer_tensor_core_gemm() && cuda_caps {
68        notes.push(
69            "CUDA lane available for densify/Q4 GEMV; full hidden-on-device layer stack still open"
70                .into(),
71        );
72    }
73
74    let timestamps_supported = {
75        #[cfg(feature = "gpu-runtime")]
76        {
77            crate::gpu_context::shared_gpu().timestamps_supported
78        }
79        #[cfg(not(feature = "gpu-runtime"))]
80        {
81            false
82        }
83    };
84    let gpu_profile_env = std::env::var("QUALIA_LLM_GPU_PROFILE")
85        .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
86        .unwrap_or(false);
87
88    if !resident_decode_enabled() {
89        notes.push("resident_decode OFF — legacy multi-fence path".into());
90    }
91    if !coop_gemv_enabled() {
92        notes.push("coop_gemv OFF — naive GEMV".into());
93    }
94
95    HotPathAudit {
96        resident_decode: resident_decode_enabled(),
97        resident_prefill: resident_prefill_enabled(),
98        resident_weights: resident_weights_enabled(),
99        coop_gemv: coop_gemv_enabled(),
100        ffn_fusion_flag: ffn_flag,
101        ffn_fusion_in_resident_decode: ffn_in_resident,
102        kv_int8: kv_int8_enabled(),
103        attention_preproject: attention_preproject_enabled(),
104        attention_o_fuse: attention_o_fuse_enabled(),
105        prefer_cuda_gemm: prefer_tensor_core_gemm(),
106        cuda_caps,
107        mode: active_inference_mode().as_str().to_string(),
108        post_turn_verify: post_turn_verify_enabled(),
109        sentinel_mid: sentinel_mid_decode_enabled(),
110        quant_graph: quant_graph_grounding_enabled(),
111        timestamps_supported,
112        gpu_profile_env,
113        notes,
114    }
115}
116
117impl HotPathAudit {
118    /// Formats `report`.
119    pub fn format_report(&self) -> String {
120        let yn = |b: bool| if b { "yes" } else { "NO " };
121        let mut s = String::from("Hot-path wiring audit\n");
122        s.push_str(&format!("  mode:                    {}\n", self.mode));
123        s.push_str(&format!(
124            "  resident_decode:         {}\n",
125            yn(self.resident_decode)
126        ));
127        s.push_str(&format!(
128            "  resident_prefill:        {}\n",
129            yn(self.resident_prefill)
130        ));
131        s.push_str(&format!(
132            "  resident_weights:        {}\n",
133            yn(self.resident_weights)
134        ));
135        s.push_str(&format!(
136            "  coop_gemv:               {}\n",
137            yn(self.coop_gemv)
138        ));
139        s.push_str(&format!(
140            "  ffn_fusion flag:         {}\n",
141            yn(self.ffn_fusion_flag)
142        ));
143        s.push_str(&format!(
144            "  ffn_fusion in resident:  {}  (must be yes for T-A1 done)\n",
145            yn(self.ffn_fusion_in_resident_decode)
146        ));
147        s.push_str(&format!(
148            "  kv_int8:                 {}\n",
149            yn(self.kv_int8)
150        ));
151        s.push_str(&format!(
152            "  attention_preproject:    {}\n",
153            yn(self.attention_preproject)
154        ));
155        s.push_str(&format!(
156            "  attention_o_fuse:        {}\n",
157            yn(self.attention_o_fuse)
158        ));
159        s.push_str(&format!(
160            "  prefer_cuda_gemm:        {}\n",
161            yn(self.prefer_cuda_gemm)
162        ));
163        s.push_str(&format!(
164            "  cuda_caps:               {}\n",
165            yn(self.cuda_caps)
166        ));
167        s.push_str(&format!(
168            "  post_turn_verify:        {}\n",
169            yn(self.post_turn_verify)
170        ));
171        s.push_str(&format!(
172            "  sentinel_mid:            {}\n",
173            yn(self.sentinel_mid)
174        ));
175        s.push_str(&format!(
176            "  quant_graph:             {}\n",
177            yn(self.quant_graph)
178        ));
179        s.push_str(&format!(
180            "  timestamps_supported:    {}\n",
181            yn(self.timestamps_supported)
182        ));
183        s.push_str(&format!(
184            "  QUALIA_LLM_GPU_PROFILE:  {}\n",
185            yn(self.gpu_profile_env)
186        ));
187        if !self.notes.is_empty() {
188            s.push_str("  notes:\n");
189            for n in &self.notes {
190                s.push_str(&format!("    - {n}\n"));
191            }
192        }
193        s
194    }
195}
196
197#[cfg(test)]
198mod tests {
199    use super::*;
200
201    #[test]
202    fn audit_runs() {
203        let a = audit_hot_path();
204        let r = a.format_report();
205        assert!(r.contains("resident_decode"));
206        assert!(r.contains("ffn_fusion in resident"));
207    }
208}