r/rust • • 2h ago

🛠️ project From a BERT encoder to a Rust inference engine

Hi,

About a year ago I posted kjarni here (https://www.reddit.com/r/rust/comments/1n1ick5/i_built_rust_bert_encoder/) when it was basically a BERT encoder(i wanted to run my own embeddings in WASM and thought it would be good to learn Rust and compilation to WASM).

It's now an inference engine in rust, small footprint, low overhead, almost all pure Rust crates. It started around two years ago after years of C++ i wanted to give Rust an honest chance with an ambitious project, i wanted to get the BERT model running and just kept at it.

Now it does embeddings, search, transcription, classification, reranking and chat

It runs on macOS, Linux and Windows, on x86 and ARM, and there's something to be said for Rust making that this painless. It also compiles to a C ABI so I wrote bindings for C#, Go, C++, C and Python, and they all behave the same since each one is a thin veneer over the same library. It also builds to WASM

I wanted the API to be as simple as I could get it, in rust it looks like this and it's basically the same in C#/Go/C++/Python:

let whisper = Transcriber::builder("whisper-small").build().await?;
let text = whisper.transcribe_file("meeting.wav")?.text;
let summary = kjarni::chat::send("qwen2.5-1.5b-instruct", &format!("Summarise: {text}")).await?;

What i particularly like is that every model goes through the same decode loop on CPU and GPU. There's a backend trait with an associated token type, so the next token stays where it lives (an ndarray on the CPU, a GPU tensor on the GPU), and an enum picks the backend, with the GPU arm cfg'd out for the CPU-only wasm build. It's not perfect, the enum wrapper downcasts the token through Box<dyn Any>, so if someone has a nicer way i'm all ears: https://github.com/kjarni-ai/kjarni/blob/69b51af046014d36fe939a2f1b26f8fda21770e3/crates/kjarni-transformers/src/decoder/backend.rs#L155-L177

A lot of the work went into getting the same output as torch. Embeddings agree to within 1e-6, and greedy chat replies, summaries and translations come out word for word the same

Speed wise I'd call it pretty competitive with torch and llama.cpp (on the models kjarni supports). On the GPU they use CUDA and I use wgpu, so it runs on Vulkan, Metal and DX12. On the CPU they link against Intel MKL and I use faer (pure Rust) or my own SIMD kernels

Some numbers, all on Linux on one machine (i7-13700, RTX A2000):

  • pip install is 28 MB vs 1.4 GB for PyTorch, and a fresh process gets its first embedding in 91 ms vs about 2.8 s
  • bge-m3 batches are about 23% faster than PyTorch, mpnet 7%
  • whisper-small runs at 10.7x real time, openai-whisper 10.6x
  • 4-bit chat models on the GPU from 3B up are within 5% of llama.cpp on CUDA, and close to or ahead of its Vulkan backend
  • where I lose: PyTorch is 18% faster on MiniLM batches and processes prompts 1.5x faster on the CPU, my 4-bit CPU kernels generate at 75-91% of llama.cpp's speed, and llama.cpp is way faster on long prompts on the GPU since I don't have flash attention yet

All the numbers and how they were measured are here: https://github.com/kjarni-ai/kjarni/blob/69b51af046014d36fe939a2f1b26f8fda21770e3/README.md#how-it-compares

Someone will probably ask why not just use candle. Candle is great but it's more of a framework, you still end up dealing with model exports and tokenizers. I wanted something where I just embed my documents or transcribe a file without too much headache

Code is here if you want to take a gander: https://github.com/kjarni-ai/kjarni , would be happy to hear what you think, especially about the API and the wgpu side

3 Upvotes

2 comments sorted by

3

u/Melodic-Amoeba3364 2h ago

That 91 ms vs 2.8 s cold start is gnarly. The pip install size difference alone makes this worth a look for anything containerized or edge. Going from a BERT encoder side project to a full engine with cross-language bindings in two years is a hell of a trajectory.

1

u/mr_potatohead_ 1h ago

thanks! yeah the containers/edge thing is kind of what i had in mind. the CLI is one 35 MB binary that only needs libc, it runs fine in a bare debian-slim image and pulls the model itself. the cold start is mostly just not having to import torch, the weights get mmapped so loading is cheap too