Skip to content
Merged
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
10 changes: 8 additions & 2 deletions tensorflow-core/tensorflow-core-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
<artifactId>jmh-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
Expand Down Expand Up @@ -96,7 +102,7 @@
</properties>
</profile>
</profiles>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -319,7 +325,7 @@
<configuration>
<classifier>${native.classifier}</classifier>
<skipIfEmpty>true</skipIfEmpty>
<includes>
<includes>
<!-- In case of successive builds for multiple platforms without cleaning, ensures
we only include files for this platform. -->
<include>org/tensorflow/internal/c_api/${native.classifier}/</include>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package org.tensorflow.device;

import javax.annotation.Nullable;

@Craigacp Craigacp Aug 6, 2020 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we get the Nullable annotation from? It's not in the JDK.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering as well, I assumed (maybe a bit too fast) that it was part of the JDK... ok, I'll remove them manually and merge back the file without it

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it was coming again from our dependency to the tensorflow-core-generator, which got it from com.google.code.findbugs, thanks for raising this @Craigacp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah I always thought @nullable was part of the JDK somewhere...
My bad for taking that for granted.
Are you folks already removing that?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. Can we remove the generator dependency somehow? It pulls in a lot of things.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'm a bit confused as to why jsr305 wasn't integrated into the JDK, but there was a talk about nullness annotations from Google at the last JVMLS discussing all the issues with integrating them into things, and it seemed to be pretty complex. Unfortunately that talk wasn't recorded.

@karllessard karllessard Aug 6, 2020 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the generator dependency somehow?

I was thinking of doing that as well but there is probably a good reason why it is there, probably maven was failing somehow to run the annotation processor without it. Will need to double check

So @hthu , it's all good now, I forced-pushed a new version without @Nullable. I also took the opportunity to move the DeviceSpec out of the device package. Since there was only one file in that package, I don't think we needed it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!
Thank you both!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the generator dependency somehow?

I was thinking of doing that as well but there is probably a good reason why it is there, probably maven was failing somehow to run the annotation processor without it. Will need to double check

I guess without the explicit dependency it's up to maven which one of the generator and the core api it compiles first. Maybe we could make it a test time dependency? That's even less clear in terms of the semantics, but it should make it run first.

import java.util.Objects;

/**
* Represents a (possibly partial) specification for a TensorFlow device.
*
* <p>This is a Java port from python device_spec.py.
*
* @see <a
* href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/device_spec.py">device_spec.py</a>.
*/
public final class DeviceSpec {
private static final String JOB_PREFIX = "/job:";
private static final String REPLICA_PREFIX = "/replica:";
private static final String TASK_PREFIX = "/task:";
private static final String DEVICE_PREFIX = "/device:";

public enum DeviceType {
CPU,
GPU,
TPU,
CUSTOM
}

@Nullable private final String job;

@Nullable private final Integer replica;

@Nullable private final Integer task;

@Nullable private final Integer deviceIndex;

@Nullable private final DeviceSpec.DeviceType deviceType;

@Nullable
public String job() {
return job;
}

@Nullable
public Integer replica() {
return replica;
}

@Nullable
public Integer task() {
return task;
}

@Nullable
public Integer deviceIndex() {
return deviceIndex;
}

@Nullable
public DeviceSpec.DeviceType deviceType() {
return deviceType;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof DeviceSpec) {
DeviceSpec that = (DeviceSpec) o;
return Objects.equals(job, that.job)
&& Objects.equals(replica, that.replica)
&& Objects.equals(task, that.task)
&& Objects.equals(deviceIndex, that.deviceIndex)
&& Objects.equals(deviceType, that.deviceType);
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(job, replica, task, deviceIndex, deviceType);
}

@Override
public String toString() {
String deviceString = null;
if (deviceType() != null) {
String deviceSuffix = deviceIndex() == null ? "*" : deviceIndex().toString();
deviceString = String.join(":", deviceType().toString().toUpperCase(), deviceSuffix);
}
return String.join(
"",
EmptyOrWithPrefix(job(), JOB_PREFIX),
EmptyOrWithPrefix(replica(), REPLICA_PREFIX),
EmptyOrWithPrefix(task(), TASK_PREFIX),
EmptyOrWithPrefix(deviceString, DEVICE_PREFIX));
}

private DeviceSpec(
@Nullable String job,
@Nullable Integer replica,
@Nullable Integer task,
@Nullable Integer deviceIndex,
@Nullable DeviceSpec.DeviceType deviceType) {
this.job = job;
this.replica = replica;
this.task = task;
this.deviceIndex = deviceIndex;
this.deviceType = deviceType;
}

public static Builder newBuilder() {
return new Builder();
}

private static String EmptyOrWithPrefix(@Nullable String s, String prefix) {
return s == null ? "" : prefix + s;
}

private static String EmptyOrWithPrefix(@Nullable Integer i, String prefix) {
return i == null ? "" : prefix + i.toString();
}

/** A Builder class for building {@link DeviceSpec} class. */
static class Builder {
private String job = null;
private Integer replica = null;
private Integer task = null;
private Integer deviceIndex = null;
private DeviceSpec.DeviceType deviceType = null;

private Builder() {}

Builder job(String job) {
this.job = job;
return this;
}

Builder replica(Integer replica) {
this.replica = replica;
return this;
}

Builder task(Integer task) {
this.task = task;
return this;
}

Builder deviceIndex(Integer deviceIndex) {
this.deviceIndex = deviceIndex;
return this;
}

Builder deviceType(DeviceSpec.DeviceType deviceType) {
this.deviceType = deviceType;
return this;
}

public DeviceSpec build() {
return new DeviceSpec(this.job, this.replica, this.task, this.deviceIndex, this.deviceType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.tensorflow.device;

import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;
import static org.tensorflow.device.DeviceSpec.DeviceType;

/** Tests for {@link DeviceSpec}. */
public class DeviceSpecTest {

@Test
public void emptyDeviceSpec() {
DeviceSpec emptySpec = DeviceSpec.newBuilder().build();

assertThat(emptySpec.job()).isNull();
assertThat(emptySpec.toString()).isEmpty();
}

@Test
public void jobOnlySpec() {
DeviceSpec jobSpec = DeviceSpec.newBuilder().job("ps").build();

assertThat(jobSpec.toString()).isEqualTo("/job:ps");
}

@Test
public void deviceTypeOnlySpec() {
DeviceSpec jobSpec = DeviceSpec.newBuilder().deviceType(DeviceType.CPU).build();

assertThat(jobSpec.toString()).isEqualTo("/device:CPU:*");
}

@Test
public void deviceTypeWithIndex() {
DeviceSpec jobSpec = DeviceSpec.newBuilder().deviceType(DeviceType.CPU).deviceIndex(1).build();

assertThat(jobSpec.toString()).isEqualTo("/device:CPU:1");
}

@Test
public void fullSpec() {
DeviceSpec fullSpec =
DeviceSpec.newBuilder()
.job("ps")
.replica(1)
.task(0)
.deviceType(DeviceType.GPU)
.deviceIndex(0)
.build();

assertThat(fullSpec.toString()).isEqualTo("/job:ps/replica:1/task:0/device:GPU:0");
}

@Test
public void emptySpecEquals() {
DeviceSpec emptySpec = DeviceSpec.newBuilder().build();

assertThat(emptySpec).isEqualTo(DeviceSpec.newBuilder().build());
}

@Test
public void fullSpecEquals() {
DeviceSpec fullSpec =
DeviceSpec.newBuilder()
.job("ps")
.replica(1)
.task(0)
.deviceType(DeviceType.GPU)
.deviceIndex(0)
.build();

assertThat(fullSpec)
.isEqualTo(
DeviceSpec.newBuilder()
.job("ps")
.replica(1)
.task(0)
.deviceType(DeviceType.GPU)
.deviceIndex(0)
.build());
}
}