Skip to main content
Clorch is a Clojure deep-learning library backed by LibTorch — the C++ engine that powers PyTorch. It gives you PyTorch-style tensors, automatic differentiation, neural-network modules, optimizers, data loaders, mixed-precision training, and NCCL distributed training through a REPL-friendly Clojure API.

Installation

Add Clorch to your project and configure the Java runtime

Quickstart

Build and train your first neural network in minutes

Tensors

Create, manipulate, and inspect LibTorch tensors from Clojure

Neural Networks

Build models with built-in layers and the defmodel macro

Distributed Training

Multi-GPU NCCL training with DDP, AMP, and checkpoints

LLM Components

RoPE, GQA, SwiGLU, KV cache, and autoregressive generation

What Clorch provides

Clorch maps directly to PyTorch’s conceptual model while presenting an idiomatic Clojure API. Every heavy operation executes in native C++ via JavaCPP, bypassing JVM overhead for tensor math.

Tensor Ops

~170 operations covering arithmetic, reductions, shape manipulation, and sampling

Autograd

Automatic differentiation with requires-grad, backward passes, and no-grad scopes

Memory Control

Deterministic native-memory scopes via with-torch and PointerScope

Optimizers

SGD, Adam, AdamW, RMSprop, and Adagrad wrapping LibTorch C++ implementations

Distributions

Probability distributions with sample, log-prob, mean, and variance

AMP

float16/bfloat16 autocast and dynamic gradient scaling for CUDA training

Getting started

1

Add the dependency

Add Clorch to your deps.edn using a git coordinate pointing to the latest tag.
deps.edn
2

Start a REPL

Launch the Clojure REPL from your project directory. Clorch automatically selects CPU or CUDA natives on startup.
3

Create tensors and run autograd

Require the core namespaces and start experimenting with tensors and gradients immediately.
4

Build a neural network

Compose layers with nn/sequential or define custom architectures with nn/defmodel.

PyTorch namespace mapping

Clorch follows PyTorch’s namespace structure. If you know PyTorch, you can navigate Clorch immediately.
Clorch follows PyTorch concepts but does not expose every PyTorch symbol. Check the API documentation or source before translating a Python call directly.