Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate how to add JIT using MLIR to micrograd #62

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

fzakaria
Copy link

@fzakaria fzakaria commented Mar 1, 2024

Hi @karpathy !

I'm not expecting you to merge this (although I'd very much welcome it!) -- but I wanted to contribute publicly work myself and @alexander-shaposhnikov have done to demonstrate adding a JIT Just In Time compiler for micrograd.

The main change here is the introduction of a new jit.py module which can take various micrograd computation graphs: Value, Neuron, Layer etc.. and produce MLIR using the arithmetic dialect. The IR is then lowered to LLVM IR which can then be executed directly via a provided CPU execution engine.

test_jit.py has some great examples but the API is straightforward

def test_mlp_complex_multiple_out():
    random.seed(10)
    nn = MLP(nin=2, nouts=[2, 2])
    jnn = jit(nn)
    args = [-30., -20.]
    for r, jr in zip(nn(args), jnn(args)):
        assert math.isclose(r.data, jr, abs_tol=1e-04)

Follow-ups:

  • There's more opportunity to improve the JIT by maybe reproducing Layer & MLP using the linalg & tensor dialects.
  • Demonstrate how MLIR can be lowered and executed via a accelerator (i.e. CUDA) using the gpu dialect.

Changes done to the repository:

  • added more to .gitignore
  • added support for direnv which automatically sets up venv
  • created a requirements.txt file -- to help bring in MLIR dependencies
  • added init.py to the test directory so that you can run pytest by itself
  • introduced a new jit.py module
  • added documentation
  • added a similar to cell to demo.ipynb to show the example using JIT

fzakaria and others added 25 commits January 12, 2024 18:59
* Added JIT doc to README
* Added more comments
* Cleaned up ctypes code
* Crazy amount of debugging to fix this. Alexander went crazy deep to
  look at the instructions in lldb to see that the MLIR CPU runner was
  doing a double dereference only when the return is a list.

  The argument is also first in the method list ... why!?

Co-authored-by: Alexander Shaposhnikov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants