Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/antlobach/clorch/llms.txt

Use this file to discover all available pages before exploring further.

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
{:deps {io.github.antlobach/clorch
        {:git/tag "v0.2.0"
         :git/sha "07642acdbc522e8aa2a20cd223912247614d2239"}}}
2

Start a REPL

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

Create tensors and run autograd

Require the core namespaces and start experimenting with tensors and gradients immediately.
(require '[clorch.torch :as t]
         '[clorch.autograd :as autograd])

(def x (t/tensor [2.0] {:requires-grad true}))
(def y (t/pow x 3))
(autograd/backward y)
(autograd/grad x) ; => [12.0]
4

Build a neural network

Compose layers with nn/sequential or define custom architectures with nn/defmodel.
(require '[clorch.nn :as nn])

(def model
  (nn/sequential
    (nn/linear 10 64)
    (nn/relu)
    (nn/linear 64 1)))

(nn/forward model (t/randn [4 10]))

PyTorch namespace mapping

Clorch follows PyTorch’s namespace structure. If you know PyTorch, you can navigate Clorch immediately.
PyTorchClorch
torchclorch.torch
torch.nnclorch.nn
torch.nn.functionalclorch.nn.functional
torch.optimclorch.optim
torch.autogradclorch.autograd
torch.cudaclorch.cuda
torch.ampclorch.amp
torch.distributedclorch.distributed
DistributedDataParallelclorch.nn.parallel
torch.distributionsclorch.distributions
torch.linalgclorch.linalg
Dataset / DataLoaderclorch.data
Clorch follows PyTorch concepts but does not expose every PyTorch symbol. Check the API documentation or source before translating a Python call directly.

Build docs developers (and LLMs) love