dynamo-mocker 1.7.0

Mock LLM scheduler and KV manager for testing
Documentation
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Performance model for timing simulations in the mocker.
//!
//! `Polynomial` remains a legacy configuration marker. Its implementation is
//! owned by `aisimulate_core::engine`; this module only implements external NPZ and
//! AI Configurator providers.

use anyhow::{Context, Result};
use ndarray::{Array1, Array2};
use ndarray_interp::InterpolateError;
use ndarray_interp::interp1d::{Interp1DBuilder, Linear};
use ndarray_interp::interp2d::{Bilinear, Interp2DBuilder};
use ndarray_npy::ReadNpyExt;
use std::io::{Read, Seek};
use std::path::Path;
use std::sync::Arc;
use zip::result::ZipError;
use zip::{ZipArchive, ZipReadOptions};

/// Trait to abstract over 1D interpolation for prefill timing
pub trait PrefillInterpolator: Send + Sync {
    fn interp(&self, x: f64) -> Result<f64, InterpolateError>;
}

/// Trait to abstract over 2D interpolation for decode timing
pub trait DecodeInterpolator: Send + Sync {
    fn interp(&self, x: f64, y: f64) -> Result<f64, InterpolateError>;
}

/// Callback trait for direct AIS SDK calls.
/// Implementors call the Rust AIS core API.
pub trait AisCallback: Send + Sync {
    /// Predict prefill latency in ms.
    /// Parameters: (batch_size, effective_isl, prefix)
    fn predict_prefill(
        &self,
        batch_size: usize,
        effective_isl: usize,
        prefix: usize,
    ) -> Result<f64>;

    /// Predict decode (generation) latency in ms.
    /// Parameters: (batch_size, isl, osl)
    fn predict_decode(&self, batch_size: usize, isl: usize, osl: usize) -> Result<f64>;
}

/// Wrapper to implement PrefillInterpolator for the concrete Interp1D type
struct PrefillInterp1D {
    inner: ndarray_interp::interp1d::Interp1D<
        ndarray::OwnedRepr<f64>,
        ndarray::OwnedRepr<f64>,
        ndarray::Ix1,
        Linear,
    >,
}

impl PrefillInterpolator for PrefillInterp1D {
    fn interp(&self, x: f64) -> Result<f64, InterpolateError> {
        self.inner.interp_scalar(x)
    }
}

/// Wrapper to implement DecodeInterpolator for the concrete Interp2D type
struct DecodeInterp2D {
    inner: ndarray_interp::interp2d::Interp2D<
        ndarray::OwnedRepr<f64>,
        ndarray::OwnedRepr<f64>,
        ndarray::OwnedRepr<f64>,
        ndarray::Ix2,
        Bilinear,
    >,
}

impl DecodeInterpolator for DecodeInterp2D {
    fn interp(&self, x: f64, y: f64) -> Result<f64, InterpolateError> {
        self.inner.interp_scalar(x, y)
    }
}

/// Performance model for predicting prefill and decode timing
#[derive(Default)]
pub enum PerfModel {
    /// Select the built-in AISimulate polynomial timing model.
    #[default]
    Polynomial,
    /// Constant per-pass latencies owned by the AISimulate engine.
    Fixed { prefill_ms: f64, decode_ms: f64 },
    /// Interpolation-based model using profiler data
    /// Decode axes: (scheduled logical KV tokens, mean context length)
    Interpolated {
        prefill_interp: Arc<dyn PrefillInterpolator>,
        decode_interp: Arc<dyn DecodeInterpolator>,
    },
    /// AI Configurator SDK calls through the configured callback.
    /// Passes the reduced prefill inputs (batch_size, effective_isl, prefix).
    Ais { callback: Arc<dyn AisCallback> },
}

impl Clone for PerfModel {
    fn clone(&self) -> Self {
        match self {
            PerfModel::Polynomial => PerfModel::Polynomial,
            PerfModel::Fixed {
                prefill_ms,
                decode_ms,
            } => PerfModel::Fixed {
                prefill_ms: *prefill_ms,
                decode_ms: *decode_ms,
            },
            PerfModel::Interpolated {
                prefill_interp,
                decode_interp,
            } => PerfModel::Interpolated {
                prefill_interp: Arc::clone(prefill_interp),
                decode_interp: Arc::clone(decode_interp),
            },
            PerfModel::Ais { callback } => PerfModel::Ais {
                callback: Arc::clone(callback),
            },
        }
    }
}

impl std::fmt::Debug for PerfModel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PerfModel::Polynomial => write!(f, "PerfModel::Polynomial"),
            PerfModel::Fixed {
                prefill_ms,
                decode_ms,
            } => write!(
                f,
                "PerfModel::Fixed {{ prefill_ms: {prefill_ms}, decode_ms: {decode_ms} }}"
            ),
            PerfModel::Interpolated { .. } => write!(f, "PerfModel::Interpolated {{ .. }}"),
            PerfModel::Ais { .. } => write!(f, "PerfModel::Ais"),
        }
    }
}

/// Reads arrays from a NumPy `.npz` archive. `ndarray_npy::NpzReader` needs zip 2.x, so this
/// opens the archive with `zip` and parses each entry with `ReadNpyExt`.
struct NpzReader<R>(ZipArchive<R>);

impl<R: Read + Seek> NpzReader<R> {
    fn new(reader: R) -> Result<Self> {
        Ok(Self(ZipArchive::new(reader)?))
    }

    /// Lists the entry names with one `.npy` suffix removed, as `numpy.load` lists them.
    #[cfg_attr(
        not(test),
        expect(
            dead_code,
            reason = "#17086 checks for the optional prefill2d_* arrays with it"
        )
    )]
    fn names(&self) -> Result<Vec<String>> {
        self.0
            .file_names()
            .map(|name| -> Result<String> {
                let name = name?;
                Ok(name.strip_suffix(".npy").unwrap_or(&name).to_owned())
            })
            .collect()
    }

    /// Reads entry `name`, else `{name}.npy`, the name that `numpy.savez` writes. It refuses an
    /// entry whose local header disagrees with the central directory.
    fn by_name<A: ReadNpyExt>(&mut self, name: &str) -> Result<A> {
        let index = self
            .0
            .index_for_name(name)
            .or_else(|| self.0.index_for_name(&format!("{name}.npy")))
            .ok_or(ZipError::FileNotFound)?;
        let options = ZipReadOptions::new().enforce_header_consistency(true);
        Ok(A::read_npy(self.0.by_index_with_options(index, options)?)?)
    }
}

impl PerfModel {
    /// Load performance model from NPZ file
    ///
    /// Expected arrays in NPZ file:
    /// - prefill_isl: 1D array of input sequence lengths
    /// - prefill_ttft_ms: 1D array of time to first token in milliseconds
    /// - decode_active_kv_tokens: 1D array of scheduled logical KV token counts
    /// - decode_context_length: 1D array of context lengths
    /// - decode_itl: 2D array of inter-token latencies in milliseconds
    pub fn from_npz(path: &Path) -> Result<Self> {
        use std::fs::File;

        tracing::info!("Loading performance model from NPZ file: {:?}", path);

        let file =
            File::open(path).with_context(|| format!("Failed to open NPZ file: {:?}", path))?;

        let mut npz = NpzReader::new(file)
            .with_context(|| format!("Failed to create NPZ reader for: {:?}", path))?;

        // Load prefill arrays
        let prefill_isl: Array1<f64> = npz
            .by_name("prefill_isl")
            .with_context(|| "Failed to load prefill_isl from NPZ")?;
        let prefill_ttft_ms: Array1<f64> = npz
            .by_name("prefill_ttft_ms")
            .with_context(|| "Failed to load prefill_ttft_ms from NPZ")?;

        // Load decode arrays
        let decode_active_kv_tokens: Array1<f64> = npz
            .by_name("decode_active_kv_tokens")
            .with_context(|| "Failed to load decode_active_kv_tokens from NPZ")?;
        let decode_context_length: Array1<f64> = npz
            .by_name("decode_context_length")
            .with_context(|| "Failed to load decode_context_length from NPZ")?;
        let decode_itl: Array2<f64> = npz
            .by_name("decode_itl")
            .with_context(|| "Failed to load decode_itl from NPZ")?;

        // Validate dimensions
        if prefill_isl.len() != prefill_ttft_ms.len() {
            anyhow::bail!(
                "Prefill array length mismatch: isl={}, ttft={}",
                prefill_isl.len(),
                prefill_ttft_ms.len()
            );
        }

        if decode_itl.nrows() != decode_active_kv_tokens.len()
            || decode_itl.ncols() != decode_context_length.len()
        {
            anyhow::bail!(
                "Decode array dimension mismatch: itl shape=({}, {}), active_kv={}, context={}",
                decode_itl.nrows(),
                decode_itl.ncols(),
                decode_active_kv_tokens.len(),
                decode_context_length.len()
            );
        }

        tracing::info!(
            "Loaded performance model: prefill_points={}, decode_grid={}x{}",
            prefill_isl.len(),
            decode_itl.nrows(),
            decode_itl.ncols()
        );

        // Build interpolators once during loading
        let prefill_interp = Interp1DBuilder::new(prefill_ttft_ms)
            .x(prefill_isl)
            .strategy(Linear::new().extrapolate(true))
            .build()
            .with_context(|| "Failed to build prefill interpolator")?;

        let decode_interp = Interp2DBuilder::new(decode_itl)
            .x(decode_active_kv_tokens)
            .y(decode_context_length)
            .strategy(Bilinear::new().extrapolate(true))
            .build()
            .with_context(|| "Failed to build decode interpolator")?;

        Ok(PerfModel::Interpolated {
            prefill_interp: Arc::new(PrefillInterp1D {
                inner: prefill_interp,
            }),
            decode_interp: Arc::new(DecodeInterp2D {
                inner: decode_interp,
            }),
        })
    }

    /// Create an Ais perf model from a callback.
    pub fn from_ais_callback(callback: Arc<dyn AisCallback>) -> Self {
        PerfModel::Ais { callback }
    }

    /// Predict prefill time in milliseconds.
    ///
    /// Callers always pass all parameters; each external variant uses what it needs:
    /// - Interpolated uses total new tokens across the batch
    ///   (`batch_size * (isl - prefix)`).
    /// - Ais: passes (batch_size, isl - prefix, prefix) to the AIS SDK
    pub fn predict_prefill_time(
        &self,
        batch_size: usize,
        isl: usize,
        prefix: usize,
    ) -> Result<f64> {
        if matches!(self, Self::Polynomial | Self::Fixed { .. }) {
            anyhow::bail!("built-in timing is implemented by the AISimulate engine");
        }
        let new_tokens_per_req = isl.saturating_sub(prefix);
        if batch_size == 0 || new_tokens_per_req == 0 {
            return Ok(0.0);
        }
        let time = match self {
            PerfModel::Polynomial => unreachable!("polynomial handled above"),
            PerfModel::Fixed { .. } => unreachable!("fixed timing handled above"),
            PerfModel::Interpolated { prefill_interp, .. } => {
                let tokens = (batch_size * new_tokens_per_req) as f64;
                prefill_interp.interp(tokens).unwrap_or(0.0)
            }
            PerfModel::Ais { callback } => callback
                .predict_prefill(batch_size, new_tokens_per_req, prefix)
                .context("AIS prefill prediction failed")?,
        };
        Ok(time.max(0.0))
    }

    /// Predict decode time in milliseconds.
    ///
    /// `active_kv_tokens` is the sum of logical context lengths in the scheduled
    /// batch, not the number of distinct physically resident tokens.
    ///
    /// Callers always pass all parameters; each variant uses what it needs:
    /// - Interpolated: uses (active_kv_tokens, context_length)
    /// - Ais: uses (batch_size, context_length)
    pub fn predict_decode_time(
        &self,
        batch_size: usize,
        active_kv_tokens: usize,
        context_length: usize,
        _total_kv_tokens: usize,
    ) -> Result<f64> {
        if matches!(self, Self::Polynomial | Self::Fixed { .. }) {
            anyhow::bail!("built-in timing is implemented by the AISimulate engine");
        }
        if batch_size == 0 {
            return Ok(0.0);
        }
        let time = match self {
            PerfModel::Polynomial => unreachable!("polynomial handled above"),
            PerfModel::Fixed { .. } => unreachable!("fixed timing handled above"),
            PerfModel::Interpolated { decode_interp, .. } => decode_interp
                .interp(active_kv_tokens as f64, context_length as f64)
                .unwrap_or(0.0),
            PerfModel::Ais { callback } => callback
                .predict_decode(batch_size, context_length, 2)
                .context("AIS decode prediction failed")?,
        };
        // Token-emitting decode steps should not collapse onto the same timestamp.
        let result = time.max(1.0);
        tracing::trace!(
            "Decode time prediction: batch_size={batch_size}, active_kv_tokens={active_kv_tokens}, context_length={context_length}, time={result:.2}ms"
        );
        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use super::{AisCallback, NpzReader, PerfModel};
    use ndarray::{Array1, array};
    use ndarray_npy::WriteNpyExt;
    use std::io::{Cursor, Seek, Write};
    use std::sync::Arc;
    use zip::CompressionMethod;
    use zip::result::ZipError;
    use zip::write::SimpleFileOptions;

    fn npy(array: &impl WriteNpyExt) -> Vec<u8> {
        let mut bytes = Vec::new();
        array.write_npy(&mut bytes).unwrap();
        bytes
    }

    /// Writes entries with the compression of `numpy.savez` (stored) or `numpy.savez_compressed`
    /// (deflated).
    fn write_npz(
        writer: impl Write + Seek,
        method: CompressionMethod,
        entries: &[(&str, Vec<u8>)],
    ) {
        let mut npz = zip::ZipWriter::new(writer);
        let options = SimpleFileOptions::default().compression_method(method);
        for (name, bytes) in entries {
            npz.start_file(*name, options).unwrap();
            npz.write_all(bytes).unwrap();
        }
        npz.finish().unwrap();
    }

    struct EchoBatchCallback;

    impl AisCallback for EchoBatchCallback {
        fn predict_prefill(
            &self,
            batch_size: usize,
            _effective_isl: usize,
            _prefix: usize,
        ) -> anyhow::Result<f64> {
            Ok(batch_size as f64)
        }

        fn predict_decode(
            &self,
            batch_size: usize,
            _isl: usize,
            _osl: usize,
        ) -> anyhow::Result<f64> {
            Ok(batch_size as f64)
        }
    }

    struct FailingCallback;

    impl AisCallback for FailingCallback {
        fn predict_prefill(
            &self,
            _batch_size: usize,
            _effective_isl: usize,
            _prefix: usize,
        ) -> anyhow::Result<f64> {
            anyhow::bail!("missing AIS prefill point")
        }

        fn predict_decode(
            &self,
            _batch_size: usize,
            _isl: usize,
            _osl: usize,
        ) -> anyhow::Result<f64> {
            anyhow::bail!("missing AIS decode point")
        }
    }

    #[test]
    fn polynomial_is_only_a_builtin_engine_marker() {
        let model = PerfModel::default();
        assert!(matches!(model, PerfModel::Polynomial));
        assert!(model.predict_prefill_time(1, 128, 0).is_err());
        assert!(model.predict_decode_time(1, 128, 128, 1024).is_err());
    }

    #[test]
    fn ais_forwards_scheduler_local_batch() {
        let model = PerfModel::from_ais_callback(Arc::new(EchoBatchCallback));

        assert_eq!(model.predict_prefill_time(7, 128, 0).unwrap(), 7.0);
        assert_eq!(model.predict_decode_time(9, 0, 128, 0).unwrap(), 9.0);
    }

    #[test]
    fn ais_prefill_prediction_errors_propagate() {
        let error = PerfModel::from_ais_callback(Arc::new(FailingCallback))
            .predict_prefill_time(2, 128, 32)
            .unwrap_err();

        assert_eq!(error.to_string(), "AIS prefill prediction failed");
        assert_eq!(error.root_cause().to_string(), "missing AIS prefill point");
    }

    #[test]
    fn ais_decode_prediction_errors_propagate() {
        let error = PerfModel::from_ais_callback(Arc::new(FailingCallback))
            .predict_decode_time(2, 64, 128, 1024)
            .unwrap_err();

        assert_eq!(error.to_string(), "AIS decode prediction failed");
        assert_eq!(error.root_cause().to_string(), "missing AIS decode point");
    }

    #[test]
    fn npz_profile_loads_numpy_entry_names() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("profile.npz");
        write_npz(
            std::fs::File::create(&path).unwrap(),
            CompressionMethod::Deflated,
            &[
                ("prefill_isl.npy", npy(&array![0.0, 128.0])),
                ("prefill_ttft_ms.npy", npy(&array![0.0, 64.0])),
                ("decode_active_kv_tokens.npy", npy(&array![0.0, 128.0])),
                ("decode_context_length.npy", npy(&array![0.0, 128.0])),
                ("decode_itl.npy", npy(&array![[2.0, 4.0], [6.0, 8.0]])),
            ],
        );

        let model = PerfModel::from_npz(&path).unwrap();
        assert_eq!(model.predict_prefill_time(1, 100, 0).unwrap(), 50.0);
        assert_eq!(model.predict_decode_time(1, 128, 0, 0).unwrap(), 6.0);
        assert_eq!(model.predict_decode_time(1, 0, 128, 0).unwrap(), 4.0);
        assert_eq!(model.predict_decode_time(1, 64, 64, 0).unwrap(), 5.0);
    }

    #[test]
    fn npz_lookup_prefers_the_exact_name_over_the_npy_suffix() {
        let mut bytes = Cursor::new(Vec::new());
        write_npz(
            &mut bytes,
            CompressionMethod::Stored,
            &[
                ("a", npy(&array![1.0])),
                ("a.npy", npy(&array![2.0])),
                ("b.npy", npy(&array![3.0])),
            ],
        );

        let mut npz = NpzReader::new(bytes).unwrap();
        assert_eq!(npz.names().unwrap(), ["a", "a", "b"]);
        assert_eq!(npz.by_name::<Array1<f64>>("a").unwrap(), array![1.0]);
        assert_eq!(npz.by_name::<Array1<f64>>("b").unwrap(), array![3.0]);
        let missing = npz.by_name::<Array1<f64>>("c").unwrap_err();
        assert!(matches!(
            missing.downcast_ref(),
            Some(ZipError::FileNotFound)
        ));
    }

    #[test]
    fn npz_lookup_rejects_a_local_header_with_another_name() {
        let mut bytes = Cursor::new(Vec::new());
        write_npz(
            &mut bytes,
            CompressionMethod::Stored,
            &[("x.npy", npy(&array![1.0]))],
        );
        let mut bytes = bytes.into_inner();
        // Rename the entry in the central directory only, so the two headers disagree.
        let central = bytes.windows(4).position(|s| s == b"PK\x01\x02").unwrap();
        bytes[central + 46..central + 51].copy_from_slice(b"a.npy");

        let mut npz = NpzReader::new(Cursor::new(bytes)).unwrap();
        let error = npz.by_name::<Array1<f64>>("a").unwrap_err();
        assert!(
            matches!(error.downcast_ref(), Some(ZipError::InvalidArchive(_))),
            "{error:#}"
        );
    }
}