Practical overview
When it fits
Use it when an OCaml project needs PyTorch tensor operations or when exploring machine-learning workflows in OCaml. Check the Jane Street torch repository for ongoing development, since this project’s README says development has moved there.
Who it is for
- OCaml developers adding tensor-based numerical computation to applications.
- Machine-learning practitioners building experiments and model workflows in OCaml.
- Developers who need differentiable tensor computations or GPU-backed tensor operations from OCaml.
Documented uses
- Create and manipulate tensors in OCaml.
- Build differentiable computations and train models using tensor operations.
- Explore documented examples such as MNIST, ResNet fine-tuning, GANs, and Python-model execution.
Requirements & limits
- The bindings use the PyTorch C++ API and are described as mostly automatically generated.
- The README associates the repository’s current tip and opam package v0.7 with PyTorch 2.0.0.
- On Linux, the README calls for libtorch with the cxx11 ABI; opam installation installs CPU libtorch automatically.
- The README provides CUDA 11.7 libtorch guidance for GPU-oriented setup.
- The repository is licensed under Apache-2.0, and its README directs ongoing development to Jane Street’s torch repository.
Check current requirements in the official documentation. Source-based guidance does not mean OpenViST has run or independently tested this project.
Install the CPU package and build a tensor example
Set up the opam package, then compile and run the README’s basic Dune tensor program.
Before you start
- An OCaml environment with opam and Dune available.
- The README states that opam installation automatically installs CPU libtorch.
Steps
Install the package with
opam install torch.Create
example.mlcontainingopen Torchand a program that creates and printsTensor.randn [ 4; 2 ].Create a Dune file declaring an executable named
examplelinked with thetorchlibrary.Run
dune exec example.exeto compile and run the example.
Expected outcome
A small OCaml program that creates and prints a random tensor using the Torch library.
Guide limitations
- The README warns that building the bytecode target may not work on macOS.
- The repository README says development has moved to Jane Street’s torch repository.
Prepare the repository examples with a downloaded libtorch
Configure the documented alternative libtorch path and build the repository examples.
Before you start
- Linux, with a libtorch package using the cxx11 ABI as specified by the README.
- Git and the tools required by the repository’s
make allbuild.
Steps
Download and extract a suitable libtorch package.
Set
LIBTORCHto the extracted directory.Clone the repository and change into its directory.
Run the command below.
make all
Expected outcome
The repository examples are built using the configured libtorch directory.
Guide limitations
- The README’s Linux guidance specifies cxx11 ABI packages and lists CPU and CUDA 11.7 builds.
- The guide documents building the examples, not running a particular model or verifying GPU operation.
Sources & review
Source record details
- Source availability
- Active
- Sources checked
- 14 October 2026
- Original website title
- LaurentMazare/ocaml-torch · Official homepage title · retained in its source language

