ferrotensor/lib.rs
1//! # Ferrotensor - GPU-Accelerated Machine Learning Library
2//!
3//! A PyTorch-style tensor and neural-network API in pure Rust. Tensors live
4//! on a [`Device`]: a GPU through wgpu (Metal, Vulkan, DX12) with hand-written
5//! WGSL kernels, or the CPU, which needs no GPU and runs the same ops.
6//!
7//! ## Features
8//!
9//! - **Tensors**: broadcasting element-wise math, reductions over any dim,
10//! batched matmul, softmax/log-softmax, permute, slicing, concatenation,
11//! gathers.
12//! - **Automatic differentiation**: every op records its backward pass.
13//! Op gradients are tested against finite differences on the CPU and
14//! against the CPU on the GPU.
15//! - **`nn`**: Linear, Embedding, LayerNorm, RMSNorm, Dropout, multi-head
16//! (causal) attention, activations, Sequential.
17//! - **`optim`**: SGD (momentum, Nesterov), AdamW, gradient clipping,
18//! warmup + cosine and other LR schedules.
19//! - **`serialize`**: safetensors save/load and module state dicts.
20//!
21//! ## Quick Start
22//!
23//! ```rust
24//! use ferrotensor::prelude::*;
25//!
26//! let device = Device::cpu(); // or Device::best() for a GPU when present
27//! let model = nn::Sequential::new(&device)
28//! .add(nn::Linear::new(2, 16, &device))
29//! .add(nn::Tanh)
30//! .add(nn::Linear::new(16, 2, &device));
31//! let mut opt = optim::AdamW::new(model.parameters(), 0.05).weight_decay(0.0);
32//!
33//! // XOR
34//! let x = Tensor::from_data(&[0., 0., 0., 1., 1., 0., 1., 1.], &[4, 2], &device);
35//! let y = Tensor::from_data(&[0., 1., 1., 0.], &[4], &device);
36//! for _ in 0..200 {
37//! opt.zero_grad();
38//! let loss = F::cross_entropy(&model.forward(&x), &y);
39//! loss.backward();
40//! opt.step();
41//! }
42//! ```
43//!
44//! The LLM fine-tuning stack (LoRA, LFM2/Llama blocks, the CLI) is built on
45//! `ferrotensor-backend`, `ferrotensor-autograd` and `ferrotensor-graph`.
46
47pub mod backend;
48pub mod data;
49pub mod functional;
50pub mod nn;
51pub mod optim;
52pub mod random;
53pub mod serialize;
54pub mod tensor;
55
56mod device;
57
58pub use device::Device;
59pub use random::manual_seed;
60pub use tensor::{no_grad, Tensor};
61
62/// Prelude module - import everything you need with `use ferrotensor::prelude::*`
63pub mod prelude {
64 pub use crate::data::{DataLoader, Dataset};
65 pub use crate::functional as F;
66 pub use crate::manual_seed;
67 pub use crate::nn;
68 pub use crate::nn::Module;
69 pub use crate::optim;
70 pub use crate::optim::Optimizer;
71 pub use crate::tensor::Tensor;
72 pub use crate::Device;
73}