aisimulate-core 0.1.0-dev.2

Engine-neutral inference simulation, deterministic replay, and performance modeling
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
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! KV-cache memory estimation.
//!
//! [`estimate_kv_cache`] is a top-level crate function (NOT a method on
//! `AicEngine`): estimation runs once at startup, uses overlapping but not
//! identical inputs to `AicEngineBuilder`, and is a separate concern from
//! latency prediction. The Dynamo Mocker is the primary external consumer; it
//! calls this once and derives `num_gpu_blocks_per_rank` from
//! `total_kv_size_tokens`.
//!
//! ## Rust is a pure forwarder; the estimate is computed in Python
//!
//! This mirrors how `AicEngineBuilder` forwards to Python's `compile_engine`:
//! ALL of the work -- fraction + tolerance validation, HF-config parsing, the
//! AIC backend memory model, the OfFree/OfTotal budget math, the naive heuristic
//! fallback, AND the tolerance margin -- lives in
//! `aiconfigurator.sdk.memory.estimate_kv_cache`. The Rust side:
//!
//! 1. crosses into Python once (`with_gil → import → call estimate_kv_cache →
//!    extract dict`), forwarding `tolerance_fraction` through;
//! 2. rebuilds a [`KvCacheEstimate`] from that dict (including
//!    `tolerance_adjusted`), with no math of its own.
//!
//! The two budget formulas (TRT-LLM free-fraction vs vLLM/SGLang total-fraction),
//! the naive fallback, and the tolerance margin all live on the Python side; see
//! the docstring of `aiconfigurator.sdk.memory.estimate_kv_cache`. The
//! [`KvCacheMemoryFraction`] enum still encodes the backend↔fraction XOR so the
//! request shape is unambiguous; the variant is validated against
//! `engine.backend` in Python.

use serde::{Deserialize, Serialize};

use crate::BackendKind;
use crate::perfmodel::EngineConfig;

/// KV-cache memory request. `engine` reuses the modularised [`EngineConfig`];
/// the remaining fields describe the runtime sizing budget.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct KvCacheEstimateRequest {
    pub engine: EngineConfig,
    pub max_num_tokens: u32,
    pub max_batch_size: u32,
    pub kv_cache_memory_fraction: KvCacheMemoryFraction,
    /// Override for unknown SKUs; when `Some`, it wins over the SystemSpec
    /// capacity reported by the native path.
    pub gpu_memory_capacity_bytes_override: Option<u64>,
    /// `None` = raw estimate only; `Some(0.05)` = 5% safety margin.
    pub tolerance_fraction: Option<f64>,
    pub options: KvCacheEstimateOptions,
}

/// Backend-tagged memory fraction. The variant encodes the XOR between
/// TRT-LLM's free-fraction and vLLM/SGLang's total-fraction semantics; the
/// Python `estimate_kv_cache` validates it against `engine.backend` and returns
/// an error (mapped to [`KvCacheEstimateError::IncompatibleMemoryFraction`]) if
/// mismatched.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub enum KvCacheMemoryFraction {
    /// Fraction of TOTAL GPU memory. Compatible with vLLM
    /// (`gpu_memory_utilization`) and SGLang (`mem_fraction_static`).
    OfTotal(f64),
    /// Fraction of FREE (post-non-KV) GPU memory. Compatible with TRT-LLM
    /// (`free_gpu_memory_fraction`).
    OfFree(f64),
}

impl KvCacheMemoryFraction {
    /// `(kind, value)` pair for the flat Python call. `kind` is the wire string
    /// the Python `estimate_kv_cache` expects (`"of_total"` / `"of_free"`).
    fn to_wire(self) -> (&'static str, f64) {
        match self {
            Self::OfTotal(f) => ("of_total", f),
            Self::OfFree(f) => ("of_free", f),
        }
    }
}

/// Default for [`KvCacheEstimateOptions::naive_kv_reservation`] so a JSON request
/// from an embedded caller (the Mocker) that omits this newer field still
/// deserializes (to the same 0.80 the Python side defaults to).
fn default_naive_kv_reservation() -> f64 {
    0.80
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub struct KvCacheEstimateOptions {
    pub allow_naive_fallback: bool,
    pub allow_hf_config_download: bool,
    /// Fraction of post-weight memory the naive fallback reserves for KV
    /// (default `0.80`). Ignored on the native path. Exposed so the Mocker can
    /// tune the crude fallback budget.
    #[serde(default = "default_naive_kv_reservation")]
    pub naive_kv_reservation: f64,
}

/// KV-cache memory estimate.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct KvCacheEstimate {
    pub total_gpu_capacity_bytes: u64,
    pub total_kv_size_bytes: u64,
    pub kv_size_per_token_bytes: u64,
    pub total_kv_size_tokens: u64,
    pub source: EstimateSource,
    /// `Some` on the native path; `None` on the naive fallback.
    pub memory_breakdown: Option<MemoryBreakdown>,
    /// `Some` iff `tolerance_fraction` set; `None` for the raw estimate.
    pub tolerance_adjusted: Option<KvCacheEstimateAdjusted>,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum EstimateSource {
    /// AIC's full backend memory model used.
    Native,
    /// Post-weight reservation heuristic (`naive_kv_reservation`, default 80%).
    NaiveFallback,
}

/// Non-KV memory components, in bytes. Maps AIC's `_get_memory_usage` dict:
/// `weights → weights`, `activations → activations`, `others →
/// runtime_overhead`, `nccl → comm_overhead`.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct MemoryBreakdown {
    pub weights_bytes: u64,
    pub activations_bytes: u64,
    pub runtime_overhead_bytes: u64,
    pub comm_overhead_bytes: u64,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub struct KvCacheEstimateAdjusted {
    pub tolerance_fraction: f64,
    pub total_kv_size_bytes: u64,
    pub total_kv_size_tokens: u64,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum KvCacheEstimateError {
    Unsupported {
        model: String,
        backend: BackendKind,
        gpu_sku: String,
        reason: String,
    },
    InsufficientModelMetadata {
        missing_fields: Vec<String>,
    },
    NoKvBudget {
        total_gpu_capacity_bytes: u64,
        non_kv_bytes: u64,
    },
    IncompatibleMemoryFraction {
        backend: BackendKind,
        variant_kind: &'static str,
    },
    BadConfig {
        field: String,
        reason: String,
    },
    HfConfigFetchFailed {
        hf_id: String,
        source: String,
    },
}

impl std::fmt::Display for KvCacheEstimateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unsupported {
                model,
                backend,
                gpu_sku,
                reason,
            } => write!(
                f,
                "unsupported model/backend/GPU for KV-cache estimation: model={model}, \
                 backend={backend:?}, gpu_sku={gpu_sku}: {reason}"
            ),
            Self::InsufficientModelMetadata { missing_fields } => {
                write!(
                    f,
                    "insufficient model metadata; missing: {missing_fields:?}"
                )
            }
            Self::NoKvBudget {
                total_gpu_capacity_bytes,
                non_kv_bytes,
            } => write!(
                f,
                "no KV budget: non-KV memory ({non_kv_bytes} bytes) meets/exceeds the \
                 KV-cache memory limit (capacity={total_gpu_capacity_bytes} bytes)"
            ),
            Self::IncompatibleMemoryFraction {
                backend,
                variant_kind,
            } => write!(
                f,
                "incompatible memory fraction: backend {backend:?} does not accept \
                 KvCacheMemoryFraction::{variant_kind}"
            ),
            Self::BadConfig { field, reason } => {
                write!(f, "bad memory config field {field:?}: {reason}")
            }
            Self::HfConfigFetchFailed { hf_id, source } => {
                write!(f, "HF config fetch failed for {hf_id:?}: {source}")
            }
        }
    }
}

impl std::error::Error for KvCacheEstimateError {}

/// Estimate KV-cache memory (raw estimate + optional tolerance margin).
///
/// Pure forwarder: crosses into Python once to compute the COMPLETE estimate. The
/// Python `aiconfigurator.sdk.memory.estimate_kv_cache` does the backend↔fraction
/// and tolerance validation, the native AIC memory breakdown + budget math, the
/// naive heuristic fallback, AND the tolerance margin (`tolerance_adjusted`); the
/// Rust side rebuilds a [`KvCacheEstimate`] from the returned dict with no math
/// of its own.
///
/// Errors from the Python side (unsupported model/backend, incompatible memory
/// fraction, out-of-range tolerance, no KV budget, HF config fetch failure) cross
/// the PyO3 boundary as a `ValueError` whose message carries the failure detail
/// and are surfaced here as [`KvCacheEstimateError::Unsupported`] with that
/// message.
#[cfg(feature = "python")]
pub fn estimate_kv_cache(
    req: KvCacheEstimateRequest,
) -> Result<KvCacheEstimate, KvCacheEstimateError> {
    fetch_python_estimate(&req)
}

/// Cross into Python once to compute the complete estimate.
///
/// Mirrors the `AicEngineBuilder` → `compile_engine` crossing: `with_gil →
/// import aiconfigurator.sdk.memory → call estimate_kv_cache(...) → extract
/// the returned dict`. `tolerance_fraction` is forwarded; the Python fn applies
/// the tolerance and returns `tolerance_adjusted` in the dict.
#[cfg(feature = "python")]
fn fetch_python_estimate(
    req: &KvCacheEstimateRequest,
) -> Result<KvCacheEstimate, KvCacheEstimateError> {
    use pyo3::prelude::*;
    use pyo3::types::PyDict;

    let engine = &req.engine;
    let parallel = &engine.parallel;
    let quant = &engine.quantization;
    let nextn = engine
        .speculative
        .as_ref()
        .and_then(|s| s.nextn)
        .unwrap_or(0);
    let (fraction_kind, fraction_value) = req.kv_cache_memory_fraction.to_wire();

    Python::with_gil(|py| -> PyResult<KvCacheEstimate> {
        let engine_mod = py.import("aiconfigurator.sdk.memory")?;
        let kwargs = PyDict::new(py);
        kwargs.set_item("backend_version", engine.backend_version.as_deref())?;
        kwargs.set_item("max_num_tokens", req.max_num_tokens)?;
        kwargs.set_item("max_batch_size", req.max_batch_size)?;
        kwargs.set_item("memory_fraction_kind", fraction_kind)?;
        kwargs.set_item("memory_fraction_value", fraction_value)?;
        kwargs.set_item("tp_size", parallel.tp_size)?;
        kwargs.set_item("pp_size", parallel.pp_size)?;
        kwargs.set_item("attention_dp_size", parallel.attention_dp_size.unwrap_or(1))?;
        kwargs.set_item("moe_tp_size", parallel.moe_tp_size)?;
        kwargs.set_item("moe_ep_size", parallel.moe_ep_size)?;
        kwargs.set_item(
            "gemm_quant_mode",
            quant.weight_dtype.as_ref().map(dtype_str),
        )?;
        kwargs.set_item("moe_quant_mode", quant.moe_dtype.as_ref().map(dtype_str))?;
        kwargs.set_item(
            "kvcache_quant_mode",
            quant.kv_cache_dtype.as_ref().map(dtype_str),
        )?;
        kwargs.set_item(
            "fmha_quant_mode",
            quant.activation_dtype.as_ref().map(dtype_str),
        )?;
        // `comm_quant_mode` is intentionally NOT forwarded: the comm/NCCL
        // overhead comes from `system_spec` (`nccl_mem` / `other_mem`), not the
        // comm quant mode, so it does not affect the non-KV breakdown.
        kwargs.set_item("nextn", nextn)?;
        kwargs.set_item(
            "systems_path",
            engine.systems_path.as_deref().and_then(|p| p.to_str()),
        )?;
        kwargs.set_item(
            "gpu_memory_capacity_bytes_override",
            req.gpu_memory_capacity_bytes_override,
        )?;
        kwargs.set_item("tolerance_fraction", req.tolerance_fraction)?;
        kwargs.set_item("naive_kv_reservation", req.options.naive_kv_reservation)?;
        kwargs.set_item("allow_naive_fallback", req.options.allow_naive_fallback)?;
        kwargs.set_item(
            "allow_hf_config_download",
            req.options.allow_hf_config_download,
        )?;

        let out = engine_mod.call_method(
            "estimate_kv_cache",
            (
                engine.model_name.as_str(),
                engine.system_name.as_str(),
                engine.backend.as_str(),
            ),
            Some(&kwargs),
        )?;

        estimate_from_dict(&out)
    })
    // PyErr → KvCacheEstimateError inline (keeps the error enum self-contained).
    // The Python side raises a ValueError whose message already carries the
    // specific failure detail; surface it as Unsupported so the (deferred)
    // Mocker fallback decision still keys on the same variant it did before.
    .map_err(|e| KvCacheEstimateError::Unsupported {
        model: engine.model_name.clone(),
        backend: engine.backend.clone(),
        gpu_sku: engine.system_name.clone(),
        reason: format!("estimate_kv_cache: {e}"),
    })
}

/// Rebuild a [`KvCacheEstimate`] from the Python `estimate_kv_cache` dict.
/// The dict carries every struct field (`total_*`, `kv_size_per_token_bytes`,
/// `source`, `memory_breakdown`, and `tolerance_adjusted`); the Python fn applies
/// the tolerance, so `tolerance_adjusted` is a nested dict iff
/// `tolerance_fraction` was set, `None` otherwise.
#[cfg(feature = "python")]
fn estimate_from_dict(
    out: &pyo3::Bound<'_, pyo3::types::PyAny>,
) -> pyo3::PyResult<KvCacheEstimate> {
    use pyo3::exceptions::PyValueError;
    use pyo3::types::PyAnyMethods;

    let u64_at = |k: &str| -> pyo3::PyResult<u64> { out.get_item(k)?.extract::<u64>() };

    let source = match out.get_item("source")?.extract::<String>()?.as_str() {
        "native" => EstimateSource::Native,
        "naive_fallback" => EstimateSource::NaiveFallback,
        other => {
            return Err(PyValueError::new_err(format!(
                "estimate_kv_cache returned unknown source {other:?}"
            )));
        }
    };

    let breakdown_item = out.get_item("memory_breakdown")?;
    let memory_breakdown = if breakdown_item.is_none() {
        None
    } else {
        let get = |k: &str| -> pyo3::PyResult<u64> { breakdown_item.get_item(k)?.extract::<u64>() };
        Some(MemoryBreakdown {
            weights_bytes: get("weights_bytes")?,
            activations_bytes: get("activations_bytes")?,
            runtime_overhead_bytes: get("runtime_overhead_bytes")?,
            comm_overhead_bytes: get("comm_overhead_bytes")?,
        })
    };

    // The Python fn applies the tolerance, so `tolerance_adjusted` is a nested
    // dict iff `tolerance_fraction` was set (keys mirror the Python emit side:
    // `tolerance_fraction` / `total_kv_size_bytes` / `total_kv_size_tokens`).
    let adjusted_item = out.get_item("tolerance_adjusted")?;
    let tolerance_adjusted = if adjusted_item.is_none() {
        None
    } else {
        Some(KvCacheEstimateAdjusted {
            tolerance_fraction: adjusted_item
                .get_item("tolerance_fraction")?
                .extract::<f64>()?,
            total_kv_size_bytes: adjusted_item
                .get_item("total_kv_size_bytes")?
                .extract::<u64>()?,
            total_kv_size_tokens: adjusted_item
                .get_item("total_kv_size_tokens")?
                .extract::<u64>()?,
        })
    };

    Ok(KvCacheEstimate {
        total_gpu_capacity_bytes: u64_at("total_gpu_capacity_bytes")?,
        total_kv_size_bytes: u64_at("total_kv_size_bytes")?,
        kv_size_per_token_bytes: u64_at("kv_size_per_token_bytes")?,
        total_kv_size_tokens: u64_at("total_kv_size_tokens")?,
        source,
        memory_breakdown,
        tolerance_adjusted,
    })
}

/// Map a [`crate::DataType`] to the snake_case quant-mode string the Python
/// `_build_model_config` accepts (the serde `rename` already produces these).
#[cfg(feature = "python")]
fn dtype_str(dt: &crate::DataType) -> &'static str {
    use crate::DataType::*;
    match dt {
        Bfloat16 => "bfloat16",
        Float16 => "float16",
        Fp8 => "fp8",
        Fp8Static => "fp8_static",
        Fp8Block => "fp8_block",
        Nvfp4 => "nvfp4",
        Int8 => "int8",
        Int4 => "int4",
        W4afp8 => "w4afp8",
        W4a16Mxfp4 => "w4a16_mxfp4",
        W4a8Mxfp4Mxfp8 => "w4a8_mxfp4_mxfp8",
        W4a8Mxfp4Mxfp8Trtllm => "w4a8_mxfp4_mxfp8_trtllm",
        W4a16Mxfp4Cutlass => "w4a16_mxfp4_cutlass",
        W4a16Nvfp4 => "w4a16_nvfp4",
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Tolerance validation + application and the native/naive budget math now
    // live entirely in Python (`aiconfigurator.sdk.memory.estimate_kv_cache`),
    // exercised by `tests/unit/sdk/test_memory_estimation.py` and the integration
    // parity test. The Rust side is a pure forwarder; the only pure-Rust unit
    // left here is the memory-fraction wire mapping. The dict round-trip
    // (`fetch_python_estimate` → `estimate_from_dict`, including the
    // `tolerance_adjusted` parse) is covered end-to-end by the Mocker consumer.

    /// The memory fraction must cross to Python as the wire `(kind, value)` pair
    /// the Python `estimate_kv_cache` expects.
    #[test]
    fn memory_fraction_to_wire() {
        assert_eq!(
            KvCacheMemoryFraction::OfFree(0.9).to_wire(),
            ("of_free", 0.9)
        );
        assert_eq!(
            KvCacheMemoryFraction::OfTotal(0.85).to_wire(),
            ("of_total", 0.85)
        );
    }
}