aisimulate_core/perfmodel/mod.rs
1// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Rust-native AIConfigurator performance-model implementation.
5//!
6//! The compiled-engine path is the only supported entry point: Python's
7//! `compile_engine` walks the model once and emits an [`engine::spec::EngineSpec`]
8//! (op lists + [`EngineConfig`] identity); the Rust [`engine::Engine`] executes
9//! it without re-entering Python. With the `python` feature enabled,
10//! [`AicEngineBuilder`] is the preferred Rust → Python → Rust embedded build
11//! entry point and [`AicEngine`] is the PyO3 hot-path pyclass.
12//!
13//! This directory remains a stable mirror of the former AIConfigurator Rust
14//! crate. Keeping the imported implementation behind one namespace makes
15//! upstream syncs mechanical while the crate root remains owned by replay.
16
17use std::path::PathBuf;
18
19#[cfg(feature = "python")]
20pub(crate) mod py;
21#[cfg(feature = "python")]
22pub(crate) mod py_ops;
23
24// Modular core. `common/` holds shared foundation types (enums, error,
25// system_spec) with no AIC-domain knowledge. Top-level files (`config`,
26// `session`) and directories (`operators`, `perf_database`) carry the domain
27// logic the compiled `engine` executes.
28pub(crate) mod common;
29pub(crate) mod config;
30pub mod engine;
31pub(crate) mod fpm;
32pub mod memory;
33pub(crate) mod operators;
34pub(crate) mod perf_database;
35pub(crate) mod session;
36
37pub use common::AicError;
38// Forward-pass perf model (PR #1152): a forward-pass latency model with online
39// correction, regression fallback, diagnostics, and readiness, built on the
40// compiled [`engine::Engine`]. Re-exported so Rust embedders (the Dynamo
41// planner / Mocker) can use it natively; also exposed to Python via the
42// `RustForwardPassPerfModel` pyclass in `py.rs`.
43pub use fpm::{
44 ForwardPassPerfDiagnostics, ForwardPassPerfModel, ForwardPassPerfOptions,
45 ForwardPassPerfReadiness, ForwardPassPerfSource,
46};
47// Forward-pass metrics telemetry types and schema version, plus the
48// crate-internal validation helper. Re-exported at the crate root so existing
49// `crate::ForwardPassMetrics` / `crate::FPM_VERSION` references (in `py.rs`,
50// `engine/runtime.rs`) keep resolving after the types moved into `fpm`.
51pub(crate) use fpm::validate_forward_pass_metrics;
52pub use fpm::{FPM_VERSION, ForwardPassMetrics, QueuedRequestMetrics, ScheduledRequestMetrics};
53// KV-cache memory API. Top-level surface, not a method on
54// `AicEngine`: estimation runs once at startup, separate from the latency path.
55#[cfg(feature = "python")]
56pub use memory::estimate_kv_cache;
57pub use memory::{
58 EstimateSource, KvCacheEstimate, KvCacheEstimateAdjusted, KvCacheEstimateError,
59 KvCacheEstimateOptions, KvCacheEstimateRequest, KvCacheMemoryFraction, MemoryBreakdown,
60};
61// PyO3 bindings. `AicEngine` is the Python -> Rust hot-path pyclass;
62// `AicEngineBuilder` is the Rust -> Python -> Rust entry point. They must be
63// `pub`-re-exported here because the `py` module itself is private.
64#[cfg(feature = "python")]
65pub use py::{AicEngine, AicEngineBuilder};
66// Public wire/identity config types live in `config`. Re-exported at the crate
67// root so existing `crate::EngineConfig` / `crate::BackendKind` / ... paths
68// resolve unchanged across the crate and for external consumers.
69pub use config::{
70 BackendKind, DataType, ENGINE_CONFIG_SCHEMA_VERSION, ENGINE_SPEC_SCHEMA_VERSION, EngineConfig,
71 ParallelMapping, QuantizationConfig, SpeculativeConfig,
72};
73
74/// Resolve a repo-relative path by walking up from the crate manifest dir.
75/// Used by [`py`] to locate the bundled data roots when developing in-tree.
76pub(crate) fn repo_relative(rel: &str) -> Option<PathBuf> {
77 let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
78 for ancestor in manifest_dir.ancestors() {
79 let candidate = ancestor.join(rel);
80 if candidate.exists() {
81 return Some(candidate);
82 }
83 }
84 None
85}
86
87/// Register the compatibility AIConfigurator API on the unified native
88/// `aisimulate._runtime` extension module.
89#[cfg(feature = "python")]
90pub fn register_python(module: &pyo3::Bound<'_, pyo3::types::PyModule>) -> pyo3::PyResult<()> {
91 py::register(module)
92}