Skip to content

Commit

Permalink
Added documentation for generating C wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
stemann committed Aug 2, 2023
1 parent 4ee07f3 commit cff0cd7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
54 changes: 54 additions & 0 deletions deps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# C Wrapper

Since Torch is a C++-library, a C wrapper is needed for Julia to interact with Torch.

## Generating

The C wrapper can be generated from the `Declarations.yaml`-file included with `Torch_jll`:
```sh
mkdir c_wrapper_generator/data
cp -v `julia --eval '
using Pkg
Pkg.activate(; temp=true)
Pkg.add(name="Torch_jll", version="1.10")
import Torch_jll
print(joinpath(dirname(Torch_jll.libtorch_path), "..", "share", "ATen", "Declarations.yaml"))
'` c_wrapper_generator/data/
```

The C wrapper can then be generated by building and running the (OCaml-based) C wrapper generator, e.g. by using the dev. container (which includes OCaml and OPAM):
```sh
cd c_wrapper_generator
opam install -y . --deps-only
opam exec -- dune build
_build/default/bin/main.exe
```
or by using an OCaml-container:
```sh
docker run -it --rm -v `pwd`:/workspace -w /workspace ocaml/opam:debian-11-ocaml-4.12 bash -c '
cd c_wrapper_generator
opam install -y . --deps-only
opam exec -- dune build
_build/default/bin/main.exe
'
```

## Building

The C wrapper can be built given that we can provide the paths to working Torch and CUDA/ CUDNN projects. The binaries can also be downloaded from the [official libtorch binaries](https://pytorch.org/get-started/locally/), which is what the wrapper is based on. This currently supports torch v1.4.0.

```sh
cd c_wrapper
mkdir build && cd build

# With a working torch install via Python (or similar): setting the CMAKE_PREFIX_PATH to point there might be sufficient
CMAKE_PREFIX_PATH=$HOME/.local/lib/python3.6/site-packages/torch\
CUDNN_LIBRARY_PATH=$HOME/cuda/lib64\
CUDNN_INCLUDE_PATH=$HOME/cuda/include\
CUDNN_INCLUDE_DIR=$HOME/cuda/include\
cmake ..

cmake --build .
```

Post this, adding the path to the project via the `LD_LIBRARY_PATH` (and also the CUDNN) binary path might be needed.
18 changes: 0 additions & 18 deletions deps/c_wrapper/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion deps/c_wrapper_generator/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,4 @@ let run ~yaml_filename ~cpp_filename =
let () =
run
~yaml_filename:"data/Declarations.yaml"
~cpp_filename:"src/wrapper/torch_api_generated"
~cpp_filename:"../c_wrapper/torch_api_generated"

0 comments on commit cff0cd7

Please sign in to comment.