This is a Java port from python device_spec.py. + * + * @see device_spec.py. + */ +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); + } + } +} diff --git a/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/device/DeviceSpecTest.java b/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/device/DeviceSpecTest.java new file mode 100644 index 00000000000..3596d58ab8f --- /dev/null +++ b/tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/device/DeviceSpecTest.java @@ -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()); + } +}