rust_ai_core/lib.rs
1// SPDX-License-Identifier: MIT
2// Copyright 2026 Tyler Zervas
3
4//! # rust-ai-core
5//!
6//! Unified AI engineering toolkit that orchestrates the complete rust-ai ecosystem.
7//!
8//! This crate integrates 8 specialized AI/ML crates into a cohesive toolkit:
9//!
10//! | Crate | Purpose |
11//! |-------|---------|
12//! | **peft-rs** | LoRA, DoRA, AdaLoRA adapters for parameter-efficient fine-tuning |
13//! | **qlora-rs** | 4-bit quantized LoRA for memory-efficient fine-tuning |
14//! | **unsloth-rs** | Optimized transformer blocks (attention, FFN, normalization) |
15//! | **axolotl-rs** | YAML-driven training orchestration and configuration |
16//! | **bitnet-quantize** | Microsoft BitNet b1.58 ternary quantization |
17//! | **trit-vsa** | Balanced ternary arithmetic and VSA operations |
18//! | **vsa-optim-rs** | VSA-based deterministic training optimization |
19//! | **tritter-accel** | GPU-accelerated ternary operations |
20//!
21//! ## Quick Start
22//!
23//! ### Using the Unified API
24//!
25//! ```rust,ignore
26//! use rust_ai_core::{RustAI, RustAIConfig};
27//!
28//! // Initialize the unified API
29//! let ai = RustAI::new(RustAIConfig::default())?;
30//!
31//! // Configure fine-tuning with LoRA
32//! let finetune_config = ai.finetune()
33//! .model("meta-llama/Llama-2-7b")
34//! .rank(64)
35//! .alpha(16.0)
36//! .build()?;
37//!
38//! // Configure quantization
39//! let quant_config = ai.quantize()
40//! .method(QuantizeMethod::Nf4)
41//! .bits(4)
42//! .build();
43//!
44//! // Configure VSA operations
45//! let vsa_config = ai.vsa()
46//! .dimension(10000)
47//! .build();
48//! ```
49//!
50//! ### Direct Crate Access
51//!
52//! ```rust,ignore
53//! use rust_ai_core::ecosystem::peft::{LoraConfig, LoraLinear};
54//! use rust_ai_core::ecosystem::qlora::QLoraConfig;
55//! use rust_ai_core::ecosystem::bitnet::TernaryLinear;
56//! use rust_ai_core::ecosystem::trit::TritVector;
57//! ```
58//!
59//! ### Using Foundation Types
60//!
61//! ```rust
62//! use rust_ai_core::{get_device, DeviceConfig, CoreError, Result};
63//!
64//! fn main() -> Result<()> {
65//! // Get CUDA device with automatic fallback + warning
66//! let device = get_device(&DeviceConfig::default())?;
67//!
68//! // Or configure from environment variables
69//! let config = DeviceConfig::from_env();
70//! let device = get_device(&config)?;
71//!
72//! Ok(())
73//! }
74//! ```
75//!
76//! ## Design Philosophy
77//!
78//! **CUDA-first**: All operations prefer GPU execution. CPU is a fallback that emits
79//! warnings, not a silent alternative. Users are immediately aware when they're not
80//! getting optimal performance.
81//!
82//! **Zero-cost abstractions**: Traits compile to static dispatch with no vtable overhead.
83//! The abstraction layer adds no runtime cost compared to direct implementations.
84//!
85//! **Fail-fast validation**: Configuration errors are caught at construction time, not
86//! deep in a training loop. This saves users from wasted compute time.
87//!
88//! **Unified API**: The [`RustAI`] facade provides a single entry point for all AI
89//! engineering tasks, automatically composing the right ecosystem crates.
90//!
91//! ## Modules
92//!
93//! ### Foundation Modules
94//!
95//! - [`device`] - CUDA-first device selection with environment variable overrides
96//! - [`error`] - Unified error types across all rust-ai crates
97//! - [`traits`] - Common traits for configs, quantization, and GPU dispatch
98//! - [`memory`] - Memory estimation and tracking utilities
99//! - [`dtype`] - Data type utilities and precision helpers
100//! - [`logging`] - Unified logging and observability
101//! - [`cubecl`] - CubeCL ↔ Candle tensor interoperability (requires `cuda` feature)
102//!
103//! ### Ecosystem Modules
104//!
105//! - [`ecosystem`] - Re-exports from all 8 rust-ai ecosystem crates
106//! - [`facade`] - High-level unified API ([`RustAI`])
107//!
108//! ## Feature Flags
109//!
110//! | Feature | Description | Dependencies |
111//! |---------|-------------|--------------|
112//! | `cuda` | Enable CUDA support via Candle and CubeCL | cubecl, cubecl-cuda |
113//! | `python` | Enable Python bindings via PyO3 | pyo3, numpy |
114//! | `napi` | Enable Node.js bindings via napi-rs | napi, napi-derive |
115//! | `wasm` | Enable WebAssembly bindings via wasm-bindgen | wasm-bindgen, js-sys |
116//! | `full` | Enable CUDA and Python | cuda, python |
117//! | `full-bindings` | Enable all bindings | cuda, python, napi, wasm |
118//!
119//! ## Environment Variables
120//!
121//! | Variable | Description | Example |
122//! |----------|-------------|---------|
123//! | `RUST_AI_FORCE_CPU` | Force CPU execution | `1` or `true` |
124//! | `RUST_AI_CUDA_DEVICE` | Select CUDA device ordinal | `0`, `1` |
125//! | `RUST_LOG` | Control logging level | `rust_ai_core=debug` |
126
127#![warn(missing_docs)]
128#![warn(clippy::all)]
129#![warn(clippy::pedantic)]
130#![allow(clippy::module_name_repetitions)]
131// Allow doc_markdown as technical terms (LoRA, DoRA, etc.) shouldn't need backticks
132#![allow(clippy::doc_markdown)]
133
134// =============================================================================
135// FOUNDATION MODULES
136// =============================================================================
137
138pub mod device;
139pub mod dtype;
140pub mod error;
141pub mod logging;
142pub mod memory;
143pub mod traits;
144
145#[cfg(feature = "cuda")]
146pub mod cubecl;
147
148#[cfg(feature = "python")]
149pub mod python;
150
151#[cfg(any(feature = "napi", feature = "wasm"))]
152pub mod typescript;
153
154// =============================================================================
155// ECOSYSTEM INTEGRATION MODULES
156// =============================================================================
157
158/// Unified re-exports from all rust-ai ecosystem crates.
159///
160/// Access individual crates through their submodules:
161/// - `ecosystem::peft` - LoRA, DoRA, AdaLoRA
162/// - `ecosystem::qlora` - 4-bit quantized LoRA
163/// - `ecosystem::unsloth` - Optimized transformers
164/// - `ecosystem::axolotl` - Training orchestration
165/// - `ecosystem::bitnet` - 1.58-bit quantization
166/// - `ecosystem::trit` - Ternary VSA
167/// - `ecosystem::vsa_optim` - VSA optimization
168/// - `ecosystem::tritter` - Ternary acceleration
169pub mod ecosystem;
170
171/// High-level unified API facade.
172///
173/// The [`RustAI`] struct provides a simplified interface for common AI
174/// engineering tasks, orchestrating multiple ecosystem crates automatically.
175pub mod facade;
176
177// =============================================================================
178// RE-EXPORTS
179// =============================================================================
180
181// Foundation re-exports for convenience.
182pub use device::{get_device, warn_if_cpu, DeviceConfig};
183pub use dtype::{bytes_per_element, is_floating_point, DTypeExt};
184pub use error::{CoreError, Result};
185pub use logging::{init_logging, LogConfig};
186pub use memory::{estimate_tensor_bytes, MemoryTracker};
187pub use traits::{Dequantize, GpuDispatchable, Quantize, ValidatableConfig};
188
189// CubeCL interop (CUDA only)
190#[cfg(feature = "cuda")]
191pub use cubecl::{
192 allocate_output_buffer, candle_to_cubecl_handle, cubecl_to_candle_tensor,
193 has_cubecl_cuda_support, TensorBuffer,
194};
195
196// Unified API facade
197pub use facade::{
198 AdapterType, FinetuneBuilder, FinetuneConfig, QuantizeBuilder, QuantizeConfig, QuantizeMethod,
199 RustAI, RustAIConfig, RustAIInfo, TrainBuilder, TrainConfig, VsaBuilder, VsaConfig,
200};
201
202// Ecosystem information
203pub use ecosystem::EcosystemInfo;
204
205/// Crate version for runtime version checking.
206///
207/// Why: Dependent crates may need to verify compatibility at runtime,
208/// especially when loading serialized data that includes version info.
209pub const VERSION: &str = env!("CARGO_PKG_VERSION");