Skip to content

Sync #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 9, 2021
Merged

Sync #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v1
- name: Setup Java
uses: actions/setup-java@v1
with:
java-version: 8
- name: Build project
run: |
git --version
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ __pycache__
cmake_build/
tensorflow/contrib/cmake/_build/
.idea/**
.run
/build/
[Bb]uild/
/tensorflow/core/util/version_info.cc
Expand Down
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,14 @@ bazel-out/k8-opt/bin/external/org_tensorflow/tensorflow/libtensorflow_cc.so --ou
```

(called in `tensorflow-core-api`).

## Adding Gradients

In some cases, a op supported by Tensorflow Java will not have a gradient defined, resulting in errors like this:
```
org.tensorflow.exceptions.TensorFlowException: No gradient defined for op: ReadVariableOp. Please see https://www.tensorflow.org/code/tensorflow/cc/gradients/README.md for instructions on how to add C++ gradients.
at org.tensorflow.internal.c_api.AbstractTF_Status.throwExceptionIfNotOK(AbstractTF_Status.java:101)
at org.tensorflow.Graph.addGradients(Graph.java:708)
at org.tensorflow.Graph.addGradients(Graph.java:291)
```
The description in the [linked file](https://www.tensorflow.org/code/tensorflow/cc/gradients/README.md) are accurate for adding C++ Graph gradients, which are used by our `Graph`. Eexamples of doing that are [tensorflow/tensorflow#46115](https://github.com/tensorflow/tensorflow/pull/46115) and [tensorflow/tensorflow#47774](https://github.com/tensorflow/tensorflow/pull/47774). However, Tensorflow Core is in the process of migrating gradient definitions to [`c/experimental/gradients`](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/c/experimental/gradients), which will be what our eager mode uses once it has gradient support. Anyone adding gradients is strongly encouraged to add one there as well, and eventually it should replace the legacy `cc/gradients` gradients.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ systems, you should add the following dependencies:
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
<classifier>linux-x86_64${javacpp.platform.extension}</classifier>
</dependency>
```
Expand All @@ -77,24 +77,24 @@ native dependencies as follows:
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
<classifier>linux-x86_64${javacpp.platform.extension}</classifier>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
<classifier>macosx-x86_64${javacpp.platform.extension}</classifier>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-api</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
<classifier>windows-x86_64${javacpp.platform.extension}</classifier>
</dependency>
```
Expand All @@ -107,7 +107,7 @@ artifact includes transitively all the artifacts above as a single dependency:
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-platform${javacpp.platform.extension}</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
</dependency>
```

Expand Down Expand Up @@ -150,6 +150,7 @@ This table shows the mapping between different version of TensorFlow for Java an
| ------------- | ------------- |
| 0.2.0 | 2.3.1 |
| 0.3.0 | 2.4.1 |
| 0.3.1 | 2.4.1 |
| 0.4.0-SNAPSHOT | 2.4.1

## How to Contribute?
Expand Down
18 changes: 18 additions & 0 deletions docs/_toc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==============================================================================
toc:
- title: Install
path: /jvm/install
52 changes: 52 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# TensorFlow for Java

<table class="tfo-notebook-buttons" align="left">
<td>
<a target="_blank" href="https://www.tensorflow.org/jvm"><img src="https://www.tensorflow.org/images/tf_logo_32px.png" />View on TensorFlow.org</a>
</td>
<td>
<a target="_blank" href="https://github.com/tensorflow/java"><img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" />View GitHub repository</a>
</td>
</table>

TensorFlow Java can run on any JVM for building, training and running machine learning models. It comes with
a series of utilities and frameworks that help achieve most of the tasks common to data scientists
and developers working in this domain. Java and other JVM languages, such as Scala or Kotlin, are
frequently used in small-to-large enterprises all over the world, which makes TensorFlow a strategic
choice for adopting machine learning at a large scale.

## The Repository

In the early days, the Java language bindings for TensorFlow were hosted in the
[main TensorFlow repository](https://github.com/tensorflow/tensorflow)
and released only when a new version of the core library was ready to be distributed, which happens only
a few times a year. Now, all Java-related code has been moved to this repository so that it can evolve and
be released independently from official TensorFlow releases. In addition, most of the build tasks have been
migrated from Bazel to Maven, which is more familiar for most Java developers.

The following describes the layout of the repository and its different artifacts:

* [tensorflow-core](https://github.com/tensorflow/java/tree/master/tensorflow-core)
* All artifacts that build up the core language bindings of TensorFlow for Java
* Intended audience: projects that provide their own APIs or frameworks on top of
TensorFlow and just want a thin layer to access the TensorFlow runtime from the JVM

* [tensorflow-framework](https://github.com/tensorflow/java/tree/master/tensorflow-framework)
* Primary API for building and training neural networks with TensorFlow
* Intended audience: neural network developers

* [ndarray](https://github.com/tensorflow/java/tree/master/ndarray)
* Generic utility library for n-dimensional data I/O operations
* Used by TensorFlow but does not depend on TensorFlow
* Intended audience: any developer who needs a Java n-dimensional array implementation, whether or not they
use it with TensorFlow


## Communication

This repository is maintained by TensorFlow JVM Special Interest Group (SIG). You can easily join the group
by subscribing to the [[email protected]](https://groups.google.com/a/tensorflow.org/forum/#!forum/jvm)
mailing list, or you can simply send pull requests and raise issues to this repository.
There is also a [sig-jvm Gitter channel](https://gitter.im/tensorflow/sig-jvm).


219 changes: 219 additions & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# Install TensorFlow Java

[TensorFlow Java](https://github.com/tensorflow/java) can run on any JVM for
building, training and deploying machine learning models. It supports both CPU
and GPU execution, in graph or eager mode, and presents a rich API for using
TensorFlow in a JVM environment. Java and other JVM languages, like Scala and
Kotlin, are frequently used in large and small enterprises all over the world,
which makes TensorFlow Java a strategic choice for adopting machine learning at
a large scale.

Caution: The TensorFlow Java API is *not* covered by the TensorFlow
[API stability guarantees](../guide/versions.md).

## Requirements

TensorFlow Java runs on Java 8 and above, and supports out-of-the-box the
following platforms:

* Ubuntu 16.04 or higher; 64-bit, x86
* macOS 10.12.6 (Sierra) or higher; 64-bit, x86
* Windows 7 or higher; 64-bit, x86

*Note: To use TensorFlow on Android, see
[TensorFlow Lite](https://tensorflow.org/lite)*

## Versions

TensorFlow Java has its own release cycle, independent from the
[TensorFlow runtime](https://github.com/tensorflow/tensorflow). Consequently,
its version does not match the version of TensorFlow runtime it runs on. Consult
the TensorFlow Java
[versioning table](https://github.com/tensorflow/java/#tensorflow-version-support)
to list all versions available and their mapping with the TensorFlow runtime.

## Artifacts

There are
[several ways](https://github.com/tensorflow/java/#using-maven-artifacts) to add
TensorFlow Java to your project. The easiest one is to add a dependency on the
`tensorflow-core-platform` artifact, which includes both the TensorFlow Java
Core API and the native dependencies it requires to run on all supported
platforms.

You can also select one of the following extensions instead of the pure CPU
version:

* `tensorflow-core-platform-mkl`: Support for Intel® MKL-DNN on all platforms
* `tensorflow-core-platform-gpu`: Support for CUDA® on Linux and Windows
platforms
* `tensorflow-core-platform-mkl-gpu`: Support for Intel® MKL-DNN and CUDA® on
Linux platform.

In addition, a separate dependency on the `tensorflow-framework` library can be
added to benefit from a rich set of utilities for TensorFlow-based machine
learning on the JVM.

## Installing with Maven

To include TensorFlow in your [Maven](http://maven.apache.org) application, add
a dependency on its [artifacts](#artifacts) to your project's `pom.xml` file.
For example,

```xml
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-platform</artifactId>
<version>0.3.1</version>
</dependency>
```

### Reducing Number of Dependencies

It is important to note that adding a dependency on a `tensorflow-core-platform`
artifact will import native libraries for all supported platforms, which can
significantly increase the size of your project.

If you wish to target a subset of the available platforms then you can exclude
the unnecessary artifacts from the other platforms using the
[Maven Dependency Exclusion](https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html#dependency-exclusions)
feature.

Another way to select which platforms you want to include in your application is
to set JavaCPP system properties, in your Maven command line or in your
`pom.xml`. Please see JavaCPP
[documentation](https://github.com/bytedeco/javacpp-presets/wiki/Reducing-the-Number-of-Dependencies)
for more details.

### Using Snapshots

The latest TensorFlow Java development snapshots from the TensorFlow Java source
repository are available on the [OSS Sonatype](https://oss.sonatype.org) Nexus
repository. To depend on these artifacts, make sure to configure the OSS
snapshots repository in your `pom.xml`.

```xml
<repositories>
<repository>
<id>tensorflow-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-platform</artifactId>
<version>0.4.0-SNAPSHOT</version>
</dependency>
</dependencies>
```

## Installing with Gradle

To include TensorFlow in your [Gradle](https://gradle.org) application, add a
dependency on its [artifacts](#artifacts) to your project's `build.gradle` file.
For example,

```groovy
repositories {
mavenCentral()
}

dependencies {
compile group: 'org.tensorflow', name: 'tensorflow-core-platform', version: '0.3.1'
}
```

### Reducing Number of Dependencies

Excluding native artifacts from TensorFlow Java with Gradle is not as easy as
with Maven. We recommend that you use Gradle JavaCPP plugins to reduce this
number of dependencies.

Please read at Gradle JavaCPP
[documentation](https://github.com/bytedeco/gradle-javacpp) for more details.

## Installing from Sources

To build TensorFlow Java from sources, and possibly customize it, please read
the following
[instructions](https://github.com/tensorflow/java/blob/master/CONTRIBUTING.md#building).

*Note: Only official builds distributed by TensorFlow are supported by its
maintainers and custom builds should be used at the user's risk.*

## Example Program

This example shows how to build an Apache Maven project with TensorFlow. First,
add the TensorFlow dependency to the project's `pom.xml` file:

```xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.myorg</groupId>
<artifactId>hellotensorflow</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<exec.mainClass>HelloTensorFlow</exec.mainClass>
<!-- Minimal version for compiling TensorFlow Java is JDK 8 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<!-- Include TensorFlow (pure CPU only) for all supported platforms -->
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-platform</artifactId>
<version>0.3.1</version>
</dependency>
</dependencies>
</project>
```

Create the source file `src/main/java/HelloTensorFlow.java`:

```java
import org.tensorflow.ConcreteFunction;
import org.tensorflow.Signature;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;
import org.tensorflow.op.Ops;
import org.tensorflow.op.core.Placeholder;
import org.tensorflow.op.math.Add;
import org.tensorflow.types.TInt32;

public class HelloTensorFlow {

public static void main(String[] args) throws Exception {
System.out.println("Hello TensorFlow " + TensorFlow.version());

try (ConcreteFunction dbl = ConcreteFunction.create(HelloTensorFlow::dbl);
TInt32 x = TInt32.scalarOf(10);
Tensor dblX = dbl.call(x)) {
System.out.println(x.getInt() + " doubled is " + ((TInt32)dblX).getInt());
}
}

private static Signature dbl(Ops tf) {
Placeholder<TInt32> x = tf.placeholder(TInt32.class);
Add<TInt32> dblX = tf.math.add(x, x);
return Signature.builder().input("x", x).output("dbl", dblX).build();
}
}
```

Compile and execute:

<pre class="devsite-terminal prettyprint lang-bsh">
mvn -q compile exec:java
</pre>

The command prints TensorFlow version and a simple calculation.

Success! TensorFlow Java is configured.
2 changes: 1 addition & 1 deletion ndarray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ To import the NdArray library in your project, simply add the following dependen
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>ndarray</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
</dependency>
```

Expand Down
Loading