Files
rustytorch/crates/integration/rtx-candle/src/lib.rs
T
2026-03-04 00:08:42 +00:00

98 lines
2.7 KiB
Rust

//! Candle ML Framework Integration for `RustyTorch`++
//!
//! This crate provides integration with [Candle](https://github.com/huggingface/candle),
//! `HuggingFace`'s minimal ML framework in Rust, enabling:
//!
//! - Lightweight inference with minimal dependencies
//! - WASM support for browser-based inference
//! - `HuggingFace` model hub integration
//! - Transformer model support (GPT, BERT, `LLaMA`, etc.)
//!
//! ## Features
//!
//! - **cpu**: CPU backend (default)
//! - **cuda**: CUDA backend for NVIDIA GPUs
//! - **metal**: Metal backend for Apple Silicon
//! - **nn**: Neural network layers
//! - **transformers**: Pre-built transformer models
//! - **wasm**: WebAssembly support
//!
//! ## Example
//!
//! ```ignore
//! use rtx_candle::{CandleSession, CandleConfig, CandleBackend};
//! use rtx_tensor::{Tensor, Device};
//!
//! // Create session with Metal backend (macOS)
//! let config = CandleConfig::default().with_backend(CandleBackend::Metal);
//! let session = CandleSession::new(config)?;
//!
//! // Load model from HuggingFace hub
//! let model = session.load_from_hub("bert-base-uncased")?;
//! let output = session.run(&model, &input)?;
//! ```
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::missing_errors_doc)]
pub mod backend;
pub mod error;
pub mod hub;
pub mod model;
pub mod session;
pub mod tensor_bridge;
// Re-export main types
pub use backend::{CandleBackend, CandleDevice, detect_best_backend};
pub use error::{CandleError, Result};
pub use hub::{HubConfig, download_model};
pub use model::{CandleModel, ModelInfo};
pub use session::{CandleConfig, CandleSession, SessionStats};
pub use tensor_bridge::{candle_to_rtx, rtx_to_candle};
/// Version information
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Check if running with CUDA backend available
pub fn is_cuda_available() -> bool {
cfg!(feature = "cuda")
}
/// Check if running with Metal backend available
pub fn is_metal_available() -> bool {
cfg!(feature = "metal") && cfg!(target_os = "macos")
}
/// Check if running in WASM environment
pub fn is_wasm() -> bool {
cfg!(target_arch = "wasm32")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_version() {
assert!(!VERSION.is_empty());
}
#[test]
fn test_backend_detection() {
let backend = detect_best_backend();
assert!(matches!(
backend,
CandleBackend::Cpu | CandleBackend::Cuda | CandleBackend::Metal
));
}
#[test]
fn test_platform_detection() {
// At least one should be true
let _ = is_cuda_available();
let _ = is_metal_available();
let _ = is_wasm();
}
}