> ## Documentation Index
> Fetch the complete documentation index at: https://antlobach-clorch-182a83cb.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Clorch: LibTorch Deep Learning for Clojure

> Clorch brings PyTorch-style tensors, autograd, neural networks, and distributed CUDA training to Clojure via high-performance LibTorch bindings.

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.

<CardGroup cols={2}>
  <Card title="Installation" icon="download" href="/installation">
    Add Clorch to your project and configure the Java runtime
  </Card>

  <Card title="Quickstart" icon="rocket" href="/quickstart">
    Build and train your first neural network in minutes
  </Card>

  <Card title="Tensors" icon="table" href="/concepts/tensors">
    Create, manipulate, and inspect LibTorch tensors from Clojure
  </Card>

  <Card title="Neural Networks" icon="brain" href="/models/neural-networks">
    Build models with built-in layers and the defmodel macro
  </Card>

  <Card title="Distributed Training" icon="server" href="/advanced/distributed">
    Multi-GPU NCCL training with DDP, AMP, and checkpoints
  </Card>

  <Card title="LLM Components" icon="microchip" href="/advanced/llm-components">
    RoPE, GQA, SwiGLU, KV cache, and autoregressive generation
  </Card>
</CardGroup>

## 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.

<CardGroup cols={3}>
  <Card title="Tensor Ops" icon="calculator" href="/concepts/tensors">
    \~170 operations covering arithmetic, reductions, shape manipulation, and sampling
  </Card>

  <Card title="Autograd" icon="arrow-rotate-left" href="/concepts/autograd">
    Automatic differentiation with requires-grad, backward passes, and no-grad scopes
  </Card>

  <Card title="Memory Control" icon="memory" href="/concepts/memory">
    Deterministic native-memory scopes via with-torch and PointerScope
  </Card>

  <Card title="Optimizers" icon="sliders" href="/models/optimizers">
    SGD, Adam, AdamW, RMSprop, and Adagrad wrapping LibTorch C++ implementations
  </Card>

  <Card title="Distributions" icon="chart-bell-curve" href="/advanced/distributions">
    Probability distributions with sample, log-prob, mean, and variance
  </Card>

  <Card title="AMP" icon="bolt" href="/advanced/amp">
    float16/bfloat16 autocast and dynamic gradient scaling for CUDA training
  </Card>
</CardGroup>

## Getting started

<Steps>
  <Step title="Add the dependency">
    Add Clorch to your `deps.edn` using a git coordinate pointing to the latest tag.

    ```clojure deps.edn theme={null}
    {:deps {io.github.antlobach/clorch
            {:git/tag "v0.2.0"
             :git/sha "07642acdbc522e8aa2a20cd223912247614d2239"}}}
    ```
  </Step>

  <Step title="Start a REPL">
    Launch the Clojure REPL from your project directory. Clorch automatically selects CPU or CUDA natives on startup.

    ```bash theme={null}
    clj
    ```
  </Step>

  <Step title="Create tensors and run autograd">
    Require the core namespaces and start experimenting with tensors and gradients immediately.

    ```clojure theme={null}
    (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]
    ```
  </Step>

  <Step title="Build a neural network">
    Compose layers with `nn/sequential` or define custom architectures with `nn/defmodel`.

    ```clojure theme={null}
    (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]))
    ```
  </Step>
</Steps>

## PyTorch namespace mapping

Clorch follows PyTorch's namespace structure. If you know PyTorch, you can navigate Clorch immediately.

| PyTorch | Clorch |
| - | - |
| `torch` | `clorch.torch` |
| `torch.nn` | `clorch.nn` |
| `torch.nn.functional` | `clorch.nn.functional` |
| `torch.optim` | `clorch.optim` |
| `torch.autograd` | `clorch.autograd` |
| `torch.cuda` | `clorch.cuda` |
| `torch.amp` | `clorch.amp` |
| `torch.distributed` | `clorch.distributed` |
| `DistributedDataParallel` | `clorch.nn.parallel` |
| `torch.distributions` | `clorch.distributions` |
| `torch.linalg` | `clorch.linalg` |
| `Dataset` / `DataLoader` | `clorch.data` |

<Note>
  Clorch follows PyTorch concepts but does not expose every PyTorch symbol. Check the API documentation or source before translating a Python call directly.
</Note>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.