ekrich / stensorflow

Scala Native support for the TensorFlow C API on Linux and macOS

Version Matrix

stensorflow - Scala Native TensorFlow

CI

This library implements the C TensorFlow API adapted for the Scala Native platform.

Scala Native is a unique platform that marries the high level language of Scala but compiles to native code with a lightweight managed runtime which includes a state of the art garbage collector. The combination allows for great programming and the ability to use high performance C language libraries like TensorFlow.

Scala Native uses the Scala compiler to produce NIR (Native Intermediate Representation) that is optimized and then converted to LLVM IR. Finally LLVM code is optimized and compiled by Clang to produce a native executable.

Getting started

Maven Central

If you are already familiar with Scala Native you can jump right in by adding the following dependency in your sbt build file.

libraryDependencies += "org.ekrich" %%% "stensorflow" % "x.y.z"

To use in sbt, replace x.y.z with the version from Maven Central badge above. All available versions can be seen at the Maven Repository.

Otherwise follow the Getting Started instructions for Scala Native if you are not already setup.

Additional libraries

The TensorFlow C library is required and the current version is 2.5.0.

  • Linux/Ubuntu can TensorFlow following the following directions:

https://www.tensorflow.org/install/lang_c

Essentially do the following for this platform and version: x84_64-2.5.0:

$ curl -fsSL https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.5.0.tar.gz \
    | tar -xz -C /usr/local
  • macOS can install TensorFlow using Homebrew which will install into the /usr/local/Cellar/libtensorflow/<version> directory.
$ brew install libtensorflow
  • Other OSes need to have libtensorflow available on the system.

Usage and Help

scaladoc Join chat https://gitter.im/ekrich/stensorflow

Reference the link above for Scaladoc. The documentation is a little sparse but hopefully will improve with time.

After sbt is installed and any other Scala Native prerequisites are met you can use the following Gitter G8 template instructions to get a fully functional Scala Native application with an example in the body of the main program.

$ sbt new ekrich/stensorflow.g8
$ cd <directory entered after the prompt>
$ sbt run

In addition, look at the stensorflow unit tests for other examples of usage.

TensorFlow References and External Documentation

TensorFlow Website
TensorFlow for JVM using JNI

Versions

Release 0.1.0 - (2021-07-02)