Repository navigation
Add a simple DeviceSpec class. #83
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/device/DeviceSpec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package org.tensorflow.device; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
82 changes: 82 additions & 0 deletions
82
tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/device/DeviceSpecTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 fromcom.google.code.findbugs, thanks for raising this @CraigacpThere was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 theDeviceSpecout of thedevicepackage. Since there was only one file in that package, I don't think we needed it.There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.