Search in sources :

Example 51 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class OperationProfilerTests method testBadTad2.

@Test
public void testBadTad2() throws Exception {
    INDArray x = Nd4j.create(2, 4, 5, 6);
    Pair<DataBuffer, DataBuffer> pair = Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(x, new int[] { 2, 3 });
    OpProfiler.PenaltyCause[] causes = OpProfiler.getInstance().processTADOperands(pair.getFirst());
    log.info("Causes: {}", Arrays.toString(causes));
    assertEquals(1, causes.length);
    assertTrue(ArrayUtils.contains(causes, OpProfiler.PenaltyCause.TAD_NON_EWS_ACCESS));
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) Test(org.junit.Test)

Example 52 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class SortCooTests method sortSparseCooIndicesSort1.

@Test
public void sortSparseCooIndicesSort1() throws Exception {
    // FIXME: we don't want this test running on cuda for now
    if (Nd4j.getExecutioner().getClass().getCanonicalName().toLowerCase().contains("cuda"))
        return;
    int[] indices = new int[] { 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1 };
    // we don't care about
    double[] values = new double[] { 2, 1, 0, 3 };
    int[] expIndices = new int[] { 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1 };
    double[] expValues = new double[] { 0, 1, 2, 3 };
    DataBuffer idx = Nd4j.getDataBufferFactory().createInt(indices);
    DataBuffer val = Nd4j.createBuffer(values);
    log.info("Old indices: {}", Arrays.toString(idx.asInt()));
    NativeOpsHolder.getInstance().getDeviceNativeOps().sortCooIndicesFloat(null, (IntPointer) idx.addressPointer(), (FloatPointer) val.addressPointer(), 4, 3);
    log.info("New indices: {}", Arrays.toString(idx.asInt()));
    assertArrayEquals(expIndices, idx.asInt());
    assertArrayEquals(expValues, val.asDouble(), 1e-5);
}
Also used : DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 53 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class NativeOpExecutioner method exec.

@Override
public INDArray exec(Accumulation op, int... dimension) {
    dimension = Shape.normalizeAxis(op.x().rank(), dimension);
    validateDataType(Nd4j.dataType(), op);
    if (extraz.get() == null)
        extraz.set(new PointerPointer(32));
    int[] maxShape = Shape.getMaxShape(op.x(), op.y());
    for (int i = 0; i < dimension.length; i++) if (dimension[i] >= maxShape.length && dimension[i] != Integer.MAX_VALUE)
        throw new ND4JIllegalStateException("Op target dimension " + Arrays.toString(dimension) + " contains element that higher then rank of op.X: [" + op.x().rank() + "]");
    for (int i = 0; i < dimension.length; i++) {
        if (dimension[i] < 0)
            dimension[i] += op.x().rank();
    }
    // do op along all dimensions
    if (dimension.length == op.x().rank())
        dimension = new int[] { Integer.MAX_VALUE };
    int[] retShape;
    if (Shape.wholeArrayDimension(dimension))
        retShape = new int[] { 1, 1 };
    else
        retShape = ArrayUtil.removeIndex(maxShape, dimension);
    // ensure vector is proper shape
    if (retShape.length == 1) {
        if (dimension[0] == 0)
            retShape = new int[] { 1, retShape[0] };
        else
            retShape = new int[] { retShape[0], 1 };
    } else if (retShape.length == 0) {
        retShape = new int[] { 1, 1 };
    }
    if (op.x().isVector() && op.x().length() == ArrayUtil.prod(retShape) && ArrayUtil.prodLong(retShape) > 1 && op.y() == null)
        return op.noOp();
    /**
     * This is the result array.
     * We create it only if we hadn't provided it before
     */
    INDArray ret;
    if (op.z() == null || op.z() == op.x()) {
        if (op.isComplexAccumulation()) {
            int xT = op.x().tensorssAlongDimension(dimension);
            int yT = op.y().tensorssAlongDimension(dimension);
            ret = Nd4j.create(xT, yT);
        } else {
            if (op.y() != null) {
                // 2 options here: either pairwise, equal sizes - OR every X TAD vs. entirety of Y
                if (op.x().lengthLong() == op.y().lengthLong()) {
                    // Pairwise
                    if (op.x().tensorssAlongDimension(dimension) != op.y().tensorssAlongDimension(dimension)) {
                        throw new ND4JIllegalStateException("Number of TADs along dimension don't match: (x shape = " + Arrays.toString(op.x().shape()) + ", y shape = " + Arrays.toString(op.y().shape()) + ", dimension = " + Arrays.toString(dimension) + ")");
                    }
                } else {
                    // Every X TAD vs. entirety of Y
                    val xTADSize = op.x().lengthLong() / op.x().tensorssAlongDimension(dimension);
                    if (xTADSize != op.y().length()) {
                        throw new ND4JIllegalStateException("Size of TADs along dimension don't match for pairwise execution:" + " (x TAD size = " + xTADSize + ", y size = " + op.y().lengthLong());
                    }
                }
            }
            if (op.x().data().dataType() == DataBuffer.Type.DOUBLE)
                ret = Nd4j.valueArrayOf(retShape, op.zeroDouble());
            else
                ret = Nd4j.valueArrayOf(retShape, op.zeroFloat());
        }
        op.setZ(ret);
    } else {
        // compare length
        if (!op.isComplexAccumulation() && op.z().lengthLong() != ArrayUtil.prodLong(retShape))
            throw new ND4JIllegalStateException("Shape of target array for reduction [" + Arrays.toString(op.z().shape()) + "] doesn't match expected [" + Arrays.toString(retShape) + "]");
        else if (op.isComplexAccumulation()) {
            int xT = op.x().tensorssAlongDimension(dimension);
            int yT = op.y().tensorssAlongDimension(dimension);
            if (op.z().lengthLong() != xT * yT)
                throw new ND4JIllegalStateException("Shape of target array for reduction [" + Arrays.toString(op.z().shape()) + "] doesn't match expected [" + (xT * yT) + "]");
        }
        if (op.x().data().dataType() == DataBuffer.Type.DOUBLE) {
            op.z().assign(op.zeroDouble());
        } else {
            op.z().assign(op.zeroFloat());
        }
        ret = op.z();
    }
    /**
     * Returns the {@link Shape#createShapeInformation(int[], int[], int, int, char)}
     * and the associated offsets for each {@link INDArray#tensorAlongDimension(int, int...)}
     * The first item is the shape information. The second one is the offsets.
     */
    Pair<DataBuffer, DataBuffer> tadBuffers = tadManager.getTADOnlyShapeInfo(op.x(), dimension);
    Pair<DataBuffer, DataBuffer> yTadBuffers = null;
    /**
     * Note that we use addresses in libnd4j.
     * We use reinterpret cast in c to take the long
     * we pass to JNI. This manages overhead.
     */
    Pointer hostTadShapeInfo = tadBuffers.getFirst().addressPointer();
    DataBuffer offsets = tadBuffers.getSecond();
    Pointer hostTadOffsets = offsets == null ? null : offsets.addressPointer();
    // we're going to check, if that's TAD vs TAD comparison or TAD vs full array. if later - we're going slightly different route
    boolean tvf = false;
    if (op.y() != null) {
        if (op.x().tensorAlongDimension(0, dimension).lengthLong() == op.y().lengthLong()) {
            tvf = true;
        }
    }
    if (op.isComplexAccumulation()) {
        yTadBuffers = tadManager.getTADOnlyShapeInfo(op.y(), dimension);
        if (op.x().tensorAlongDimension(0, dimension).lengthLong() != op.y().tensorAlongDimension(0, dimension).lengthLong())
            throw new ND4JIllegalStateException("Impossible to issue AllDistances operation: TAD lengths mismatch along given dimension");
    }
    /**
     * This is a pointer to a pointer in c.
     */
    // FIXME: we need something better then 3rd element being non-null here...
    PointerPointer dummy = extraz.get().put(hostTadShapeInfo, hostTadOffsets, tvf ? hostTadOffsets : null);
    long st = profilingHookIn(op, tadBuffers.getFirst());
    /**
     * Note because dimension arrays don't change,
     * we use an {@link ConstantHandler} which knows how to reserve memory
     * for immutable buffers for the dimensions.
     * This gives us a pointer which is passed around in libnd4j.
     */
    Pointer dimensionAddress = constantHandler.getConstantBuffer(dimension).addressPointer();
    if (op.x().data().dataType() == DataBuffer.Type.DOUBLE) {
        if (op instanceof Variance) {
            if (ret.isScalar()) {
                ret.putScalar(0, loop.execSummaryStatsScalarDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op), true));
            } else {
                Variance var = (Variance) op;
                loop.execSummaryStatsDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op), (DoublePointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (IntPointer) dimensionAddress, dimension.length, var.isBiasCorrected());
            }
        } else // pairwise reduction like similarity of two arrays
        if (op.y() != null && op.getOpType() == Op.Type.REDUCE3) {
            if (op.isComplexAccumulation()) {
                loop.execReduce3AllDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op), (DoublePointer) op.y().data().addressPointer(), (IntPointer) op.y().shapeInfoDataBuffer().addressPointer(), (DoublePointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (IntPointer) dimensionAddress, dimension.length, (IntPointer) tadBuffers.getFirst().addressPointer(), new LongPointerWrapper(tadBuffers.getSecond().addressPointer()), (IntPointer) yTadBuffers.getFirst().addressPointer(), new LongPointerWrapper(yTadBuffers.getSecond().addressPointer()));
            } else if (ret.isScalar()) {
                ret.putScalar(0, loop.execReduce3ScalarDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op), (DoublePointer) op.y().data().addressPointer(), (IntPointer) op.y().shapeInfoDataBuffer().addressPointer()));
            } else {
                loop.execReduce3Double(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op), (DoublePointer) op.y().data().addressPointer(), (IntPointer) op.y().shapeInfoDataBuffer().addressPointer(), (DoublePointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (IntPointer) dimensionAddress, dimension.length);
            }
        } else {
            if (ret.isScalar()) {
                ret.putScalar(0, loop.execReduceScalarDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op)));
            } else {
                loop.execReduceDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op), (DoublePointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (IntPointer) dimensionAddress, dimension.length);
            }
        }
    } else {
        if (op instanceof Variance) {
            Variance variance = (Variance) op;
            if (ret.isScalar()) {
                ret.putScalar(0, loop.execSummaryStatsScalarFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op), variance.isBiasCorrected()));
            } else {
                loop.execSummaryStatsFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op), (FloatPointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (IntPointer) dimensionAddress, dimension.length, variance.isBiasCorrected());
            }
        } else if (op.y() != null && op.getOpType() == Op.Type.REDUCE3) {
            if (op.isComplexAccumulation()) {
                loop.execReduce3AllFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op), (FloatPointer) op.y().data().addressPointer(), (IntPointer) op.y().shapeInfoDataBuffer().addressPointer(), (FloatPointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (IntPointer) dimensionAddress, dimension.length, (IntPointer) tadBuffers.getFirst().addressPointer(), new LongPointerWrapper(tadBuffers.getSecond().addressPointer()), (IntPointer) yTadBuffers.getFirst().addressPointer(), new LongPointerWrapper(yTadBuffers.getSecond().addressPointer()));
            } else if (ret.isScalar()) {
                ret.putScalar(0, loop.execReduce3ScalarFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op), (FloatPointer) op.y().data().addressPointer(), (IntPointer) op.y().shapeInfoDataBuffer().addressPointer()));
            } else {
                loop.execReduce3Float(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op), (FloatPointer) op.y().data().addressPointer(), (IntPointer) op.y().shapeInfoDataBuffer().addressPointer(), (FloatPointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (IntPointer) dimensionAddress, dimension.length);
            }
        } else {
            if (ret.isScalar()) {
                ret.putScalar(0, loop.execReduceScalarFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op)));
            } else {
                loop.execReduceFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op), (FloatPointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (IntPointer) dimensionAddress, dimension.length);
            }
        }
    }
    return ret;
}
Also used : lombok.val(lombok.val) PagedPointer(org.nd4j.linalg.api.memory.pointers.PagedPointer) Variance(org.nd4j.linalg.api.ops.impl.accum.Variance) INDArray(org.nd4j.linalg.api.ndarray.INDArray) LongPointerWrapper(org.nd4j.nativeblas.LongPointerWrapper) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 54 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class NativeOpExecutioner method bitmapEncode.

@Override
public long bitmapEncode(INDArray indArray, INDArray target, double threshold) {
    long length = indArray.lengthLong();
    long tLen = target.data().length();
    if (tLen != (length / 16 + 5))
        throw new ND4JIllegalStateException("Length of target array should be " + (length / 16 + 5));
    if (target.data().dataType() != DataBuffer.Type.INT)
        throw new ND4JIllegalStateException("Target array should have INT dataType");
    DataBuffer buffer = target.data();
    buffer.put(0, (int) length);
    buffer.put(1, (int) length);
    buffer.put(2, Float.floatToIntBits((float) threshold));
    // format id
    buffer.put(3, ThresholdCompression.BITMAP_ENCODING);
    long affected = 0;
    if (indArray.data().dataType() == DataBuffer.Type.FLOAT) {
        affected = loop.encodeBitmapFloat(null, (FloatPointer) indArray.data().addressPointer(), length, (IntPointer) buffer.addressPointer(), (float) threshold);
    } else if (indArray.data().dataType() == DataBuffer.Type.DOUBLE) {
        affected = loop.encodeBitmapDouble(null, (DoublePointer) indArray.data().addressPointer(), length, (IntPointer) buffer.addressPointer(), (float) threshold);
    } else
        throw new UnsupportedOperationException("HALF precision isn't supported on CPU yet");
    return affected;
}
Also used : ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 55 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class NativeOpExecutioner method thresholdDecode.

@Override
public INDArray thresholdDecode(INDArray encoded, INDArray target) {
    DataBuffer buffer = encoded.data();
    if (buffer.dataType() != DataBuffer.Type.INT)
        throw new ND4JIllegalStateException("thresholdEncoded array should have dataType of INT");
    long compressedLength = buffer.getInt(0);
    long originalLength = buffer.getInt(1);
    float threshold = buffer.getInt(2);
    if (target.lengthLong() != originalLength)
        throw new ND4JIllegalStateException("originalLength [" + originalLength + "] stored in encoded array doesn't match target length [" + target.lengthLong() + "]");
    DataBuffer.TypeEx typeDst = AbstractCompressor.getBufferTypeEx(target.data());
    loop.convertTypes(null, DataBuffer.TypeEx.THRESHOLD.ordinal(), buffer.addressPointer(), target.length(), typeDst.ordinal(), target.data().addressPointer());
    return target;
}
Also used : ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Aggregations

DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)186 INDArray (org.nd4j.linalg.api.ndarray.INDArray)79 Test (org.junit.Test)47 CompressedDataBuffer (org.nd4j.linalg.compression.CompressedDataBuffer)44 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)39 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)30 AllocationPoint (org.nd4j.jita.allocator.impl.AllocationPoint)25 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)23 BaseDataBuffer (org.nd4j.linalg.api.buffer.BaseDataBuffer)19 Pointer (org.bytedeco.javacpp.Pointer)18 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)16 CudaDoubleDataBuffer (org.nd4j.linalg.jcublas.buffer.CudaDoubleDataBuffer)16 IntPointer (org.bytedeco.javacpp.IntPointer)13 PagedPointer (org.nd4j.linalg.api.memory.pointers.PagedPointer)13 CudaIntDataBuffer (org.nd4j.linalg.jcublas.buffer.CudaIntDataBuffer)13 DoublePointer (org.bytedeco.javacpp.DoublePointer)12 FloatPointer (org.bytedeco.javacpp.FloatPointer)12 GridExecutioner (org.nd4j.linalg.api.ops.executioner.GridExecutioner)12 LongPointerWrapper (org.nd4j.nativeblas.LongPointerWrapper)11 CUstream_st (org.bytedeco.javacpp.cuda.CUstream_st)10