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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! 2026-09-25: Tests that `prefilling` is served in arrival order. The
//! single-stream path advances `prefilling.first_mut()`, and
//! `promote_completed_prefills` must remove entries without reordering the
//! rest. The tick-loop tests run `continue_in_progress_prefills` with one
//! active decode and `use_mtp`: `mixing_blocked_by_spec` then rules out
//! the batched mixed step, and the non-empty `active` rules out the batched
//! prefill-only step, so every tick takes the single-stream path.
//!
//! Owner: scheduler.
//! Invariants: none beyond the types.

use anyhow::Result;
use metrale_gpu_runtime::gpu::DevicePtr;
use metrale_model_engine::traits::{
    Model, ModelAdapters, ModelDeviceFeed, ModelDraft, ModelEp, ModelForward, ModelLifecycle,
    ModelLogits, ModelSsmState, ModelStreams, ModelVerify, ModelVision, SequenceState,
};

use super::io::SchedIo;
use super::phase_continue_prefills::continue_in_progress_prefills;
use super::phase_promote_prefills::promote_completed_prefills;
use super::sched_ctx::SchedCtx;
use super::test_support::{test_prefill_ident, test_seq};
use super::types::{ActiveSeq, PrefillInProgress};
use crate::scheduling_policy::FifoPolicy;

/// 2026-09-25: The sampled first token. The fixture's `eos_tokens` is
/// empty, so it is never an EOS.
const FIRST: u32 = 7;

/// 2026-09-25: The prompt length and the prefill budget, so each prompt is
/// one chunk.
const CHUNK: usize = 4;

/// 2026-09-25: Minimal `Model`: every prefill chunk succeeds, and the
/// greedy first-token sample (temperature 0.0, no suppressed ids) is
/// `argmax_on_device`. The other methods are unreachable in these tests.
#[derive(Default)]
struct PrefillStubModel;

impl Model for PrefillStubModel {}

impl ModelLifecycle for PrefillStubModel {
    fn free_sequence(&self, _seq: &mut SequenceState) -> Result<()> {
        Ok(())
    }
    fn cache_sequence(&self, _seq: &SequenceState) {}
    fn detach_slot_for_reuse(&self, _seq: &mut SequenceState) {}
    fn bind_gpu_to_thread(&self) -> Result<()> {
        Ok(())
    }
    fn alloc_sequence(&self) -> Result<SequenceState> {
        Ok(SequenceState::host_only(0))
    }
    fn compact_sequence(&self, _s: &mut SequenceState, _new_slot: usize) -> Result<()> {
        unreachable!("no compaction in this harness")
    }
    fn kv_block_bytes(&self) -> usize {
        0
    }
}

impl ModelForward for PrefillStubModel {
    fn prefill_chunk(
        &self,
        tokens: &[u32],
        seq: &mut SequenceState,
        chunk_start: usize,
        chunk_len: usize,
        _is_last: bool,
        _stream: u64,
    ) -> Result<DevicePtr> {
        seq.tokens
            .extend_from_slice(&tokens[chunk_start..chunk_start + chunk_len]);
        seq.seq_len = seq.tokens.len();
        Ok(DevicePtr::NULL)
    }
    fn prefill(&self, _t: &[u32], _s: &mut SequenceState, _st: u64) -> Result<DevicePtr> {
        unreachable!("chunked prefill only")
    }
    fn decode(&self, _t: u32, _s: &mut SequenceState, _st: u64) -> Result<DevicePtr> {
        unreachable!("decode is driven by mod.rs, not by this harness")
    }
    fn decode_batch(
        &self,
        _t: &[u32],
        _s: &mut [&mut SequenceState],
        _st: u64,
    ) -> Result<DevicePtr> {
        unreachable!("decode is driven by mod.rs, not by this harness")
    }
}

impl ModelLogits for PrefillStubModel {
    fn argmax_on_device(&self, _logits_ptr: DevicePtr, _stream: u64) -> Result<u32> {
        Ok(FIRST)
    }
    fn vocab_size(&self) -> usize {
        32
    }
    fn logits_buffer_ptr(&self) -> DevicePtr {
        DevicePtr::NULL
    }
    fn hidden_after_norm(&self) -> DevicePtr {
        DevicePtr::NULL
    }
    fn copy_logits_to_host(&self, _l: DevicePtr, _dst: &mut [u8]) -> Result<()> {
        unreachable!("greedy fast path never reads logits back")
    }
    fn argmax_batch(&self, _l: DevicePtr, _n: usize, _st: u64) -> Result<Vec<u32>> {
        unreachable!("batched decode is not driven here")
    }
}

impl ModelAdapters for PrefillStubModel {}

impl ModelSsmState for PrefillStubModel {
    fn checkpoint_ssm_states(&self, _s: &mut SequenceState) -> Result<()> {
        unreachable!("no speculation in this harness")
    }
    fn rollback_ssm_states(&self, _s: &mut SequenceState, _n: usize) -> Result<()> {
        unreachable!("no speculation in this harness")
    }
}

impl ModelVerify for PrefillStubModel {
    fn decode_verify(&self, _t: &[u32], _s: &mut SequenceState, _st: u64) -> Result<Vec<u32>> {
        unreachable!("no speculation in this harness")
    }
    fn decode_verify_graphed(
        &self,
        _t: &[u32; 2],
        _s: &mut SequenceState,
        _st: u64,
    ) -> Result<[u32; 2]> {
        unreachable!("no speculation in this harness")
    }
    fn decode_verify_graphed_k3(
        &self,
        _t: &[u32; 3],
        _s: &mut SequenceState,
        _st: u64,
    ) -> Result<[u32; 3]> {
        unreachable!("no speculation in this harness")
    }
    fn decode_verify_graphed_k4(
        &self,
        _t: &[u32; 4],
        _s: &mut SequenceState,
        _st: u64,
    ) -> Result<[u32; 4]> {
        unreachable!("no speculation in this harness")
    }
}

impl ModelDraft for PrefillStubModel {
    fn has_proposer(&self) -> bool {
        false
    }
    fn has_self_speculative(&self) -> bool {
        false
    }
    fn decode_draft(&self, _t: u32, _s: &mut SequenceState, _st: u64) -> Result<DevicePtr> {
        unreachable!("no speculation in this harness")
    }
    fn save_hidden_for_mtp(&self, _token_idx: usize, _st: u64) -> Result<()> {
        unreachable!("no speculation in this harness")
    }
    fn run_mtp_propose(
        &self,
        _t: u32,
        _p: usize,
        _s: &mut SequenceState,
        _st: u64,
    ) -> Result<Option<u32>> {
        unreachable!("no speculation in this harness")
    }
    fn run_mtp_propose_multi(
        &self,
        _t: u32,
        _p: usize,
        _n: usize,
        _s: &mut SequenceState,
        _st: u64,
        _mask: Option<&[i32]>,
    ) -> Result<Vec<u32>> {
        unreachable!("no speculation in this harness")
    }
    fn trim_proposer_state(&self, _s: &mut SequenceState, _n: usize, _st: u64) -> Result<()> {
        unreachable!("no speculation in this harness")
    }
    fn generate_speculative(
        &self,
        _p: &[u32],
        _params: &metrale_sampling::SamplingParams,
        _n: usize,
    ) -> Result<metrale_model_engine::engine::GenerateResult> {
        unreachable!("no speculation in this harness")
    }
}

impl ModelVision for PrefillStubModel {}

impl ModelEp for PrefillStubModel {}
impl metrale_model_engine::traits::ModelCircuit for PrefillStubModel {}

impl ModelStreams for PrefillStubModel {}

impl ModelDeviceFeed for PrefillStubModel {}

/// 2026-09-25: What one run of the tick loop observed.
struct Run {
    /// 2026-09-25: `(session_hash, tick)` for every prefill promoted into
    /// `active`.
    promoted: Vec<(u64, usize)>,
    /// 2026-09-25: Initially queued ids that were never promoted.
    stuck: Vec<u64>,
}

/// 2026-09-25: Run the arrive → continue → promote loop for `ticks` ticks.
///
/// `seed_depth` requests (ids `1..=seed_depth`) are queued at the start, and
/// one new request arrives every tick, before the continue phase, as in
/// `core/lanes.rs` (`start_new_requests`, then
/// `continue_in_progress_prefills`).
fn drive(seed_depth: usize, ticks: usize) -> Run {
    let model = PrefillStubModel;
    let policy = FifoPolicy;
    let sched = SchedCtx::for_test();

    // 2026-09-25: Response receivers, kept alive so no sink sees a closed
    // channel.
    let mut keep = Vec::new();
    let mut active: Vec<ActiveSeq> = Vec::new();
    let mut prefilling: Vec<PrefillInProgress> = Vec::new();

    // 2026-09-25: The one decode occupant (see the module doc). Its
    // `session_hash` is 0; prefill ids start at 1.
    let (occupant, rx) = test_seq(vec![1], usize::MAX, None, 8);
    keep.push(rx);
    active.push(occupant);

    let seeded: Vec<u64> = (1..=seed_depth as u64).collect();
    for &id in &seeded {
        let (p, rx) = test_prefill_ident(id, CHUNK);
        keep.push(rx);
        prefilling.push(p);
    }

    let mut next_id = seed_depth as u64 + 1;
    let mut promoted: Vec<(u64, usize)> = Vec::new();

    for tick in 0..ticks {
        let (p, rx) = test_prefill_ident(next_id, CHUNK);
        keep.push(rx);
        prefilling.push(p);
        next_id += 1;

        continue_in_progress_prefills(
            &model,
            &policy,
            &mut active,
            &mut prefilling,
            CHUNK,
            CHUNK,
            false,
            0,
            0,
            true,
            false,
            false,
            None,
            None,
            None,
            None,
            None,
            false,
            &sched,
        );

        // 2026-09-25: Retire every promoted prefill at once; promotion
        // appends, so the occupant stays at index 0.
        for a in active.drain(1..) {
            promoted.push((a.session_hash, tick));
        }
    }

    let stuck = seeded
        .into_iter()
        .filter(|id| !promoted.iter().any(|(p, _)| p == id))
        .collect();
    Run { promoted, stuck }
}

/// 2026-09-25: Every request queued at the start is promoted although a
/// new one arrives every tick. A removal by `swap_remove` would move the
/// newest arrival to the head, so ids 2..=depth would never be served;
/// depth 1 passes either way.
#[test]
fn every_queued_prefill_advances_under_sustained_arrivals() {
    for depth in 1..=8 {
        let run = drive(depth, 500);
        assert!(
            run.stuck.is_empty(),
            "queue depth {depth}: request(s) {:?} never got a prefill chunk in 500 ticks",
            run.stuck,
        );
    }
}

/// 2026-09-25: With one chunk per request, the request `k` places from the
/// head is promoted on tick `k`.
#[test]
fn queued_prefills_are_served_in_arrival_order() {
    for depth in 1..=8 {
        let run = drive(depth, 500);
        for (rank, id) in (1..=depth as u64).enumerate() {
            let tick = run
                .promoted
                .iter()
                .find(|(p, _)| *p == id)
                .map(|(_, t)| *t)
                .unwrap_or_else(|| panic!("depth {depth}: request {id} never promoted"));
            assert_eq!(
                tick, rank,
                "depth {depth}: request {id} was {rank} places from the head but ran on tick {tick}",
            );
        }
    }
}

/// 2026-09-25: Control: each tick promotes one prefill, so a failure above
/// is about which requests ran, not a loop that makes no progress.
#[test]
fn the_tick_loop_makes_progress_every_tick() {
    let run = drive(8, 500);
    assert_eq!(
        run.promoted.len(),
        500,
        "expected one promotion per tick, got {}",
        run.promoted.len(),
    );
}

/// 2026-09-25: Promoting the head leaves the rest of the queue in arrival
/// order.
#[test]
fn promoting_the_head_preserves_the_order_of_the_remainder() {
    let model = PrefillStubModel;
    let mut keep = Vec::new();
    let mut prefilling: Vec<PrefillInProgress> = (1..=4)
        .map(|id| {
            let (p, rx) = test_prefill_ident(id, CHUNK);
            keep.push(rx);
            p
        })
        .collect();
    let mut active: Vec<ActiveSeq> = Vec::new();

    promote_completed_prefills(
        &model,
        &SchedIo::for_test_with(std::sync::Arc::new(PrefillStubModel)),
        &mut prefilling,
        vec![(0, Some(FIRST))],
        &mut active,
        None,
        None,
        None,
        None,
        4096,
    );

    let order: Vec<u64> = prefilling.iter().map(|p| p.session_hash).collect();
    assert_eq!(
        order,
        vec![2, 3, 4],
        "head removal must not reorder the queue"
    );
    assert_eq!(active.len(), 1);
    assert_eq!(active[0].session_hash, 1);
}

/// 2026-09-25: The batched paths pass several completed indices at once.
/// They are removed in reverse index order, and the rest of the queue stays
/// in arrival order.
#[test]
fn promoting_several_at_once_preserves_the_order_of_the_remainder() {
    let model = PrefillStubModel;
    let mut keep = Vec::new();
    let mut prefilling: Vec<PrefillInProgress> = (1..=5)
        .map(|id| {
            let (p, rx) = test_prefill_ident(id, CHUNK);
            keep.push(rx);
            p
        })
        .collect();
    let mut active: Vec<ActiveSeq> = Vec::new();

    promote_completed_prefills(
        &model,
        &SchedIo::for_test_with(std::sync::Arc::new(PrefillStubModel)),
        &mut prefilling,
        vec![(0, Some(FIRST)), (2, Some(FIRST))],
        &mut active,
        None,
        None,
        None,
        None,
        4096,
    );

    let order: Vec<u64> = prefilling.iter().map(|p| p.session_hash).collect();
    assert_eq!(
        order,
        vec![2, 4, 5],
        "multi-removal must not reorder the queue"
    );
    let promoted: Vec<u64> = active.iter().map(|a| a.session_hash).collect();
    assert_eq!(
        promoted,
        vec![3, 1],
        "promotion still walks the indices in reverse"
    );
}