Repository: deepjavalibrary/djl
Pull request #1801 (2022-07-15 19:52:57)

[BugFix] Fix breaking behaviour in 0.18.0
## Description ##

This fixes [issue](https://github.com/deepjavalibrary/djl/issues/1799). 

The fix is to allow other datatype of index than just integer. The dataType conversion to `Long` has already implemented before in JniUtils


codecov-commenter: # [Codecov](https://codecov.io/gh/deepjavalibrary/djl/pull/1801?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) Report
> Merging [#1801](https://codecov.io/gh/deepjavalibrary/djl/pull/1801?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary) (3534b94) 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 `2.55%`.
> The diff coverage is `65.10%`.

> :exclamation: Current head 3534b94 differs from pull request most recent head c24f9f0. Consider uploading reports for the commit c24f9f0 to get more accurate results

```diff
@@             Coverage Diff              @@
##             master    #1801      +/-   ##
============================================
- Coverage     72.08%   69.53%   -2.56%     
- Complexity     5126     5485     +359     
============================================
  Files           473      524      +51     
  Lines         21970    24315    +2345     
  Branches       2351     2643     +292     
============================================
+ Hits          15838    16907    +1069     
- Misses         4925     6096    +1171     
- Partials       1207     1312     +105     
```


| [Impacted Files](https://codecov.io/gh/deepjavalibrary/djl/pull/1801?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/1801/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: |
| [...rc/main/java/ai/djl/modality/cv/MultiBoxPrior.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1801/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L011bHRpQm94UHJpb3IuamF2YQ==) | `76.00% <ø> (ø)` | |
| [...rc/main/java/ai/djl/modality/cv/output/Joints.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1801/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9Kb2ludHMuamF2YQ==) | `71.42% <ø> (ø)` | |
| [.../main/java/ai/djl/modality/cv/output/Landmark.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1801/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9MYW5kbWFyay5qYXZh) | `100.00% <ø> (ø)` | |
| [...main/java/ai/djl/modality/cv/output/Rectangle.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1801/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L291dHB1dC9SZWN0YW5nbGUuamF2YQ==) | `72.41% <0.00%> (ø)` | |
| [...i/djl/modality/cv/translator/BigGANTranslator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1801/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/1801/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/1801/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: |
| [.../cv/translator/SemanticSegmentationTranslator.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1801/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvU2VtYW50aWNTZWdtZW50YXRpb25UcmFuc2xhdG9yLmphdmE=) | `0.00% <0.00%> (ø)` | |
| [.../cv/translator/StyleTransferTranslatorFactory.java](https://codecov.io/gh/deepjavalibrary/djl/pull/1801/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary#diff-YXBpL3NyYy9tYWluL2phdmEvYWkvZGpsL21vZGFsaXR5L2N2L3RyYW5zbGF0b3IvU3R5bGVUcmFuc2ZlclRyYW5zbGF0b3JGYWN0b3J5LmphdmE=) | `40.00% <ø> (ø)` | |
| ... and [414 more](https://codecov.io/gh/deepjavalibrary/djl/pull/1801/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/1801?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/1801?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=deepjavalibrary). Last update [cea6f94...c24f9f0](https://codecov.io/gh/deepjavalibrary/djl/pull/1801?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/ndarray/index/NDIndex.java b/api/src/main/java/ai/djl/ndarray/index/NDIndex.java
index a5fb0a2f1..61a6f5453 100644
--- a/api/src/main/java/ai/djl/ndarray/index/NDIndex.java
+++ b/api/src/main/java/ai/djl/ndarray/index/NDIndex.java
@@ -21,7 +21,6 @@ import ai.djl.ndarray.index.dim.NDIndexNull;
 import ai.djl.ndarray.index.dim.NDIndexPick;
 import ai.djl.ndarray.index.dim.NDIndexSlice;
 import ai.djl.ndarray.index.dim.NDIndexTake;
-import ai.djl.ndarray.types.DataType;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -370,10 +369,11 @@ public class NDIndex {
                     return argIndex + 1;
                 } else if (arg instanceof NDArray) {
                     NDArray array = (NDArray) arg;
-                    if (array.getDataType() == DataType.BOOLEAN) {
+                    if (array.getDataType().isBoolean()) {
                         indices.add(new NDIndexBooleans(array));
                         return argIndex + 1;
-                    } else if (array.getDataType().isInteger()) {
+                    } else if (array.getDataType().isInteger()
+                            || array.getDataType().isFloating()) {
                         indices.add(new NDIndexTake(array));
                         return argIndex + 1;
                     }
diff --git a/api/src/main/java/ai/djl/ndarray/types/DataType.java b/api/src/main/java/ai/djl/ndarray/types/DataType.java
index 4101054fc..620cad66f 100644
--- a/api/src/main/java/ai/djl/ndarray/types/DataType.java
+++ b/api/src/main/java/ai/djl/ndarray/types/DataType.java
@@ -90,6 +90,15 @@ public enum DataType {
         return format == Format.UINT || format == Format.INT;
     }
 
+    /**
+     * Checks whether it is a boolean data type.
+     *
+     * @return whether it is a boolean data type
+     */
+    public boolean isBoolean() {
+        return format == Format.BOOLEAN;
+    }
+
     /**
      * Returns the data type to use for a data buffer.
      *


Test patch:
diff --git a/integration/src/main/java/ai/djl/integration/tests/ndarray/NDIndexTest.java b/integration/src/main/java/ai/djl/integration/tests/ndarray/NDIndexTest.java
index 7e38fe9f5..4920187a3 100644
--- a/integration/src/main/java/ai/djl/integration/tests/ndarray/NDIndexTest.java
+++ b/integration/src/main/java/ai/djl/integration/tests/ndarray/NDIndexTest.java
@@ -57,7 +57,7 @@ public class NDIndexTest {
     public void testGather() {
         try (NDManager manager = NDManager.newBaseManager()) {
             NDArray original = manager.arange(20f).reshape(-1, 4);
-            NDArray index = manager.create(new long[] {0, 0, 2, 1, 1, 2}, new Shape(3, 2));
+            NDArray index = manager.create(new float[] {0, 0, 2, 1, 1, 2}, new Shape(3, 2));
             NDArray actual = original.gather(index, 1);
             NDArray expected = manager.create(new float[] {0, 0, 6, 5, 9, 10}, new Shape(3, 2));
             Assert.assertEquals(actual, expected);
@@ -68,7 +68,7 @@ public class NDIndexTest {
     public void testTake() {
         try (NDManager manager = NDManager.newBaseManager()) {
             NDArray original = manager.arange(1, 7f).reshape(-1, 3);
-            NDArray index = manager.create(new long[] {0, 4, 1, 2}, new Shape(2, 2));
+            NDArray index = manager.create(new float[] {0, 4, 1, 2}, new Shape(2, 2));
             NDArray actual = original.take(index);
             NDArray expected = manager.create(new float[] {1, 5, 2, 3}, new Shape(2, 2));
             Assert.assertEquals(actual, expected);
@@ -119,25 +119,28 @@ public class NDIndexTest {
             expected = manager.arange(5).reshape(1, 5);
             Assert.assertEquals(original.get(bool), expected);
 
-            // get from int array
+            // get from integer array (higher rank included) or float array
             original = manager.arange(1, 7f).reshape(-1, 2);
             NDArray index = manager.create(new long[] {0, 0, 1, 2}, new Shape(2, 2));
+            NDArray indexFloat = manager.create(new float[] {0, 0, 1, 2}, new Shape(2, 2));
             NDArray actual = original.get(index);
+            NDArray actual2 = original.get(indexFloat);
             expected = manager.create(new float[] {1, 2, 1, 2, 3, 4, 5, 6}, new Shape(2, 2, 2));
             Assert.assertEquals(actual, expected);
+            Assert.assertEquals(actual2, expected);
 
-            // indexing with boolean, broadcast int array and slice
+            // indexing with boolean, slice, and integer array (higher rank included) or float array
             original = manager.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3);
             NDArray bool1 = manager.create(new boolean[] {true, false, true});
             NDArray index1 = manager.create(new long[] {2, 2}, new Shape(1, 2));
-            NDArray index2 = manager.create(new long[] {0, 1}, new Shape(1, 2));
+            NDArray index2 = manager.create(new float[] {0, 1}, new Shape(1, 2));
             actual = original.get(":{}, {}, {}, {}", 2, index1, bool1, index2);
             expected = manager.create(new int[] {18, 25, 45, 52}, new Shape(2, 1, 2));
             Assert.assertEquals(actual, expected);
 
-            // indexing with null, broadcast int array and slice
+            // indexing with null, slice and integer array (higher rank included) or float array
             original = manager.arange(3 * 3 * 3).reshape(3, 3, 3);
-            index1 = manager.create(new long[] {0, 1}, new Shape(2));
+            index1 = manager.create(new float[] {0, 1}, new Shape(2));
             index2 = manager.create(new long[] {0, 0, 2, 1}, new Shape(2, 2));
             actual = original.get(":{}, {}, {}, {}", 2, index1, index2, null);
             expected = manager.create(new int[] {0, 3, 2, 4, 9, 12, 11, 13}, new Shape(2, 2, 2, 1));
@@ -160,7 +163,7 @@ public class NDIndexTest {
 
             // set by index array
             original = manager.arange(1, 10).reshape(3, 3);
-            NDArray index = manager.create(new long[] {0, 1}, new Shape(2));
+            NDArray index = manager.create(new float[] {0, 1}, new Shape(2));
             value = manager.create(new int[] {666, 777, 888, 999}, new Shape(2, 2));
             original.set(new NDIndex("{}, :{}", index, 2), value);
             expected =
@@ -258,7 +261,7 @@ public class NDIndexTest {
         try (NDManager manager = NDManager.newBaseManager()) {
             NDArray original = manager.create(new float[] {1, 2, 3, 4}, new Shape(2, 2));
             NDArray expected = manager.create(new float[] {1, 8, 666, 77}, new Shape(2, 2));
-            NDArray idx = manager.create(new long[] {2, 3, 1}, new Shape(3));
+            NDArray idx = manager.create(new float[] {2, 3, 1}, new Shape(3));
             NDArray data = manager.create(new float[] {666, 77, 8}, new Shape(3));
             Assert.assertEquals(original.put(idx, data), expected);
         }