Skip to content

Instantly share code, notes, and snippets.

View nihalpasham's full-sized avatar
🐢
regionaltantrums!

nihalpasham

🐢
regionaltantrums!
View GitHub Profile
@nihalpasham
nihalpasham / .md
Last active July 20, 2025 09:52
How does automatic kernel fusion work in burn?

How does automatic kernel fusion work in burn?

Screenshot 2025-07-07 at 10 50 37 AM

Burn’s Tensor Abstraction:

  1. Generic Parameters Tensor<B, D, K> :
    • B: Backend - execution backend (composable)
    • D: usize - dimensionality (compile-time constant)
@nihalpasham
nihalpasham / rust_to_spirv.md
Last active June 15, 2025 10:51
🦀 Mapping Rust Shaders to SPIR-V (via rust-gpu): This Gist demonstrates how a simple Rust fragment shader is compiled into SPIR-V using rust-gpu. It includes the original Rust code, the corresponding SPIR-V output, and a concise mapping between the two. It also outlines how the rustc_codegen_spirv backend translates MIR to SPIR-V and how SPIR-T …

Mapping Rust Shader to SPIR-V

#[spirv(fragment)]				
pub fn main_fs(output: &mut Vec4) {
 *output = vec4(1.0, 0.0, 0.0, 1.0);
}
@nihalpasham
nihalpasham / .md
Last active May 24, 2025 10:35
rust-gpu

rust-gpu

What is it?

  • is a custom backend for rustc that compiles native rust code (albeit a sub-set of it) to spir-v
    • MIR -> SPIR-V: to be precise, it takes in rust’s MIR and converts it to SPIR-V

rustc front-end IR(s):

Stuff that happens at each stage in the front-end

  • AST: macro-expansion, name resolution
  • HIR (High-level IR): type-checking, type-inference and trait solving
@nihalpasham
nihalpasham / .md
Last active May 21, 2025 13:03
The "Two-Language Problem" in AI

Rust: A Modern Language for Safe and Fast General-Purpose Programming

Rust is a relatively young, but increasingly popular, systems programming language that emphasizes safety, performance, and concurrency. Developed by Mozilla, its stable version 1.0 was released in 2015.

Here's why Rust is gaining traction, especially in areas like AI:

  • Memory Safety without Garbage Collection: This is a core strength of Rust. Unlike languages like C and C++ which offer low-level memory management but are prone to errors (e.g., buffer overflows, null pointer dereferences, data races), Rust uses an "ownership model" and "borrowing rules" to enforce memory safety at compile time. This catches many potential bugs before the code even runs, leading to more reliable and secure software. Crucially, it achieves this without a garbage collector, which can introduce unpredictable pauses in performance.
  • High Performance: Rust compiles directly to machine code, similar to C and C++. This allows for highly optimized exe
@nihalpasham
nihalpasham / .md
Last active February 20, 2025 08:50
Pliron Compiler Framework

Pliron

#mlir #llvm #compiler

  • An MLIR-inspired extensible compiler framework written in pure Rust.

⠀Design Evaluation Notes:

  • IR Format: Pliron’s generic IR (Intermediate Representation) format is based on SSA (Static Single Assignment) form and is conceptually similar to MLIR. Like MLIR, it is a nested IR, meaning it supports hierarchical structures such as operations containing regions, which in turn contain blocks and other operations. However, there may be differences in specific implementation details.
@nihalpasham
nihalpasham / cranelift_backend.md
Last active November 2, 2024 02:28
Plan for Building a Backend in Cranelift

Plan for Building a Backend in Cranelift

Steps to Add a New Backend:

  1. Create a Folder for the Backend
    • Place the new backend directory under /cranelift/codegen/src/isa, where each backend resides.
  2. Define Backend and Implement Required Traits
    • Ensure the backend implements these essential traits:
      • TargetIsa: Specifies the target architecture’s interface.
  • LowerBackend: Manages instruction lowering for the architecture.
@nihalpasham
nihalpasham / cubecl_ir_gelu
Last active September 28, 2024 02:00
CubeCL IR for the gelu_example
KernelDefinition { inputs: [Binding { location: Storage, visibility: Read, item: Item { elem: Float(F32), vectorization: Some(1)
}, size: None
}
], outputs: [Binding { location: Storage, visibility: ReadWrite, item: Item { elem: Float(F32), vectorization: Some(1)
}, size: None
}
], named: [("info", Binding { location: Storage, visibility: Read, item: Item { elem: UInt, vectorization: None
}, size: None
})
], cube_dim: CubeDim { x: 4, y: 1, z: 1
@nihalpasham
nihalpasham / CubeCL_Architecture_Overview.md
Last active April 17, 2025 18:53
CubeCL Architecture Overview - Running Rust on your GPU (WebGPU, CUDA)

CubeCL

#gpu #kernel #rust

High Level Overview:

  • GPU kernels in Rust
  • Comptime
    • Automatic vectorization
    • Instruction and shape specialization
  • Loop unrolling
@nihalpasham
nihalpasham / cubecl_gelu_expanded.rs
Last active September 28, 2024 01:44
Expanding CubeCL's gelu example
// 🦀 Generated by Rust Macro Expand 🦀
// 🦀 Timestamp: 16/09/2024, 12:50:30 🦀
#![allow(warnings)]
#![feature(print_internals)]
#![feature(panic_internals)]
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
@nihalpasham
nihalpasham / gelu.wgsl
Created August 28, 2024 03:11
WGSL generated with the CubeCL framework for the gelu example
[START_KERNEL_COMPILATION]
name: gelu::gelu_array::GeluArray<
cubecl_core::frontend::element::float::F32,
cubecl_wgpu::runtime::WgpuRuntime,
>
cube_dim: (4, 1, 1)
shared_memory: 0 bytes
info: (
KernelSettings {