-
Notifications
You must be signed in to change notification settings - Fork 688
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
[pytorch] Add NDList to IValue unit test #1762
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,26 +12,27 @@ | |
*/ | ||
package ai.djl.pytorch.jni; | ||
|
||
import ai.djl.ndarray.NDArray; | ||
import ai.djl.ndarray.NDList; | ||
import ai.djl.ndarray.NDManager; | ||
import ai.djl.ndarray.types.Shape; | ||
import ai.djl.pytorch.engine.PtNDArray; | ||
import ai.djl.pytorch.engine.PtNDManager; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
public class IValueUtilsTest { | ||
|
||
@Test | ||
public void getInputsTestTupleSyntax() { | ||
try (PtNDManager manager = (PtNDManager) NDManager.newBaseManager()) { | ||
PtNDArray array1 = (PtNDArray) manager.zeros(new Shape(1)); | ||
array1.setName("Test()"); | ||
PtNDArray array2 = (PtNDArray) manager.ones(new Shape(1)); | ||
array2.setName("Test()"); | ||
public void testTuple() { | ||
try (NDManager manager = NDManager.newBaseManager()) { | ||
NDArray array1 = manager.zeros(new Shape(1)); | ||
array1.setName("input1()"); | ||
NDArray array2 = manager.ones(new Shape(1)); | ||
array2.setName("input1()"); | ||
NDList input = new NDList(array1, array2); | ||
// the NDList is mapped to (input1: Tuple(Tensor)) | ||
input.attach(manager); | ||
|
||
IValue[] iValues = IValueUtils.getInputs(input); | ||
|
@@ -42,4 +43,54 @@ public void getInputsTestTupleSyntax() { | |
Arrays.stream(iValues).forEach(IValue::close); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMapOfTensor() { | ||
try (NDManager manager = NDManager.newBaseManager()) { | ||
NDArray array1 = manager.zeros(new Shape(1)); | ||
array1.setName("input1.key1"); | ||
NDArray array2 = manager.ones(new Shape(1)); | ||
array2.setName("input1.key2"); | ||
NDArray array3 = manager.zeros(new Shape(1)); | ||
array3.setName("input2.key1"); | ||
NDArray array4 = manager.ones(new Shape(1)); | ||
array4.setName("input2.key2"); | ||
NDArray array5 = manager.ones(new Shape(1)); | ||
array5.setName("input2.key3"); | ||
NDList input = new NDList(array1, array2, array3, array4, array5); | ||
// the NDList is mapped to (input1: Dict(str, Tensor), input2: Dict(str, Tensor)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any way to generate something like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a PR for your issue: #1765 |
||
// the first part of NDArray name is the variable name of the inputs | ||
// the 2nd part is the key of each value in the dict | ||
input.attach(manager); | ||
|
||
IValue[] iValues = IValueUtils.getInputs(input); | ||
Assert.assertEquals(iValues.length, 2); | ||
Assert.assertTrue(iValues[0].isMap()); | ||
Assert.assertEquals(iValues[1].toIValueMap().size(), 3); | ||
|
||
Arrays.stream(iValues).forEach(IValue::close); | ||
} | ||
} | ||
|
||
@Test | ||
public void testListOfTensor() { | ||
try (NDManager manager = NDManager.newBaseManager()) { | ||
NDArray array1 = manager.zeros(new Shape(1)); | ||
array1.setName("input1[]"); | ||
NDArray array2 = manager.ones(new Shape(1)); | ||
array2.setName("input1[]"); | ||
NDArray array3 = manager.ones(new Shape(1)); | ||
array3.setName("input2"); | ||
NDList input = new NDList(array1, array2, array3); | ||
// the NDList is mapped to (input1: list(Tensor), input2: Tensor) | ||
input.attach(manager); | ||
|
||
IValue[] iValues = IValueUtils.getInputs(input); | ||
Assert.assertEquals(iValues.length, 2); | ||
Assert.assertTrue(iValues[0].isList()); | ||
Assert.assertEquals(iValues[0].toIValueArray().length, 2); | ||
|
||
Arrays.stream(iValues).forEach(IValue::close); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: add some comments to explain why use
input1
as prefix? Like group all keys with same prefix into one Map?