The JNI bindings are built with clang++ for Mac (x86 and arm64) and for Linux (x86).
The build runs on a Mac and uses Docker for cross compilation.
The docker image used for building (and possible at runtime) is defined in docker-runtime/Dockerfile
. This image is pushed to the Docker Hub (https://hub.docker.com/r/pityka/base-ubuntu-libtorch/tags?page=1&ordering=last_updated), and by default the build will pull it.
- needs a macs with clang++ installed and set up for cross compiling.
- needs docker on the mac
- on the mac needs the libtorch shared libraries in /usr/local/lib/. Read on.
- needs bloop (https://scalacenter.github.io/bloop/) and sbt (https://www.scala-sbt.org/).
make prepare
- this creates bloop build definitions from sbt the sbt build definition- Copy from one to the other
make test
make test-linux
- builds for linux and runs tests in a linux container (needs docker for mac, will pullpityka/base-ubuntu-libtorch
from docker hub)make test-cuda
- runs tests in a cuda enabled remote docker context
make publishLocal
- publishes artifacts locallymake publish
- publishes artifacts to github packages.bash publish.sh
- publishes the artifacts to Maven Central
The Makefile will generate Java and C++ sources and create a JNI native library.
parser/
holds a parser for parseable.h
which is a subset of the libtorch/include/ATen/Functions.h
. This parser generates cpp (./wrapper.cpp
) and java JNI code (./aten-scala/core/src/main/java/ATen.java
). The cpp code is compiled to ./aten-scala/jni-osx/src/main/resources/libatenscalajni.dylib
.
aten-scala/
holds an sbt project with two subprojects. aten-scala/core
is the Java counterpart of the binding. It depends on Scala because std::tuple
is translated into Scala tuples. aten-scala/jni-osx
contains no source code but its Maven artifact contains the native library.
Libtorch is linked dynamically during runtime.
On OSX the jni shared library always looks for libtorch in /usr/local/lib/
.
You can get the libtorch libraries with pip3 install torch
then copy the necessary files to /usr/local/lib/. E.g. cp /usr/local/lib/python3.9/site-packages/torch/lib/* /usr/local/lib/
.
However one needs to copy /usr/local/lib/python3.9/site-packages/torch/.dylibs/libomp.dylib
as well to /usr/local/.dylibs/
(depends on the libtorch version).
On Linux there is no @rpath baked in the jni shared library.
Use LD_LIBRARY_PATH to make sure the linker finds libtorch.
See the docker-runtime/Dockerfile
s on how to do this.
See docker-runtime/Dockerfile
. This image is pushed to the Docker Hub (https://hub.docker.com/r/pityka/base-ubuntu-libtorch/tags?page=1&ordering=last_updated).
The image also has Java 8 and sbt. It is ready to make use of this library on Linux.
See LICENSE file.