Search in sources :

Example 1 with GridPointers

use of org.nd4j.linalg.api.ops.grid.GridPointers in project nd4j by deeplearning4j.

the class GridExecutionerTest method testOpPointerizeScalar1.

// ///////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////
/*
    Pointerize tests are checking how Ops are converted into GridPointers
*/
// ///////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////
@Test
public void testOpPointerizeScalar1() throws Exception {
    CudaGridExecutioner executioner = new CudaGridExecutioner();
    INDArray array = Nd4j.create(10);
    ScalarMultiplication opA = new ScalarMultiplication(array, 10f);
    GridPointers pointers = executioner.pointerizeOp(opA, null);
    assertEquals(opA.opNum(), pointers.getOpNum());
    assertEquals(Op.Type.SCALAR, pointers.getType());
    CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();
    Pointer x = AtomicAllocator.getInstance().getPointer(array, context);
    Pointer xShapeInfo = AtomicAllocator.getInstance().getPointer(array.shapeInfoDataBuffer(), context);
    assertEquals(x, pointers.getX());
    assertEquals(null, pointers.getY());
    assertEquals(x, pointers.getZ());
    assertEquals(1, pointers.getXStride());
    assertEquals(-1, pointers.getYStride());
    assertEquals(1, pointers.getZStride());
    assertEquals(xShapeInfo, pointers.getXShapeInfo());
    assertEquals(null, pointers.getYShapeInfo());
    assertEquals(xShapeInfo, 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) ScalarMultiplication(org.nd4j.linalg.api.ops.impl.scalar.ScalarMultiplication) Pointer(org.bytedeco.javacpp.Pointer) Test(org.junit.Test)

Example 2 with GridPointers

use of org.nd4j.linalg.api.ops.grid.GridPointers 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 3 with GridPointers

use of org.nd4j.linalg.api.ops.grid.GridPointers in project nd4j by deeplearning4j.

the class CudaGridExecutioner method prepareGrid.

protected void prepareGrid(MetaOp op) {
    GridPointers ptrA = pointerizeOp(op.getFirstOpDescriptor());
    GridPointers ptrB = pointerizeOp(op.getSecondOpDescriptor());
    op.setFirstPointers(ptrA);
    op.setSecondPointers(ptrB);
}
Also used : GridPointers(org.nd4j.linalg.api.ops.grid.GridPointers)

Example 4 with GridPointers

use of org.nd4j.linalg.api.ops.grid.GridPointers in project nd4j by deeplearning4j.

the class CudaGridExecutioner method exec.

@Override
public void exec(MetaOp op) {
    if (extraz.get() == null)
        extraz.set(new PointerPointer(32));
    prepareGrid(op);
    GridPointers first = op.getGridDescriptor().getGridPointers().get(0);
    GridPointers second = op.getGridDescriptor().getGridPointers().get(1);
    // we need to use it only for first op, since for MetaOps second op shares the same X & Z by definition
    CudaContext context = AtomicAllocator.getInstance().getFlowController().prepareAction(first.getOpZ(), first.getOpY());
    // AtomicAllocator.getInstance().getFlowController().prepareAction(second.getOpX(), second.getOpY(), second.getOpZ());
    // CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();
    PointerPointer extras = extraz.get().put(null, context.getOldStream());
    double scalarA = 0.0;
    double scalarB = 0.0;
    if (op.getFirstOp() instanceof ScalarOp)
        scalarA = ((ScalarOp) op.getFirstOp()).scalar().doubleValue();
    if (op.getSecondOp() instanceof ScalarOp)
        scalarB = ((ScalarOp) op.getSecondOp()).scalar().doubleValue();
    // logger.info("FirstOp: {}, SecondOp: {}", op.getFirstOp().getClass().getSimpleName(), op.getSecondOp().getClass().getSimpleName());
    /*
            TODO: launch can be either strided, or shapeInfo-based, it doesn't really matters for us.
            We just need to pass all pointers.
        
            TODO: obviously, execMetaPredicateElementwiseFloat should be renamed to execMetaPredicateStridedFloat
         */
    // FIXME: this is bad hack, reconsider this one
    GridPointers yGrid = first;
    if (op.getSecondOp().y() != null) {
        yGrid = second;
    }
    if (op instanceof PredicateMetaOp || op instanceof InvertedPredicateMetaOp) {
        if (first.getDtype() == DataBuffer.Type.FLOAT) {
            if (yGrid.getYOrder() == yGrid.getXOrder() && yGrid.getXStride() >= 1 && yGrid.getYStride() >= 1) {
                nativeOps.execMetaPredicateStridedFloat(extras, first.getType().ordinal(), first.getOpNum(), second.getType().ordinal(), second.getOpNum(), first.getXLength(), // can be null
                (FloatPointer) first.getX(), // can be null
                first.getXStride(), // can be null
                (FloatPointer) yGrid.getY(), // cane be -1
                yGrid.getYStride(), (FloatPointer) second.getZ(), second.getZStride(), (FloatPointer) first.getExtraArgs(), (FloatPointer) second.getExtraArgs(), (float) scalarA, (float) scalarB);
            } else {
                nativeOps.execMetaPredicateShapeFloat(extras, first.getType().ordinal(), first.getOpNum(), second.getType().ordinal(), second.getOpNum(), first.getXLength(), (FloatPointer) first.getX(), (IntPointer) first.getXShapeInfo(), // can be null
                (FloatPointer) yGrid.getY(), // cane be -1
                (IntPointer) yGrid.getYShapeInfo(), (FloatPointer) second.getZ(), (IntPointer) second.getZShapeInfo(), (FloatPointer) first.getExtraArgs(), (FloatPointer) second.getExtraArgs(), (float) scalarA, (float) scalarB);
            }
        } else if (first.getDtype() == DataBuffer.Type.DOUBLE) {
            if (yGrid.getYOrder() == yGrid.getXOrder() && yGrid.getXStride() >= 1 && yGrid.getYStride() >= 1) {
                nativeOps.execMetaPredicateStridedDouble(extras, first.getType().ordinal(), first.getOpNum(), second.getType().ordinal(), second.getOpNum(), first.getXLength(), // can be null
                (DoublePointer) first.getX(), // can be null
                first.getXStride(), // can be null
                (DoublePointer) yGrid.getY(), // cane be -1
                yGrid.getYStride(), (DoublePointer) second.getZ(), second.getZStride(), (DoublePointer) first.getExtraArgs(), (DoublePointer) second.getExtraArgs(), scalarA, scalarB);
            } else {
                nativeOps.execMetaPredicateShapeDouble(extras, first.getType().ordinal(), first.getOpNum(), second.getType().ordinal(), second.getOpNum(), first.getXLength(), (DoublePointer) first.getX(), (IntPointer) first.getXShapeInfo(), // can be null
                (DoublePointer) yGrid.getY(), // cane be -1
                (IntPointer) yGrid.getYShapeInfo(), (DoublePointer) second.getZ(), (IntPointer) second.getZShapeInfo(), (DoublePointer) first.getExtraArgs(), (DoublePointer) second.getExtraArgs(), scalarA, scalarB);
            }
        } else {
            if (yGrid.getYOrder() == yGrid.getXOrder() && yGrid.getXStride() >= 1 && yGrid.getYStride() >= 1) {
                nativeOps.execMetaPredicateStridedHalf(extras, first.getType().ordinal(), first.getOpNum(), second.getType().ordinal(), second.getOpNum(), first.getXLength(), // can be null
                (ShortPointer) first.getX(), // can be null
                first.getXStride(), // can be null
                (ShortPointer) yGrid.getY(), // cane be -1
                yGrid.getYStride(), (ShortPointer) second.getZ(), second.getZStride(), (ShortPointer) first.getExtraArgs(), (ShortPointer) second.getExtraArgs(), (float) scalarA, (float) scalarB);
            } else {
                nativeOps.execMetaPredicateShapeHalf(extras, first.getType().ordinal(), first.getOpNum(), second.getType().ordinal(), second.getOpNum(), first.getXLength(), (ShortPointer) first.getX(), (IntPointer) first.getXShapeInfo(), // can be null
                (ShortPointer) yGrid.getY(), // cane be -1
                (IntPointer) yGrid.getYShapeInfo(), (ShortPointer) second.getZ(), (IntPointer) second.getZShapeInfo(), (ShortPointer) first.getExtraArgs(), (ShortPointer) second.getExtraArgs(), (float) scalarA, (float) scalarB);
            }
        }
    } else if (op instanceof ReduceMetaOp) {
        if (first.getDtype() == DataBuffer.Type.FLOAT) {
            nativeOps.execMetaPredicateReduceFloat(extras, first.getType().ordinal(), first.getOpNum(), second.getType().ordinal(), second.getOpNum(), (FloatPointer) first.getX(), (IntPointer) first.getXShapeInfo(), (FloatPointer) second.getY(), (IntPointer) second.getYShapeInfo(), (FloatPointer) second.getZ(), (IntPointer) second.getZShapeInfo(), (IntPointer) second.getDimensions(), second.getDimensionsLength(), (IntPointer) second.getTadShape(), new LongPointerWrapper(second.getTadOffsets()), (FloatPointer) first.getExtraArgs(), (FloatPointer) second.getExtraArgs(), (float) scalarA, 0.0f, false);
        }
    }
    AtomicAllocator.getInstance().getFlowController().registerAction(context, first.getOpZ(), first.getOpY());
// AtomicAllocator.getInstance().getFlowController().registerAction(context, second.getOpX(), second.getOpY(), second.getOpZ());
}
Also used : ReduceMetaOp(org.nd4j.linalg.api.ops.impl.meta.ReduceMetaOp) GridPointers(org.nd4j.linalg.api.ops.grid.GridPointers) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) InvertedPredicateMetaOp(org.nd4j.linalg.api.ops.impl.meta.InvertedPredicateMetaOp) PredicateMetaOp(org.nd4j.linalg.api.ops.impl.meta.PredicateMetaOp) InvertedPredicateMetaOp(org.nd4j.linalg.api.ops.impl.meta.InvertedPredicateMetaOp) LongPointerWrapper(org.nd4j.nativeblas.LongPointerWrapper)

Example 5 with GridPointers

use of org.nd4j.linalg.api.ops.grid.GridPointers in project nd4j by deeplearning4j.

the class CudaGridExecutioner method pointerizeOp.

/**
 * This method returns Op as set of required pointers for it
 * @param op
 * @param dimensions
 * @return
 */
protected GridPointers pointerizeOp(Op op, int... dimensions) {
    GridPointers pointers = new GridPointers(op, dimensions);
    AtomicAllocator allocator = AtomicAllocator.getInstance();
    // CudaContext context = AtomicAllocator.getInstance().getFlowController().prepareAction(op.z(), op.x(), op.y());
    // FIXME: do not leave it as is
    CudaContext context = (CudaContext) allocator.getDeviceContext().getContext();
    pointers.setX(allocator.getPointer(op.x(), context));
    pointers.setXShapeInfo(allocator.getPointer(op.x().shapeInfoDataBuffer(), context));
    pointers.setZ(allocator.getPointer(op.z(), context));
    pointers.setZShapeInfo(allocator.getPointer(op.z().shapeInfoDataBuffer(), context));
    pointers.setZLength(op.z().length());
    if (op.y() != null) {
        pointers.setY(allocator.getPointer(op.y(), context));
        pointers.setYShapeInfo(allocator.getPointer(op.y().shapeInfoDataBuffer(), context));
    }
    if (dimensions != null && dimensions.length > 0) {
        DataBuffer dimensionBuffer = Nd4j.getConstantHandler().getConstantBuffer(dimensions);
        pointers.setDimensions(allocator.getPointer(dimensionBuffer, context));
        pointers.setDimensionsLength(dimensions.length);
    }
    // we build TADs
    if (dimensions != null && dimensions.length > 0) {
        Pair<DataBuffer, DataBuffer> tadBuffers = tadManager.getTADOnlyShapeInfo(op.x(), dimensions);
        Pointer devTadShapeInfo = AtomicAllocator.getInstance().getPointer(tadBuffers.getFirst(), context);
        Pointer devTadOffsets = tadBuffers.getSecond() == null ? null : AtomicAllocator.getInstance().getPointer(tadBuffers.getSecond(), context);
        // we don't really care, if tadOffsets will be nulls
        pointers.setTadShape(devTadShapeInfo);
        pointers.setTadOffsets(devTadOffsets);
    }
    return pointers;
}
Also used : GridPointers(org.nd4j.linalg.api.ops.grid.GridPointers) AtomicAllocator(org.nd4j.jita.allocator.impl.AtomicAllocator) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Aggregations

GridPointers (org.nd4j.linalg.api.ops.grid.GridPointers)6 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)5 Pointer (org.bytedeco.javacpp.Pointer)3 Test (org.junit.Test)3 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)3 INDArray (org.nd4j.linalg.api.ndarray.INDArray)3 Sum (org.nd4j.linalg.api.ops.impl.accum.Sum)2 AtomicAllocator (org.nd4j.jita.allocator.impl.AtomicAllocator)1 InvertedPredicateMetaOp (org.nd4j.linalg.api.ops.impl.meta.InvertedPredicateMetaOp)1 PredicateMetaOp (org.nd4j.linalg.api.ops.impl.meta.PredicateMetaOp)1 ReduceMetaOp (org.nd4j.linalg.api.ops.impl.meta.ReduceMetaOp)1 ScalarMultiplication (org.nd4j.linalg.api.ops.impl.scalar.ScalarMultiplication)1 LongPointerWrapper (org.nd4j.nativeblas.LongPointerWrapper)1