Rust binding to LuisaCompute (WIP)

Overview

luisa-compute-rs

Rust binding to LuisaCompute (WIP)

Inside this crate:

  • An almost safe binding to LuisaCompute
  • An EDSL for writing kernels
  • A new backend implementation in pure Rust

Table of Contents

  • Example
  • Usage
  • Safety

Example

use luisa_compute as luisa;
use luisa::prelude::*;
#[derive(Value)]
struct Vec2 {
    x: f32,
    y: f32,
}
#[proxy]
impl Vec2 {
    fn dot(&self, other: &Self) -> f32 {
        self.x * other.x + self.y * other.y
    }
}
#[kernel]
fn dot(u: BufferVar<Vec2>, v:  BufferVar<Vec2>, out:BufferVar<f32>) {
    let tid = luisa::dispatch_id().x;
    out.write(tid, u.read(tid).dot(v.read(tid)))
}

Usage

Variables

// all variables should not be mut
// otherwise the compute graph will be recorded incorrectly
let v = local(Float::new(0.0));
let shared_v = shared(Float::new(0.0));
// rust cannot overload = so...
v.store(1.0);
// other operators work fine!
v += 1.0;
let u = local(Float2::new(0.0, 0.0));
u.x.store(1.0);

Functions

#[callable] // optional
pub fn foo(p: Vec2, q:Vec2) -> f32 {
    let mut d = p.x * q.x + p.y * q.y;
    flow!(if d < 0.0 => {
        d = 0.0;
    })
    d
}

let d = foo(p, q); // inlined
let d = call!(foo, p, q); // translate to function call

Structs & Methods

#[derive(Value)]
struct Ray {
    o: Vec3,
    d: Vec3,
}
#[proxy]
impl Ray::Proxy {
    #[callable]
    fn at(&self, t: Var<f32>) -> Var<Vec3> {
        self.o + self.d * t
    }
}

Kernel

#[kernel]
fn foo(u: BufferVar<Vec2>, v: BufferVar<Vec2>, out: BufferVar<f32>) {
    let tid = dispatch_id().x;
    out.write(tid, u.read(tid).dot(v.read(tid)));
}

Control Flow

flow!(if cond {
    // do something
} else {
    // do something else
})

flow!(for i in 0..10 {
    // do something
})

flow!(while cond {
    // do something
})

flow!(match x => { // x must be a Var<i32>
    0 => {
        // do something
    }
    1 => {
        // do something else
    }
    _ => {
        // do something else
    }
})

Polymorphism

#[polymorphic]
pub trait Area {
    fn area(&self) -> Var<f32>;
}
// Since a trait cannot be generic paraeter, we need to use a macro
type AreaArrayBuilder = polymorphic_array_builder!(Area);
type AreaArray = polymorphic_array!(Area);
type AreaProxy = polymorphic_proxy!(Area); // this is normally not needed

#[derive(Value)]
pub struct Circle {
    r: f32,
}
#[proxy]
impl Area for Circle::Proxy {
    fn area(&self) -> Var<f32> {
        self.r * self.r * PI
    }
}

#[kernel]
fn compute_areas(array: AreaArray::Var, out:BufferVar<f32>) {
    let tid = dispatch_id().x;
    out.write(tid, array.read(tid).area());
}

let mut area_objects = AreaArrayBuilder::new();
area_objects.push(Circle { r: 1.0 });
let area_objects = area_objects.build();

Autodiff

autodiff! {
    let x = Var::new(1.0);
    let y = Var::new(2.0);
    requires_grad!(x, y);
    let z = x * x + y * y;
    backward!(z);  // only one backward pass is allowed
    let (dx, dy) = grad!(x, y);
    ...
}

Command

let command_buffer = stream.command_buffer();
cmd_submit!(command_buffer, 
    raytrace_shader(framebuffer, accel, resolution)
        .dispatch(resolution),
    accumulate_shader(accum_image, framebuffer)
        .dispatch(resolution),
)

stream

Safety

API

The API is safe to a large extent. However, async operations are difficult to be completely safe without requiring users to write boilerplate. Thus, all async operations are marked unsafe.

Backend

Safety checks such as OOB is generally not available for GPU backends. As it is difficult to produce meaningful debug message in event of a crash. However, the Rust backend provided in the crate contains full safety checks and is recommended for debugging.

You might also like...
🦀Rust Turkiye - Rust Dersleri

Rust Turkiye - Rust Dersleri CURIOSITY - Featuring Richard Feynman Bu repo Rust Turkiye tarafindan duzenlenen Rust Dersleri egitiminin alistirma ve ko

A Rust machine learning framework.

Linfa linfa (Italian) / sap (English): The vital circulating fluid of a plant. linfa aims to provide a comprehensive toolkit to build Machine Learning

Machine Learning library for Rust

rusty-machine This library is no longer actively maintained. The crate is currently on version 0.5.4. Read the API Documentation to learn more. And he

Rust library for Self Organising Maps (SOM).
Rust library for Self Organising Maps (SOM).

RusticSOM Rust library for Self Organising Maps (SOM). Using this Crate Add rusticsom as a dependency in Cargo.toml [dependencies] rusticsom = "1.1.0"

Rust language bindings for TensorFlow
Rust language bindings for TensorFlow

TensorFlow Rust provides idiomatic Rust language bindings for TensorFlow. Notice: This project is still under active development and not guaranteed to

Machine learning crate for Rust

rustlearn A machine learning package for Rust. For full usage details, see the API documentation. Introduction This crate contains reasonably effectiv

Rust bindings for the C++ api of PyTorch.

tch-rs Rust bindings 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. libtorc

个人的 rust 学习资料

🔝 通知: 项目文档迁移到: https://github.com/higker/learn-rust learning-rust-zh 个人的 rust 学习资料 学习目录 目录 源代码地址 相关解析 第一个rust程序 https://github.com/higker/learning-ru

Distributed compute platform implemented in Rust, and powered by Apache Arrow.
Distributed compute platform implemented in Rust, and powered by Apache Arrow.

Ballista: Distributed Compute Platform Overview Ballista is a distributed compute platform primarily implemented in Rust, powered by Apache Arrow. It

Owner
Luisa Group
High-performance graphics and beyond
Luisa Group
LightGBM Rust binding

lightgbm-rs LightGBM Rust binding Require You need an environment that can build LightGBM. # linux apt install -y cmake libclang-dev libc++-dev gcc-mu

vaaaaanquish 42 Dec 15, 2022
Rust binding of primitiv

Rust frontend of primitiv Prerequisites Rust (1.26 or later) Clang (3.9 or later) (optional) CUDA (8.0 or later) Install mkdir work cd work # build p

primitiv 19 Nov 22, 2021
Simple WIP GPGPU framework for Rust built on top of wgpu

gpgpu A simple GPU compute library based on wgpu. It is meant to be used alongside wgpu if desired. To start using gpgpu, just create a Framework inst

Jerónimo Sánchez 97 Dec 26, 2022
Neural Networks in Rust, without backpropagation. WIP

Deep Thought As of right now, this crate is far from a usable state. This crate implements feedforward-neural Networks in rust. Unlike the vast majori

null 5 Apr 10, 2022
🚧 WIP 🚧 Vector database plugin for Postgres, written in Rust, specifically designed for LLM.

pgvecto.rs pgvecto.rs is a Postgres extension that provides vector similarity search functions. It is written in Rust and based on pgrx. Features cosi

TensorChord 74 Apr 26, 2023
[WIP] An experimental Java-like language and it's virtual machine, for learning Java and JVM.

Sky VM An experimental Java-like language and it's virtual machine, for learning Java and JVM. Dependencies Rust (rust-lang/rust) 2021 Edition, dual-l

Kk Shinkai 2 Jan 3, 2022
Towards fully autonomous driving (WIP)

?? openpilot openpilot is a comprehensive Rust crate designed to assist in building fully autonomous vehicles. The primary focus of this crate is to p

Mahmoud 5 Feb 23, 2024
Msgpack serialization/deserialization library for Python, written in Rust using PyO3, and rust-msgpack. Reboot of orjson. msgpack.org[Python]

ormsgpack ormsgpack is a fast msgpack library for Python. It is a fork/reboot of orjson It serializes faster than msgpack-python and deserializes a bi

Aviram Hassan 139 Dec 30, 2022
Practice repo for learning Rust. Currently going through "Rust for JavaScript Developers" course.

rust-practice ?? Practice repo for learning Rust. Directories /rust-for-js-dev Files directed towards "Rust for JavaScript Developers" course. Thank y

Sammy Samkough 0 Dec 25, 2021
A Rust library with homemade machine learning models to classify the MNIST dataset. Built in an attempt to get familiar with advanced Rust concepts.

mnist-classifier Ideas UPDATED: Finish CLI Flags Parallelize conputationally intensive functions Class-based naive bayes README Image parsing Confusio

Neil Kaushikkar 0 Sep 2, 2021