Skip to content
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

Fix one array case for concat #739

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/src/main/java/ai/djl/ndarray/NDArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package ai.djl.ndarray;

import ai.djl.ndarray.types.Shape;
import ai.djl.util.Preconditions;
import java.util.Arrays;

/** This class contains various methods for manipulating NDArrays. */
Expand Down Expand Up @@ -1818,6 +1819,7 @@ public static NDArray stack(NDList arrays) {
* the the {@link NDArray}
*/
public static NDArray stack(NDList arrays, int axis) {
Preconditions.checkArgument(arrays.size() > 0, "need at least one array to stack");
NDArray array = arrays.head();
return array.getNDArrayInternal().stack(arrays.subNDList(1), axis);
}
Expand Down Expand Up @@ -1871,6 +1873,10 @@ public static NDArray concat(NDList arrays) {
* @return the concatenated {@link NDArray}
*/
public static NDArray concat(NDList arrays, int axis) {
Preconditions.checkArgument(arrays.size() > 0, "need at least one array to concatenate");
if (arrays.size() == 1) {
return arrays.singletonOrThrow().duplicate();
}
NDArray array = arrays.head();
return array.getNDArrayInternal().concat(arrays.subNDList(1), axis);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ public void testStack() {
expected = manager.create(new Shape(0, 0, 2));
Assert.assertEquals(array1.stack(array1, 2), expected);
Assert.assertEquals(NDArrays.stack(new NDList(array1, array1), 2), expected);

// one array
array1 = manager.ones(new Shape(2, 2));
expected = manager.ones(new Shape(1, 2, 2));
Assert.assertEquals(NDArrays.stack(new NDList(array1)), expected);
}
}

Expand Down Expand Up @@ -254,6 +259,11 @@ public void testConcat() {
array1 = manager.create(1f);
array2 = manager.create(2f);
array1.concat(array2);

// one array
array1 = manager.ones(new Shape(2, 2));
expected = manager.ones(new Shape(2, 2));
Assert.assertEquals(NDArrays.concat(new NDList(array1)), expected);
}
}

Expand Down