A functional language for machine learning. Inspired by Clojure, compiled to GPUs. https://www.sheaf-lang.org
  • Rust 91.5%
  • Clojure 4.2%
  • Starlark 2%
  • Python 1.4%
  • Shell 0.9%
Find a file
dbrll 61589a3439 [Compiler] Keep shape results as i32 after compilation
Shape results were i32 in the interpreter but became f32 after
compilation. Keep them as i32 so they can be reused by zeros and
reshape.
2026-11-03 21:33:11 +01:00
.github/workflows [CI] Refresh nightly release after publication 2026-10-01 19:29:20 +02:00
bazel [CI] Automate examples packaging 2026-08-24 22:41:50 +02:00
benchmarks [Compiler] Evaluate dimensions before converting to f32 2026-10-28 21:49:01 +01:00
docs [Docs] Update docs for Sheaf 2.3 2026-09-25 21:11:29 +02:00
examples [Tests] Test the checkpoint resuming in NanoGPT 2026-10-30 21:58:32 +01:00
sheaf [Compiler] Keep shape results as i32 after compilation 2026-11-03 21:33:11 +01: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] Bump version to 2.3.0 2026-09-25 21:10:51 +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 [Build] Bump version to 2.3.0 2026-09-25 21:10:51 +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 docs for Sheaf 2.3 2026-09-25 21:11:29 +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.