Skip to content

vthemelis/torch

 
 

Repository files navigation

ocaml-torch

ocaml-torch provides OCaml bindings for the PyTorch tensor library. This brings to OCaml NumPy-like tensor computations with GPU acceleration and tape-based automatic differentiation.

These bindings use the PyTorch C++ API and are mostly automatically generated. The current GitHub tip corresponds to PyTorch v1.13.0.

Installation with Libtorch

Torch depends on libtorch, so when you opam install this package, it will try linking to libtorch depending on your environment variables. The code for discovering libtorch is in src/config/discover.ml. In order to change how torch binds to libtorch, you must uninstall and reinstall libtorch.

To install with any of these methods, after configuring your environment, you may either

  • opam install torch, or
  • build from source:
git clone https://github.com/LaurentMazare/ocaml-torch.git
cd ocaml-torch
make all

On Linux note that you will need the libtorch PyTorch version using the appropriate cxx11 abi depending on your g++ version.

Option 1: OPAM switch (CPU only)

The opam libtorch package will be automatically installed as a dependency and requires no special environment variables. However, it is CPU-only, so will not support GPUs.

Option 2: Conda

If you've installed libtorch via Conda, ensure that you are in the Conda environment with the CONDA_PREFIX set before installing.

Option 3: System Libraries

If you have libtorch installed as a system library (e.g. RPM), run export LIBTORCH_USE_SYSTEM=1 before installing.

Option 4: Custom Libtorch Location

If you have downloaded libtorch somewhere, run export LIBTORCH=/path/to/libtorch/ before installing.

Examples

Utop

ocaml-torch can be used in interactive mode via utop or ocaml-jupyter.

Here is a sample utop session:

utop

Simple Script

To build a simple torch program, create a file example.ml:

open Torch

let () =
  let tensor = Tensor.randn [ 4; 2 ] in
  Tensor.print tensor

Then create a dune file with the following content:

(executables
  (names example)
  (libraries torch))

Run dune exec example.exe to compile the program and run it!

Alternatively you can first compile the code via dune build example.exe then run the executable _build/default/example.exe (note that building the bytecode target example.bc may not work on macos).

Demos

Some more advanced applications from external repos:

Models and Weights

Various pre-trained computer vision models are implemented in the vision library. The weight files can be downloaded at the following links:

Running the pre-trained models on sample images can the easily be done via:

dune exec examples/pretrained/predict.exe path/to/resnet18.ot images/tiger.jpg

Acknowledgements

Many thanks to @LaurentMazare for the original work of ocaml-torch.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • OCaml 51.2%
  • C++ 24.9%
  • C 23.7%
  • Other 0.2%