Repository: deepjavalibrary/djl
Pull request #2721 (2023-07-24 07:22:17)

[api] Makes getNeuronDevices() public
## Description ##

Brief description of what this PR is about

- If this change is a backward incompatible change, why must this change be made?
- Interesting edge cases to note here


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) Report
Patch coverage: **`54.72`**% and no project coverage change.
> Comparison is base [(`bb5073f`)](https://app.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) 72.08% compared to head [(`8abd04f`)](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) 72.09%.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please [install the Github App Integration](https://github.com/apps/codecov) for your organization. [Read more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##             master    #2721     +/-   ##
===========================================
  Coverage     72.08%   72.09%             
- Complexity     5126     7026   +1900     
===========================================
  Files           473      698    +225     
  Lines         21970    31265   +9295     
  Branches       2351     3224    +873     
===========================================
+ Hits          15838    22541   +6703     
- Misses         4925     7189   +2264     
- Partials       1207     1535    +328     
```


| [Impacted Files](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?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://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?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: |
| [...rc/main/java/ai/djl/modality/cv/MultiBoxPrior.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L011bHRpQm94UHJpb3IuamF2YQ==) | `76.00% <ø> (ø)` | |
| [.../main/java/ai/djl/modality/cv/output/Landmark.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9MYW5kbWFyay5qYXZh) | `100.00% <ø> (ø)` | |
| [...djl/modality/cv/transform/RandomFlipLeftRight.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zZm9ybS9SYW5kb21GbGlwTGVmdFJpZ2h0LmphdmE=) | `25.00% <0.00%> (-25.00%)` | :arrow_down: |
| [...djl/modality/cv/transform/RandomFlipTopBottom.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zZm9ybS9SYW5kb21GbGlwVG9wQm90dG9tLmphdmE=) | `25.00% <0.00%> (-25.00%)` | :arrow_down: |
| [...i/djl/modality/cv/translator/BigGANTranslator.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvQmlnR0FOVHJhbnNsYXRvci5qYXZh) | `21.42% <0.00%> (-5.24%)` | :arrow_down: |
| [.../modality/cv/translator/ImageFeatureExtractor.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvSW1hZ2VGZWF0dXJlRXh0cmFjdG9yLmphdmE=) | `0.00% <0.00%> (ø)` | |
| [.../ai/djl/modality/cv/translator/YoloTranslator.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvWW9sb1RyYW5zbGF0b3IuamF2YQ==) | `27.77% <0.00%> (+18.95%)` | :arrow_up: |
| [...ain/java/ai/djl/modality/cv/util/NDImageUtils.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3V0aWwvTkRJbWFnZVV0aWxzLmphdmE=) | `67.10% <0.00%> (+7.89%)` | :arrow_up: |
| [api/src/main/java/ai/djl/modality/nlp/Decoder.java](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L25scC9EZWNvZGVyLmphdmE=) | `63.63% <ø> (ø)` | |
| ... and [226 more](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) | |

... and [368 files with indirect coverage changes](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary)


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/deepjavalibrary/djl/pull/2721?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?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/util/NeuronUtils.java b/api/src/main/java/ai/djl/util/NeuronUtils.java
index 76e1e853f..685dbd772 100644
--- a/api/src/main/java/ai/djl/util/NeuronUtils.java
+++ b/api/src/main/java/ai/djl/util/NeuronUtils.java
@@ -46,29 +46,42 @@ public final class NeuronUtils {
      * @return the number of NeuronCores available in the system
      */
     public static int getNeuronCores() {
-        List<Path> nd = getNeuronDevices("/dev/");
+        List<String> nd = getNeuronDevices("/dev/");
         if (nd.isEmpty()) {
             return 0;
         }
-        String dev = nd.get(0).toFile().getName();
-        int cores = getNeuronCoresPerDevice("/sys/devices/virtual/neuron_device/" + dev);
+        int cores = getNeuronCoresForDevice(nd.get(0));
         return nd.size() * cores;
     }
 
-    static List<Path> getNeuronDevices(String location) {
+    /**
+     * Returns a list of neuron device file path.
+     *
+     * @param location the neuron device path
+     * @return a list of neuron device name
+     */
+    public static List<String> getNeuronDevices(String location) {
         Path path = Paths.get(location);
         if (!Files.exists(path)) {
             return Collections.emptyList();
         }
         try (Stream<Path> dev = Files.list(path)) {
-            return dev.filter(p -> matches(p, "neuron")).collect(Collectors.toList());
+            return dev.filter(p -> matches(p, "neuron"))
+                    .map(p -> "/sys/devices/virtual/neuron_device/" + p.toFile().getName())
+                    .collect(Collectors.toList());
         } catch (IOException e) {
             logger.warn("Failed to list neuron cores", e);
         }
         return Collections.emptyList();
     }
 
-    static int getNeuronCoresPerDevice(String location) {
+    /**
+     * Returns the number of neuron cores per device.
+     *
+     * @param location the neuron device file path
+     * @return the number of neuron cores
+     */
+    public static int getNeuronCoresForDevice(String location) {
         Path path = Paths.get(location);
         if (!Files.exists(path)) {
             return 0;


Test patch:
diff --git a/api/src/test/java/ai/djl/util/NeuronUtilsTest.java b/api/src/test/java/ai/djl/util/NeuronUtilsTest.java
index 665a8f551..2248c99e2 100644
--- a/api/src/test/java/ai/djl/util/NeuronUtilsTest.java
+++ b/api/src/test/java/ai/djl/util/NeuronUtilsTest.java
@@ -47,10 +47,10 @@ public class NeuronUtilsTest {
 
             logger.info("hasNeuron: {}", NeuronUtils.hasNeuron());
             logger.info("# neuron cores: {}", NeuronUtils.getNeuronCores());
-            List<Path> devices = NeuronUtils.getNeuronDevices(dev.toString());
+            List<String> devices = NeuronUtils.getNeuronDevices(dev.toString());
             Assert.assertEquals(devices.size(), 3);
             String vd = sys.resolve("neuron1").toString();
-            Assert.assertEquals(NeuronUtils.getNeuronCoresPerDevice(vd), 2);
+            Assert.assertEquals(NeuronUtils.getNeuronCoresForDevice(vd), 2);
         } finally {
             Utils.deleteQuietly(sys);
             Utils.deleteQuietly(dev);