met 1.0.0-beta-preview

Metrale Engine: pure Rust LLM inference server (HTTP API, scheduler, TUI, CLI).
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// SPDX-License-Identifier: MIT OR Apache-2.0

//! 2026-09-25: Drives the real scheduler loop with a [`RecordingModel`] and turns one scenario into a text trace.
//!
//! Owner: scheduler.
//! Invariants: none beyond the types.
//!
//! The trace is the model-call sequence, then the LoRA rotation ack if the
//! scenario queued one, then what every client received.

use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::time::{Duration, Instant};

use super::model::{ModelCfg, RecordingModel, SeqScript, Shared};
use crate::api::{InferenceRequest, InferenceResponse, StreamEvent};
use crate::scheduler::levers::SchedLevers;
use crate::scheduler::limits::SchedLimits;
use crate::scheduler::vocab_masks::VocabMasks;
use crate::scheduler::{LoraCommand, WatchdogParams};
use crate::scheduling_policy::{FifoPolicy, SchedulingPolicy, SlaiPolicy};

pub(super) const EOS: u32 = 63;

#[derive(Clone, Debug)]
pub(super) struct ReqSpec {
    /// 2026-09-25: Session hash and the first prompt token: how the fake finds the script.
    pub id: u64,
    pub prompt_len: usize,
    /// 2026-09-25: Scripted generated tokens (end with [`EOS`] for a natural stop).
    pub toks: Vec<u32>,
    pub max_tokens: usize,
    pub min_tokens: usize,
    pub streaming: bool,
    pub temperature: f32,
    pub top_logprobs: Option<u8>,
    pub num_beams: u32,
    pub beam_hyp: Option<Vec<u32>>,
    pub draft_wrong_every: Option<usize>,
    pub cancel_at: Option<usize>,
    /// 2026-09-25: A deadline set when the request is built, so it has passed by the time
    /// the scheduler checks it.
    pub expired_deadline: bool,
    /// 2026-09-25: `None` means queued before the loop starts; `Some(t)` means it arrives while the
    /// loop is parked at tick `t`.
    pub arrive_at_tick: Option<usize>,
    /// 2026-10-04: The request's tenant (`auth::LookupTenant`); the open
    /// serve's by default.
    pub lookup_tenant: Option<crate::auth::LookupTenant>,
}

impl ReqSpec {
    pub fn new(id: u64, prompt_len: usize, toks: Vec<u32>) -> Self {
        let max_tokens = toks.len();
        Self {
            id,
            prompt_len,
            toks,
            max_tokens,
            min_tokens: 0,
            streaming: true,
            temperature: 0.0,
            top_logprobs: None,
            num_beams: 1,
            beam_hyp: None,
            draft_wrong_every: None,
            cancel_at: None,
            expired_deadline: false,
            arrive_at_tick: None,
            lookup_tenant: Some(crate::auth::LookupTenant::OPEN),
        }
    }
}

pub(super) use super::run_options::RunOptions;

#[derive(Clone)]
pub(super) struct Scenario {
    pub name: &'static str,
    pub cfg: ModelCfg,
    pub opts: RunOptions,
    pub reqs: Vec<ReqSpec>,
}

enum Sink {
    Blocking(tokio::sync::oneshot::Receiver<anyhow::Result<InferenceResponse>>),
    Streaming(std::thread::JoinHandle<Vec<String>>),
}

fn fmt_event(ev: &StreamEvent) -> String {
    match ev {
        StreamEvent::Token(t) => format!("T{t}"),
        StreamEvent::TokenWithLogprobs(t, lp) => format!("TL{t}:{}", lp.top.len()),
        StreamEvent::PromptLogprobs(v) => format!("PL{}", v.len()),
        StreamEvent::Done {
            finish_reason,
            completion_tokens,
            reasoning_tokens,
            cached_prompt_tokens,
            accepted_prediction_tokens,
            guard_stop,
            ..
        } => format!(
            "Done({finish_reason}, n={completion_tokens}, think={reasoning_tokens}, cached={cached_prompt_tokens}, acc={accepted_prediction_tokens}, guard={guard_stop:?})"
        ),
        StreamEvent::Error(m) => format!("Error({m})"),
    }
}

fn build_request(r: &ReqSpec, shared: &Shared) -> (InferenceRequest, Sink) {
    let prompt: Vec<u32> = (0..r.prompt_len)
        .map(|i| {
            if i == 0 {
                r.id as u32
            } else {
                2 + (i as u32 % 7)
            }
        })
        .collect();
    shared.add_script(
        r.id,
        SeqScript {
            prompt_len: r.prompt_len,
            tokens: r.toks.clone(),
            draft_wrong_every: r.draft_wrong_every,
            beam_hyp: r.beam_hyp.clone(),
            cancel_at: r.cancel_at,
        },
    );
    let timeout_at = r.expired_deadline.then(Instant::now);
    macro_rules! common {
        ($variant:ident, $($extra:tt)*) => {
            InferenceRequest::$variant {
                prompt_tokens: Arc::new(prompt),
                session_hash: r.id,
                lookup_tenant: r.lookup_tenant,
                adapter_slot: -1,
                src_lang_id: 0,
                tgt_lang_id: 0,
                num_beams: r.num_beams,
                length_penalty: 1.0,
                early_stopping: false,
                image_pixels: vec![],
                max_tokens: r.max_tokens,
                min_tokens: r.min_tokens,
                temperature: r.temperature,
                top_k: 0,
                top_p: 1.0,
                top_n_sigma: 0.0,
                min_p: 0.0,
                repetition_penalty: 1.0,
                presence_penalty: 0.0,
                frequency_penalty: 0.0,
                dry_multiplier: 0.0,
                dry_base: 0.0,
                dry_allowed_length: 0,
                lz_penalty: 0.0,
                logit_bias: vec![],
                stop_tokens: vec![],
                enable_thinking: false,
                thinking_budget: None,
                repetition_detection: None,
                require_tool_call: false,
                tools_present: false,
                suppress_tool_call: false,
                disable_mtp: false,
                grammar_spec: None,
                seed: Some(42),
                top_logprobs: r.top_logprobs,
                prompt_logprobs: None,
                echo: false,
                timeout_at,
                $($extra)*
            }
        };
    }
    if r.streaming {
        let (token_tx, mut rx) = tokio::sync::mpsc::channel::<StreamEvent>(256);
        let cancel_flag = Arc::new(AtomicBool::new(false));
        shared.register_cancel(r.id, Arc::clone(&cancel_flag));
        let collector = std::thread::spawn(move || {
            let mut out = Vec::new();
            while let Some(ev) = rx.blocking_recv() {
                out.push(fmt_event(&ev));
            }
            out
        });
        (
            common!(Streaming, token_tx, cancel_flag),
            Sink::Streaming(collector),
        )
    } else {
        let (response_tx, rx) = tokio::sync::oneshot::channel();
        (common!(Blocking, response_tx), Sink::Blocking(rx))
    }
}

/// 2026-09-25: Wait until the forwarder thread has pulled everything off the request
/// channel, then give it a moment to push into the scheduler's queue.
fn settle(tx: &tokio::sync::mpsc::Sender<InferenceRequest>) {
    while tx.capacity() < tx.max_capacity() {
        std::thread::sleep(Duration::from_millis(1));
    }
    std::thread::sleep(Duration::from_millis(20));
}

/// 2026-09-25: How a run builds its device router over the recording model. The default
/// is the sync router under the effect-trace decorator; the scripted tests
/// insert `ScriptedDeviceIo`, the pipeline tests the asynchronous router.
pub(super) type DeviceBuilder = Arc<
    dyn Fn(
            Arc<dyn metrale_model_engine::traits::Model>,
            metrale_scheduler::TraceSink,
        ) -> Box<crate::scheduler::io::DynDevice>
        + Send
        + Sync,
>;

/// 2026-09-25: The hard deadline for one scenario. A scenario that has not produced its
/// outputs by then is a hang (the loop parked on its inbox or a client
/// stream never closed), and the test fails with the scenario's name
/// instead of blocking the run.
const SCENARIO_DEADLINE: Duration = Duration::from_secs(120);

pub(super) fn run_scenario(sc: &Scenario) -> Vec<String> {
    run_scenario_with(
        sc,
        Arc::new(|model, sink| {
            Box::new(crate::scheduler::io::TracingDeviceIo::new(
                crate::scheduler::io::SyncDeviceIo::new(model),
                sink,
            ))
        }),
    )
}

/// 2026-09-25: `run_scenario` with the device router that `build` makes from the
/// recording model and the trace sink. The run happens on its own thread
/// under [`SCENARIO_DEADLINE`].
pub(super) fn run_scenario_with(sc: &Scenario, build: DeviceBuilder) -> Vec<String> {
    let (tx, rx) = std::sync::mpsc::channel();
    let name = sc.name;
    let sc = sc.clone();
    std::thread::spawn(move || {
        let _ = tx.send(run_scenario_inner(&sc, build));
    });
    match rx.recv_timeout(SCENARIO_DEADLINE) {
        Ok(lines) => lines,
        Err(std::sync::mpsc::RecvTimeoutError::Timeout) => panic!(
            "scenario {name} exceeded {SCENARIO_DEADLINE:?}: the loop or a client stream hung"
        ),
        Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
            panic!("scenario {name} panicked before producing its outputs")
        }
    }
}

fn run_scenario_inner(sc: &Scenario, build: DeviceBuilder) -> Vec<String> {
    let model = RecordingModel::new(sc.cfg.clone());
    let shared = Arc::clone(&model.shared);
    let (request_tx, request_rx) = tokio::sync::mpsc::channel::<InferenceRequest>(1024);
    let (rotation_tx, rotation_rx) = tokio::sync::mpsc::channel(8);

    let mut sinks: Vec<(u64, Sink)> = Vec::new();
    let mut later: Vec<(usize, InferenceRequest)> = Vec::new();
    for r in &sc.reqs {
        let (req, sink) = build_request(r, &shared);
        sinks.push((r.id, sink));
        match r.arrive_at_tick {
            None => request_tx.blocking_send(req).expect("channel open"),
            Some(t) => later.push((t, req)),
        }
    }
    let rotation_ack = sc.opts.lora_rotation.as_ref().map(|name| {
        let (ack_tx, ack_rx) = tokio::sync::oneshot::channel();
        rotation_tx
            .blocking_send((LoraCommand::Rotate(name.clone()), ack_tx))
            .expect("rotation channel open");
        ack_rx
    });

    let opts = sc.opts.clone();
    let block_size = sc.cfg.block_size;
    let vocab = sc.cfg.vocab;
    let policy: Box<dyn SchedulingPolicy> = if opts.slai_policy {
        Box::new(SlaiPolicy::new(1_000_000))
    } else {
        Box::new(FifoPolicy)
    };
    let mut levers = SchedLevers::defaults();
    levers.mtp_gate_force = opts.mtp_gate_force;
    levers.prefill_codispatch = opts.prefill_codispatch;
    levers.ep_prefill_batch = opts.ep_prefill_batch;
    levers.dflash_seam_serial = opts.dflash_seam_serial;
    let levers = Arc::new(levers);
    levers.set_loop_watchdog(opts.loop_watchdog);
    let masks = VocabMasks {
        numeric: None,
        mid_word: None,
        boundary: (!opts.boundary_tokens.is_empty()).then(|| {
            let mut m = vec![false; vocab];
            for &t in &opts.boundary_tokens {
                m[t as usize] = true;
            }
            Arc::from(m.into_boxed_slice())
        }),
    };
    let snapshot = Arc::new(metrale_speculative::snapshot::SnapshotCell::default());
    let trace_shared = Arc::clone(&shared);
    let dev: Box<crate::scheduler::io::DynDevice> = build(
        Arc::new(model),
        Arc::new(move |line| trace_shared.rec(line)),
    );
    let handle = std::thread::spawn(move || {
        crate::scheduler::run(
            dev,
            request_rx,
            rotation_rx,
            crate::scheduler::config::SchedulerConfig {
                eos_tokens: vec![EOS],
                max_batch_size: opts.max_batch_size,
                use_speculative: opts.use_speculative,
                dflash_verify_raw_argmax: opts.dflash,
                num_drafts: opts.num_drafts,
                policy,
                max_prefill_tokens: opts.max_prefill_tokens,
                max_batch_tokens: opts.max_batch_tokens,
                use_self_speculative: opts.self_speculative,
                use_ngram_speculative: opts.ngram_speculative,
                prompt_lookup: opts.prompt_lookup,
                shared_lookup: opts.shared_lookup,
                swap_space_gb: opts.swap_space_gb,
                high_speed_swap_cfg: None,
                block_size,
                think_end_token: opts.think_end_token,
                think_start_token: opts.think_start_token,
                code_fence_token: None,
                tool_call_start_token: None,
                tool_call_end_token: None,
                grammar_engine: None,
                adaptive_sampling: false,
                session_manager: crate::session_manager::SessionSsmManager::new(3600),
                spontaneous_think_budget: Some(512),
                vocab_masks: masks,
                limits: SchedLimits {
                    im_start_hard_stop: None,
                    tool_response_hard_stop: None,
                    thinking_closed_by: None,
                    max_seq_len: 4096,
                },
                watchdog: WatchdogParams::default(),
                levers,
                snapshot,
                dflash_rung: {
                    let r = metrale_speculative::dflash_rung::DflashRung::new();
                    r.set_adaptive_k(
                        opts.dflash_adaptive_k
                            .clone()
                            .map(metrale_speculative::dflash_adaptive_k::AdaptiveK::new),
                    );
                    r
                },
                telemetry: opts.telemetry,
                pipeline_faults: opts.pipeline_faults,
            },
        );
    });

    later.sort_by_key(|(t, _)| *t);
    settle(&request_tx);
    if let Some(prefix) = sc.opts.close_inbox_at {
        shared.park.arm(prefix);
    }
    if let Some((t, _)) = later.first() {
        shared.gate.block_at_tick(*t);
    }
    shared.gate.start();
    let i = 0;
    while i < later.len() {
        let t = later[i].0;
        shared.gate.wait_tick(t);
        while i < later.len() && later[i].0 == t {
            let (_, req) = later.remove(i);
            request_tx.blocking_send(req).expect("channel open");
        }
        settle(&request_tx);
        if i < later.len() {
            shared.gate.block_at_tick(later[i].0);
        }
        shared.gate.release();
    }
    // 2026-09-25: collect every client's output before dropping the
    // request channel: a sink closes when its sequence retires, so the
    // loop sees the channel close only once every request is done, and
    // its shutdown is the same in every run.
    let outputs: Vec<(u64, String)> = sinks
        .into_iter()
        .map(|(id, sink)| {
            let out = match sink {
                Sink::Streaming(h) => h.join().expect("collector").join(" "),
                Sink::Blocking(rx) => match rx.blocking_recv() {
                    Ok(Ok(r)) => format!(
                        "Response(tokens={:?}, {}, think={}, cached={}, acc={}, logprobs={})",
                        r.output_tokens,
                        r.finish_reason,
                        r.reasoning_tokens,
                        r.cached_prompt_tokens,
                        r.accepted_prediction_tokens,
                        r.logprobs.len()
                    ),
                    Ok(Err(e)) => format!("Err({e})"),
                    Err(_) => "dropped".to_string(),
                },
            };
            (id, out)
        })
        .collect();
    let mut request_tx = Some(request_tx);
    if sc.opts.close_inbox_at.is_some() {
        shared.park.wait_parked();
        drop(request_tx.take());
        // 2026-10-03: Long enough for the inbox's forwarder thread to see the close; nothing
        // makes that observable from here.
        std::thread::sleep(Duration::from_millis(100));
        shared.park.release();
    }
    let rotation_result = rotation_ack.map(|rx| match rx.blocking_recv() {
        Ok(Ok(ack)) => format!("{ack:?}"),
        Ok(Err(e)) => format!("Err({e})"),
        Err(_) => "dropped".to_string(),
    });
    drop(request_tx);
    drop(rotation_tx);
    handle.join().expect("scheduler thread");

    let mut lines = vec![format!("# scenario {}", sc.name)];
    lines.extend(shared.take_trace());
    if let Some(r) = rotation_result {
        lines.push(format!("rotation ack: {r}"));
    }
    for (id, out) in outputs {
        lines.push(format!("out s{id}: {out}"));
    }
    lines
}