Posts

Showing posts with the label Rust

Inference: Build an ML Runtime with Rust and K3s | ML Engineering & MLOps

Image
  Hi All  Machine learning (ML) runtimes are the backbone of executing trained models in production, but understanding their inner workings can be daunting for beginners. In this post, we’ll build a ML Runtime in Rust from scratch and deploy it on Kubernetes—a minimal yet functional system that loads, executes, and manages simple ML models.   By focusing on core concepts like model serialization, inference, and basic error handling, we’ll demystify how ML runtimes operate under the hood. This is a hands-on guide for developers who want to bridge the gap between high-level ML frameworks and low-level system design. View the architecture of the ML runtime below, source code available here :        Components Explained: 1. Rust Application ( main.rs ) * Entry point of the program * Uses tch-rs (PyTorch bindings) for ML operations * Contains the model defini...

Post-training GPT Neo 1B with Rust & PyTorch Pt. 1 | ML Engineering

Image
 Hi All  Today we have an example of post-training a small model (GPT Neo 1B) in pure Rust. This example focuses of the training loop and model updates, assuming you've already loaded the model weights. Key Areas : 1.      Model Loading : We'll use the rust-bert crate for model loading. (supports GPT-Neo) 2.      Training Loop : Pure Rust implementation with tch-rs (Torch bindings) for tensor operations. 3.      Optimizer : AdamW optimizer for training.   View full code example below: The following is a block-by-block breakdown of the Rust post-training code above.   1. Dependencies and Imports use tch::{nn, Device, Tensor, Kind}; // Torch bindings for Rust use rust_bert::pipelines::common::ModelType; // Model types (GPT-Neo/Llama) use rust_bert::pipelines::text_generation::TextGenerationModel; // Model pipeline use anyhow::Result; // Error handling *      tch - Rust binding for PyTorch *  ...

Advanced Rust ML: Custom Modules with Tch-rs | ML Engineering

Image
  Hi All Another day, another Rust ML backend application. Today we're looking at defining custom modules in Rust in the ML backend. We're going to be creating a custom linear layer in Rust using Tch-rs ( Rust PyTorch bindings ).   View full source below, also available here :   Let's break the above code down. Block by block. 1. Imports use tch::{nn, nn::Module, Tensor}; What it does:  *        tch::nn - Neural network module containing layer definitions. *      nn:Module - The trait that all neural network modules must implement. *      Tensor - The Tensor type used throughout tch-rs   2. Struct Definition   struct CustomLayer { weight: Tensor, bias: Tensor, } What it does: *      weight - A tensor holding a layer's weights (matrix) *      bias -  A tensor holding the layer's bias (vector) This is essentially a linear layer (fully connected laye...

Advanced Rust ML: Loading Pre-trained models with Tch-rs | ML Engineering

Image
  Hi All Today we're looking at some Rust ML with  Tch-rs . Now Tch-rs is a  Rust bindings library for the C++ api of PyTorch. "The goal of the tch crate is to provide some thin wrappers around the C++ PyTorch api (a.k.a. libtorch)" - github . Let's get to it then. Let's demonstrate tch-rs's core capability: creating tensors, moving them to devices, performing operations and getting results - all in Rust while using PyTorch's backend. See introductory example below.  use tch::{Device, Tensor, Kind}; fn main() { let device = Device::cuda_if_available(); let x = Tensor::of_slice(&[1.0, 2.0, 3.0]).to_device(device); let y = Tensor::of_slice(&[4.0, 5.0, 6.0]).to_device(device); let z = x + y; println!("{:?}", z); } Breakdown: *      Imports : Device , Tensor and Kind   from Tch-rs, the core types for device management and memory operations. *        Device Selection : Device::cuda_if_available(); a...

Rust experiments: Wrapping unsafe (malloc/free) in safe abstractions | GPU engineering

Image
    I'll have a full example soon on how to wrap unsafe code (malloc/free like in a C++ Cuda kernel), in safe abstractions using Rust. See snippet below: