Git Product home page Git Product logo

torch's Introduction

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 v2.1.

Installation with Libtorch

Torch depends on libtorch, so when you 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 torch.

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/janestreet/torch.git
cd torch
make all

Option 1: OPAM switch (CPU only)

The opam libtorch package (an optional dependency) can be installed, and torch will automatically detect it and build with it. However, it might not suit your needs if you use any of these:

  • Windows operating system,
  • ARM processors, or
  • 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), set LIBTORCH_USE_SYSTEM=1 before installing.

Option 4: Custom Libtorch Location

If you have downloaded libtorch somewhere, set 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

Internals

ocaml-torch uses extensive code generation to produce bindings to thousands of torch C++ functions. Read internals.md for details.

Acknowledgements

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

torch's People

Contributors

andreslavescu avatar public-release avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

torch's Issues

Tensor.argmax behaves differently than its Python counterpart

I've compiled this repo's current master (93ed82c) using OCaml 4.14.1 on an AMD64 system running (K)ubuntu 23.10. I've used what the README refers to as "Option 3" for installation, ie, using the system libraries. The corresponding distro packages are libtorch-dev (version 2.0.1+dfsg-4) and libtorch2.0:amd64 (version 2.0.1+dfsg-4). Note that therefore the OPAM package libtorch is not installed.

Consider the following Python REPL session (using the distro package python3-torch version 2.0.1+dfsg-4):

>>> import torch
>>> t = torch.tensor([ [ 10, 20, 30 ], [ 60, 50, 40 ] ])
>>> torch.argmax(t, dim=1)
tensor([2, 0])

And a similar session using Utop; note the discrepancy with the invocation of Tensor.argmax:

utop # open Torch;;
utop # let t = Tensor.of_float2 [| [| 10.; 20.; 30. |]; [| 60.; 50.; 40. |] |];;
val t : Tensor.t = 
 10  20  30
 60  50  40
[ CPUFloatType{2,3} ]
utop # Tensor.argmax ~dim:1 t;;
- : Tensor.t = 
3
[ CPULongType{} ]

Failing to build `v0.17~preview.129.17+77`

Hello,

I am trying to build the preview package v0.17~preview.129.17+77 of the torch library on my Linux machine. I have locally installed libtorch.2.1.2+linux-x86_64 using opam.

During the build process, I encountered the following error:

File "src/wrapper/dune", line 4, characters 9-18: 
4 |   (names torch_api)
             ^^^^^^^^^

torch_api.cpp:944:10: fatal error: torch_api_generated.cpp: No such file or directory
  944 | #include "torch_api_generated.cpp"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

I haven't tried with other version of libtorch, since this version seems to be the only available that satisfies the conflicts indications from torch.opam.

conflicts: [
  "libtorch" {< "2.1.0" | >= "2.2.0"}
]

Any help would be greatly appreciated, including workarounds if you know of any.

Thank you for your time and assistance!

Support for MacOS Arm64 (M1 and M2 series chips)

Hi,

I am trying to compile with dune a basic program with a Mac Studio (M1 Pro) using torch/libtorch installed with opam and get errors that seem to point to incorrect support:

ld: warning: ignoring file /Users/rival/.opam/default/lib/libtorch/lib/libc10.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64 ld: warning: ignoring file /Users/rival/.opam/default/lib/libtorch/lib/libtorch_cpu.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64 ld: warning: ignoring file /Users/rival/.opam/default/lib/libtorch/lib/libtorch.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64

This is followed by a very large number of errors of the form "Undefined symbols for architecture arm64", all related to libtorch.

I understand the errors come from the libtorch opam component, but the libtorch page says the development is now moved here.

Can libtorch/torch run on MacOS Arm ? Are they supported for this architecture ? Do we have to take extra steps when installing and using torch on this architecture ? Thanks!

Xavier

Potential addition of tracking issue for torch.nn mappings?

Hi, I'm a recent contributor (PR can be found here: #4) to this awesome project, and had an idea for having a torch.nn layer mapping tracking issue.
Would this be something on the roadmap for this project's development? Would love some feedback for the idea. Below is a skeleton example of how it might look like:

torch.nn layer mappings

files: layer.ml, layer.mli

convolutional layers

  • conv1d
  • conv2d
  • conv3d
  • conv1d_transpose
  • conv2d_transpose
  • conv3d_transpose

normalization layers

  • batch_norm2d
  • instance_norm2d
  • layer_norm
  • group_norm

non-linear activations

  • Relu
  • Leaky Relu
  • Tanh

etc.

Open to any feedback on this idea.

Request: Update LibTorch library

Hey! Just playing around with the repo and I thought this little bit could be useful to somebody. Would it be possible to update the included LibTorch library or perhaps instructions?

I am on NVIDIA 12.2 and had to download the LibTorch c++ version to the latest nightly build (cuda 12.1) to get opam to install torch:
https://download.pytorch.org/libtorch/nightly/cu121/libtorch-cxx11-abi-shared-with-deps-latest.zip

Previously, it was failing for me but once I changed it over to this library, it installed.

undefined symbol: ffi_type_void, version LIBFFI_BASE_7.0

Hi,
When trying the example in README:

open Torch

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

I encountered the following error:

_build/install/default/bin/torch_example: symbol lookup error: _build/install/default/bin/torch_example: undefined symbol: ffi_type_void, version LIBFFI_BASE_7.0

The package is installed with:

opam install torch

And the versions of all the dependencies are:

The following actions will be performed:
  \u2217 install ppx_derivers                1.2.1
          [required by ppxlib]
  \u2217 install typerep                     v0.16.0             [required by core]
  \u2217 install bigarray-compat             1.1.0
          [required by ctypes]
  \u2217 install variantslib                 v0.16.0             [required by core]
  \u2217 install ocaml-compiler-libs         v0.12.4             [required by torch]
  \u2217 install conf-pkg-config             3
          [required by ctypes-foreign]
  \u2217 install libtorch                    1.13.0+linux-x86_64 [required by torch]
  \u2217 install fieldslib                   v0.16.0             [required by core]
  \u2217 install integers                    0.7.0
          [required by ctypes]
  \u2217 install jane-street-headers         v0.16.0             [required by core]
  \u2217 install num                         1.4
          [required by sexplib]
  \u2217 install parsexp                     v0.16.0
          [required by sexplib]
  \u2217 install ppxlib                      0.31.0
          [required by ppx_jane]
  \u2217 install conf-libffi                 2.0.0
          [required by ctypes-foreign]
  \u2217 install ctypes                      0.21.1              [required by torch]
  \u2217 install sexplib                     v0.16.0             [required by core]
  \u2217 install ppx_variants_conv           v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_typerep_conv            v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_tydi                    v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_stable_witness          v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_stable                  v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_sexp_conv               v0.16.0             [required by core]
  \u2217 install ppx_pipebang                v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_optional                v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_optcomp                 v0.16.0             [required by core]
  \u2217 install ppx_ignore_instrumentation  v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_here                    v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_globalize               v0.16.0
          [required by ppx_base]
  \u2217 install ppx_fixed_literal           v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_fields_conv             v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_enumerate               v0.16.0
          [required by ppx_base]
  \u2217 install ppx_disable_unused_warnings v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_compare                 v0.16.0
          [required by ppx_base, bin_prot]
  \u2217 install ppx_cold                    v0.16.0
          [required by ppx_base]
  \u2217 install ctypes-foreign              0.21.1              [required by torch]
  \u2217 install ppx_custom_printf           v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_sexp_value              v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_sexp_message            v0.16.0             [required by core]
  \u2217 install ppx_let                     v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_hash                    v0.16.0             [required by core]
  \u2217 install ppx_assert                  v0.16.0             [required by core]
  \u2217 install bin_prot                    v0.16.0             [required by core]
  \u2217 install ppx_log                     v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_base                    v0.16.0             [required by core]
  \u2217 install jst-config                  v0.16.0             [required by core]
  \u2217 install ppx_bin_prot                v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_string                  v0.16.0
          [required by ppx_jane]
  \u2217 install time_now                    v0.16.0             [required by core]
  \u2217 install ppx_module_timer            v0.16.0
          [required by ppx_jane]
  \u2217 install ppx_inline_test             v0.16.1             [required by torch]
  \u2217 install ppx_expect                  v0.16.0             [required by torch]
  \u2217 install ppx_bench                   v0.16.0             [required by torch]
  \u2217 install splittable_random           v0.16.0             [required by core]
  \u2217 install base_quickcheck             v0.16.0             [required by core]
  \u2217 install ppx_jane                    v0.16.0             [required by torch]
  \u2217 install int_repr                    v0.16.0
          [required by base_bigstring]
  \u2217 install base_bigstring              v0.16.0             [required by core]
  \u2217 install core                        v0.16.2             [required by torch]
  \u2217 install torch                       v0.16.0

And libffi-dev is installed through apt-get(I'm on ubuntu 20.04)

So what's going on? and how could I solve this to move on?

Thanks

`v0.17~preview.129.07+242` fails to build

I can build the previous version v0.17~preview.129.00+111 with either the libtorch on opam or my custom torch library. But there are a few problems with the latest v0.17~preview.129.07+242.

  1. npy listed in the bin/dune is not public. The one I can find https://github.com/LaurentMazare/npy-ocaml doesn't have module Npy_lib which bin/tensor_tools.ml will include.

I can pass the problem by commenting this dune file.

  1. Missing header file
File "src/wrapper/dune", line 4, characters 9-18:
4 |   (names torch_api)
             ^^^^^^^^^
torch_api.cpp:1:10: fatal error: torch/csrc/autograd/engine.h: No such file or directory
    1 | #include <torch/csrc/autograd/engine.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

This doesn't look like a fresh question.

It can be solved by set LIBTORCH_INCLUDE /path/to/torch/include (also need to set LIBTORCH).

  1. When both LIBTORCH and LIBTORCH_INCLUDE are set, they report on so files missing but I do have them.
$ dune build
File "src/wrapper/dune", line 1, characters 0-379:
 1 | (library
 2 |  (foreign_stubs
 3 |   (language cxx)
....
18 |  (libraries ctypes.foreign ctypes.stubs ctypes)
19 |  (preprocess
20 |   (pps ppx_jane)))
/usr/bin/ld: cannot find -l/home/ex/.opam/5.1.1/lib/libtorch/lib/libc10.so: No such file or directory
/usr/bin/ld: cannot find -l/home/ex/.opam/5.1.1/lib/libtorch/lib/libtorch_cpu.so: No such file or directory
/usr/bin/ld: cannot find -l/home/ex/.opam/5.1.1/lib/libtorch/lib/libtorch.so: No such file or directory
collect2: error: ld returned 1 exit status

I am unclear about this error. I tried both libtorch=2.1.2+linux-x86_64 on opam or my custom torch. The error message is the same.

$ ls /home/ex/.opam/5.1.1/lib/libtorch/lib/*.so
/home/ex/.opam/5.1.1/lib/libtorch/lib/libbackend_with_compiler.so*  /home/ex/.opam/5.1.1/lib/libtorch/lib/libtorchbind_test.so*
/home/ex/.opam/5.1.1/lib/libtorch/lib/libc10.so*                    /home/ex/.opam/5.1.1/lib/libtorch/lib/libtorch_cpu.so*
/home/ex/.opam/5.1.1/lib/libtorch/lib/libjitbackend_test.so*        /home/ex/.opam/5.1.1/lib/libtorch/lib/libtorch_global_deps.so*
/home/ex/.opam/5.1.1/lib/libtorch/lib/libnnapi_backend.so*          /home/ex/.opam/5.1.1/lib/libtorch/lib/libtorch_python.so*
/home/ex/.opam/5.1.1/lib/libtorch/lib/libshm.so*                    /home/ex/.opam/5.1.1/lib/libtorch/lib/libtorch.so*

Current master fails to build

I've tried to compile this repo's current master (b1ee8db) using OCaml 4.14.1 on an AMD64 system running (K)ubuntu 23.10. I've used what the README refers to as "Option 3" for installation, ie, using the system libraries. The corresponding distro packages are libtorch-dev (version 2.0.1+dfsg-4) and libtorch2.0:amd64 (version 2.0.1+dfsg-4). Note that therefore the OPAM package libtorch is not installed.

The compilation fails with the following error:

torch_api_generated.cpp.h: In function ‘void atg_var_mean_correction_out(at::Tensor**, tensor, tensor, tensor, int64_t*, int, scalar, int)’:
torch_api_generated.cpp.h:18062:174: error: could not convert ‘* correction’ from ‘c10::Scalar’ to ‘c10::optional<long int>’
18062 |     auto outputs__ = torch::var_mean_out(*out0, *out1, *self, dim_data == nullptr ? c10::nullopt : c10::optional<torch::IntArrayRef>(torch::IntArrayRef(dim_data, dim_len)), *correction, (bool)keepdim);
      |                                                                                                                                                                              ^~~~~~~~~~~
      |                                                                                                                                                                              |
      |                                                                                                                                                                              c10::Scalar
torch_api.h:15:5: note: in definition of macro ‘PROTECT’
   15 |     x                                                                          \
      |     ^

Also, I'm a bit puzzled by the inclusion of file third_party/pytorch/Declarations-v1.13.0.yaml in the repo, even though src/gen/gen.ml references third_party/pytorch/Declarations-v2.1.0.yaml.

Suggest to add a function to convert from `float Scalar.t` to `float`

It's easy to put an OCaml primitive value into Scalar e.g. Scalar.float: float -> float Scalar.t.
However, I don't figure out a simple way to unbox it.

A workaround is using a Tensor as an intermediate step but it doesn't look elegant.
let float_of_scalar s = Tensor.(add_scalar (zeros [ 1 ]) s |> float_value).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.