Search in sources :

Example 26 with IntPointer

use of org.bytedeco.javacpp.IntPointer in project nd4j by deeplearning4j.

the class MetaOpTests method testPooling2D.

@Test
public void testPooling2D() {
    Nd4j.create(1);
    val input = Nd4j.linspace(1, 600, 600).reshape(2, 10, 10, 3);
    val permuted = input.permute(0, 3, 1, 2);
    val nativeOps = NativeOpsHolder.getInstance().getDeviceNativeOps();
    val output = Nd4j.create(2, 3, 4, 4);
    val context = AtomicAllocator.getInstance().getFlowController().prepareAction(output, permuted);
    val ptrBIn = (FloatPointer) AtomicAllocator.getInstance().getPointer(permuted, context);
    val ptrBOut = (FloatPointer) AtomicAllocator.getInstance().getPointer(output, context);
    val ptrSIn = (IntPointer) AtomicAllocator.getInstance().getPointer(permuted.shapeInfoDataBuffer());
    val ptrSOut = (IntPointer) AtomicAllocator.getInstance().getPointer(output.shapeInfoDataBuffer());
    // kY  kX  sY  sX  pY  pX  dY  dX  N   M   P
    val bufParams = Nd4j.getConstantHandler().getConstantBuffer(new float[] { 3, 3, 3, 3, 1, 1, 1, 1, 1, 2, 2 });
    val ptrBParams = (FloatPointer) AtomicAllocator.getInstance().getPointer(bufParams, context);
    PointerPointer xShapeInfoHostPointer = // 0
    new PointerPointer(32).put(// 0
    AddressRetriever.retrieveHostPointer(permuted.shapeInfoDataBuffer()), // 1
    context.getOldStream(), // 2
    AtomicAllocator.getInstance().getDeviceIdPointer(), // 3
    context.getBufferAllocation(), // 4
    context.getBufferReduction(), // 5
    context.getBufferScalar(), // 6
    context.getBufferSpecial(), // 7
    null, // 8
    AddressRetriever.retrieveHostPointer(output.shapeInfoDataBuffer()));
    nativeOps.execTransformFloat(xShapeInfoHostPointer, 71, ptrBIn, ptrSIn, ptrBOut, ptrSOut, ptrBParams);
    AtomicAllocator.getInstance().getFlowController().registerAction(context, output, permuted);
    nativeOps.streamSynchronize(context.getOldStream());
    nativeOps.streamSynchronize(context.getOldStream());
    val reverted = output.permute(0, 2, 3, 1);
    System.out.println("Result: " + reverted.toString());
}
Also used : lombok.val(lombok.val) FloatPointer(org.bytedeco.javacpp.FloatPointer) PointerPointer(org.bytedeco.javacpp.PointerPointer) IntPointer(org.bytedeco.javacpp.IntPointer) Test(org.junit.Test)

Example 27 with IntPointer

use of org.bytedeco.javacpp.IntPointer 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);
    }
}
Also used : LongBuffer(org.nd4j.linalg.api.buffer.LongBuffer) IntBuffer(org.nd4j.linalg.api.buffer.IntBuffer) LongPointerWrapper(org.nd4j.nativeblas.LongPointerWrapper) IntPointer(org.bytedeco.javacpp.IntPointer) Pointer(org.bytedeco.javacpp.Pointer) TadDescriptor(org.nd4j.linalg.cache.TadDescriptor) Pair(org.nd4j.linalg.primitives.Pair) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 28 with IntPointer

use of org.bytedeco.javacpp.IntPointer in project nd4j by deeplearning4j.

the class CpuFlexibleThreshold method compress.

@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[] { 1, (int) buffer.length() }).getFirst());
    double max = temp.amaxNumber().doubleValue();
    int cntAbs = temp.scan(Conditions.absGreaterThanOrEqual(max - (max * threshold))).intValue();
    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    // please note, this value will be ovewritten anyway
    pointer.put(2, Float.floatToIntBits(threshold));
    pointer.put(3, 0);
    CompressionDescriptor descriptor = new CompressionDescriptor();
    // sizeOf(INT)
    descriptor.setCompressedLength(compressedLength * 4);
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());
    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());
    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);
    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.FTHRESHOLD, pointer, buffer.length());
    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);
    return cbuff;
}
Also used : CompressionDescriptor(org.nd4j.linalg.compression.CompressionDescriptor) INDArray(org.nd4j.linalg.api.ndarray.INDArray) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer) IntPointer(org.bytedeco.javacpp.IntPointer)

Example 29 with IntPointer

use of org.bytedeco.javacpp.IntPointer in project nd4j by deeplearning4j.

the class CpuThreshold method compress.

@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[] { 1, (int) buffer.length() }).getFirst());
    MatchCondition condition = new MatchCondition(temp, Conditions.absGreaterThanOrEqual(threshold));
    int cntAbs = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);
    if (cntAbs < 2)
        return null;
    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold));
    pointer.put(3, 0);
    CompressionDescriptor descriptor = new CompressionDescriptor();
    // sizeOf(INT)
    descriptor.setCompressedLength(compressedLength * 4);
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());
    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());
    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);
    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.THRESHOLD, pointer, buffer.length());
    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);
    return cbuff;
}
Also used : CompressionDescriptor(org.nd4j.linalg.compression.CompressionDescriptor) INDArray(org.nd4j.linalg.api.ndarray.INDArray) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer) IntPointer(org.bytedeco.javacpp.IntPointer) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition)

Example 30 with IntPointer

use of org.bytedeco.javacpp.IntPointer in project nd4j by deeplearning4j.

the class CpuSparseNDArrayFactory method sort.

@Override
public INDArray sort(INDArray x, boolean descending, int... dimension) {
    if (x.isScalar())
        return x;
    Arrays.sort(dimension);
    Pair<DataBuffer, DataBuffer> tadBuffers = Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(x, dimension);
    if (x.data().dataType() == DataBuffer.Type.FLOAT) {
        NativeOpsHolder.getInstance().getDeviceNativeOps().sortTadFloat(null, (FloatPointer) x.data().addressPointer(), (IntPointer) x.shapeInfoDataBuffer().addressPointer(), new IntPointer(dimension), dimension.length, (IntPointer) tadBuffers.getFirst().addressPointer(), new LongPointerWrapper(tadBuffers.getSecond().addressPointer()), descending);
    } else if (x.data().dataType() == DataBuffer.Type.DOUBLE) {
        NativeOpsHolder.getInstance().getDeviceNativeOps().sortTadDouble(null, (DoublePointer) x.data().addressPointer(), (IntPointer) x.shapeInfoDataBuffer().addressPointer(), new IntPointer(dimension), dimension.length, (IntPointer) tadBuffers.getFirst().addressPointer(), new LongPointerWrapper(tadBuffers.getSecond().addressPointer()), descending);
    } else {
        throw new UnsupportedOperationException("Unknown datatype " + x.data().dataType());
    }
    return x;
}
Also used : IntPointer(org.bytedeco.javacpp.IntPointer) DoublePointer(org.bytedeco.javacpp.DoublePointer) LongPointerWrapper(org.nd4j.nativeblas.LongPointerWrapper) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Aggregations

IntPointer (org.bytedeco.javacpp.IntPointer)35 DoublePointer (org.bytedeco.javacpp.DoublePointer)19 FloatPointer (org.bytedeco.javacpp.FloatPointer)19 Pointer (org.bytedeco.javacpp.Pointer)14 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)13 INDArray (org.nd4j.linalg.api.ndarray.INDArray)13 CUstream_st (org.bytedeco.javacpp.cuda.CUstream_st)12 CublasPointer (org.nd4j.linalg.jcublas.CublasPointer)12 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)12 BytePointer (org.bytedeco.javacpp.BytePointer)11 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)10 org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t (org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t)10 GridExecutioner (org.nd4j.linalg.api.ops.executioner.GridExecutioner)10 ByteBuffer (java.nio.ByteBuffer)8 BlasException (org.nd4j.linalg.api.blas.BlasException)8 IntBuffer (java.nio.IntBuffer)7 DoubleBuffer (java.nio.DoubleBuffer)6 FloatBuffer (java.nio.FloatBuffer)6 ShortBuffer (java.nio.ShortBuffer)6 ShortPointer (org.bytedeco.javacpp.ShortPointer)6