llama_cpp_4/context/perf.rs
1//! Safe wrapper around `llama_perf_context_data`.
2use std::fmt::{Debug, Display, Formatter};
3
4use llama_cpp_sys_4::{ggml_time_us, llama_perf_context_print};
5
6use crate::context::LlamaContext;
7
8/// A wrapper around `llama_perf_context_data`.
9#[derive(Clone, Copy, Debug)]
10pub struct PerfContextData {
11 pub(crate) perf_context_data: llama_cpp_sys_4::llama_perf_context_data,
12}
13
14impl PerfContextData {
15 /// Create a new `PerfContextData`.
16 /// ```
17 /// # use llama_cpp_4::context::perf::PerfContextData;
18 /// let timings = PerfContextData::new(1.0, 2.0, 3.0, 4.0, 5, 6);
19 /// assert_eq!(timings.t_load_ms(), 2.0);
20 /// assert_eq!(timings.t_p_eval_ms(), 3.0);
21 /// assert_eq!(timings.t_eval_ms(), 4.0);
22 /// assert_eq!(timings.n_p_eval(), 5);
23 /// assert_eq!(timings.n_eval(), 6);
24 /// ```
25 #[allow(clippy::too_many_arguments)]
26 #[must_use]
27 pub fn new(
28 t_start_ms: f64,
29 // t_end_ms: f64,
30 t_load_ms: f64,
31 // t_sample_ms: f64,
32 t_p_eval_ms: f64,
33 t_eval_ms: f64,
34 // n_sample: i32,
35 n_p_eval: i32,
36 n_eval: i32,
37 ) -> Self {
38 Self {
39 perf_context_data: llama_cpp_sys_4::llama_perf_context_data {
40 t_start_ms,
41 // t_end_ms,
42 t_load_ms,
43 // t_sample_ms,
44 t_p_eval_ms,
45 t_eval_ms,
46 // n_sample,
47 n_p_eval,
48 n_eval,
49 n_reused: 0,
50 // Upstream counters, added after this constructor's signature
51 // was fixed (llama.cpp b11829). They are only meaningful on a
52 // value read back from a context, so a hand-built one reports
53 // zero; see the accessors below.
54 n_reallocs_same: 0,
55 n_reallocs_diff: 0,
56 },
57 }
58 }
59
60 /// print llama context performance data
61 /// load time
62 /// prompt eval time
63 /// eval time
64 /// total time
65 pub fn print(ctx: &LlamaContext<'_>) {
66 unsafe {
67 llama_perf_context_print(ctx.context.as_ptr());
68 };
69 }
70
71 /// Get the start time in milliseconds.
72 #[must_use]
73 pub fn t_start_ms(&self) -> f64 {
74 self.perf_context_data.t_start_ms
75 }
76
77 /// Get the end time in milliseconds.
78 #[must_use]
79 pub fn t_end_ms(&self) -> f64 {
80 // self.perf_context_data.t_end_ms
81
82 #[allow(clippy::cast_precision_loss)]
83 {
84 1e-3 * (unsafe { ggml_time_us() }) as f64
85 }
86 }
87
88 /// Get the load time in milliseconds.
89 #[must_use]
90 pub fn t_load_ms(&self) -> f64 {
91 self.perf_context_data.t_load_ms
92 }
93
94 // /// Get the sample time in milliseconds.
95 // #[must_use]
96 // pub fn t_sample_ms(&self) -> f64 {
97 // self.perf_context_data.t_sample_ms
98 // }
99
100 /// Get the prompt evaluation time in milliseconds.
101 #[must_use]
102 pub fn t_p_eval_ms(&self) -> f64 {
103 self.perf_context_data.t_p_eval_ms
104 }
105
106 /// Get the evaluation time in milliseconds.
107 #[must_use]
108 pub fn t_eval_ms(&self) -> f64 {
109 self.perf_context_data.t_eval_ms
110 }
111
112 // /// Get the number of samples.
113 // #[must_use]
114 // pub fn n_sample(&self) -> i32 {
115 // self.perf_context_data.n_sample
116 // }
117
118 /// Get the number of prompt evaluations.
119 #[must_use]
120 pub fn n_p_eval(&self) -> i32 {
121 self.perf_context_data.n_p_eval
122 }
123
124 /// Get the number of evaluations.
125 #[must_use]
126 pub fn n_eval(&self) -> i32 {
127 self.perf_context_data.n_eval
128 }
129
130 /// How many times llama.cpp reused a cached compute graph instead of
131 /// rebuilding it.
132 ///
133 /// Graph reuse is the fast path. A low count against a high
134 /// [`Self::n_eval`] means something is invalidating the graph every step —
135 /// a changing batch shape, or toggling a context flag mid-generation.
136 #[must_use]
137 pub fn n_reused(&self) -> i32 {
138 self.perf_context_data.n_reused
139 }
140
141 /// Graph reallocations where the graph size was unchanged.
142 ///
143 /// These should be rare: the shape did not change, so the buffers should
144 /// have been reusable. A climbing count points at allocator churn rather
145 /// than at your batching.
146 #[must_use]
147 pub fn n_reallocs_same(&self) -> i32 {
148 self.perf_context_data.n_reallocs_same
149 }
150
151 /// Graph reallocations where the graph size changed.
152 ///
153 /// Expected whenever the batch shape moves — a prefill of a different
154 /// length, or switching between prompt and single-token decode.
155 #[must_use]
156 pub fn n_reallocs_diff(&self) -> i32 {
157 self.perf_context_data.n_reallocs_diff
158 }
159
160 /// Set the start time in milliseconds.
161 pub fn set_t_start_ms(&mut self, t_start_ms: f64) {
162 self.perf_context_data.t_start_ms = t_start_ms;
163 }
164
165 // /// Set the end time in milliseconds.
166 // pub fn set_t_end_ms(&mut self, t_end_ms: f64) {
167 // self.perf_context_data.t_end_ms = t_end_ms;
168 // }
169
170 /// Set the load time in milliseconds.
171 pub fn set_t_load_ms(&mut self, t_load_ms: f64) {
172 self.perf_context_data.t_load_ms = t_load_ms;
173 }
174
175 // /// Set the sample time in milliseconds.
176 // pub fn set_t_sample_ms(&mut self, t_sample_ms: f64) {
177 // self.perf_context_data.t_sample_ms = t_sample_ms;
178 // }
179
180 /// Set the prompt evaluation time in milliseconds.
181 pub fn set_t_p_eval_ms(&mut self, t_p_eval_ms: f64) {
182 self.perf_context_data.t_p_eval_ms = t_p_eval_ms;
183 }
184
185 /// Set the evaluation time in milliseconds.
186 pub fn set_t_eval_ms(&mut self, t_eval_ms: f64) {
187 self.perf_context_data.t_eval_ms = t_eval_ms;
188 }
189
190 // /// Set the number of samples.
191 // pub fn set_n_sample(&mut self, n_sample: i32) {
192 // self.perf_context_data.n_sample = n_sample;
193 // }
194
195 /// Set the number of prompt evaluations.
196 pub fn set_n_p_eval(&mut self, n_p_eval: i32) {
197 self.perf_context_data.n_p_eval = n_p_eval;
198 }
199
200 /// Set the number of evaluations.
201 pub fn set_n_eval(&mut self, n_eval: i32) {
202 self.perf_context_data.n_eval = n_eval;
203 }
204}
205
206impl Display for PerfContextData {
207 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
208 writeln!(f, "load time = {:.2} ms", self.t_load_ms())?;
209 // writeln!(
210 // f,
211 // "sample time = {:.2} ms / {} runs ({:.2} ms per token, {:.2} tokens per second)",
212 // self.t_sample_ms(),
213 // self.n_sample(),
214 // self.t_sample_ms() / f64::from(self.n_sample()),
215 // 1e3 / self.t_sample_ms() * f64::from(self.n_sample())
216 // )?;
217 writeln!(
218 f,
219 "prompt eval time = {:.2} ms / {} tokens ({:.2} ms per token, {:.2} tokens per second)",
220 self.t_p_eval_ms(),
221 self.n_p_eval(),
222 self.t_p_eval_ms() / f64::from(self.n_p_eval()),
223 1e3 / self.t_p_eval_ms() * f64::from(self.n_p_eval())
224 )?;
225 writeln!(
226 f,
227 "eval time = {:.2} ms / {} runs ({:.2} ms per token, {:.2} tokens per second)",
228 self.t_eval_ms(),
229 self.n_eval(),
230 self.t_eval_ms() / f64::from(self.n_eval()),
231 1e3 / self.t_eval_ms() * f64::from(self.n_eval())
232 )?;
233 write!(
234 f,
235 "total time = {:.2} ms",
236 self.t_end_ms() - self.t_start_ms()
237 )
238 }
239}