2026-06-28 20:05:10 -07:00
<p align="center"><img src=".github/hero.svg" alt="ml" width="880"></p>
2026-02-22 01:15:52 -08:00
# Hanzo ML
2026-06-27 22:02:38 +00:00
[](https://crates.io/crates/hanzo-ml)
[](https://docs.rs/hanzo-ml)
[](https://github.com/hanzoai/ml)
2023-08-02 11:36:43 +01:00
2026-07-24 12:33:23 -07:00
**Fast, multi-backend tensor & ML for Rust — CPU · CUDA · Metal · ROCm · Vulkan, with quantization built in.**
Hanzo ML is a lightweight, memory-safe machine-learning framework for Rust, tuned for performance and
built for real deployment — the compute core beneath [Hanzo ](https://hanzo.ai ) inference. Rebuilt for the
Hanzo stack with optimizations for Edge AI, quantization, multimodal workloads, and NVIDIA GB10 / DGX
Spark unified memory. It's the canonical Rust implementation of the Hanzo compute substrate: one core,
every backend, from a browser tab to a datacenter GPU.
2026-02-22 01:15:52 -08:00
## Key Features
2026-06-27 22:02:38 +00:00
- **High Performance**: GPU acceleration via CUDA (incl. unified/managed memory for NVIDIA GB10 / DGX Spark), Metal (Apple Silicon), ROCm (AMD RDNA3.5 APUs), Vulkan, and CPU optimizations
2026-06-26 19:56:53 +00:00
- **Edge AI Optimized**: Quantization support (GGUF, GGML, AFQ, GPTQ, AWQ). On AMD `gfx1151` the full GGUF quant zoo (22 types: Q/K, legacy, IQ1-4, TQ) decodes resident + bit-exact through one unified compute core -- prefill beats llama.cpp (1.1-1.26x), decode at HIP parity.
2026-02-22 01:15:52 -08:00
- **Multimodal**: Text, vision, audio, and 3D model support
- **WebAssembly**: Run models in the browser with WASM support
- **Rust Native**: Memory-safe, zero-cost abstractions
- **Hanzo Integration**: Works seamlessly with Hanzo Engine for inference
2023-07-10 12:51:37 +02:00
2023-08-23 08:32:59 +00:00
## Get started
2023-07-10 12:51:37 +02:00
2026-06-27 22:02:38 +00:00
Make sure that you have [`hanzo-ml` ](https://github.com/hanzoai/ml/tree/main/hanzo-ml ) correctly installed as described in the [installation guide ](https://github.com/hanzoai/ml/blob/main/hanzo-ml-book/src/guide/installation.md ).
2023-08-23 08:32:59 +00:00
2023-08-23 08:52:53 +00:00
Let's see how to run a simple matrix multiplication.
2023-08-23 13:26:21 +02:00
Write the following to your `myapp/src/main.rs` file:
2023-08-23 08:32:59 +00:00
```rust
2026-02-22 01:15:52 -08:00
use hanzo_ml_core ::{ Device , Tensor };
2023-08-23 08:32:59 +00:00
2023-08-23 13:26:21 +02:00
fn main () -> Result < (), Box < dyn std ::error ::Error >> {
2023-08-23 08:42:47 +00:00
let device = Device ::Cpu ;
let a = Tensor ::randn ( 0 f32 , 1. , ( 2 , 3 ), & device ) ? ;
let b = Tensor ::randn ( 0 f32 , 1. , ( 3 , 4 ), & device ) ? ;
2023-08-23 08:32:59 +00:00
let c = a . matmul ( & b ) ? ;
println! ( " {c} " );
Ok (())
}
2023-07-10 12:51:37 +02:00
```
2023-08-23 13:33:45 +02:00
`cargo run` should display a tensor of shape `Tensor[[2, 4], f32]` .
2023-08-23 08:32:59 +00:00
2026-02-22 01:15:52 -08:00
Having installed `hanzo` with Cuda support, simply define the `device` to be on GPU:
2023-08-23 08:32:59 +00:00
```diff
2023-08-23 08:42:47 +00:00
- let device = Device::Cpu;
2023-08-23 08:40:23 +00:00
+ let device = Device::new_cuda(0)?;
2023-08-23 08:32:59 +00:00
```
2023-08-23 08:52:53 +00:00
For more advanced examples, please have a look at the following section.
2023-07-10 12:51:37 +02:00
2023-07-14 16:56:33 +02:00
## Check out our examples
2026-06-27 22:02:38 +00:00
Browser (WASM) examples — build and run locally from [`hanzo-ml-wasm-examples` ](https://github.com/hanzoai/ml/tree/main/hanzo-ml-wasm-examples ):
- **yolo**: pose estimation and object recognition.
- **whisper**: speech recognition.
- **llama2.c**: text generation.
- **T5**: text generation.
- **Phi-1.5 / Phi-2**: text generation.
- **Segment Anything Model**: image segmentation.
- **BLIP**: image captioning.
2023-09-13 10:05:47 +02:00
2025-06-25 00:15:18 +02:00
We also provide some command line based examples using state of the art models:
2023-07-14 16:56:33 +02:00
2026-02-22 01:15:52 -08:00
- [LLaMA v1, v2, and v3 ](./hanzo-ml-examples/examples/llama/ ): general LLM, includes
2023-12-27 15:25:39 +01:00
the SOLAR-10.7B variant.
2026-02-22 01:15:52 -08:00
- [Falcon ](./hanzo-ml-examples/examples/falcon/ ): general LLM.
- [Codegeex4 ](./hanzo-ml-examples/examples/codegeex4-9b/ ): Code completion, code interpreter, web search, function calling, repository-level
- [GLM4 ](./hanzo-ml-examples/examples/glm4/ ): Open Multilingual Multimodal Chat LMs by THUDM
- [Gemma v1 and v2 ](./hanzo-ml-examples/examples/gemma/ ): 2b and 7b+/9b general LLMs from Google Deepmind.
- [RecurrentGemma ](./hanzo-ml-examples/examples/recurrent-gemma/ ): 2b and 7b
2024-04-13 20:07:01 +02:00
Griffin based models from Google that mix attention with a RNN like state.
2026-02-22 01:15:52 -08:00
- [Phi-1, Phi-1.5, Phi-2, and Phi-3 ](./hanzo-ml-examples/examples/phi/ ): 1.3b,
2024-04-24 20:54:24 +02:00
2.7b, and 3.8b general LLMs with performance on par with 7b models.
2026-02-22 01:15:52 -08:00
- [StableLM-3B-4E1T ](./hanzo-ml-examples/examples/stable-lm/ ): a 3b general LLM
2024-02-03 15:19:57 +01:00
pre-trained on 1T tokens of English and code datasets. Also supports
StableLM-2, a 1.6b LLM trained on 2T tokens, as well as the code variants.
2026-02-22 01:15:52 -08:00
- [Mamba ](./hanzo-ml-examples/examples/mamba/ ): an inference only
2023-12-23 10:46:02 +01:00
implementation of the Mamba state space model.
2026-02-22 01:15:52 -08:00
- [Mistral7b-v0.1 ](./hanzo-ml-examples/examples/mistral/ ): a 7b general LLM with
2023-12-16 06:23:12 -06:00
better performance than all publicly available 13b models as of 2023-09-28.
2026-02-22 01:15:52 -08:00
- [Mixtral8x7b-v0.1 ](./hanzo-ml-examples/examples/mixtral/ ): a sparse mixture of
2023-12-16 06:23:12 -06:00
experts 8x7b general LLM with better performance than a Llama 2 70B model with
much faster inference.
2026-02-22 01:15:52 -08:00
- [StarCoder ](./hanzo-ml-examples/examples/bigcode/ ) and
[StarCoder2 ](./hanzo-ml-examples/examples/starcoder2/ ): LLM specialized to code generation.
- [Qwen1.5 ](./hanzo-ml-examples/examples/qwen/ ): Bilingual (English/Chinese) LLMs.
- [RWKV v5 and v6 ](./hanzo-ml-examples/examples/rwkv/ ): An RNN with transformer level LLM
2024-02-14 10:58:32 +01:00
performance.
2026-02-22 01:15:52 -08:00
- [Replit-code-v1.5 ](./hanzo-ml-examples/examples/replit-code/ ): a 3.3b LLM specialized for code completion.
- [Yi-6B / Yi-34B ](./hanzo-ml-examples/examples/yi/ ): two bilingual
2023-11-11 12:39:11 +01:00
(English/Chinese) general LLMs with 6b and 34b parameters.
2026-02-22 01:15:52 -08:00
- [Quantized LLaMA ](./hanzo-ml-examples/examples/quantized/ ): quantized version of
2023-08-20 14:33:21 +01:00
the LLaMA model using the same quantization techniques as
[llama.cpp ](https://github.com/ggerganov/llama.cpp ).
2023-09-13 10:05:47 +02:00
2026-06-27 22:02:38 +00:00
<img src="https://github.com/hanzoai/ml/raw/main/hanzo-ml-examples/examples/quantized/assets/aoc.gif" width="600">
2023-09-13 10:05:47 +02:00
2026-02-22 01:15:52 -08:00
- [Stable Diffusion ](./hanzo-ml-examples/examples/stable-diffusion/ ): text to
2023-12-03 15:37:10 +08:00
image generative model, support for the 1.5, 2.1, SDXL 1.0 and Turbo versions.
2023-09-13 10:05:47 +02:00
2026-06-27 22:02:38 +00:00
<img src="https://github.com/hanzoai/ml/raw/main/hanzo-ml-examples/examples/stable-diffusion/assets/stable-diffusion-xl.jpg" width="200">
2023-09-13 10:05:47 +02:00
2026-02-22 01:15:52 -08:00
- [Wuerstchen ](./hanzo-ml-examples/examples/wuerstchen/ ): another text to
2023-09-20 13:09:35 +01:00
image generative model.
2026-06-27 22:02:38 +00:00
<img src="https://github.com/hanzoai/ml/raw/main/hanzo-ml-examples/examples/wuerstchen/assets/cat.jpg" width="200">
2023-09-20 13:09:35 +01:00
2026-02-22 01:15:52 -08:00
- [yolo-v3 ](./hanzo-ml-examples/examples/yolo-v3/ ) and
[yolo-v8 ](./hanzo-ml-examples/examples/yolo-v8/ ): object detection and pose
2023-08-25 18:01:38 +01:00
estimation models.
2023-09-13 10:05:47 +02:00
2026-06-27 22:02:38 +00:00
<img src="https://github.com/hanzoai/ml/raw/main/hanzo-ml-examples/examples/yolo-v8/assets/bike.od.jpg" width="200"><img src="https://github.com/hanzoai/ml/raw/main/hanzo-ml-examples/examples/yolo-v8/assets/bike.pose.jpg" width="200">
2026-02-22 01:15:52 -08:00
- [segment-anything ](./hanzo-ml-examples/examples/segment-anything/ ): image
2023-09-08 14:04:34 +01:00
segmentation model with prompt.
2023-09-13 10:05:47 +02:00
2026-06-27 22:02:38 +00:00
<img src="https://github.com/hanzoai/ml/raw/main/hanzo-ml-examples/examples/segment-anything/assets/sam_merged.jpg" width="200">
2023-09-13 10:05:47 +02:00
2026-02-22 01:15:52 -08:00
- [SegFormer ](./hanzo-ml-examples/examples/segformer/ ): transformer based semantic segmentation model.
- [Whisper ](./hanzo-ml-examples/examples/whisper/ ): speech recognition model.
- [EnCodec ](./hanzo-ml-examples/examples/encodec/ ): high-quality audio compression
2024-03-03 16:25:14 +01:00
model using residual vector quantization.
2026-02-22 01:15:52 -08:00
- [MetaVoice ](./hanzo-ml-examples/examples/metavoice/ ): foundational model for
2024-03-03 16:25:14 +01:00
text-to-speech.
2026-02-22 01:15:52 -08:00
- [Parler-TTS ](./hanzo-ml-examples/examples/parler-tts/ ): large text-to-speech
2024-08-19 08:30:12 +01:00
model.
2026-02-22 01:15:52 -08:00
- [T5 ](./hanzo-ml-examples/examples/t5 ), [Bert ](./hanzo-ml-examples/examples/bert/ ),
[JinaBert ](./hanzo-ml-examples/examples/jina-bert/ ) : useful for sentence embeddings.
- [DINOv2 ](./hanzo-ml-examples/examples/dinov2/ ): computer vision model trained
2023-09-13 10:05:47 +02:00
using self-supervision (can be used for imagenet classification, depth
evaluation, segmentation).
2026-02-22 01:15:52 -08:00
- [VGG ](./hanzo-ml-examples/examples/vgg/ ),
[RepVGG ](./hanzo-ml-examples/examples/repvgg ): computer vision models.
- [BLIP ](./hanzo-ml-examples/examples/blip/ ): image to text model, can be used to
2023-10-22 09:44:48 +01:00
generate captions for an image.
2026-02-22 01:15:52 -08:00
- [CLIP ](./hanzo-ml-examples/examples/clip/ ): multi-model vision and language
2024-03-28 23:10:57 +01:00
model.
2026-02-22 01:15:52 -08:00
- [TrOCR ](./hanzo-ml-examples/examples/trocr/ ): a transformer OCR model, with
2024-02-10 15:49:38 +01:00
dedicated submodels for hand-writing and printed recognition.
2026-02-22 01:15:52 -08:00
- [Marian-MT ](./hanzo-ml-examples/examples/marian-mt/ ): neural machine translation
2023-10-30 18:29:36 +01:00
model, generates the translated text from the input text.
2026-02-22 01:15:52 -08:00
- [Moondream ](./hanzo-ml-examples/examples/moondream/ ): tiny computer-vision model
2024-03-30 23:54:56 -07:00
that can answer real-world questions about images.
2023-09-13 10:05:47 +02:00
Run them using commands like:
2023-07-14 16:56:33 +02:00
```
2023-08-20 14:33:21 +01:00
cargo run --example quantized --release
2023-07-14 16:56:33 +02:00
```
2023-08-18 10:00:05 +01:00
In order to use **CUDA** add `--features cuda` to the example command line. If
you have cuDNN installed, use `--features cudnn` for even more speedups.
2023-07-14 17:08:05 +02:00
2023-07-24 15:28:27 +01:00
There are also some wasm examples for whisper and
2026-06-27 22:02:38 +00:00
[llama2.c ](https://github.com/karpathy/llama2.c ). Build and run them locally with
`trunk` from [`hanzo-ml-wasm-examples` ](https://github.com/hanzoai/ml/tree/main/hanzo-ml-wasm-examples )
(whisper, llama2.c, T5, Phi-1.5 / Phi-2, Segment Anything Model).
2023-07-24 15:28:27 +01:00
2023-08-20 14:33:21 +01:00
For LLaMA2, run the following command to retrieve the weight files and start a
2023-07-24 15:28:27 +01:00
test server:
```bash
2026-02-22 01:15:52 -08:00
cd hanzo-ml-wasm-examples/llama2-c
2026-06-27 22:02:38 +00:00
# Tiny Llama-2 weights from Andrej Karpathy's tinyllamas (see the example README for tokenizer.json):
wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin -O model.bin
2023-08-28 11:34:54 +01:00
trunk serve --release --port 8081
2023-07-24 15:28:27 +01:00
```
2023-08-09 19:36:03 +01:00
And then head over to
2023-08-28 11:34:54 +01:00
[http://localhost:8081/ ](http://localhost:8081/ ).
2023-07-14 16:56:33 +02:00
2023-09-12 06:06:21 -04:00
<!--- ANCHOR: useful_libraries --->
2023-10-20 09:08:39 +01:00
## Useful External Resources
2026-02-22 01:15:52 -08:00
- [`hanzo-ml-tutorial` ](https://github.com/ToluClassics/hanzo-ml-tutorial ): A
very detailed tutorial showing how to convert a PyTorch model to ML.
2026-06-01 11:48:33 -07:00
- [`hanzo-ml-lora` ](https://github.com/hanzoai/hanzo-ml-lora ): Efficient and
2026-02-22 01:15:52 -08:00
ergonomic LoRA implementation for ML. `hanzo-ml-lora` has
out-of-the-box LoRA support for many models from ML, which can be found
2026-06-01 11:48:33 -07:00
[here ](https://github.com/hanzoai/hanzo-ml-lora/tree/master/hanzo-ml-lora-transformers/examples ).
2023-11-21 12:54:24 -05:00
- [`optimisers` ](https://github.com/KGrewal1/optimisers ): A collection of optimisers
2023-10-25 21:51:45 +01:00
including SGD with momentum, AdaGrad, AdaDelta, AdaMax, NAdam, RAdam, and RMSprop.
2026-06-01 11:48:33 -07:00
- [`hanzo-ml-vllm` ](https://github.com/hanzoai/hanzo-ml-vllm ): Efficient platform for inference and
2026-07-24 12:33:23 -07:00
serving local LLMs, with a built-in `/v1` HTTP inference server.
2026-02-22 01:15:52 -08:00
- [`hanzo-ml-ext` ](https://github.com/mokeyish/hanzo-ml-ext ): An extension library to ML that provides PyTorch functions not currently available in ML.
- [`hanzo-ml-coursera-ml` ](https://github.com/vishpat/hanzo-ml-coursera-ml ): Implementation of ML algorithms from Coursera's [Machine Learning Specialization ](https://www.coursera.org/specializations/machine-learning-introduction ) course.
2023-11-21 05:18:14 +00:00
- [`kalosm` ](https://github.com/floneum/floneum/tree/master/interfaces/kalosm ): A multi-modal meta-framework in Rust for interfacing with local pre-trained models with support for controlled generation, custom samplers, in-memory vector databases, audio transcription, and more.
2026-06-01 11:48:33 -07:00
- [`hanzo-ml-sampling` ](https://github.com/hanzoai/hanzo-ml-sampling ): Sampling techniques for ML.
2026-02-22 01:15:52 -08:00
- [`gpt-from-scratch-rs` ](https://github.com/jeroenvlek/gpt-from-scratch-rs ): A port of Andrej Karpathy's _Let's build GPT_ tutorial on YouTube showcasing the ML API on a toy problem.
- [`hanzo-ml-einops` ](https://github.com/tomsanbear/hanzo-ml-einops ): A pure rust implementation of the python [einops ](https://github.com/arogozhnikov/einops ) library.
2026-07-24 12:33:23 -07:00
- [`atoma-infer` ](https://github.com/atoma-network/atoma-infer ): A Rust library for fast inference at scale, leveraging FlashAttention2 for efficient attention computation, PagedAttention for efficient KV-cache memory management, and multi-GPU support. It exposes a `/v1` HTTP inference API.
2025-01-04 17:07:30 -05:00
- [`llms-from-scratch-rs` ](https://github.com/nerdai/llms-from-scratch-rs ): A comprehensive Rust translation of the code from Sebastian Raschka's Build an LLM from Scratch book.
2023-09-12 06:06:21 -04:00
If you have an addition to this list, please submit a pull request.
<!--- ANCHOR_END: useful_libraries --->
2023-07-27 12:41:15 +02:00
<!--- ANCHOR: features --->
2023-07-10 12:51:37 +02:00
## Features
2023-08-09 21:09:21 +02:00
- Simple syntax, looks and feels like PyTorch.
2023-08-14 08:09:27 +01:00
- Model training.
2026-06-27 22:02:38 +00:00
- Embed user-defined ops/kernels, such as [flash-attention v2 ](https://github.com/hanzoai/ml/blob/89ba005962495f2bfbda286e185e9c3c7f5300a3/hanzo-flash-attn/src/lib.rs#L152 ).
2023-08-14 08:09:27 +01:00
- Backends.
- Optimized CPU backend with optional MKL support for x86 and Accelerate for macs.
- CUDA backend for efficiently running on GPUs, multiple GPU distribution via NCCL.
- WASM support, run your models in a browser.
2023-08-18 08:52:14 +01:00
- Included models.
2023-09-16 08:22:24 +02:00
- Language Models.
2024-04-20 16:11:24 +02:00
- LLaMA v1, v2, and v3 with variants such as SOLAR-10.7B.
2023-09-16 08:22:24 +02:00
- Falcon.
2024-02-28 21:02:41 +01:00
- StarCoder, StarCoder2.
2024-04-24 20:54:24 +02:00
- Phi 1, 1.5, 2, and 3.
2024-02-11 17:04:57 +01:00
- Mamba, Minimal Mamba
2024-08-17 19:31:23 +01:00
- Gemma v1 2b and 7b+, v2 2b and 9b.
2023-09-29 12:50:50 +02:00
- Mistral 7b v0.1.
2023-12-15 19:29:03 -06:00
- Mixtral 8x7b v0.1.
2024-02-03 15:19:57 +01:00
- StableLM-3B-4E1T, StableLM-2-1.6B, Stable-Code-3B.
2023-10-18 11:27:23 +01:00
- Replit-code-v1.5-3B.
2023-09-16 08:22:24 +02:00
- Bert.
2023-11-11 12:39:11 +01:00
- Yi-6B and Yi-34B.
2026-02-22 01:15:52 -08:00
- Qwen1.5, Qwen1.5 MoE.
2024-03-01 08:58:30 +01:00
- RWKV v5 and v6.
2023-11-21 09:38:27 +00:00
- Quantized LLMs.
- Llama 7b, 13b, 70b, as well as the chat and code variants.
- Mistral 7b, and 7b instruct.
2023-12-15 19:29:03 -06:00
- Mixtral 8x7b.
2023-12-16 06:23:12 -06:00
- Zephyr 7b a and b (Mistral-7b based).
- OpenChat 3.5 (Mistral-7b based).
2023-11-07 10:14:04 -05:00
- Text to text.
2023-11-09 12:55:09 -05:00
- T5 and its variants: FlanT5, UL2, MADLAD400 (translation), CoEdit (Grammar correction).
2023-11-07 10:14:04 -05:00
- Marian MT (Machine Translation).
2023-10-22 09:44:48 +01:00
- Text to image.
- Stable Diffusion v1.5, v2.1, XL v1.0.
- Wurstchen v2.
- Image to text.
- BLIP.
2024-02-10 15:49:38 +01:00
- TrOCR.
2024-02-28 14:58:42 +01:00
- Audio.
2024-03-03 16:25:14 +01:00
- Whisper, multi-lingual speech-to-text.
2024-02-28 14:58:42 +01:00
- EnCodec, audio compression model.
2024-03-03 16:25:14 +01:00
- MetaVoice-1B, text-to-speech model.
2024-08-19 08:30:12 +01:00
- Parler-TTS, text-to-speech model.
2023-09-16 08:22:24 +02:00
- Computer Vision Models.
2024-02-14 10:58:32 +01:00
- DINOv2, ConvMixer, EfficientNet, ResNet, ViT, VGG, RepVGG, ConvNeXT,
2024-08-28 12:20:09 +03:00
ConvNeXTv2, MobileOne, EfficientVit (MSRA), MobileNetv4, Hiera, FastViT.
2023-10-20 09:08:39 +01:00
- yolo-v3, yolo-v8.
2023-09-16 08:22:24 +02:00
- Segment-Anything Model (SAM).
2024-03-03 16:25:14 +01:00
- SegFormer.
2023-08-20 17:02:05 +01:00
- File formats: load models from safetensors, npz, ggml, or PyTorch files.
2023-08-14 08:09:27 +01:00
- Serverless (on CPU), small and fast deployments.
2023-08-18 08:52:14 +01:00
- Quantization support using the llama.cpp quantized types.
2023-07-10 12:51:37 +02:00
2023-07-27 12:41:15 +02:00
<!--- ANCHOR_END: features --->
2023-08-09 19:36:03 +01:00
## How to use
2023-07-10 12:51:37 +02:00
2023-07-27 12:41:15 +02:00
<!--- ANCHOR: cheatsheet --->
2023-07-12 18:35:34 +02:00
Cheatsheet:
2026-02-22 01:15:52 -08:00
| | Using PyTorch | Using ML |
2023-07-12 18:43:52 +01:00
|------------|------------------------------------------|------------------------------------------------------------------|
2023-08-09 19:24:28 +02:00
| Creation | `torch.Tensor([[1, 2], [3, 4]])` | `Tensor::new(&[[1f32, 2.], [3., 4.]], &Device::Cpu)?` |
2023-07-27 15:20:13 +02:00
| Creation | `torch.zeros((2, 2))` | `Tensor::zeros((2, 2), DType::F32, &Device::Cpu)?` |
2023-07-12 18:43:52 +01:00
| Indexing | `tensor[:, :4]` | `tensor.i((.., ..4))?` |
| Operations | `tensor.view((2, 2))` | `tensor.reshape((2, 2))?` |
| Operations | `a.matmul(b)` | `a.matmul(&b)?` |
| Arithmetic | `a + b` | `&a + &b` |
2023-08-23 10:29:50 +02:00
| Device | `tensor.to(device="cuda")` | `tensor.to_device(&Device::new_cuda(0)?)?` |
2023-07-12 18:43:52 +01:00
| Dtype | `tensor.to(dtype=torch.float16)` | `tensor.to_dtype(&DType::F16)?` |
2026-02-22 01:15:52 -08:00
| Saving | `torch.save({"A": A}, "model.bin")` | `hanzo::safetensors::save(&HashMap::from([("A", A)]), "model.safetensors")?` |
| Loading | `weights = torch.load("model.bin")` | `hanzo::safetensors::load("model.safetensors", &device)` |
2023-07-12 18:35:34 +02:00
2023-07-27 12:41:15 +02:00
<!--- ANCHOR_END: cheatsheet --->
2023-07-12 18:35:34 +02:00
2023-07-14 16:56:33 +02:00
## Structure
2023-07-10 12:51:37 +02:00
2026-02-22 01:15:52 -08:00
- [hanzo-ml ](./hanzo-ml ): Core ops, devices, and `Tensor` struct definition
- [hanzo-nn ](./hanzo-nn/ ): Tools to build real models
- [hanzo-ml-examples ](./hanzo-ml-examples/ ): Examples of using the library in realistic settings
- [hanzo-kernels ](./hanzo-kernels/ ): CUDA custom kernels
- [hanzo-datasets ](./hanzo-datasets/ ): Datasets and data loaders.
- [hanzo-transformers ](./hanzo-transformers ): transformers-related utilities.
- [hanzo-flash-attn ](./hanzo-flash-attn ): Flash attention v2 layer.
- [hanzo-onnx ](./hanzo-onnx/ ): ONNX model evaluation.
2023-07-06 13:25:05 +01:00
## FAQ
2026-02-22 01:15:52 -08:00
### Why should I use ML?
2023-07-10 12:51:37 +02:00
2025-04-15 12:41:10 -07:00
<!--- ANCHOR: goals --->
2026-02-22 01:15:52 -08:00
ML's core goal is to *make serverless inference possible* . Full machine learning frameworks like PyTorch
are very large, which makes creating instances on a cluster slow. ML allows deployment of lightweight
2023-08-09 19:36:03 +01:00
binaries.
2023-07-10 12:51:37 +02:00
2026-02-22 01:15:52 -08:00
Secondly, ML lets you *remove Python* from production workloads. Python overhead can seriously hurt performance,
2023-08-09 19:36:03 +01:00
and the [GIL ](https://www.backblaze.com/blog/the-python-gil-past-present-and-future/ ) is a notorious source of headaches.
2023-07-10 12:51:37 +02:00
2023-08-09 19:36:03 +01:00
Finally, Rust is cool! A lot of the HF ecosystem already has Rust crates, like [safetensors ](https://github.com/huggingface/safetensors ) and [tokenizers ](https://github.com/huggingface/tokenizers ).
2023-07-10 12:51:37 +02:00
2025-04-15 12:41:10 -07:00
<!--- ANCHOR_END: goals --->
2023-07-12 18:35:34 +02:00
### Other ML frameworks
- [dfdx ](https://github.com/coreylowman/dfdx ) is a formidable crate, with shapes being included
2023-08-09 19:36:03 +01:00
in types. This prevents a lot of headaches by getting the compiler to complain about shape mismatches right off the bat.
However, we found that some features still require nightly, and writing code can be a bit daunting for non rust experts.
2023-07-12 18:35:34 +02:00
We're leveraging and contributing to other core crates for the runtime so hopefully both crates can benefit from each
2023-08-09 19:36:03 +01:00
other.
2023-07-12 18:35:34 +02:00
- [burn ](https://github.com/burn-rs/burn ) is a general crate that can leverage multiple backends so you can choose the best
2023-08-09 19:36:03 +01:00
engine for your workload.
2023-07-12 18:35:34 +02:00
- [tch-rs ](https://github.com/LaurentMazare/tch-rs.git ) Bindings to the torch library in Rust. Extremely versatile, but they
2023-08-09 19:36:03 +01:00
bring in the entire torch library into the runtime. The main contributor of `tch-rs` is also involved in the development
2026-02-22 01:15:52 -08:00
of `hanzo` .
2023-07-12 18:35:34 +02:00
2023-08-12 17:45:58 +02:00
### Common Errors
#### Missing symbols when compiling with the mkl feature.
2023-07-06 13:25:05 +01:00
If you get some missing symbols when compiling binaries/tests using the mkl
2023-08-17 08:26:02 +01:00
or accelerate features, e.g. for mkl you get:
2023-07-06 13:25:05 +01:00
```
= note: /usr/bin/ld: (....o): in function `blas::sgemm':
.../blas-0.22.0/src/lib.rs:1944: undefined reference to `sgemm_' collect2: error: ld returned 1 exit status
= note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the `-l` flag to specify native libraries to link
2023-08-17 08:26:02 +01:00
= note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo
```
or for accelerate:
```
Undefined symbols for architecture arm64:
"_dgemm_", referenced from:
2026-02-22 01:15:52 -08:00
hanzo_core::accelerate::dgemm::h1b71a038552bcabe in libhanzo_core...
2023-08-17 08:26:02 +01:00
"_sgemm_", referenced from:
2026-02-22 01:15:52 -08:00
hanzo_core::accelerate::sgemm::h2cf21c592cba3c47 in libhanzo_core...
2023-08-17 08:26:02 +01:00
ld: symbol(s) not found for architecture arm64
2023-07-06 13:25:05 +01:00
```
2023-08-09 19:36:03 +01:00
This is likely due to a missing linker flag that was needed to enable the mkl library. You
2023-08-17 08:26:02 +01:00
can try adding the following for mkl at the top of your binary:
```rust
2023-07-06 13:25:05 +01:00
extern crate intel_mkl_src ;
```
2023-08-17 08:26:02 +01:00
or for accelerate:
```rust
extern crate accelerate_src ;
```
2023-07-14 09:31:25 +01:00
2023-08-20 14:33:21 +01:00
#### Cannot run the LLaMA examples: access to source requires login credentials
2023-08-12 17:45:58 +02:00
```
Error: request error: https://huggingface.co/meta-llama/Llama-2-7b-hf/resolve/main/tokenizer.json: status code 401
```
2023-08-20 14:33:21 +01:00
This is likely because you're not permissioned for the LLaMA-v2 model. To fix
this, you have to register on the huggingface-hub, accept the [LLaMA-v2 model
2023-08-12 17:45:58 +02:00
conditions ](https://huggingface.co/meta-llama/Llama-2-7b-hf ), and set up your
authentication token. See issue
2026-06-27 22:02:38 +00:00
[#350 ](https://github.com/hanzoai/ml/issues/350 ) for more details.
2023-08-12 17:45:58 +02:00
2026-02-22 01:15:52 -08:00
#### Missing cute/cutlass headers when compiling flash-attn
2023-09-02 08:56:12 +02:00
```
2026-02-22 01:15:52 -08:00
In file included from kernels/flash_fwd_launch_template.h:11:0,
from kernels/flash_fwd_hdim224_fp16_sm80.cu:5:
kernels/flash_fwd_kernel.h:8:10: fatal error: cute/algorithm/copy.hpp: No such file or directory
#include <cute/algorithm/copy.hpp>
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Error: nvcc error while compiling:
```
[cutlass ](https://github.com/NVIDIA/cutlass ) is provided as a git submodule so you may want to run the following command to check it in properly.
```bash
git submodule update --init
2023-09-02 08:56:12 +02:00
```
2023-09-08 09:15:14 +02:00
#### Compiling with flash-attention fails
```
/usr/include/c++/11/bits/std_function.h:530:146: error: parameter packs not expanded with ‘ ...’ :
```
2024-04-17 22:43:00 +02:00
This is a bug in gcc-11 triggered by the Cuda compiler. To fix this, install a different, supported gcc version - for example gcc-10, and specify the path to the compiler in the NVCC_CCBIN environment variable.
2023-09-08 09:15:14 +02:00
```
2024-04-17 22:43:00 +02:00
env NVCC_CCBIN=/usr/lib/gcc/x86_64-linux-gnu/10 cargo ...
2023-09-08 09:15:14 +02:00
```
2023-09-15 13:25:10 +07:00
#### Linking error on windows when running rustdoc or mdbook tests
```
Couldn't compile the test.
2026-02-22 01:15:52 -08:00
---- .\hanzo-ml-book\src\inference\hub.md - Using_the_hub::Using_in_a_real_model_ (line 50) stdout ----
2023-09-15 13:25:10 +07:00
error: linking with `link.exe` failed: exit code: 1181
//very long chain of linking
= note: LINK : fatal error LNK1181: cannot open input file 'windows.0.48.5.lib'
```
Make sure you link all native libraries that might be located outside a project target, e.g., to run mdbook tests, you should run:
```
2026-02-22 01:15:52 -08:00
mdbook test hanzo-ml-book -L .\target\debug\deps\ `
2023-09-15 13:25:10 +07:00
-L native=$env:USERPROFILE\.cargo\registry\src\index.crates.io-6f17d22bba15001f\windows_x86_64_msvc-0.42.2\lib `
-L native=$env:USERPROFILE\.cargo\registry\src\index.crates.io-6f17d22bba15001f\windows_x86_64_msvc-0.48.5\lib
```
2023-09-29 18:04:52 +02:00
#### Extremely slow model load time with WSL
This may be caused by the models being loaded from `/mnt/c` , more details on
[stackoverflow ](https://stackoverflow.com/questions/68972448/why-is-wsl-extremely-slow-when-compared-with-native-windows-npm-yarn-processing ).
2023-08-12 17:45:58 +02:00
#### Tracking down errors
2023-07-14 09:31:25 +01:00
2026-02-22 01:15:52 -08:00
You can set `RUST_BACKTRACE=1` to be provided with backtraces when a hanzo
2023-07-14 09:31:25 +01:00
error is generated.
2024-05-16 14:32:50 +02:00
#### CudaRC error
If you encounter an error like this one `called ` Result::unwrap()` on an ` Err` value: LoadLibraryExW { source: Os { code: 126, kind: Uncategorized, message: "The specified module could not be found." } }` on windows. To fix copy and rename these 3 files (make sure they are in path). The paths depend on your cuda version.
`c:\Windows\System32\nvcuda.dll` -> `cuda.dll`
`c:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\bin\cublas64_12.dll` -> `cublas.dll`
`c:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\bin\curand64_10.dll` -> `curand.dll`
2026-07-24 12:33:23 -07:00
## Hanzo — the Open AI Cloud
Open source · every language · on-chain settlement. [hanzo.ai ](https://hanzo.ai ) · [docs.hanzo.ai ](https://docs.hanzo.ai )
**SDKs in every language** — [Python ](https://github.com/hanzoai/python-sdk ) (flagship) · [TypeScript ](https://github.com/hanzo-js/sdk ) · [Go ](https://github.com/hanzo-go/sdk ) · [Rust ](https://github.com/hanzo-rs/sdk ) · [C++ ](https://github.com/hanzo-cpp/sdk ) · [Swift ](https://github.com/hanzo-swift/sdk ) · [Kotlin ](https://github.com/hanzo-kt/sdk ) · [umbrella ](https://github.com/hanzoai/sdk )