aiconfigurator_core/lib.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 core latency API for AIConfigurator.
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. [`AicEngineBuilder`] is the preferred Rust →
10//! Python → Rust embedded build entry point. [`AicEngine`] is the PyO3 hot-path
11//! pyclass.
12
13use std::path::PathBuf;
14
15mod py;
16mod py_ops;
17
18// Modular core. `common/` holds shared foundation types (enums, error,
19// system_spec) with no AIC-domain knowledge. Top-level files (`config`,
20// `session`) and directories (`operators`, `perf_database`) carry the domain
21// logic the compiled `engine` executes.
22mod common;
23mod config;
24pub mod engine;
25mod fpm;
26pub mod memory;
27mod operators;
28mod perf_database;
29mod session;
30
31pub use common::AicError;
32// Forward-pass perf model (PR #1152): a forward-pass latency model with online
33// correction, regression fallback, diagnostics, and readiness, built on the
34// compiled [`engine::Engine`]. Re-exported so Rust embedders (the Dynamo
35// planner / Mocker) can use it natively; also exposed to Python via the
36// `RustForwardPassPerfModel` pyclass in `py.rs`.
37pub use fpm::{
38 ForwardPassPerfDiagnostics, ForwardPassPerfModel, ForwardPassPerfOptions,
39 ForwardPassPerfReadiness, ForwardPassPerfSource,
40};
41// Forward-pass metrics telemetry types and schema version, plus the
42// crate-internal validation helper. Re-exported at the crate root so existing
43// `crate::ForwardPassMetrics` / `crate::FPM_VERSION` references (in `py.rs`,
44// `engine/runtime.rs`) keep resolving after the types moved into `fpm`.
45pub(crate) use fpm::validate_forward_pass_metrics;
46pub use fpm::{ForwardPassMetrics, QueuedRequestMetrics, ScheduledRequestMetrics, FPM_VERSION};
47// KV-cache memory API. Top-level surface, not a method on
48// `AicEngine`: estimation runs once at startup, separate from the latency path.
49pub use memory::{
50 estimate_kv_cache, EstimateSource, KvCacheEstimate, KvCacheEstimateAdjusted,
51 KvCacheEstimateError, KvCacheEstimateOptions, KvCacheEstimateRequest, KvCacheMemoryFraction,
52 MemoryBreakdown,
53};
54// PyO3 bindings. `AicEngine` is the Python -> Rust hot-path pyclass;
55// `AicEngineBuilder` is the Rust -> Python -> Rust entry point. They must be
56// `pub`-re-exported here because the `py` module itself is private.
57pub use py::{AicEngine, AicEngineBuilder};
58// Public wire/identity config types live in `config`. Re-exported at the crate
59// root so existing `crate::EngineConfig` / `crate::BackendKind` / ... paths
60// resolve unchanged across the crate and for external consumers.
61pub use config::{
62 BackendKind, DataType, EngineConfig, ParallelMapping, QuantizationConfig, SpeculativeConfig,
63 ENGINE_CONFIG_SCHEMA_VERSION, ENGINE_SPEC_SCHEMA_VERSION,
64};
65
66/// Resolve a repo-relative path by walking up from the crate manifest dir.
67/// Used by [`py`] (e.g. `crate::repo_relative("src/aiconfigurator_core/systems")`)
68/// to locate the bundled data roots when developing in-tree.
69pub(crate) fn repo_relative(rel: &str) -> Option<PathBuf> {
70 let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
71 for ancestor in manifest_dir.ancestors() {
72 let candidate = ancestor.join(rel);
73 if candidate.exists() {
74 return Some(candidate);
75 }
76 }
77 None
78}