use of org.nd4j.linalg.primitives.Pair in project nd4j by deeplearning4j.
the class CpuTADManager method getTADOnlyShapeInfo.
@Override
public Pair<DataBuffer, DataBuffer> getTADOnlyShapeInfo(INDArray array, int[] dimension) {
if (dimension != null && dimension.length > 1)
Arrays.sort(dimension);
if (dimension == null || dimension.length >= 1 && dimension[0] == Integer.MAX_VALUE) {
return new Pair<>(array.shapeInfoDataBuffer(), null);
} else {
TadDescriptor descriptor = new TadDescriptor(array, dimension);
if (!cache.containsKey(descriptor)) {
int dimensionLength = dimension.length;
// FIXME: this is fast triage, remove it later
// dimensionLength <= 1 ? 2 : dimensionLength;
int targetRank = array.rank();
long offsetLength;
long tadLength = 1;
for (int i = 0; i < dimensionLength; i++) {
tadLength *= array.shape()[dimension[i]];
}
offsetLength = array.lengthLong() / tadLength;
DataBuffer outputBuffer = new IntBuffer(targetRank * 2 + 4);
DataBuffer offsetsBuffer = new LongBuffer(offsetLength);
DataBuffer dimensionBuffer = constantHandler.getConstantBuffer(dimension);
Pointer dimensionPointer = dimensionBuffer.addressPointer();
Pointer xShapeInfo = array.shapeInfoDataBuffer().addressPointer();
Pointer targetPointer = outputBuffer.addressPointer();
Pointer offsetsPointer = offsetsBuffer.addressPointer();
nativeOps.tadOnlyShapeInfo((IntPointer) xShapeInfo, (IntPointer) dimensionPointer, dimension.length, (IntPointer) targetPointer, new LongPointerWrapper(offsetsPointer));
// If the line below will be uncommented, shapes from JVM will be used on native side
// outputBuffer = array.tensorAlongDimension(0, dimension).shapeInfoDataBuffer();
Pair<DataBuffer, DataBuffer> pair = new Pair<>(outputBuffer, offsetsBuffer);
if (counter.get() < MAX_ENTRIES) {
counter.incrementAndGet();
cache.put(descriptor, pair);
bytes.addAndGet((outputBuffer.length() * 4) + (offsetsBuffer.length() * 8));
}
return pair;
}
return cache.get(descriptor);
}
}
use of org.nd4j.linalg.primitives.Pair in project nd4j by deeplearning4j.
the class Nd4jTestsC method testToOffsetZeroCopy.
@Test
public void testToOffsetZeroCopy() {
List<Pair<INDArray, String>> testInputs = NDArrayCreationUtil.getAllTestMatricesWithShape(ordering(), 4, 5, 123);
for (int i = 0; i < testInputs.size(); i++) {
Pair<INDArray, String> pair = testInputs.get(i);
String msg = pair.getSecond();
msg += "Failed on " + i;
INDArray in = pair.getFirst();
INDArray dup = Shape.toOffsetZeroCopy(in, ordering());
INDArray dupc = Shape.toOffsetZeroCopy(in, 'c');
INDArray dupf = Shape.toOffsetZeroCopy(in, 'f');
INDArray dupany = Shape.toOffsetZeroCopyAnyOrder(in);
assertEquals(msg, in, dup);
assertEquals(msg, in, dupc);
assertEquals(msg, in, dupf);
assertEquals(msg, dupc.ordering(), 'c');
assertEquals(msg, dupf.ordering(), 'f');
assertEquals(msg, in, dupany);
assertEquals(dup.offset(), 0);
assertEquals(dupc.offset(), 0);
assertEquals(dupf.offset(), 0);
assertEquals(dupany.offset(), 0);
assertEquals(dup.length(), dup.data().length());
assertEquals(dupc.length(), dupc.data().length());
assertEquals(dupf.length(), dupf.data().length());
assertEquals(dupany.length(), dupany.data().length());
}
}
use of org.nd4j.linalg.primitives.Pair in project nd4j by deeplearning4j.
the class Nd4jTestsC method testPermutei.
@Test
public void testPermutei() {
// Check in-place permute vs. copy array permute
// 2d:
INDArray orig = Nd4j.linspace(1, 3 * 4, 3 * 4).reshape('c', 3, 4);
INDArray exp01 = orig.permute(0, 1);
INDArray exp10 = orig.permute(1, 0);
List<Pair<INDArray, String>> list1 = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 4, 12345);
List<Pair<INDArray, String>> list2 = NDArrayCreationUtil.getAllTestMatricesWithShape(3, 4, 12345);
for (int i = 0; i < list1.size(); i++) {
INDArray p1 = list1.get(i).getFirst().assign(orig).permutei(0, 1);
INDArray p2 = list2.get(i).getFirst().assign(orig).permutei(1, 0);
assertEquals(exp01, p1);
assertEquals(exp10, p2);
assertEquals(3, p1.rows());
assertEquals(4, p1.columns());
assertEquals(4, p2.rows());
assertEquals(3, p2.columns());
}
// 2d, v2
orig = Nd4j.linspace(1, 4, 4).reshape('c', 1, 4);
exp01 = orig.permute(0, 1);
exp10 = orig.permute(1, 0);
list1 = NDArrayCreationUtil.getAllTestMatricesWithShape(1, 4, 12345);
list2 = NDArrayCreationUtil.getAllTestMatricesWithShape(1, 4, 12345);
for (int i = 0; i < list1.size(); i++) {
INDArray p1 = list1.get(i).getFirst().assign(orig).permutei(0, 1);
INDArray p2 = list2.get(i).getFirst().assign(orig).permutei(1, 0);
assertEquals(exp01, p1);
assertEquals(exp10, p2);
assertEquals(1, p1.rows());
assertEquals(4, p1.columns());
assertEquals(4, p2.rows());
assertEquals(1, p2.columns());
assertTrue(p1.isRowVector());
assertFalse(p1.isColumnVector());
assertFalse(p2.isRowVector());
assertTrue(p2.isColumnVector());
}
// 3d:
INDArray orig3d = Nd4j.linspace(1, 3 * 4 * 5, 3 * 4 * 5).reshape('c', 3, 4, 5);
INDArray exp012 = orig3d.permute(0, 1, 2);
INDArray exp021 = orig3d.permute(0, 2, 1);
INDArray exp120 = orig3d.permute(1, 2, 0);
INDArray exp102 = orig3d.permute(1, 0, 2);
INDArray exp201 = orig3d.permute(2, 0, 1);
INDArray exp210 = orig3d.permute(2, 1, 0);
List<Pair<INDArray, String>> list012 = NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, 3, 4, 5);
List<Pair<INDArray, String>> list021 = NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, 3, 4, 5);
List<Pair<INDArray, String>> list120 = NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, 3, 4, 5);
List<Pair<INDArray, String>> list102 = NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, 3, 4, 5);
List<Pair<INDArray, String>> list201 = NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, 3, 4, 5);
List<Pair<INDArray, String>> list210 = NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, 3, 4, 5);
for (int i = 0; i < list012.size(); i++) {
INDArray p1 = list012.get(i).getFirst().assign(orig3d).permutei(0, 1, 2);
INDArray p2 = list021.get(i).getFirst().assign(orig3d).permutei(0, 2, 1);
INDArray p3 = list120.get(i).getFirst().assign(orig3d).permutei(1, 2, 0);
INDArray p4 = list102.get(i).getFirst().assign(orig3d).permutei(1, 0, 2);
INDArray p5 = list201.get(i).getFirst().assign(orig3d).permutei(2, 0, 1);
INDArray p6 = list210.get(i).getFirst().assign(orig3d).permutei(2, 1, 0);
assertEquals(exp012, p1);
assertEquals(exp021, p2);
assertEquals(exp120, p3);
assertEquals(exp102, p4);
assertEquals(exp201, p5);
assertEquals(exp210, p6);
}
}
use of org.nd4j.linalg.primitives.Pair in project nd4j by deeplearning4j.
the class NDArrayTestsFortran method testTensorStats.
@Test
public void testTensorStats() {
List<Pair<INDArray, String>> testInputs = NDArrayCreationUtil.getAllTestMatricesWithShape(9, 13, 123);
for (Pair<INDArray, String> pair : testInputs) {
INDArray arr = pair.getFirst();
String msg = pair.getSecond();
int nTAD0 = arr.tensorssAlongDimension(0);
int nTAD1 = arr.tensorssAlongDimension(1);
OpExecutionerUtil.Tensor1DStats t0 = OpExecutionerUtil.get1DTensorStats(arr, 0);
OpExecutionerUtil.Tensor1DStats t1 = OpExecutionerUtil.get1DTensorStats(arr, 1);
assertEquals(nTAD0, t0.getNumTensors());
assertEquals(nTAD1, t1.getNumTensors());
INDArray tFirst0 = arr.tensorAlongDimension(0, 0);
INDArray tSecond0 = arr.tensorAlongDimension(1, 0);
INDArray tFirst1 = arr.tensorAlongDimension(0, 1);
INDArray tSecond1 = arr.tensorAlongDimension(1, 1);
assertEquals(tFirst0.offset(), t0.getFirstTensorOffset());
assertEquals(tFirst1.offset(), t1.getFirstTensorOffset());
long separation0 = tSecond0.offset() - tFirst0.offset();
long separation1 = tSecond1.offset() - tFirst1.offset();
assertEquals(separation0, t0.getTensorStartSeparation());
assertEquals(separation1, t1.getTensorStartSeparation());
for (int i = 0; i < nTAD0; i++) {
INDArray tad0 = arr.tensorAlongDimension(i, 0);
assertEquals(tad0.length(), t0.getTensorLength());
assertEquals(tad0.elementWiseStride(), t0.getElementWiseStride());
long offset = tad0.offset();
long calcOffset = t0.getFirstTensorOffset() + i * t0.getTensorStartSeparation();
assertEquals(offset, calcOffset);
}
for (int i = 0; i < nTAD1; i++) {
INDArray tad1 = arr.tensorAlongDimension(i, 1);
assertEquals(tad1.length(), t1.getTensorLength());
assertEquals(tad1.elementWiseStride(), t1.getElementWiseStride());
long offset = tad1.offset();
long calcOffset = t1.getFirstTensorOffset() + i * t1.getTensorStartSeparation();
assertEquals(offset, calcOffset);
}
}
}
use of org.nd4j.linalg.primitives.Pair in project nd4j by deeplearning4j.
the class NDArrayTestsFortran method testBroadcastingGenerated.
@Test
public void testBroadcastingGenerated() {
int[][] broadcastShape = NDArrayCreationUtil.getRandomBroadCastShape(7, 6, 10);
List<List<Pair<INDArray, String>>> broadCastList = new ArrayList<>(broadcastShape.length);
for (int[] shape : broadcastShape) {
List<Pair<INDArray, String>> arrShape = NDArrayCreationUtil.get6dPermutedWithShape(7, shape);
broadCastList.add(arrShape);
broadCastList.add(NDArrayCreationUtil.get6dReshapedWithShape(7, shape));
broadCastList.add(NDArrayCreationUtil.getAll6dTestArraysWithShape(7, shape));
}
for (List<Pair<INDArray, String>> b : broadCastList) {
for (Pair<INDArray, String> val : b) {
INDArray inputArrBroadcast = val.getFirst();
int[] destShape = NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7);
INDArray output = inputArrBroadcast.broadcast(NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7));
assertArrayEquals(destShape, output.shape());
}
}
}
Aggregations