qualia_core_db/inference/lab/
timeline.rs1use std::path::Path;
4use std::time::Instant;
5
6use crate::hardware_passport::measure_decode_proxy_tok_s;
7use crate::llm_bench::{
8 phase_snapshot, reset_phase_metrics, reset_resident_path_counts, resident_path_counts,
9};
10#[cfg(feature = "gpu-runtime")]
11use crate::llm_gpu_profiler::{self, Phase};
12
13pub use etalon_inference_lab::timeline::DecodeTimeline;
14pub fn run_decode_timeline(model: &Path, tokens: u32) -> DecodeTimeline {
16 let _state = super::state::TrialState::capture();
17 let tokens = tokens.max(1).min(64);
18 reset_phase_metrics();
19 reset_resident_path_counts();
20 #[cfg(feature = "gpu-runtime")]
21 {
22 llm_gpu_profiler::set_enabled(true);
23 llm_gpu_profiler::reset();
24 }
25
26 let t0 = Instant::now();
27 let tok_s = measure_decode_proxy_tok_s(model, tokens);
28 let wall_ms = t0.elapsed().as_secs_f64() * 1e3;
29 let (hits, falls) = resident_path_counts();
30 let snap = phase_snapshot();
31 let host_phase_ns = format!(
32 "{{\"load_ns\":{},\"prefill_ns\":{},\"prefill_tokens\":{},\"decode_ns\":{},\"decode_tokens\":{},\"decode_forward_ns\":{},\"decode_output_ns\":{}}}",
33 snap.load_ns,
34 snap.prefill_ns,
35 snap.prefill_tokens,
36 snap.decode_ns,
37 snap.decode_tokens,
38 snap.decode_forward_ns,
39 snap.decode_output_ns
40 );
41
42 let gpu_phase_ns = {
43 #[cfg(feature = "gpu-runtime")]
44 {
45 let mut gpu_parts = Vec::new();
46 for pt in llm_gpu_profiler::snapshot() {
47 if pt.calls > 0 || pt.total_ns > 0 {
48 gpu_parts.push(format!(
49 "\"{}\":{{\"ns\":{},\"calls\":{}}}",
50 pt.phase.label(),
51 pt.total_ns,
52 pt.calls
53 ));
54 }
55 }
56 format!("{{{}}}", gpu_parts.join(","))
57 }
58 #[cfg(not(feature = "gpu-runtime"))]
59 {
60 "{}".to_string()
61 }
62 };
63
64 let mut notes = Vec::new();
65 #[cfg(feature = "gpu-runtime")]
66 if !llm_gpu_profiler::enabled() {
67 notes.push("GPU timestamps not active (device or flag)".into());
68 }
69 #[cfg(not(feature = "gpu-runtime"))]
70 {
71 notes.push("GPU timestamps not compiled (gpu-runtime feature off)".into());
72 }
73 if falls > 0 {
74 notes.push(format!("resident fallbacks={falls}"));
75 }
76 if hits == 0 && tok_s.is_some() {
77 notes.push("no resident hits counted - check path".into());
78 }
79 #[cfg(feature = "gpu-runtime")]
80 let _ = Phase::COUNT;
81
82 #[cfg(feature = "gpu-runtime")]
83 llm_gpu_profiler::set_enabled(false);
84
85 DecodeTimeline {
86 model: model.display().to_string(),
87 tokens,
88 tok_s,
89 wall_ms,
90 resident_hits: hits,
91 resident_fallbacks: falls,
92 host_phase_ns,
93 gpu_phase_ns,
94 notes: notes.join("; "),
95 }
96}