GithubHelp home page GithubHelp logo

micrograd-cuda's Introduction

micrograd CUDA

Teaching myself basic CUDA by building GPU-accelerated tensor-based autodiff from the ground up, inspired by Andrej's micrograd.

No dependencies other than Python's standard library and CUDA.

Compiling

To compile the CUDA kernels:

nvcc -shared -o liboperations.so micrograd_cuda/operations.cu -Xcompiler -fPIC

Usage

import random
import time

from micrograd_cuda.mlp import MLP
from micrograd_cuda.tensor import Tensor
from micrograd_cuda.operations import Operations

# Model
model = MLP(100, [100, 100, 1])
epochs = 20
device = "cpu"

# Data
xs_batch = Tensor([[random.choice([-1, 1]) for _ in range(100)] for _ in range(10)]).T
ys_batch = Tensor([[random.choice([-1, 1])] for _ in range(10)]).T

# Move to device
model.to(device)
xs_batch.to(device)
ys_batch.to(device)

start = time.time()

for k in range(epochs):

    # Forward pass
    ypred = model(xs_batch)
    diff = ypred - ys_batch
    loss = (diff**2).sum()

    # Backward pass
    for p in model.parameters():
        p.zero_grad()
        
    loss.backward()
    
    # Update
    for p in model.parameters():
        p.data = (-0.1 * p.grad + p).data_copy()

print(f"Elapsed: {time.time() - start:.2f} sec")
    
loss.to("cpu")
print(loss.data)

Speedup

Roadmap

The codebase is still WIP with some rough spots, especially around CUDA Tensor data manipulation and copying.

  • Micrograd extension with basic 2D tensors and naïve matrix multiplication for MLP
  • Batching
  • CUDA kernel for matrix multiplication
  • Less verbose code
  • Error handling
  • CUDA optimizations
  • >2D tensors
  • Rust

Running tests

python -m pytest

License

MIT

micrograd-cuda's People

Contributors

mlecauchois avatar

Stargazers

George Brown avatar  avatar liuxiaobo avatar Jeff Carpenter avatar Jonno avatar Sam/Samuel avatar  avatar Gleb Sterkin avatar  avatar lemo avatar  avatar Vlad Florea avatar Bryan avatar maritse avatar Malhar Patel avatar Roberto Amelio avatar Sam Yu avatar Ashvanth.S avatar Sergei Rybakov avatar Vrindavan Sanap avatar  avatar Alex Wang avatar noman avatar Lukas Nitzsche avatar Rain Jiang avatar  avatar  avatar Dammian Miller avatar Dan Quach avatar J. Pritsker avatar Henrik Thostrup Jensen avatar  avatar Zsolt Balla avatar Tommy Bui Nguyen avatar Sagchakr avatar Patryk Tabiś avatar Marco Salvalaggio avatar Arseny Ivanov avatar Alexey Yalovski avatar Akash avatar Frank Röder avatar Prashanth Ramanna avatar Sagyndyk avatar  avatar Adharsh Babu avatar Florin Gogianu avatar Ye Hao avatar  avatar R. Cooper Snyder avatar Yeongjae Jang avatar Sandalots avatar Sam Gammon avatar Koen avatar Blake Jennings avatar Roman Solomatin avatar  avatar Mohammed Makhlouf (Mak) avatar Viranchee Lotia avatar  avatar p0123n avatar Alexey Zemtsov avatar Art Shendrik avatar Martin Chtilianov avatar Miguel Oller avatar Cem Turan avatar  avatar Shi Yan avatar Sayan Goswami avatar  avatar Isaac David avatar  avatar nicognaw avatar Axel Lundberg avatar Lars avatar aniket mishrikotkar avatar Geoffrey Roeder avatar Yavor Ivanov avatar Sachin avatar  avatar K Delcheva avatar Leonardo Mariscal avatar Montague Moran avatar Iron-Bound avatar Bruno De Backer avatar minjunsz avatar Anton Repushko avatar  avatar Nicola Muneratto avatar  avatar Dhruv Karan avatar Martin Gammelsæter avatar David Otero avatar Ognyan Kulev avatar ahmet avatar Ryan Mitts avatar  avatar Vaughan Rouesnel avatar Vishal Patil avatar Anton Vasin avatar Nimar avatar

Watchers

 avatar Sagyndyk avatar  avatar

Forkers

ikhomyakov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.