cd /news/machine-learning/sheaf-brings-clojure-s-code-as-data-… Β· home β€Ί topics β€Ί machine-learning β€Ί article
[ARTICLE Β· art-56780] src=sheaf-lang.org β†— pub= topic=machine-learning verified=true sentiment=↑ positive

Sheaf brings Clojure's code-as-data to machine learning

Sheaf, a new functional language for differentiable computation, brings Clojure's code-as-data paradigm to machine learning, enabling models to be inspectable, composable, and compiled data structures. The language eliminates boilerplate, provides runtime observability, and runs as a single binary on GPU, targeting ML researchers and agentic AI developers.

read4 min views1 publishedJul 13, 2026
Sheaf brings Clojure's code-as-data to machine learning
Image: source

A functional language for differentiable computation #

Sheaf brings Clojure’s code-as-data to machine learning, with models as inspectable, composable, and compiled data structures.

For ML Researchers #

No classes, no boilerplateβ€” Write math, not plumbing** Runtime Observability**β€” Catch NaN, trace shapes and profile performance without code changes** Single binary framework**β€” One executable, no dependencies. Train and run on GPU out of the box

For Agentic AI #

Context Densityβ€” 60-75% fewer tokens than equivalent Python for the same architecture** Uniform Syntax**β€” Single syntactic form for all operations reduces ambiguity and generation errors** Immediate Onboarding**β€” Built-in context generator for Claude Code, Cursor, and Copilot

Neural Networks as Math

In Sheaf, a neural network is a composition of mathematical functions over a parameter tree.

Sheaf is purely functional, so differentiation and compilation require no annotations. Any pure function can be differentiated with value-and-grad and is automatically compiled to GPU code.

(defn forward [x p]
  (as-> x h
    (with-params [p :l1] (relu    (+ (@ h W) b)))
    (with-params [p :l2] (softmax (+ (@ h W) b)))))
php
(defn transformer-block [x layer-p config]
  (as-> x h
    (-> h
        (layer-norm (get layer-p :ln1) 2)
        (multi-head-attention layer-p config)
        (first)
        (+ h))   ;; residual

    (-> h
        (layer-norm (get layer-p :ln2) 2)
        (mlp (get layer-p :mlp))
        (+ h))))

Models as Data

Because models are data, Sheaf requires no module classes, registration, or parameter groups. Even structural operations like pruning, freezing, or weight sharing are expressed as regular data transformations.

Sheaf brings compile-time macros to the computation graph itself, generating architecture variants from a single template.

;; Grow a model: add a layer at runtime
(defn append-layer [params new-layer]
  (assoc params :layers
    (append (get params :layers) new-layer)))

;; Swap the output head for a different task
(defn hot-swap-head [model task-id heads]
  (assoc model :head (get heads task-id)))

Observability

In Sheaf, every function call, tensor shape, and numerical statistic is observable at runtime.

A tracer logs the full call hierarchy with tensor statistics. Guards halt execution on numerical invariants like NaN or range violations. A profiler attributes wall time to each function in the call tree.

β”œβ”€ [train-step] dict(keys:["l1", "l2"]), f32[4x2] [min:0.00e0 max:1.00e0] (32B), f32[4x1] [min:0.00e0 max:1.00e0] (16B), 0.700000
β”‚ β”œβ”€ [forward] f32[4x2] [min:0.00e0 max:1.00e0] (32B), dict(keys:["l1", "l2"])
β”‚ β”‚ β”œβ”€ [relu] f32[4x8] [min:-1.37e0 max:2.33e0] (128B)
β”‚ β”‚ └─ ← f32[4x8] [min:0.00e0 max:2.33e0] (128B) (0.8ΞΌs)
β”‚ β”‚ β”œβ”€ [sigmoid] f32[4x1] [min:-5.48e-2 max:1.18e0] (16B)
β”‚ β”‚ └─ ← f32[4x1] [min:4.86e-1 max:7.66e-1] (16B) (1.8ΞΌs)
β”‚ └─ ← f32[4x1] [min:4.86e-1 max:7.66e-1] (16B) (0.0ΞΌs)
...
bash
$ sheaf train.shf --guard no-nan
Step 1 | Loss: 0.306990
Step 2 | Loss: 0.500000

/!\ Guard Breached: NoNan
Function: sigmoid
Tensor contains NaN or Inf values: f32[4x1] [min:inf max:-inf]

Backtrace (last 26 operations):

β”œβ”€ [train-step] dict(keys:["l1", "l2"]), f32[4x2], f32[4x1], 1000.0
β”‚ β”œβ”€ [forward] f32[4x2], dict(keys:["l1", "l2"])
β”‚ β”‚ β”œβ”€ [relu] f32[4x8] [min:-2.67e0 max:1.73e0]
β”‚ β”‚ └─ ← f32[4x8] [min:0.00e0 max:1.73e0] (0.6ΞΌs)
β”‚ β”‚ β”œβ”€ [sigmoid] f32[4x1] [min:inf max:-inf] [NaN DETECTED]
...
Profiler: 3.63s wall

  Function                          Calls      Total       Self   Avg/call
  ------------------------------------------------------------------------
  gpt-forward                         100      1.72s      3.72s    37.23ms
  reshape                             301   900.57ms   900.57ms     2.99ms
  choice                              100   622.85ms   622.85ms     6.23ms
  softmax                             100   158.67ms   158.67ms     1.59ms
  generate-token                      100      5.56s   158.37ms    55.65ms
  io                                    4    45.11ms    45.11ms    11.28ms
  <lambda>                            102      5.58s    12.88ms    54.71ms
  ... 23 others                      1728                5.42ms

  Call tree:

  β”œβ”€β”€ generate (3.58s, 1 call)
  β”‚   β”œβ”€β”€ reduce (3.58s, 1 call)
  β”‚   β”‚   └── <lambda> (3.58s, 101 calls)
  β”‚   β”‚       β”œβ”€β”€ generate-token (3.56s, 100 calls)
  β”‚   β”‚       β”‚   β”œβ”€β”€ gpt-forward (1.72s, 100 calls)
  β”‚   β”‚       β”‚   β”œβ”€β”€ reshape (900.16ms, 100 calls)
  β”‚   β”‚       β”‚   β”œβ”€β”€ choice (622.85ms, 100 calls)
  β”‚   β”‚       β”‚   β”œβ”€β”€ softmax (158.67ms, 100 calls)
  β”‚   β”‚       β”‚   └── ... 8 others (1.47ms, 900 calls)
  β”‚   β”‚       └── ... 5 others (3.37ms, 1002 calls)
  β”‚   └── ... 2 others (1.9ΞΌs, 2 calls)
  └── ... 7 others (45.64ms, 19 calls)

Resource Efficiency

Context usage counts GPT-4 tokens (tiktoken) across model, training, and sampling code. Deploy size is the minimal runtime required to train and run a model on a CUDA GPU.

Code size(GPT-4 tokens)

Deploy size

Clean Implementation

Sheaf is written in Rust. The complete runtime with GPU backends ships as a single 4 MB executable.

The compiler toolchain is downloaded on first use and is not required to run a compiled model.

$ du -h *
128K	__sheaf__                 # compiled model
3.2M	data
4.0K	model.shf
164M	out-weights
3.8M	sheaf                     # runtime
4.0K	train.shf
── more in #machine-learning 4 stories Β· sorted by recency
── more on @sheaf 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain β€” perfect for shipping the agent you just read about.

$git push zahid main
β†’ Live at https://your-agent.zahid.host βœ“
Get free account β†’ Pricing
from €0/mo Β· no card required
LIVE [news/sheaf-brings-clojure…] indexed:0 read:4min 2026-07-13 Β· β€”