Repository: deepjavalibrary/djl
Pull request #1529 (2022-03-15 16:23:16)

Parse default device with Device.fromName
This makes the names "" and null return the default device (with potentially
specified engine) instead of CPU.

codecov-commenter: # [Codecov](https://codecov.io/gh/deepjavalibrary/djl/pull/1529?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) Report
> Merging [#1529](https://codecov.io/gh/deepjavalibrary/djl/pull/1529?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) (8b18f00) into [master](https://codecov.io/gh/deepjavalibrary/djl/commit/bb5073f7ffc33f1883621fc1783dbb01e70b418f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) (bb5073f) will **decrease** coverage by `1.38%`.
> The diff coverage is `50.07%`.

```diff
@@             Coverage Diff              @@
##             master    #1529      +/-   ##
============================================
- Coverage     72.08%   70.70%   -1.39%     
- Complexity     5126     5350     +224     
============================================
  Files           473      500      +27     
  Lines         21970    23370    +1400     
  Branches       2351     2552     +201     
============================================
+ Hits          15838    16523     +685     
- Misses         4925     5565     +640     
- Partials       1207     1282      +75     
```


| [Impacted Files](https://codecov.io/gh/deepjavalibrary/djl/pull/1529?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) | Coverage Δ | |
|---|---|---|
| [api/src/main/java/ai/djl/modality/cv/Image.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L0ltYWdlLmphdmE=) | `69.23% <ø> (-4.11%)` | :arrow_down: |
| [...i/djl/modality/cv/translator/BigGANTranslator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvQmlnR0FOVHJhbnNsYXRvci5qYXZh) | `21.42% <ø> (-5.24%)` | :arrow_down: |
| [...odality/cv/translator/BigGANTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvQmlnR0FOVHJhbnNsYXRvckZhY3RvcnkuamF2YQ==) | `33.33% <0.00%> (+8.33%)` | :arrow_up: |
| [...nslator/InstanceSegmentationTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvSW5zdGFuY2VTZWdtZW50YXRpb25UcmFuc2xhdG9yRmFjdG9yeS5qYXZh) | `14.28% <0.00%> (-3.90%)` | :arrow_down: |
| [.../modality/cv/translator/YoloTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvWW9sb1RyYW5zbGF0b3JGYWN0b3J5LmphdmE=) | `8.33% <0.00%> (-1.67%)` | :arrow_down: |
| [...i/djl/modality/cv/translator/YoloV5Translator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvWW9sb1Y1VHJhbnNsYXRvci5qYXZh) | `5.69% <0.00%> (ø)` | |
| [...odality/cv/translator/YoloV5TranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvWW9sb1Y1VHJhbnNsYXRvckZhY3RvcnkuamF2YQ==) | `8.33% <0.00%> (-1.67%)` | :arrow_down: |
| [...pi/src/main/java/ai/djl/ndarray/BytesSupplier.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL25kYXJyYXkvQnl0ZXNTdXBwbGllci5qYXZh) | `54.54% <0.00%> (-12.13%)` | :arrow_down: |
| [...pi/src/main/java/ai/djl/repository/Repository.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL3JlcG9zaXRvcnkvUmVwb3NpdG9yeS5qYXZh) | `83.33% <ø> (ø)` | |
| [...l/training/loss/SigmoidBinaryCrossEntropyLoss.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL3RyYWluaW5nL2xvc3MvU2lnbW9pZEJpbmFyeUNyb3NzRW50cm9weUxvc3MuamF2YQ==) | `64.00% <0.00%> (ø)` | |
| ... and [198 more](https://codecov.io/gh/deepjavalibrary/djl/pull/1529/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/deepjavalibrary/djl/pull/1529?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/deepjavalibrary/djl/pull/1529?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary). Last update [b364e7c...8b18f00](https://codecov.io/gh/deepjavalibrary/djl/pull/1529?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).


Patch:
diff --git a/api/src/main/java/ai/djl/Device.java b/api/src/main/java/ai/djl/Device.java
index ed90c4ef2..a360ba1e5 100644
--- a/api/src/main/java/ai/djl/Device.java
+++ b/api/src/main/java/ai/djl/Device.java
@@ -12,6 +12,7 @@
  */
 package ai.djl;
 
+import ai.djl.engine.Engine;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
@@ -67,22 +68,36 @@ public final class Device {
         return CACHE.computeIfAbsent(key, k -> new Device(deviceType, deviceId));
     }
 
+    /**
+     * Parses a deviceName string into a device for the default engine.
+     *
+     * @param deviceName deviceName String to parse
+     * @return the parsed device
+     * @see #fromName(String, Engine)
+     */
+    public static Device fromName(String deviceName) {
+        return fromName(deviceName, Engine.getInstance());
+    }
+
     /**
      * Parses a deviceName string into a device.
      *
      * <p>The main format of a device name string is "cpu", "gpu0", or "nc1". This is simply
      * deviceType concatenated with the deviceId. If no deviceId is used, -1 will be assumed.
      *
-     * <p>There are also several simplified formats. "-1", "", and null deviceNames correspond to
-     * cpu. Non-negative integer deviceNames such as "0", "1", or "2" correspond to gpus with those
+     * <p>There are also several simplified formats. The "-1", deviceNames corresponds to cpu.
+     * Non-negative integer deviceNames such as "0", "1", or "2" correspond to gpus with those
      * deviceIds.
      *
+     * <p>Finally, unspecified deviceNames (null or "") are parsed into the engine's default device.
+     *
      * @param deviceName deviceName string
+     * @param engine the engine the devie is for
      * @return the device
      */
-    public static Device fromName(String deviceName) {
+    public static Device fromName(String deviceName, Engine engine) {
         if (deviceName == null || deviceName.isEmpty()) {
-            return Device.cpu();
+            return engine.defaultDevice();
         }
 
         Matcher matcher = DEVICE_NAME.matcher(deviceName);


Test patch:
diff --git a/api/src/test/java/ai/djl/DeviceTest.java b/api/src/test/java/ai/djl/DeviceTest.java
index da9b736ff..5a7359aee 100644
--- a/api/src/test/java/ai/djl/DeviceTest.java
+++ b/api/src/test/java/ai/djl/DeviceTest.java
@@ -40,9 +40,7 @@ public class DeviceTest {
     @Test
     public void testDeviceName() {
         Assert.assertEquals(Device.fromName("cpu"), Device.cpu());
-        Assert.assertEquals(Device.fromName(""), Device.cpu());
         Assert.assertEquals(Device.fromName("-1"), Device.cpu());
-        Assert.assertEquals(Device.fromName(null), Device.cpu());
 
         Assert.assertEquals(Device.fromName("gpu0"), Device.gpu());
         Assert.assertEquals(Device.fromName("0"), Device.gpu());
@@ -50,5 +48,9 @@ public class DeviceTest {
 
         Assert.assertEquals(Device.fromName("nc1"), Device.of("nc", 1));
         Assert.assertEquals(Device.fromName("a999"), Device.of("a", 999));
+
+        Device defaultDevice = Engine.getInstance().defaultDevice();
+        Assert.assertEquals(Device.fromName(""), defaultDevice);
+        Assert.assertEquals(Device.fromName(null), defaultDevice);
     }
 }