Skip to content

Latest commit

 

History

History

scala-package

MXNet Package for Scala/Java

The MXNet Scala/Java Package brings flexible and efficient GPU/CPU computing and state-of-art deep learning to JVM.

  • It enables you to write seamless tensor/matrix computation with multiple GPUs in Scala, Java and other languages built on JVM.
  • It also enables you to construct and customize the state-of-art deep learning models in JVM languages, and apply them to tasks such as image classification and data science challenges.
  • The Scala/Java Inferece APIs provides an easy out of the box solution for loading pre-trained MXNet models and running inference on them.

Pre-Built Maven Packages

Stable

The MXNet Scala/Java packages can be easily included in your Maven managed project. The stable jar files for the packages are available on the MXNet Maven Package Repository Currently we provide packages for Linux (Ubuntu 16.04) (CPU and GPU) and macOS (CPU only). Stable packages for Windows and CentOS will come soon. For now, if you have a CentOS machine, follow the Build From Source section below.

To add MXNet Scala/Java package to your project, add the dependency as shown below corresponding to your platform, under the dependencies tag in your project's pom.xml :

Linux GPU

maven badge

<dependency>
  <groupId>org.apache.mxnet</groupId>
  <artifactId>mxnet-full_2.11-linux-x86_64-gpu</artifactId>
  <version>[1.3.1,)</version>
</dependency>

Linux CPU

maven badge

<dependency>
  <groupId>org.apache.mxnet</groupId>
  <artifactId>mxnet-full_2.11-linux-x86_64-cpu</artifactId>
  <version>[1.3.1,)</version>
</dependency>

macOS CPU

maven badge

<dependency>
  <groupId>org.apache.mxnet</groupId>
  <artifactId>mxnet-full_2.11-osx-x86_64-cpu</artifactId>
  <version>[1.3.1,)</version>
</dependency>

Note: <version>[1.3.1,)<\version> indicates that we will fetch packages with version 1.3.1 or higher. This will always ensure that the pom.xml is able to fetch the latest and greatest jar files from Maven.

Nightly

Apart from these, the nightly builds representing the bleeding edge development on Scala/Java packages are also available on the MXNet Maven Nexus Package Repository. Currently we provide nightly packages for Linux (CPU and GPU) and MacOS (CPU only). The Linux nightly jar files also work on CentOS. Nightly packages for Windows will come soon.

Add the following repository to your project's pom.xml file :

<repositories>
    <repository>
      <id>Apache Snapshot</id>
      <url>https://repository.apache.org/content/groups/snapshots</url>
    </repository>
</repositories>

Also, add the dependency which corresponds to your platform to the dependencies tag :

Linux GPU

maven badge

<dependency>
  <groupId>org.apache.mxnet</groupId>
  <artifactId>mxnet-full_2.11-linux-x86_64-gpu</artifactId>
  <version>[1.5.0-SNAPSHOT,)</version>
</dependency>

Linux CPU

maven badge

<dependency>
  <groupId>org.apache.mxnet</groupId>
  <artifactId>mxnet-full_2.11-linux-x86_64-cpu</artifactId>
  <version>[1.5.0-SNAPSHOT,)</version>
</dependency>

macOS CPU

maven badge

<dependency>
  <groupId>org.apache.mxnet</groupId>
  <artifactId>mxnet-full_2.11-osx-x86_64-cpu</artifactId>
  <version>[1.5.0-SNAPSHOT,)</version>
</dependency>

Note: <version>[1.5.0-SNAPSHOT,)</version> indicates that we will fetch packages with version 1.5.0 or higher. This will always ensure that the pom.xml is able to fetch the latest and greatest jar files from Maven Snapshot repository.

Build From Source

Checkout the Installation Guide contains instructions to install mxnet package and build it from source. Scala maven build assume you already have a lib/libmxnet.so file. If you have built MXNet from source and are looking to setup Scala from that point, you may simply run the following from the MXNet source root, Scala build will detect your platform (OSX/Linux) and libmxnet.so flavor (CPU/GPU):

cd scala-package
mvn install

You can also run the unit tests and integration tests on the Scala Package by :

cd scala-package
mvn integration-test -DskipTests=false

Or run a subset of unit tests, for e.g.,

cd scala-package
mvn -Dsuites=org.apache.mxnet.NDArraySuite integration-test

If everything goes well, you will find jars for assembly, core and example modules. Also it produces the native library in native/target, which you can use to cooperate with the core module.

Deploy to repository

By default, maven deploy will deploy artifacts to local file system, you can file then in: scala-package/deploy/target/repo folder.

For nightly build in CI, a snapshot build will be uploaded to apache repository with follow command:

cd scala-package
mvn deploy -Pnightly

Use following command to deploy release build (push artifacts to apache staging repository):

cd scala-package
mvn deploy -Pstaging

Examples & Usage

Assuming you use mvn install, you can find the mxnet-full_scala_version-INTERNAL.jar e.g. mxnet-full_2.11-INTERNAL.jar under the path incubator-mxnet/scala-package/assembly/target.

Adding the following configuration in pom.xml

<dependency>
  <groupId>org.apache.mxnet</groupId>
  <artifactId>mxnet-full_2.11-INTERNAL</artifactId>
  <version>1.5.0</version>
  <scope>system</scope>
  <systemPath>path_to_jar/mxnet-full_2.11-INTERNAL.jar</systemPath>
</dependency>

If you have following error message

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/mxnet/NDArray
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
        at java.lang.Class.getMethod0(Class.java:3018)
        at java.lang.Class.getMethod(Class.java:1784)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.mxnet.NDArray
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Please make sure your $CLASSPATH is able to find mxnet-full_scala_version-INTERNAL.jar.

  • To set up the Scala Project using IntelliJ IDE on macOS follow the instructions here.
  • Several examples on using the Scala APIs are provided in the Scala Examples Folder

Scala Training APIs

  • Module API : The Module API provides an intermediate and high-level interface for performing computation with neural networks in MXNet. Modules provide high-level APIs for training, predicting, and evaluating.

  • KVStore API : To run training over multiple GPUs and multiple hosts, one can use the KVStore API.

  • IO/Data Loading : MXNet Scala provides APIs for preparing data to feed as an input to models. Check out Data Loading API for more info.

Other available Scala APIs for training can be found here.

Scala Inference APIs

The Scala Inference APIs provide an easy, out of the box solution to load a pre-trained MXNet model and run inference on it. The Inference APIs are present in the Infer Package under the MXNet Scala Package repository, while the documentation for the Infer API is available here.

Java Inference APIs

The Java Inference APIs also provide an easy, out of the box solution to load a pre-trained MXNet model and run inference on it. The Inference APIs are present in the Infer Package under the MXNet Scala Package repository, while the documentation for the Infer API is available here. More APIs will be added to the Java Inference APIs soon.

JVM Memory Management

The Scala/Java APIs also provide an automated resource management system, thus making it easy to manage the native memory footprint without any degradation in performance. More details about JVM Memory Management are available here.

License

MXNet Scala Package is licensed under Apache-2 license.

MXNet uses some 3rd party softwares. Following 3rd party license files are bundled inside Scala jar file:

  • cub/LICENSE.TXT
  • mkldnn/external/mklml_mac_2019.0.1.20180928/license.txt