clorch.torch namespace, so every operation you know from PyTorch maps directly onto an idiomatic Clojure call. Tensor storage can live on CPU or CUDA VRAM, and any tensor whose :requires-grad flag is set participates automatically in Clorch’s autodiff graph.
Creation
From Clojure Data
tensor accepts scalars, flat vectors, and nested vectors. An optional map configures the dtype, device, and gradient tracking.
tensor throws IllegalArgumentException if data is nil or an empty collection. Every element is cast to the target dtype at construction time, so passing integers with {:dtype :float32} is safe.Standard Initializers
- Zeros / Ones / Eye / Full / Empty
- Range / Spacing
- Random
Supported Dtypes
Every factory and conversion function accepts a dtype keyword from this set:
Complex dtypes (
:complex64, :complex128) and quantised dtypes (:qint8, :quint8, :qint32) are also exposed in dtype-map for advanced use.
Basic Operations
Element-wise Math
All four arithmetic operators broadcast scalars automatically:Math Functions
Clorch exposes the complete set of LibTorch pointwise math functions. Below is the full reference — each function takes a tensor and returns a new tensor of the same shape.Linear Algebra
Reductions
Shape Management
Reshape and View
reshape may return a copy; view is strictly zero-copy and requires a contiguous tensor. Both support -1 for dimension inference.
Flatten and Unflatten
Unsqueeze and Expand
Concat, Stack, Split, and Chunk
Stacking Variants
Transpose, T, Permute, and Swap
Other Shape Helpers
Slicing and Indexing
ix is Clorch’s ergonomic indexer that mirrors Python’s [] syntax. It accepts integers, Clojure ranges, :_ for “all elements”, keyword :... for ellipsis, and nested vectors for advanced indexing. Full slicing documentation is on the Advanced Slicing page.
Linear Algebra (linalg- functions)
The clorch.torch namespace exposes the full torch.linalg surface as linalg- prefixed functions.
Utility Methods
Type Conversion
Device Placement
Clorch picks up the best available LibTorch backend whenclorch.torch loads. Move tensors and models to a device explicitly:
Inspection
Printing
tprint is a minimalist REPL printer that shows shape, dtype, and values without flooding the output for large tensors:
tensor-string returns the same representation as a Clojure String, useful for logging:
print-method, so they render sensibly via println and pr.
Logic and Search
Comparisons
All comparison functions return a:bool tensor of the same shape:
Floating-point Predicates
Logical Operations
Conditional Selection with where
Searching
Sampling
Multinomial
Sample one or more indices according to a probability distribution:Nucleus (Top-p) Sampling
top-p implements the nucleus sampling algorithm used in autoregressive text generation. It applies softmax, accumulates sorted probabilities, masks out tokens beyond the probability threshold p, renormalises, and draws one sample:
Rotary Position Embeddings (RoPE)
Clorch provides first-class RoPE support for transformer models.precompute-rope-freqs generates the cosine and sine embedding tables once, then apply-rope rotates query and key tensors in-place.
JIT Save and Load
Clorch supports both eager-mode checkpoint save/load and TorchScript JIT modules.Eager checkpoint: save and load
Eager checkpoint: save and load
save and load also accept nn/Module and optimizer objects.TorchScript JIT: jit-save, jit-load, jit-forward
TorchScript JIT: jit-save, jit-load, jit-forward
jit-forward wraps the input tensors in IValueVector automatically and unwraps the returned IValue back to a tensor.jit-trace and jit-script are not available at the Clojure level — tracing and scripting must be done in Python and the resulting .pt file loaded into Clorch with jit-load.Memory Management
The following functions belong toclorch.torch and are central to native memory control. For a complete explanation see the Memory Management page.