Search in sources :

Example 91 with ND4JIllegalStateException

use of org.nd4j.linalg.exception.ND4JIllegalStateException in project nd4j by deeplearning4j.

the class CudaExecutioner 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);
    CudaContext context = AtomicAllocator.getInstance().getFlowController().prepareAction(indArray);
    if (extraz.get() == null)
        extraz.set(new PointerPointer(32));
    PointerPointer extras = extraz.get().put(AtomicAllocator.getInstance().getHostPointer(indArray), context.getOldStream(), context.getBufferScalar(), context.getBufferReduction());
    long val = 0;
    if (indArray.data().dataType() == DataBuffer.Type.FLOAT) {
        val = nativeOps.encodeBitmapFloat(extras, (FloatPointer) AtomicAllocator.getInstance().getPointer(indArray, context), length, (IntPointer) AtomicAllocator.getInstance().getPointer(buffer, context), (float) threshold);
    } else if (indArray.data().dataType() == DataBuffer.Type.DOUBLE) {
        val = nativeOps.encodeBitmapDouble(extras, (DoublePointer) AtomicAllocator.getInstance().getPointer(indArray, context), length, (IntPointer) AtomicAllocator.getInstance().getPointer(buffer, context), (float) threshold);
    } else if (indArray.data().dataType() == DataBuffer.Type.HALF) {
        val = nativeOps.encodeBitmapHalf(extras, (ShortPointer) AtomicAllocator.getInstance().getPointer(indArray, context), length, (IntPointer) AtomicAllocator.getInstance().getPointer(buffer, context), (float) threshold);
    } else
        throw new ND4JIllegalStateException("Unknown dataType " + indArray.data().dataType());
    AtomicAllocator.getInstance().getFlowController().registerAction(context, indArray);
    AtomicAllocator.getInstance().getAllocationPoint(buffer).tickDeviceWrite();
    return val;
}
Also used : CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) BaseDataBuffer(org.nd4j.linalg.api.buffer.BaseDataBuffer)

Example 92 with ND4JIllegalStateException

use of org.nd4j.linalg.exception.ND4JIllegalStateException in project nd4j by deeplearning4j.

the class CudaExecutioner method exec.

@Override
public INDArray exec(IndexAccumulation op, int... dimension) {
    long st = profilingHookIn(op);
    checkForCompression(op);
    validateDataType(Nd4j.dataType(), op);
    if (extraz.get() == null)
        extraz.set(new PointerPointer(32));
    Arrays.sort(dimension);
    for (int i = 0; i < dimension.length; i++) if (dimension[i] >= op.x().rank() && 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 = Shape.wholeArrayDimension(dimension) ? new int[] { 1, 1 } : ArrayUtil.removeIndex(op.x().shape(), dimension);
    if (op.x().isVector() && op.x().length() == ArrayUtil.prod(retShape)) {
        return op.x();
    }
    // 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 };
    }
    INDArray ret = null;
    if (0.0 + Math.abs(op.zeroDouble()) <= Nd4j.EPS_THRESHOLD) {
        ret = Nd4j.zeros(retShape);
    } else {
        if (op.x().data().dataType() == DataBuffer.Type.DOUBLE)
            ret = Nd4j.valueArrayOf(retShape, op.zeroDouble());
        else if (op.x().data().dataType() == DataBuffer.Type.FLOAT)
            ret = Nd4j.valueArrayOf(retShape, op.zeroFloat());
        else if (op.x().data().dataType() == DataBuffer.Type.HALF)
            ret = Nd4j.valueArrayOf(retShape, op.zeroHalf());
    }
    op.setZ(ret);
    // do op along all dimensions
    if (dimension.length == op.x().rank())
        dimension = new int[] { Integer.MAX_VALUE };
    if (CudaEnvironment.getInstance().getConfiguration().isDebug())
        lastOp.set(op.opName());
    CudaContext context = AtomicAllocator.getInstance().getFlowController().prepareAction(op.z(), op.x(), op.y());
    Pointer hostYShapeInfo = op.y() == null ? null : AddressRetriever.retrieveHostPointer(op.y().shapeInfoDataBuffer());
    Pointer hostZShapeInfo = op.z() == null ? null : AddressRetriever.retrieveHostPointer(op.z().shapeInfoDataBuffer());
    Pointer x = AtomicAllocator.getInstance().getPointer(op.x(), context);
    Pointer xShapeInfo = AtomicAllocator.getInstance().getPointer(op.x().shapeInfoDataBuffer(), context);
    Pointer z = AtomicAllocator.getInstance().getPointer(op.z(), context);
    Pointer zShapeInfo = AtomicAllocator.getInstance().getPointer(op.z().shapeInfoDataBuffer(), context);
    Pair<DataBuffer, DataBuffer> tadBuffers = tadManager.getTADOnlyShapeInfo(op.x(), dimension);
    Pointer hostTadShapeInfo = AddressRetriever.retrieveHostPointer(tadBuffers.getFirst());
    Pointer devTadShapeInfo = AtomicAllocator.getInstance().getPointer(tadBuffers.getFirst(), context);
    DataBuffer offsets = tadBuffers.getSecond();
    Pointer devTadOffsets = offsets == null ? null : AtomicAllocator.getInstance().getPointer(offsets, context);
    PointerPointer xShapeInfoHostPointer = extraz.get().put(AddressRetriever.retrieveHostPointer(op.x().shapeInfoDataBuffer()), context.getOldStream(), AtomicAllocator.getInstance().getDeviceIdPointer(), context.getBufferAllocation(), context.getBufferReduction(), context.getBufferScalar(), context.getBufferSpecial(), hostYShapeInfo, hostZShapeInfo, hostTadShapeInfo, devTadShapeInfo, devTadOffsets);
    Pointer extraArgs = op.extraArgs() != null ? AtomicAllocator.getInstance().getPointer(op.extraArgsDataBuff(), context) : null;
    // Pointer dimensionPointer = AtomicAllocator.getInstance().getPointer(Nd4j.createBuffer(dimension), context);
    Pointer dimensionPointer = AtomicAllocator.getInstance().getPointer(AtomicAllocator.getInstance().getConstantBuffer(dimension), context);
    if (op.x().data().dataType() == DataBuffer.Type.DOUBLE) {
        nativeOps.execIndexReduceDouble(xShapeInfoHostPointer, op.opNum(), (DoublePointer) x, (IntPointer) xShapeInfo, (DoublePointer) extraArgs, (DoublePointer) z, (IntPointer) zShapeInfo, (IntPointer) dimensionPointer, dimension.length);
    } else if (op.x().data().dataType() == DataBuffer.Type.FLOAT) {
        nativeOps.execIndexReduceFloat(xShapeInfoHostPointer, op.opNum(), (FloatPointer) x, (IntPointer) xShapeInfo, (FloatPointer) extraArgs, (FloatPointer) z, (IntPointer) zShapeInfo, (IntPointer) dimensionPointer, dimension.length);
    } else {
        nativeOps.execIndexReduceHalf(xShapeInfoHostPointer, op.opNum(), (ShortPointer) x, (IntPointer) xShapeInfo, (ShortPointer) extraArgs, (ShortPointer) z, (IntPointer) zShapeInfo, (IntPointer) dimensionPointer, dimension.length);
    }
    AtomicAllocator.getInstance().registerAction(context, op.z(), op.x(), op.y());
    profilingHookOut(op, st);
    return op.z();
}
Also used : CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) CudaPointer(org.nd4j.jita.allocator.pointers.CudaPointer) PagedPointer(org.nd4j.linalg.api.memory.pointers.PagedPointer) AllocationPoint(org.nd4j.jita.allocator.impl.AllocationPoint) INDArray(org.nd4j.linalg.api.ndarray.INDArray) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) BaseDataBuffer(org.nd4j.linalg.api.buffer.BaseDataBuffer)

Example 93 with ND4JIllegalStateException

use of org.nd4j.linalg.exception.ND4JIllegalStateException in project nd4j by deeplearning4j.

the class TensorMmul method calculateOutputShape.

@Override
public List<int[]> calculateOutputShape() {
    List<int[]> ret = new ArrayList<>(1);
    int[] aShape = mMulTranspose.isTransposeA() ? ArrayUtil.reverseCopy(larg().getShape()) : larg().getShape();
    int[] bShape = mMulTranspose.isTransposeB() ? ArrayUtil.reverseCopy(rarg().getShape()) : rarg().getShape();
    if (Shape.isPlaceholderShape(aShape) || Shape.isPlaceholderShape(bShape))
        return Collections.emptyList();
    if (aShape != null && bShape != null) {
        val shape = getTensorMmulShape(aShape, bShape, axes);
        ret.add(shape);
    }
    if (!ret.isEmpty()) {
        for (int i = 0; i < ret.get(0).length; i++) {
            if (ret.get(0)[i] < 1)
                throw new ND4JIllegalStateException("Invalid shape computed at index " + i);
        }
    }
    return ret;
}
Also used : lombok.val(lombok.val) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Example 94 with ND4JIllegalStateException

use of org.nd4j.linalg.exception.ND4JIllegalStateException in project nd4j by deeplearning4j.

the class Mmul method calculateOutputShape.

@Override
public List<int[]> calculateOutputShape() {
    if (mMulTranspose == null)
        mMulTranspose = MMulTranspose.allFalse();
    List<int[]> ret = new ArrayList<>(1);
    int[] aShape = mMulTranspose.isTransposeA() ? ArrayUtil.reverseCopy(larg().getShape()) : larg().getShape();
    int[] bShape = mMulTranspose.isTransposeB() ? ArrayUtil.reverseCopy(rarg().getShape()) : rarg().getShape();
    if (Shape.isPlaceholderShape(aShape) || Shape.isPlaceholderShape(bShape))
        return Collections.emptyList();
    if (aShape != null && bShape != null) {
        val shape = Shape.getMatrixMultiplyShape(aShape, bShape);
        ret.add(shape);
    }
    if (!ret.isEmpty()) {
        for (int i = 0; i < ret.get(0).length; i++) {
            if (ret.get(0)[i] < 1)
                throw new ND4JIllegalStateException("Invalid shape computed at index " + i);
        }
    }
    return ret;
}
Also used : lombok.val(lombok.val) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Example 95 with ND4JIllegalStateException

use of org.nd4j.linalg.exception.ND4JIllegalStateException in project nd4j by deeplearning4j.

the class CpuNDArrayFactory method concat.

/**
 * concatenate ndarrays along a dimension
 *
 * @param dimension the dimension to concatenate along
 * @param toConcat  the ndarrays to concatenate
 * @return the concatenate ndarrays
 */
@Override
public INDArray concat(int dimension, INDArray... toConcat) {
    if (toConcat.length == 1)
        return toConcat[0];
    // if reusable var wasn't created for this thread, or is smaller then needed - set it to new value
    if (extrazA.get() == null || extrazB.get() == null || extrazSize.get() == null || extrazSize.get() < toConcat.length) {
        extrazA.set(new PointerPointer(toConcat.length));
        extrazB.set(new PointerPointer(toConcat.length));
        extrazSize.set(toConcat.length);
    }
    PointerPointer shapeInfoPointers = extrazA.get();
    PointerPointer dataPointers = extrazB.get();
    int sumAlongDim = 0;
    int[] outputShape = ArrayUtil.copy(toConcat[0].shape());
    for (int i = 0; i < toConcat.length; i++) {
        if (toConcat[i].isCompressed())
            Nd4j.getCompressor().decompressi(toConcat[i]);
        shapeInfoPointers.put(i, toConcat[i].shapeInfoDataBuffer().addressPointer());
        dataPointers.put(i, toConcat[i].data().addressPointer());
        sumAlongDim += toConcat[i].size(dimension);
        for (int j = 0; j < toConcat[i].rank(); j++) if (j != dimension && toConcat[i].size(j) != outputShape[j]) {
            throw new IllegalArgumentException("Illegal concatenation at array " + i + " and shape element " + j);
        }
    // log.info("Shape[{}]: {}", i, Arrays.toString(toConcat[i].shapeInfoDataBuffer().asInt()));
    }
    outputShape[dimension] = sumAlongDim;
    // PointerPointer dummy = new PointerPointer(new Pointer[] {null});
    INDArray ret = Nd4j.createUninitialized(outputShape, Nd4j.order());
    if (ret.data().dataType() == DataBuffer.Type.DOUBLE) {
        nativeOps.concatDouble(null, dimension, toConcat.length, dataPointers, shapeInfoPointers, (DoublePointer) ret.data().addressPointer(), (IntPointer) ret.shapeInfoDataBuffer().addressPointer(), // new PointerPointer(new Pointer[] {null}), new PointerPointer(new Pointer[] {null}));
        null, null);
    } else if (ret.data().dataType() == DataBuffer.Type.FLOAT) {
        nativeOps.concatFloat(null, dimension, toConcat.length, dataPointers, shapeInfoPointers, (FloatPointer) ret.data().addressPointer(), (IntPointer) ret.shapeInfoDataBuffer().addressPointer(), // new PointerPointer(new Pointer[] {null}), new PointerPointer(new Pointer[] {null}));
        null, null);
    } else if (ret.data().dataType() == DataBuffer.Type.HALF) {
        nativeOps.concatHalf(null, dimension, toConcat.length, dataPointers, shapeInfoPointers, (ShortPointer) ret.data().addressPointer(), (IntPointer) ret.shapeInfoDataBuffer().addressPointer(), // new PointerPointer(new Pointer[]{null}), new PointerPointer(new Pointer[]{null}));
        null, null);
    } else {
        throw new ND4JIllegalStateException("Unknown dataType: " + ret.data().dataType());
    }
    return ret;
// return super.concat(dimension,toConcat);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Aggregations

ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)116 lombok.val (lombok.val)26 INDArray (org.nd4j.linalg.api.ndarray.INDArray)23 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)21 AllocationPoint (org.nd4j.jita.allocator.impl.AllocationPoint)19 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)17 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)15 PagedPointer (org.nd4j.linalg.api.memory.pointers.PagedPointer)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 BaseDataBuffer (org.nd4j.linalg.api.buffer.BaseDataBuffer)7 IComplexNDArray (org.nd4j.linalg.api.complex.IComplexNDArray)6 Pointer (org.bytedeco.javacpp.Pointer)5 ArrayList (java.util.ArrayList)4 DifferentialFunction (org.nd4j.autodiff.functions.DifferentialFunction)4 Aeron (io.aeron.Aeron)3 FragmentAssembler (io.aeron.FragmentAssembler)3 MediaDriver (io.aeron.driver.MediaDriver)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Slf4j (lombok.extern.slf4j.Slf4j)3 CloseHelper (org.agrona.CloseHelper)3