Skip to main content

qualia_cli/handlers/
llm.rs

1use std::path::PathBuf;
2
3use crate::cli::LlmAction;
4use crate::llm_lifecycle;
5use crate::llm_testing;
6use crate::qwen_testing;
7
8/// Handles the subcommand action.
9pub async fn handle(action: &LlmAction) -> Result<(), Box<dyn std::error::Error>> {
10    let vault = |opt: &Option<PathBuf>| {
11        opt.clone()
12            .unwrap_or_else(llm_lifecycle::default_vault_path)
13    };
14    match action {
15        LlmAction::List { vault_path } => {
16            llm_lifecycle::run_list(&vault(vault_path))?;
17        }
18        LlmAction::Duplicates { vault_path } => {
19            llm_lifecycle::run_duplicate_audit(&vault(vault_path))?;
20        }
21        LlmAction::Load { model, vault_path } => {
22            llm_lifecycle::run_load(&vault(vault_path), model)?;
23        }
24        LlmAction::Status => {
25            llm_lifecycle::run_status()?;
26        }
27        LlmAction::Eval {
28            prompt,
29            authority,
30            orchestrated,
31            stream,
32            lora,
33        } => {
34            let _authority = authority.as_ref()
35                .map(|path| super::inference_policy::prepare_eval(path))
36                .transpose()?;
37            if let Some(l) = lora {
38                println!("Multiplexing LoRA adapter from {:?}", l);
39            }
40            llm_lifecycle::run_eval(prompt, *orchestrated, *stream)?;
41        }
42        LlmAction::Evict { model_id } => {
43            llm_lifecycle::run_evict(model_id)?;
44        }
45        LlmAction::Test {
46            vault_path,
47            models,
48            quantization,
49            details,
50        } => {
51            llm_testing::run_test_models(
52                vault_path.clone(),
53                models.clone(),
54                quantization.clone(),
55                *details,
56            )?;
57        }
58        LlmAction::Validate { vault_path, strict } => {
59            llm_testing::run_validate_models(vault_path.clone(), *strict)?;
60        }
61        LlmAction::ComprehensiveTest {
62            vault_path,
63            model,
64            details,
65        } => {
66            llm_testing::run_comprehensive_llm_test(vault_path.clone(), model.clone(), *details)?;
67        }
68        LlmAction::Benchmark {
69            vault_path,
70            models,
71            iterations,
72            warmup,
73        } => {
74            llm_testing::run_benchmark_models(
75                vault_path.clone(),
76                models.clone(),
77                *iterations,
78                *warmup,
79            )?;
80        }
81        LlmAction::Report {
82            vault_path,
83            output,
84            format,
85        } => {
86            llm_testing::run_generate_report(vault_path.clone(), output.clone(), format.clone())?;
87        }
88        LlmAction::Convert {
89            input,
90            out,
91            page_log2,
92            layout,
93        } => {
94            llm_testing::run_convert_gguf_to_p64(input, out, *page_log2, layout)?;
95        }
96        LlmAction::PrepareQwen4Exp {
97            input,
98            out,
99            ple_out,
100        } => {
101            llm_testing::run_prepare_qwen4exp_native(input, out, ple_out)?;
102        }
103        LlmAction::VerifyQwen4Exp { package } => {
104            llm_testing::run_verify_qwen4exp_native(package)?;
105        }
106        LlmAction::ProbeQwen4ExpPle { package } => {
107            llm_testing::run_probe_qwen4exp_ple(package)?;
108        }
109        LlmAction::GatherQwen4ExpPle { package, token_ids } => {
110            llm_testing::run_gather_qwen4exp_ple(package, token_ids)?;
111        }
112        LlmAction::ProbeQwen4ExpPleBlock { package, token_id } => {
113            llm_testing::run_probe_qwen4exp_ple_block(package, *token_id)?;
114        }
115        LlmAction::ProbeQwen4ExpHyper {
116            package,
117            layer,
118            ffn,
119            token_id,
120        } => {
121            llm_testing::run_probe_qwen4exp_hyper(package, *layer, *ffn, *token_id)?;
122        }
123        LlmAction::ProbeQwen4ExpGdn {
124            package,
125            layer,
126            token_id,
127        } => {
128            llm_testing::run_probe_qwen4exp_gdn(package, *layer, *token_id)?;
129        }
130        LlmAction::ProbeQwen4ExpQsa {
131            package,
132            layer,
133            token_id,
134        } => {
135            qwen_testing::run_probe_qwen4exp_qsa(package, *layer, *token_id)?;
136        }
137        LlmAction::ProbeQwen4ExpLayer {
138            package,
139            layer,
140            token_id,
141        } => {
142            qwen_testing::run_probe_qwen4exp_gdn_moe_layer(package, *layer, *token_id)?;
143        }
144        LlmAction::ProbeQwen4ExpTrunk { package, token_id } => {
145            llm_testing::run_probe_qwen4exp_trunk(package, *token_id)?;
146        }
147        LlmAction::ActivateQwen4Exp { package } => {
148            llm_testing::run_activate_qwen4exp_native(package)?;
149        }
150        LlmAction::ProbeQwen4ExpMoe {
151            package,
152            layer,
153            token_id,
154            tile,
155        } => {
156            llm_testing::run_probe_qwen4exp_moe(package, *layer, *token_id, tile.as_deref())?;
157        }
158        LlmAction::PromoteQwen4ExpExpert {
159            package,
160            layer,
161            expert,
162            cache_dir,
163            max_cache_gib,
164        } => {
165            llm_testing::run_promote_qwen4exp_expert(
166                package,
167                *layer,
168                *expert,
169                cache_dir,
170                *max_cache_gib,
171            )?;
172        }
173        LlmAction::ProbeQwen4ExpExpertTile {
174            package,
175            tile,
176            token_id,
177        } => {
178            llm_testing::run_probe_qwen4exp_expert_tile(package, tile, *token_id)?;
179        }
180        LlmAction::DecodeQwen4Exp {
181            package,
182            authority,
183            developer,
184            prompt,
185            token_ids,
186            chat,
187            max_tokens,
188            context,
189            trunk_mmap,
190        } => {
191            if authority.is_some() && *developer {
192                return Err("decode-qwen4exp accepts either --authority or --developer, never both".into());
193            }
194            if let Some(authority) = authority {
195                super::inference_policy::run_qwen_governed(authority, package,
196                    prompt.as_deref(), token_ids, *max_tokens, *context, *chat, *trunk_mmap)?;
197            } else if *developer {
198                qwen_testing::run_decode_qwen4exp(
199                    package,
200                    prompt.as_deref(),
201                    token_ids,
202                    *max_tokens,
203                    *context,
204                    *chat,
205                    *trunk_mmap,
206                )?;
207            } else {
208                return Err("decode-qwen4exp requires --authority or explicit --developer".into());
209            }
210        }
211        LlmAction::ProbeQwen4ExpTrace {
212            package,
213            prompt,
214            token_ids,
215            chat,
216            context,
217            topk,
218            rank_tokens,
219            dump,
220        } => {
221            qwen_testing::run_probe_qwen4exp_trace(
222                package,
223                prompt.as_deref(),
224                token_ids,
225                *chat,
226                *context,
227                *topk,
228                rank_tokens,
229                dump.as_deref(),
230            )?;
231        }
232        LlmAction::Optimize {
233            input,
234            out,
235            skip_passport,
236        } => {
237            llm_testing::run_optimize_pipeline(input, out.clone(), *skip_passport)?;
238        }
239        LlmAction::Passport {
240            reprobe,
241            gemv_n,
242            cache,
243            apply_env_hint,
244            decode_proxy,
245            decode_proxy_tokens,
246        } => {
247            llm_testing::run_hardware_passport(
248                *reprobe,
249                *gemv_n,
250                cache.clone(),
251                *apply_env_hint,
252                decode_proxy.clone(),
253                *decode_proxy_tokens,
254            )?;
255        }
256        LlmAction::DecodeProxy { model, tokens } => {
257            llm_testing::run_decode_proxy(model, *tokens)?;
258        }
259        LlmAction::RawDecodeBench {
260            model,
261            steps,
262            warmups,
263            runs,
264            quantization,
265            prompt,
266            target_prompt_tokens,
267            retain_artifacts,
268        } => {
269            crate::llm_raw_bench::run(crate::llm_raw_bench::CommandConfig {
270                model,
271                steps: *steps,
272                warmups: *warmups,
273                runs: *runs,
274                quantization,
275                prompt,
276                target_prompt_tokens: *target_prompt_tokens,
277                retain_artifacts: retain_artifacts.as_deref(),
278            })?;
279        }
280        LlmAction::Mode { name } => {
281            llm_testing::run_inference_mode(name.as_deref())?;
282        }
283        LlmAction::PathSelect { reprobe, apply } => {
284            llm_testing::run_path_select(*reprobe, *apply)?;
285        }
286        LlmAction::Profile { name } => {
287            llm_testing::run_app_profile(name.as_deref())?;
288        }
289        LlmAction::Lab {
290            action,
291            model,
292            tokens,
293            n_in,
294            n_out,
295            gemv_n,
296            out,
297            hours,
298            max_generations,
299            ollama_model,
300            ollama_url,
301            no_ollama,
302        } => {
303            llm_testing::run_lab(
304                action,
305                model.as_deref(),
306                *tokens,
307                *n_in,
308                *n_out,
309                *gemv_n,
310                out.as_deref(),
311                *hours,
312                *max_generations,
313                ollama_model.as_deref(),
314                ollama_url,
315                *no_ollama,
316            )?;
317        }
318        LlmAction::Ground { prompt, answer } => {
319            llm_testing::run_ground_check(prompt, answer)?;
320        }
321        LlmAction::SeedGrounding => {
322            llm_testing::run_seed_grounding()?;
323        }
324        LlmAction::CudaTcBench { side } => {
325            llm_testing::run_cuda_tc_microbench(*side)?;
326        }
327        LlmAction::Explore {
328            input,
329            out,
330            tokens,
331            layouts,
332            skip_convert,
333            sweep_ffn_f16,
334            modes,
335        } => {
336            llm_testing::run_explore_pipeline(
337                input,
338                out.clone(),
339                *tokens,
340                layouts,
341                *skip_convert,
342                *sweep_ffn_f16,
343                modes.as_deref(),
344            )?;
345        }
346    }
347    Ok(())
348}