qualia_core_db/inference/
application_profile.rs1use std::sync::atomic::{AtomicU8, Ordering};
16
17use crate::inference_modes::{set_inference_mode, InferenceMode};
18#[cfg(not(target_arch = "wasm32"))]
19use crate::llm_bench::{set_decode_budget_override, set_inference_timeout_override_ms};
20
21#[cfg(target_arch = "wasm32")]
22fn set_decode_budget_override(_n: u32) {}
23#[cfg(target_arch = "wasm32")]
24fn set_inference_timeout_override_ms(_ms: u64) {}
25
26#[repr(u8)]
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum ApplicationProfile {
29 Interactive = 0,
31 LiveFast = 1,
33 BatchOvernight = 2,
35}
36
37impl ApplicationProfile {
38 pub const ALL: [ApplicationProfile; 3] = [
39 ApplicationProfile::Interactive,
40 ApplicationProfile::LiveFast,
41 ApplicationProfile::BatchOvernight,
42 ];
43
44 pub fn as_str(self) -> &'static str {
46 match self {
47 Self::Interactive => "interactive",
48 Self::LiveFast => "live-fast",
49 Self::BatchOvernight => "batch",
50 }
51 }
52
53 pub fn parse(s: &str) -> Option<Self> {
55 match s.trim().to_ascii_lowercase().as_str() {
56 "interactive" | "chat" | "ui" | "0" => Some(Self::Interactive),
57 "live-fast" | "live_fast" | "live" | "fast" | "1" => Some(Self::LiveFast),
58 "batch" | "batch-overnight" | "overnight" | "offline" | "async" | "email" | "2" => {
59 Some(Self::BatchOvernight)
60 }
61 _ => None,
62 }
63 }
64
65 pub fn description(self) -> &'static str {
67 match self {
68 Self::Interactive => {
69 "interactive chat: path-selected GPU backend, 256-tok budget, 30s wall-clock"
70 }
71 Self::LiveFast => {
72 "live-fast: FastVerify (uninterrupted decode → post graph/CML heal), still local"
73 }
74 Self::BatchOvernight => {
75 "batch/overnight: up to 2048 tokens, 8h wall-clock, HTML+CML verify — multi-system eval, email-ready"
76 }
77 }
78 }
79}
80
81static PROFILE: AtomicU8 = AtomicU8::new(ApplicationProfile::Interactive as u8);
82
83pub fn active_application_profile() -> ApplicationProfile {
85 if let Ok(s) = std::env::var("QUALIA_APP_PROFILE") {
86 if let Some(p) = ApplicationProfile::parse(&s) {
87 return p;
88 }
89 }
90 match PROFILE.load(Ordering::Relaxed) {
91 1 => ApplicationProfile::LiveFast,
92 2 => ApplicationProfile::BatchOvernight,
93 _ => ApplicationProfile::Interactive,
94 }
95}
96
97pub fn set_application_profile(profile: ApplicationProfile) {
99 PROFILE.store(profile as u8, Ordering::Relaxed);
100 apply_application_profile(profile);
101 log::info!("APP_PROFILE|{}|{}", profile.as_str(), profile.description());
102}
103
104pub fn apply_application_profile(profile: ApplicationProfile) {
106 match profile {
107 ApplicationProfile::Interactive => {
108 set_decode_budget_override(0); set_inference_timeout_override_ms(0); if std::env::var("QUALIA_INFERENCE_MODE").is_err() {
111 set_inference_mode(InferenceMode::Portable);
112 }
113 }
114 ApplicationProfile::LiveFast => {
115 set_decode_budget_override(0);
116 set_inference_timeout_override_ms(0);
117 if std::env::var("QUALIA_INFERENCE_MODE").is_err() {
118 set_inference_mode(InferenceMode::FastVerify);
119 }
120 }
121 ApplicationProfile::BatchOvernight => {
122 set_decode_budget_override(2048);
124 set_inference_timeout_override_ms(8 * 60 * 60 * 1000); std::env::set_var("QUALIA_RETURN_VERIFY_HTML", "1");
127 if std::env::var("QUALIA_INFERENCE_MODE").is_err() {
128 set_inference_mode(InferenceMode::FastVerify);
129 }
130 let n = crate::quant_graph_grounding::seed_facts_from_bundled();
132 log::info!("APP_PROFILE|batch|facts_seeded|{n}|budget=2048|timeout=8h|html=1");
133 }
134 }
135}
136
137pub fn bootstrap_application_profile() -> ApplicationProfile {
139 let p = active_application_profile();
140 apply_application_profile(p);
141 p
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 #[test]
149 fn parse_profiles() {
150 assert_eq!(
151 ApplicationProfile::parse("overnight"),
152 Some(ApplicationProfile::BatchOvernight)
153 );
154 assert_eq!(
155 ApplicationProfile::parse("live-fast"),
156 Some(ApplicationProfile::LiveFast)
157 );
158 }
159}