Skip to main content

hanzo_ml/
lib.rs

1//! ML framework for Rust
2//!
3//! ```rust
4//! use hanzo_ml::{Tensor, DType, Device};
5//! # use hanzo_ml::Error;
6//! # fn main() -> Result<(), Error>{
7//!
8//! let a = Tensor::arange(0f32, 6f32, &Device::Cpu)?.reshape((2, 3))?;
9//! let b = Tensor::arange(0f32, 12f32, &Device::Cpu)?.reshape((3, 4))?;
10//! let c = a.matmul(&b)?;
11//!
12//! # Ok(())}
13//! ```
14//!
15//! ## Features
16//!
17//! - Simple syntax (looks and feels like PyTorch)
18//! - CPU and Cuda backends (and M1 support)
19//! - Enable serverless (CPU) small and fast deployments
20//! - Model training
21//! - Distributed computing (NCCL).
22//! - Models out of the box (Llama, Whisper, Falcon, ...)
23//!
24//! ## FAQ
25//!
26//! - Why Hanzo?
27//!
28//! Hanzo stems from the need to reduce binary size in order to *enable serverless*
29//! possible by making the whole engine smaller than PyTorch very large library volume
30//!
31//! And simply *removing Python* from production workloads.
32//! Python can really add overhead in more complex workflows and the [GIL](https://www.backblaze.com/blog/the-python-gil-past-present-and-future/) is a notorious source of headaches.
33//!
34//! Rust is cool, and a lot of the HF ecosystem already has Rust crates [safetensors](https://github.com/huggingface/safetensors) and [tokenizers](https://github.com/huggingface/tokenizers)
35//!
36//! ## Other Crates
37//!
38//! Hanzo consists of a number of crates. This crate holds core the common data structures but you may wish
39//! to look at the docs for the other crates which can be found here:
40//!
41//! - [hanzo-ml](https://docs.rs/hanzo-ml/). Core Datastructures and DataTypes.
42//! - [hanzo-nn](https://docs.rs/hanzo-nn/). Building blocks for Neural Nets.
43//! - [hanzo-datasets](https://docs.rs/hanzo-datasets/). Rust access to commonly used Datasets like MNIST.
44//! - [hanzo-ml-examples](https://docs.rs/hanzo-ml-examples/). Examples of Hanzo in Use.
45//! - [hanzo-onnx](https://docs.rs/hanzo-onnx/). Loading and using ONNX models.
46//! - [hanzo-ml-pyo3](https://docs.rs/hanzo-ml-pyo3/). Access to Hanzo from Python.
47//! - [hanzo-transformers](https://docs.rs/hanzo-transformers/). Hanzo implementation of many published transformer models.
48//!
49
50#[cfg(feature = "accelerate")]
51mod accelerate;
52pub mod backend;
53pub mod backprop;
54pub mod conv;
55mod convert;
56pub mod cpu;
57pub mod cpu_backend;
58#[cfg(feature = "cuda")]
59pub mod cuda_backend;
60mod custom_op;
61mod device;
62pub mod display;
63mod dtype;
64pub mod dummy_cuda_backend;
65pub mod dummy_dtype;
66mod dummy_metal_backend;
67pub mod dummy_vulkan_backend;
68pub mod error;
69mod indexer;
70pub mod layout;
71#[cfg(feature = "metal")]
72pub mod metal_backend;
73#[cfg(feature = "mkl")]
74mod mkl;
75pub mod model_delta;
76pub mod npy;
77pub mod op;
78pub mod pickle;
79pub mod quantized;
80#[cfg(feature = "rocm")]
81pub mod rocm_backend;
82pub mod safetensors;
83pub mod scalar;
84pub mod shape;
85mod sort;
86mod storage;
87pub mod streaming;
88mod strided_index;
89mod tensor;
90mod tensor_cat;
91pub mod test_utils;
92pub mod utils;
93mod variable;
94#[cfg(feature = "vulkan")]
95pub mod vulkan_backend;
96
97#[cfg(feature = "cudnn")]
98pub use cuda_backend::cudnn;
99
100pub use cpu_backend::{CpuStorage, CpuStorageRef};
101#[cfg(feature = "ug")]
102pub use custom_op::UgIOp1;
103pub use custom_op::{CustomOp1, CustomOp2, CustomOp3, InplaceOp1, InplaceOp2, InplaceOp3};
104pub use device::{Device, DeviceLocation, NdArray};
105pub use dtype::{DType, DTypeParseError, FloatDType, IntDType, WithDType};
106pub use dummy_dtype::{F4, F6E2M3, F6E3M2, F8E8M0};
107pub use error::{Context, Error, Result};
108pub use indexer::{IndexOp, TensorIndexer};
109pub use layout::Layout;
110pub use shape::{Shape, D};
111pub use storage::Storage;
112pub use streaming::{StreamTensor, StreamingBinOp, StreamingModule};
113pub use strided_index::{StridedBlocks, StridedIndex};
114pub use tensor::{Tensor, TensorId};
115pub use variable::Var;
116
117#[cfg(feature = "cuda")]
118pub use cuda_backend as cuda;
119
120#[cfg(not(feature = "cuda"))]
121pub use dummy_cuda_backend as cuda;
122
123pub use cuda::{CudaDevice, CudaStorage};
124
125#[cfg(feature = "metal")]
126pub use metal_backend::{MetalDevice, MetalError, MetalStorage};
127
128#[cfg(not(feature = "metal"))]
129pub use dummy_metal_backend::{MetalDevice, MetalError, MetalStorage};
130
131#[cfg(feature = "rocm")]
132pub use rocm_backend::{RocmDevice, RocmError, RocmStorage};
133
134#[cfg(feature = "vulkan")]
135pub use vulkan_backend as vulkan;
136
137#[cfg(not(feature = "vulkan"))]
138pub use dummy_vulkan_backend as vulkan;
139
140pub use vulkan::{VulkanDevice, VulkanStorage};
141
142#[cfg(feature = "mkl")]
143extern crate intel_mkl_src;
144
145#[cfg(feature = "accelerate")]
146extern crate accelerate_src;
147
148pub trait ToUsize2 {
149    fn to_usize2(self) -> (usize, usize);
150}
151
152impl ToUsize2 for usize {
153    fn to_usize2(self) -> (usize, usize) {
154        (self, self)
155    }
156}
157
158impl ToUsize2 for (usize, usize) {
159    fn to_usize2(self) -> (usize, usize) {
160        self
161    }
162}
163
164/// Defining a module with forward method using a single argument.
165pub trait Module {
166    fn forward(&self, xs: &Tensor) -> Result<Tensor>;
167}
168
169impl<T: Fn(&Tensor) -> Result<Tensor>> Module for T {
170    fn forward(&self, xs: &Tensor) -> Result<Tensor> {
171        self(xs)
172    }
173}
174
175impl<M: Module> Module for Option<&M> {
176    fn forward(&self, xs: &Tensor) -> Result<Tensor> {
177        match self {
178            None => Ok(xs.clone()),
179            Some(m) => m.forward(xs),
180        }
181    }
182}
183
184/// A single forward method using a single single tensor argument and a flag to
185/// separate the training and evaluation behaviors.
186pub trait ModuleT {
187    fn forward_t(&self, xs: &Tensor, train: bool) -> Result<Tensor>;
188}
189
190impl<M: Module> ModuleT for M {
191    fn forward_t(&self, xs: &Tensor, _train: bool) -> Result<Tensor> {
192        self.forward(xs)
193    }
194}