Search in sources :

Example 56 with DataBuffer

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

the class NativeOpExecutioner method exec.

private void exec(TransformOp op) {
    long st = 0;
    validateDataType(Nd4j.dataType(), op);
    if (extraz.get() == null)
        extraz.set(new PointerPointer(32));
    PointerPointer dummy = extraz.get();
    // Pow operations might be special
    if (op.opNum() == 7) {
        if (op.y() != null && op.y().isScalar()) {
            op.setY(Nd4j.valueArrayOf(op.x().shape(), op.y().getDouble(0)));
        }
    }
    /**
     * This is the {@link org.nd4j.linalg.api.ops.impl.transforms.IsMax}
     * operation.
     *
     * @see {@link Op#extraArgs()}
     * for what an extra argument is in an op.
     *
     * The extra argument in the op here is the {@link org.nd4j.linalg.api.ops.impl.transforms.IsMax#IsMax(INDArray, int...)}
     * dimension to do the ismax along
     */
    if (op.opNum() == 41 && op.extraArgs() != null) {
        int[] dimension = new int[(int) op.extraArgs()[0]];
        for (int i = 0; i < dimension.length; i++) {
            dimension[i] = (int) op.extraArgs()[i + 1];
        }
        /**
         * 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.z(), dimension);
        Pointer tad = tadBuffers.getFirst().addressPointer();
        DataBuffer offsets = tadBuffers.getSecond();
        Pointer off = offsets == null ? null : offsets.addressPointer();
        dummy.put(0, tad);
        dummy.put(1, off);
        st = profilingHookIn(op, tadBuffers.getFirst());
    } else
        st = profilingHookIn(op);
    if (op.x().data().dataType() == DataBuffer.Type.DOUBLE) {
        if (op.y() != null) {
            int xEWS = op.x().elementWiseStride();
            int yEWS = op.y().elementWiseStride();
            int zEWS = op.z().elementWiseStride();
            boolean xRow = op.x().isRowVector();
            boolean yRow = op.y().isRowVector();
            boolean zRow = op.z().isRowVector();
            if ((xEWS >= 1 && yEWS >= 1 && xEWS == yEWS && !op.isExecSpecial() && op.x().ordering() == op.y().ordering() && op.x().ordering() == op.z().ordering()) || (xEWS >= 1 && yEWS == xEWS && zEWS == xEWS && xRow && yRow && zRow)) {
                loop.execPairwiseTransformDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), xEWS, (DoublePointer) op.y().data().addressPointer(), yEWS, (DoublePointer) op.z().data().addressPointer(), zEWS, (DoublePointer) getPointerForExtraArgs(op), op.n());
            } else {
                loop.execPairwiseTransformDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) op.y().data().addressPointer(), (IntPointer) op.y().shapeInfoDataBuffer().addressPointer(), (DoublePointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op));
            }
        } else {
            if (op.x().elementWiseStride() >= 1 && !op.isExecSpecial() && !op.isExecSpecial() && op.x().ordering() == op.z().ordering()) {
                loop.execTransformDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), op.x().elementWiseStride(), (DoublePointer) op.z().data().addressPointer(), op.z().elementWiseStride(), (DoublePointer) getPointerForExtraArgs(op), op.n());
            } else {
                loop.execTransformDouble(dummy, op.opNum(), (DoublePointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (DoublePointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (DoublePointer) getPointerForExtraArgs(op));
            }
        }
    } else {
        if (op.y() != null) {
            int xEWS = op.x().elementWiseStride();
            int yEWS = op.y().elementWiseStride();
            int zEWS = op.z().elementWiseStride();
            boolean xRow = op.x().isRowVector();
            boolean yRow = op.y().isRowVector();
            boolean zRow = op.z().isRowVector();
            if ((xEWS >= 1 && yEWS >= 1 && xEWS == yEWS && !op.isExecSpecial() && op.x().ordering() == op.y().ordering() && op.x().ordering() == op.z().ordering()) || (xEWS >= 1 && yEWS == xEWS && zEWS == xEWS && xRow && yRow && zRow)) {
                loop.execPairwiseTransformFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), xEWS, (FloatPointer) op.y().data().addressPointer(), yEWS, (FloatPointer) op.z().data().addressPointer(), zEWS, (FloatPointer) getPointerForExtraArgs(op), op.n());
            } else {
                loop.execPairwiseTransformFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) op.y().data().addressPointer(), (IntPointer) op.y().shapeInfoDataBuffer().addressPointer(), (FloatPointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op));
            }
        } else {
            if (op.x().elementWiseStride() >= 1 && !op.isExecSpecial() && op.x().ordering() == op.z().ordering()) {
                loop.execTransformFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), op.x().elementWiseStride(), (FloatPointer) op.z().data().addressPointer(), op.z().elementWiseStride(), (FloatPointer) getPointerForExtraArgs(op), op.n());
            } else {
                loop.execTransformFloat(dummy, op.opNum(), (FloatPointer) op.x().data().addressPointer(), (IntPointer) op.x().shapeInfoDataBuffer().addressPointer(), (FloatPointer) op.z().data().addressPointer(), (IntPointer) op.z().shapeInfoDataBuffer().addressPointer(), (FloatPointer) getPointerForExtraArgs(op));
            }
        }
    }
    profilingHookOut(op, st);
}
Also used : PagedPointer(org.nd4j.linalg.api.memory.pointers.PagedPointer) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 57 with DataBuffer

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

the class GridExecutionerTest method testOpPointerizeReduce2.

/**
 * Reduce along all dimensions
 *
 * @throws Exception
 */
@Test
public void testOpPointerizeReduce2() throws Exception {
    CudaGridExecutioner executioner = new CudaGridExecutioner();
    INDArray array = Nd4j.create(10, 10);
    Sum opA = new Sum(array);
    // we need exec here, to init Op.Z for specific dimension
    executioner.exec(opA);
    GridPointers pointers = executioner.pointerizeOp(opA, null);
    assertEquals(opA.opNum(), pointers.getOpNum());
    assertEquals(Op.Type.REDUCE, pointers.getType());
    CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();
    Pointer x = AtomicAllocator.getInstance().getPointer(array, context);
    Pointer xShapeInfo = AtomicAllocator.getInstance().getPointer(array.shapeInfoDataBuffer(), context);
    Pointer z = AtomicAllocator.getInstance().getPointer(opA.z(), context);
    Pointer zShapeInfo = AtomicAllocator.getInstance().getPointer(opA.z().shapeInfoDataBuffer(), context);
    DataBuffer dimBuff = Nd4j.getConstantHandler().getConstantBuffer(new int[] { 1 });
    Pointer ptrBuff = AtomicAllocator.getInstance().getPointer(dimBuff, context);
    assertEquals(x, pointers.getX());
    assertEquals(null, pointers.getY());
    assertNotEquals(null, pointers.getZ());
    assertEquals(z, pointers.getZ());
    assertEquals(1, opA.z().length());
    assertEquals(1, pointers.getZLength());
    /*      // We dont really care about EWS here, since we're testing TAD-based operation

        assertEquals(1, pointers.getXStride());
        assertEquals(-1, pointers.getYStride());
        assertEquals(1, pointers.getZStride());
*/
    assertEquals(xShapeInfo, pointers.getXShapeInfo());
    assertEquals(null, pointers.getYShapeInfo());
    assertEquals(zShapeInfo, pointers.getZShapeInfo());
    assertEquals(null, pointers.getDimensions());
    assertEquals(0, pointers.getDimensionsLength());
    assertEquals(null, pointers.getTadShape());
    assertEquals(null, pointers.getTadOffsets());
    assertEquals(null, pointers.getExtraArgs());
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) GridPointers(org.nd4j.linalg.api.ops.grid.GridPointers) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) Sum(org.nd4j.linalg.api.ops.impl.accum.Sum) Pointer(org.bytedeco.javacpp.Pointer) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) Test(org.junit.Test)

Example 58 with DataBuffer

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

the class CpuNDArrayFactory method convertDataEx.

/**
 * This method converts Half-precision databuffer to current dType buffer.
 *
 * @param buffer
 * @return
 */
/*
    @Override
    public DataBuffer restoreFromHalfs(DataBuffer buffer) {
        if (buffer.dataType() != DataBuffer.Type.COMPRESSED)
            throw new IllegalStateException("DataBuffer contains wrong data: " + buffer.dataType());
    
        CompressedDataBuffer comp = (CompressedDataBuffer) buffer;
        CompressionDescriptor descriptor = comp.getCompressionDescriptor();
    
        DataBuffer targetBuffer = Nd4j.createBuffer(descriptor.getCompressedLength() / 2);
    
        if (Nd4j.dataType() == DataBuffer.Type.DOUBLE) {
            nativeOps.convertHalfsToDoubles(
                    null,
                    comp.addressPointer(),
                    (int) descriptor.getCompressedLength() / 2,
                    targetBuffer.addressPointer()
            );
        } else if (Nd4j.dataType() == DataBuffer.Type.FLOAT) {
            nativeOps.convertHalfsToFloats(
                    null,
                    comp.addressPointer(),
                    (int) descriptor.getCompressedLength() / 2,
                    targetBuffer.addressPointer()
            );
        } else {
            throw new UnsupportedOperationException("Target dtype isn't supported: " + Nd4j.dataType());
        }
    
        return targetBuffer;
    }
    */
/**
 * This method converts Single/Double precision databuffer to Half-precision databuffer
 *
 * @param buffer
 * @return
 */
/*@Override
    public DataBuffer convertToHalfs(DataBuffer buffer) {
        // we allocate pointer
        ShortPointer pointer = new ShortPointer(buffer.length());
    
        if (buffer.dataType() == DataBuffer.Type.DOUBLE) {
            nativeOps.convertDoublesToHalfs(
                    null,
                    buffer.addressPointer(),
                    (int) buffer.length(),
                    pointer
            );
        } else if (buffer.dataType() == DataBuffer.Type.FLOAT) {
            nativeOps.convertFloatsToHalfs(
                    null,
                    buffer.addressPointer(),
                    (int) buffer.length(),
                    pointer
            );
        } else {
            throw new UnsupportedOperationException("Source dtype isn't supported: " + buffer.dataType());
        }
    
        CompressionDescriptor descriptor = new CompressionDescriptor(buffer, new Float16());
        descriptor.setCompressedLength(buffer.length() * 2);
    
    
        CompressedDataBuffer result = new CompressedDataBuffer(pointer, descriptor);
        return result;
    }
    */
/**
 * This method converts Single/Double precision databuffer to Half-precision databuffer
 *
 * @param typeSrc
 * @param source
 * @param typeDst @return
 */
@Override
public INDArray convertDataEx(DataBuffer.TypeEx typeSrc, INDArray source, DataBuffer.TypeEx typeDst) {
    if (source.isView())
        throw new UnsupportedOperationException("Impossible to compress View. Consider using dup() before. ");
    DataBuffer buffer = convertDataEx(typeSrc, source.data(), typeDst);
    source.setData(buffer);
    if (buffer instanceof CompressedDataBuffer)
        source.markAsCompressed(true);
    else
        source.markAsCompressed(false);
    return source;
}
Also used : CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer)

Example 59 with DataBuffer

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

the class CpuNDArrayFactory method shuffle.

/**
 * Symmetric in place shuffle of an ndarray
 * along a specified set of dimensions. Each array in list should have it's own dimension at the same index of dimensions array
 *
 * @param arrays      the ndarrays to shuffle
 * @param dimensions the dimensions to do the shuffle
 * @return
 */
@Override
public void shuffle(List<INDArray> arrays, Random rnd, List<int[]> dimensions) {
    if (dimensions == null || dimensions.size() == 0)
        throw new RuntimeException("Dimension can't be null or 0-length");
    if (arrays == null || arrays.size() == 0)
        throw new RuntimeException("No input arrays provided");
    if (dimensions.size() > 1 && arrays.size() != dimensions.size())
        throw new IllegalStateException("Number of dimensions do not match number of arrays to shuffle");
    int tadLength = 1;
    for (int i = 0; i < dimensions.get(0).length; i++) {
        tadLength *= arrays.get(0).shape()[dimensions.get(0)[i]];
    }
    int numTads = arrays.get(0).length() / tadLength;
    int[] map = ArrayUtil.buildInterleavedVector(rnd, numTads);
    PointerPointer dataPointers = new PointerPointer(arrays.size());
    PointerPointer shapePointers = new PointerPointer(arrays.size());
    PointerPointer tadPointers = new PointerPointer(arrays.size());
    PointerPointer offsetPointers = new PointerPointer(arrays.size());
    PointerPointer dummy = new PointerPointer(new Pointer[] { null });
    List<Pair<DataBuffer, DataBuffer>> list = new ArrayList<>();
    TADManager tadManager = Nd4j.getExecutioner().getTADManager();
    IntPointer ptrMap = new IntPointer(map);
    long[] ptrs = new long[arrays.size()];
    for (int i = 0; i < arrays.size(); i++) {
        INDArray array = arrays.get(i);
        Nd4j.getCompressor().autoDecompress(array);
        int[] dimension = dimensions.size() > 1 ? dimensions.get(i) : dimensions.get(0);
        Pair<DataBuffer, DataBuffer> tadBuffers = tadManager.getTADOnlyShapeInfo(array, dimension);
        list.add(tadBuffers);
        Pointer hostTadShapeInfo = tadBuffers.getFirst().addressPointer();
        DataBuffer offsets = tadBuffers.getSecond();
        if (offsets.length() != numTads)
            throw new ND4JIllegalStateException("Can't symmetrically shuffle arrays with non-equal number of TADs");
        if (offsets == null)
            throw new ND4JIllegalStateException("Offsets for shuffle can't be null");
        dataPointers.put(i, array.data().addressPointer());
        shapePointers.put(i, array.shapeInfoDataBuffer().addressPointer());
        offsetPointers.put(i, offsets.addressPointer());
        tadPointers.put(i, tadBuffers.getFirst().addressPointer());
    }
    if (Nd4j.dataType() == DataBuffer.Type.DOUBLE) {
        nativeOps.shuffleDouble(dummy, dataPointers, shapePointers, dataPointers, shapePointers, arrays.size(), ptrMap, tadPointers, offsetPointers);
    } else if (Nd4j.dataType() == DataBuffer.Type.FLOAT) {
        nativeOps.shuffleFloat(dummy, dataPointers, shapePointers, dataPointers, shapePointers, arrays.size(), ptrMap, tadPointers, offsetPointers);
    } else {
    // HALFs
    }
    dataPointers.address();
    shapePointers.address();
    tadPointers.address();
    offsetPointers.address();
}
Also used : ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) INDArray(org.nd4j.linalg.api.ndarray.INDArray) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) TADManager(org.nd4j.linalg.cache.TADManager) Pair(org.nd4j.linalg.primitives.Pair) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer)

Example 60 with DataBuffer

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

the class CpuNDArrayFactory 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(), (IntPointer) Nd4j.getConstantHandler().getConstantBuffer(dimension).addressPointer(), 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(), (IntPointer) Nd4j.getConstantHandler().getConstantBuffer(dimension).addressPointer(), dimension.length, (IntPointer) tadBuffers.getFirst().addressPointer(), new LongPointerWrapper(tadBuffers.getSecond().addressPointer()), descending);
    } else {
        throw new UnsupportedOperationException("Unknown dataype " + x.data().dataType());
    }
    return x;
}
Also used : LongPointerWrapper(org.nd4j.nativeblas.LongPointerWrapper) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer)

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