A functional language for machine learning. Inspired by Clojure, compiled to GPUs. https://www.sheaf-lang.org
  • Rust 90.6%
  • Clojure 4.7%
  • Starlark 2.2%
  • Python 1.6%
  • Shell 0.9%
Find a file
dbrll deaacc6457 [Compiler] Fix tuple bindings in reduce unrolling
After unrolling reduce, tuple accesses could still use the old lambda
parameters instead of the current values.

Bind the carry and element for each iteration.
Also support tuple indexing in the interpreter.
2026-09-05 22:13:06 +02:00
.github/workflows [CI] Automate examples packaging 2026-08-24 22:41:50 +02:00
bazel [CI] Automate examples packaging 2026-08-24 22:41:50 +02:00
benchmarks [Benchmarks] Use Bazel build and improve baseline 2026-08-21 14:41:48 +02:00
docs [Docs] Update NanoGPT token counts 2026-07-29 01:28:22 +02:00
examples [CI] Automate examples packaging 2026-08-24 22:41:50 +02:00
sheaf [Compiler] Fix tuple bindings in reduce unrolling 2026-09-05 22:13:06 +02:00
.bazelignore [Build] Migrate IREE build from Bash to Bazel 2026-08-20 21:30:34 +02:00
.bazelrc [Build] Add lto to bazelrc 2026-08-21 15:22:17 +02:00
.bazelversion [Build] Add Bazel Rust prototype 2026-08-19 10:48:56 +02:00
.gitattributes Add .gitattributes to highlight Sheaf code with Clojure style 2026-01-11 13:22:26 +01:00
.gitignore [Build] Migrate IREE build from Bash to Bazel 2026-08-20 21:30:34 +02:00
BUILD.bazel [Tests] Make CLEVR regression deterministic 2026-09-02 00:38:09 +02:00
cargo-bazel-lock.json [Build] Embed generated stdlib prelude 2026-08-19 11:44:39 +02:00
LICENSE Create LICENSE 2026-01-24 20:10:35 +01:00
mkexamples.sh [CI] Automate examples packaging 2026-08-24 22:41:50 +02:00
MODULE.bazel [Examples] Have Bazel manage NanoGPT 2026-08-24 21:53:56 +02:00
MODULE.bazel.lock [Build] Migrate IREE build from Bash to Bazel 2026-08-20 21:30:34 +02:00
README.md [Docs] Update README with Bazel instructions 2026-08-21 00:54:35 +02:00

Sheaf logo

Release License Stars

Sheaf is a functional language for machine learning that combines the expressiveness of Lisp with the performance of modern ML compilers.

Highlights

  • Clojure for tensors: homoiconicity, immutability, minimalist syntax, threading macros
  • REPL-driven development: immediate tensor shapes and dtypes, inline documentation, and environment inspection
  • GPU-first: compiles to StableHLO and runs on CUDA, Metal, Vulkan, and CPUs through IREE
  • Reverse-mode autodiff: differentiates supported tensor programs before StableHLO code generation
  • JIT compilation: eligible pure functions are automatically compiled from runtime shapes
  • Single native binary: the Sheaf runtime ships as one executable for Linux and Apple Silicon, with no Python environment required
  • LLM-native: built-in context generation for AI assistants

Sample code

Define a model, differentiate, get gradients:

sheaf> (def x (tensor [[1.0 0.0] [0.0 1.0]]))    ;; inputs
sheaf> (def y (tensor [[1.0 1.0 0.0 0.0]         ;; targets
                       [0.0 0.0 1.0 1.0]]))
sheaf> (def W (zeros '[2 4]))                    ;; weights

sheaf> ((value-and-grad
           (fn [W] (mse-loss (@ x W) y)))        ;; loss + gradients
         W)
=> [0.5 [[-0.25 -0.25 0.0 0.0]
        [0.0 0.0 -0.25 -0.25]]]

Transformer block with residual connections:

(defn transformer-block [x layer-p config]
  (as-> x h
    ;; 1. Self-Attention
    (-> h
        (layer-norm (get layer-p :ln1) 2)
        (multi-head-attention layer-p config)
        (first) ;; Get the attention output, ignore weights
        (+ h))  ;; Residual 1

    ;; 2. MLP
    (-> h
        (layer-norm (get layer-p :ln2) 2)
        (mlp (get layer-p :mlp))
        (+ h)))) ;; Residual 2

Use macros to derive three compiled graphs from the same layer list:

(defmacro defresidual [name args & layers] ...)   ;; generates residual graph
(defmacro definspect  [name args & layers] ...)   ;; generates monitoring graph

(defmodel    net         (x) [linear :h1 gelu] [linear :h2 gelu])
(defresidual res-net     (x) [linear :h1 gelu] [linear :h2 gelu])
(definspect  inspect-net (x) [linear :h1 gelu] [linear :h2 gelu])

Check out the examples for more code samples.

Install

Note: Sheaf is under active development. The language and compiler interfaces may still change between releases.

On macOS, the Sheaf binary might be blocked by Gatekeeper. Unlock it with:

xattr -dr com.apple.quarantine /path/to/sheaf

Quick test

Run the nanoGPT example:

cd examples/nanoGPT
sheaf train.shf   # training
sheaf sample.shf  # autoregressive inference

Build from source

Sheaf and its dependencies are built with Bazel. We recommend installing it through Bazelisk:

# macOS
brew install bazelisk
# linux x86_64
sudo curl -L -o /usr/local/bin/bazel \
  "https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64"
sudo chmod +x /usr/local/bin/bazel
# linux aarch64
sudo curl -L -o /usr/local/bin/bazel \
  "https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-arm64"
sudo chmod +x /usr/local/bin/bazel

Then build Sheaf from the repository root:

bazel build --config=release //sheaf:bin
cp bazel-bin/sheaf/sheaf ~/.local/bin    # or /usr/local/bin

On macOS, Bazel requires the Xcode Command Line Tools:

xcode-select --install

Finally, Sheaf requires curl and unzip to download the matching IREE compiler toolchain at first use.

GPU support

CUDA is automatically enabled on Linux when nvcc is available, or you can specify a specific toolkit at build time with --repo_env=IREE_CUDA_TOOLKIT_ROOT=/path/to/cuda. Running Sheaf with CUDA requires a compatible NVIDIA driver.

Vulkan is enabled by default on Linux, and the required headers are automatically fetched. Running Sheaf with Vulkan requires a Vulkan loader and a compatible device driver.

Metal is enabled by default on Apple Silicon. No extra dependency is needed beyond the Xcode Command Line Tools.