hf2q 0.1.33

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! ADR-063 C5 — opt-in warm-request frontend timing.
//!
//! `HF2Q_FRONTEND_TIMING=1` makes every dispatched chat completion emit one
//! `frontend timing` line (target `hf2q::serve::frontend_timing`, INFO) with
//! microsecond spans for the handler-owned work: pool admission, vision
//! preflight, constraint compilation, primary render and tokenization, the
//! stable-prefix render/tokenize/match, the reasoning probe render and the
//! thinking-budget marker encoding. The same line carries the time from
//! request receipt to the first semantic stream event (or to unary
//! completion), so frontend work can be compared against warm TTFT without
//! timestamped logs.
//!
//! The variable is read once per process. When it is unset every span is a
//! branch on `None`: no clock reads, no allocation, no extra task and no
//! extra stream hop.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::LazyLock;
use std::time::{Duration, Instant};

use tokio::sync::mpsc;

use super::sse::{DeltaKind, GenerationEvent};

pub(super) const FRONTEND_TIMING_ENV: &str = "HF2Q_FRONTEND_TIMING";

/// Handler-owned phases. Each accumulates, so a phase entered twice
/// (constraint compilation) reports its total.
#[derive(Clone, Copy, Debug)]
pub(super) enum Span {
    /// Pool resolution and model lease (`resolve_engine_for_request`).
    Admission,
    /// Image fetch/preprocess/ViT preflight; zero for text-only requests.
    Vision,
    /// Request output constraint and tool grammar compilation.
    Constraint,
    /// Primary chat-template render (with generation prompt).
    Render,
    /// Primary prompt tokenization.
    Tokenize,
    /// Truncate/summarize rewrite; zero unless the prompt overflowed.
    OverflowRewrite,
    /// Stable-prefix render (no generation prompt; Gemma 4 and Qwen 3.5+).
    StableRender,
    /// Stable-prefix tokenization.
    StableTokenize,
    /// Stable-prefix token match and Gemma full-input checkpoint tag.
    PrefixMatch,
    /// Minimal-transcript render probing for an open reasoning block.
    ProbeRender,
    /// Thinking-budget resolution, including marker tokenization.
    Budget,
    /// Stream budget admission and worker enqueue; zero for unary.
    Enqueue,
}

const SPAN_COUNT: usize = Span::Enqueue as usize + 1;

static NEXT_SEQ: AtomicU64 = AtomicU64::new(0);

static ENABLED: LazyLock<bool> =
    LazyLock::new(|| env_enables(std::env::var(FRONTEND_TIMING_ENV).ok().as_deref()));

fn env_enables(value: Option<&str>) -> bool {
    value == Some("1")
}

fn micros(duration: Duration) -> u64 {
    u64::try_from(duration.as_micros()).unwrap_or(u64::MAX)
}

struct Record {
    seq: u64,
    started: Instant,
    stream: bool,
    messages: usize,
    tools: usize,
    family: String,
    rendered_bytes: usize,
    prompt_tokens: usize,
    stable_prefix_tokens: Option<usize>,
    span_us: [u64; SPAN_COUNT],
    prepare_us: u64,
}

impl Record {
    fn emit(
        &self,
        outcome: &'static str,
        first_output_us: Option<u64>,
        completion_us: Option<u64>,
    ) {
        let s = &self.span_us;
        tracing::info!(
            target: "hf2q::serve::frontend_timing",
            seq = self.seq,
            family = %self.family,
            stream = self.stream,
            outcome,
            messages = self.messages,
            tools = self.tools,
            rendered_bytes = self.rendered_bytes,
            prompt_tokens = self.prompt_tokens,
            stable_prefix_tokens = self.stable_prefix_tokens,
            admission_us = s[Span::Admission as usize],
            vision_us = s[Span::Vision as usize],
            constraint_us = s[Span::Constraint as usize],
            render_us = s[Span::Render as usize],
            tokenize_us = s[Span::Tokenize as usize],
            overflow_rewrite_us = s[Span::OverflowRewrite as usize],
            stable_render_us = s[Span::StableRender as usize],
            stable_tokenize_us = s[Span::StableTokenize as usize],
            prefix_match_us = s[Span::PrefixMatch as usize],
            probe_render_us = s[Span::ProbeRender as usize],
            budget_us = s[Span::Budget as usize],
            enqueue_us = s[Span::Enqueue as usize],
            prepare_us = self.prepare_us,
            first_output_us,
            completion_us,
            "frontend timing"
        );
    }
}

/// Per-request timing record; `None` inside when the env gate is off.
pub(super) struct FrontendTiming(Option<Box<Record>>);

impl FrontendTiming {
    /// Gate on `HF2Q_FRONTEND_TIMING=1` (read once per process).
    pub(super) fn from_env(stream: bool, messages: usize, tools: usize) -> Self {
        Self::new(*ENABLED, stream, messages, tools)
    }

    pub(super) fn new(enabled: bool, stream: bool, messages: usize, tools: usize) -> Self {
        if !enabled {
            return Self(None);
        }
        Self(Some(Box::new(Record {
            seq: NEXT_SEQ.fetch_add(1, Ordering::Relaxed),
            started: Instant::now(),
            stream,
            messages,
            tools,
            family: String::new(),
            rendered_bytes: 0,
            prompt_tokens: 0,
            stable_prefix_tokens: None,
            span_us: [0; SPAN_COUNT],
            prepare_us: 0,
        })))
    }

    #[cfg(test)]
    pub(super) fn disabled() -> Self {
        Self(None)
    }

    /// Clock read only when enabled; pass the result to [`Self::record`].
    #[inline]
    pub(super) fn start(&self) -> Option<Instant> {
        self.0.as_ref().map(|_| Instant::now())
    }

    #[inline]
    pub(super) fn record(&mut self, span: Span, started: Option<Instant>) {
        if let (Some(record), Some(started)) = (self.0.as_mut(), started) {
            record.span_us[span as usize] =
                record.span_us[span as usize].saturating_add(micros(started.elapsed()));
        }
    }

    pub(super) fn set_family(&mut self, family: impl FnOnce() -> String) {
        if let Some(record) = self.0.as_mut() {
            record.family = family();
        }
    }

    pub(super) fn set_rendered_bytes(&mut self, bytes: usize) {
        if let Some(record) = self.0.as_mut() {
            record.rendered_bytes = bytes;
        }
    }

    /// Final prompt shape handed to the worker; closes the prepare window.
    pub(super) fn mark_prepared(
        &mut self,
        prompt_tokens: usize,
        stable_prefix_tokens: Option<usize>,
    ) {
        if let Some(record) = self.0.as_mut() {
            record.prompt_tokens = prompt_tokens;
            record.stable_prefix_tokens = stable_prefix_tokens;
            record.prepare_us = micros(record.started.elapsed());
        }
    }

    /// Unary path: one line once the worker result (or error) is back.
    pub(super) fn finish_unary(self, succeeded: bool) {
        if let Some(record) = self.0 {
            let completion_us = micros(record.started.elapsed());
            record.emit(
                if succeeded { "ok" } else { "error" },
                None,
                Some(completion_us),
            );
        }
    }

    /// Streaming path: when enabled, interpose a relay that emits the line
    /// at the first semantic event and forwards every event unchanged and
    /// in order. Disabled returns `upstream` itself (no relay).
    ///
    /// Dropping the returned receiver (client disconnect) ends the relay,
    /// which drops `upstream`, so the worker still observes cancellation
    /// on its next send.
    pub(super) fn relay_stream(
        self,
        mut upstream: mpsc::Receiver<GenerationEvent>,
    ) -> mpsc::Receiver<GenerationEvent> {
        let Some(record) = self.0 else {
            return upstream;
        };
        let (tx, rx) = mpsc::channel(64);
        tokio::spawn(async move {
            let mut pending = Some(record);
            while let Some(event) = upstream.recv().await {
                if let Some(outcome) = first_output_outcome(&event) {
                    if let Some(record) = pending.take() {
                        let elapsed = micros(record.started.elapsed());
                        record.emit(outcome, Some(elapsed), None);
                    }
                }
                if tx.send(event).await.is_err() {
                    break;
                }
            }
            if let Some(record) = pending.take() {
                record.emit("closed_before_output", None, None);
            }
        });
        rx
    }
}

/// Classify the event that ends the wait for semantic output: any
/// reasoning/content/tool-call delta, or a terminal event. Logprobs alone
/// do not count.
fn first_output_outcome(event: &GenerationEvent) -> Option<&'static str> {
    match event {
        GenerationEvent::Delta {
            kind: DeltaKind::Reasoning,
            ..
        } => Some("first_reasoning"),
        GenerationEvent::Delta {
            kind: DeltaKind::Content,
            ..
        } => Some("first_content"),
        GenerationEvent::ToolCallDelta { .. } => Some("first_tool_call"),
        GenerationEvent::Done { .. } => Some("done_without_output"),
        GenerationEvent::Error(_) => Some("error"),
        GenerationEvent::Logprobs(_) => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::serve::api::schema::ChoiceLogprobs;
    use crate::serve::api::sse::StreamStats;

    fn delta(kind: DeltaKind, text: &str) -> GenerationEvent {
        GenerationEvent::Delta {
            kind,
            text: text.to_string(),
        }
    }

    fn done() -> GenerationEvent {
        GenerationEvent::Done {
            finish_reason: "stop",
            prompt_tokens: 3,
            completion_tokens: 2,
            stats: StreamStats::default(),
        }
    }

    fn describe(event: &GenerationEvent) -> String {
        match event {
            GenerationEvent::Delta { kind, text } => format!("{kind:?}:{text}"),
            GenerationEvent::ToolCallDelta {
                index, arguments, ..
            } => format!("tool{index}:{}", arguments.as_deref().unwrap_or("")),
            GenerationEvent::Logprobs(_) => "logprobs".into(),
            GenerationEvent::Done {
                finish_reason,
                completion_tokens,
                ..
            } => format!("done:{finish_reason}:{completion_tokens}"),
            GenerationEvent::Error(error) => format!("error:{error}"),
        }
    }

    #[test]
    fn only_the_exact_value_one_enables_timing() {
        assert!(env_enables(Some("1")));
        for value in [None, Some(""), Some("0"), Some("true"), Some(" 1")] {
            assert!(!env_enables(value), "{value:?} must leave timing off");
        }
    }

    #[test]
    fn disabled_timing_takes_no_clock_reads_and_records_nothing() {
        let mut timing = FrontendTiming::disabled();
        assert!(timing.start().is_none());
        timing.record(Span::Render, Some(Instant::now()));
        timing.mark_prepared(10, Some(4));
        assert!(timing.0.is_none());
    }

    #[test]
    fn enabled_spans_accumulate_per_phase() {
        let mut timing = FrontendTiming::new(true, true, 4, 2);
        let earlier = Instant::now() - Duration::from_millis(3);
        timing.record(Span::Constraint, Some(earlier));
        timing.record(Span::Constraint, Some(earlier));
        timing.record(Span::Render, None);
        let record = timing.0.as_ref().expect("enabled");
        assert!(record.span_us[Span::Constraint as usize] >= 6_000);
        assert_eq!(record.span_us[Span::Render as usize], 0);
        assert_eq!(record.span_us[Span::Tokenize as usize], 0);
    }

    #[test]
    fn first_output_ignores_logprobs_and_accepts_every_semantic_or_terminal_event() {
        assert_eq!(
            first_output_outcome(&delta(DeltaKind::Reasoning, "r")),
            Some("first_reasoning")
        );
        assert_eq!(
            first_output_outcome(&delta(DeltaKind::Content, "c")),
            Some("first_content")
        );
        assert_eq!(first_output_outcome(&done()), Some("done_without_output"));
        assert_eq!(
            first_output_outcome(&GenerationEvent::Error("x".into())),
            Some("error")
        );
        assert_eq!(
            first_output_outcome(&GenerationEvent::Logprobs(ChoiceLogprobs {
                content: Vec::new()
            })),
            None
        );
    }

    #[tokio::test]
    async fn disabled_stream_is_returned_without_a_relay() {
        let (tx, rx) = mpsc::channel(4);
        let mut out = FrontendTiming::disabled().relay_stream(rx);
        tx.send(delta(DeltaKind::Content, "a")).await.unwrap();
        drop(tx);
        assert_eq!(describe(&out.recv().await.unwrap()), "Content:a");
        assert!(out.recv().await.is_none());
    }

    #[tokio::test]
    async fn enabled_relay_forwards_every_event_in_order_then_closes() {
        let (tx, rx) = mpsc::channel(4);
        let mut out = FrontendTiming::new(true, true, 2, 0).relay_stream(rx);
        let producer = tokio::spawn(async move {
            for i in 0..100 {
                tx.send(delta(DeltaKind::Reasoning, &i.to_string()))
                    .await
                    .unwrap();
            }
            tx.send(GenerationEvent::ToolCallDelta {
                index: 0,
                id: Some("call_0".into()),
                call_type: Some("function".into()),
                name: Some("read".into()),
                arguments: Some("{}".into()),
            })
            .await
            .unwrap();
            tx.send(done()).await.unwrap();
        });
        let mut seen = Vec::new();
        while let Some(event) = out.recv().await {
            seen.push(describe(&event));
        }
        producer.await.unwrap();
        let mut expected: Vec<String> = (0..100).map(|i| format!("Reasoning:{i}")).collect();
        expected.push("tool0:{}".into());
        expected.push("done:stop:2".into());
        assert_eq!(seen, expected);
    }

    #[tokio::test]
    async fn dropping_the_relayed_stream_closes_the_worker_sender() {
        let (tx, rx) = mpsc::channel(1);
        let out = FrontendTiming::new(true, true, 2, 0).relay_stream(rx);
        tx.send(delta(DeltaKind::Content, "a")).await.unwrap();
        drop(out);
        let closed = tokio::time::timeout(Duration::from_secs(5), async {
            loop {
                if tx.send(delta(DeltaKind::Content, "b")).await.is_err() {
                    break;
                }
            }
        })
        .await;
        assert!(
            closed.is_ok(),
            "client disconnect must still reach the worker"
        );
    }
}