Search in sources :

Example 71 with ND4JIllegalStateException

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

the class OpExecutionerUtil method checkForInf.

public static void checkForInf(INDArray z) {
    if (Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.INF_PANIC && Nd4j.getExecutioner().getProfilingMode() != OpExecutioner.ProfilingMode.ANY_PANIC)
        return;
    int match = 0;
    if (!z.isScalar()) {
        MatchCondition condition = new MatchCondition(z, Conditions.isInfinite());
        match = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);
    } else {
        if (z.data().dataType() == DataBuffer.Type.DOUBLE) {
            if (Double.isInfinite(z.getDouble(0)))
                match = 1;
        } else {
            if (Float.isInfinite(z.getFloat(0)))
                match = 1;
        }
    }
    if (match > 0)
        throw new ND4JIllegalStateException("P.A.N.I.C.! Op.Z() contains " + match + " Inf value(s)");
}
Also used : MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Example 72 with ND4JIllegalStateException

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

the class BaseNDArray method reshape.

@Override
public INDArray reshape(char order, int... newShape) {
    Nd4j.getCompressor().autoDecompress(this);
    if (newShape == null || newShape.length < 1)
        throw new ND4JIllegalStateException("Can't reshape(int...) without shape arguments. Got empty shape instead.");
    // reshape(-1) special case
    if (newShape.length == 1 && newShape[0] == -1)
        newShape[0] = this.length();
    int numberNegativesOnes = 0;
    int[] shape = ArrayUtil.copy(newShape);
    for (int i = 0; i < shape.length; i++) {
        if (shape[i] < 0) {
            if (numberNegativesOnes >= 1)
                throw new IllegalArgumentException("Only one dimension can be negative ones. Got shape " + Arrays.toString(newShape));
            numberNegativesOnes++;
            int shapeLength = 1;
            for (int j = 0; j < shape.length; j++) if (shape[j] >= 1)
                shapeLength *= shape[j];
            int realShape = Math.abs(length() / shapeLength);
            int[] thisNewShape = new int[shape.length];
            for (int j = 0; j < shape.length; j++) {
                if (i != j) {
                    thisNewShape[j] = shape[j];
                } else
                    thisNewShape[j] = realShape;
            }
            shape = thisNewShape;
            break;
        }
    }
    long prod = ArrayUtil.prodLong(shape);
    if (prod != this.lengthLong())
        throw new ND4JIllegalStateException("New shape length doesn't match original length: [" + prod + "] vs [" + this.lengthLong() + "]");
    INDArray reshapeAttempt = Shape.newShapeNoCopy(this, shape, order == 'f');
    if (reshapeAttempt != null) {
        // reshapeAttempt.setOrder(Shape.getOrder(reshapeAttempt));
        return reshapeAttempt;
    }
    INDArray ret = Nd4j.createUninitialized(shape, order);
    if (order != ordering()) {
        ret.setData(dup(order).data());
    } else
        ret.assign(this);
    return ret;
}
Also used : ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) ND4JIllegalArgumentException(org.nd4j.linalg.exception.ND4JIllegalArgumentException)

Example 73 with ND4JIllegalStateException

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

the class DynamicCustomOp method populateInputsAndOutputsFromSameDiff.

@Override
public void populateInputsAndOutputsFromSameDiff() {
    val descriptor = getDescriptor();
    if (descriptor == null)
        throw new ND4JIllegalStateException("No op found for " + opName());
    log.debug("Op <{}>, isInplace: {}", opName(), isInplaceCall());
    if (isInplaceCall()) {
        if (numInputArguments() != descriptor.getNumInputs()) {
            // clear just in case
            val args = args();
            inputArguments.clear();
            for (val arg : args) {
                inputArguments.add(arg.getArr());
            }
        }
        if (numOutputArguments() != descriptor.getNumOutputs()) {
            // clear just in case
            val arg = args()[0];
            outputArguments.clear();
            outputArguments.add(arg.getArr());
        }
    } else {
        if (numInputArguments() != descriptor.getNumInputs()) {
            // clear just in case
            val args = args();
            inputArguments.clear();
            boolean nullArr = false;
            for (val arg : args()) {
                // outputs when null inputs exist
                if (arg.getArr() == null) {
                    nullArr = true;
                    log.warn("No input found for " + arg.getVarName() + " and op name " + opName());
                    continue;
                }
            }
            if (!nullArr) {
                for (val arg : args) {
                    inputArguments.add(arg.getArr());
                }
            }
        }
        val numOutputs = numOutputArguments();
        val dO = descriptor.getNumOutputs();
        if (numOutputArguments() != descriptor.getNumOutputs()) {
            // clear just in case
            val outputVars = outputVariables();
            outputArguments.clear();
            for (val arg : outputVars) {
                INDArray arr = arg.getArr();
                if (arr == null) {
                    outputArguments.clear();
                    // placeholder case,  will try to resolve shapes one more time
                    val shapes = calculateOutputShape();
                    for (int i = 0; i < shapes.size(); i++) {
                        if (outputVars[i].getArr() != null)
                            outputArguments.add(outputVars[i].getArr());
                        else if (outputVars[i].getShape() == null) {
                            sameDiff.putShapeForVarName(outputVars[i].getVarName(), shapes.get(i));
                            outputArguments.add(outputVars[i].storeAndAllocateNewArray());
                        } else
                            throw new ND4JIllegalStateException("Unable to resolve for variable " + outputVars[i].getVarName());
                    }
                } else
                    outputArguments.add(arr);
            }
        }
    }
}
Also used : lombok.val(lombok.val) INDArray(org.nd4j.linalg.api.ndarray.INDArray) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Example 74 with ND4JIllegalStateException

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

the class DynamicCustomOp method addInputArgument.

@Override
public void addInputArgument(INDArray... arg) {
    for (int i = 0; i < arg.length; i++) {
        if (arg[i] == null)
            throw new ND4JIllegalStateException("Input " + i + " was null!");
    }
    inputArguments.addAll(Arrays.asList(arg));
    val args = sameDiff != null ? args() : null;
    val arrsSoFar = inputArguments();
    // refresh the current list
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            // it's possible to get into situation where number of args > number of arrays AT THIS MOMENT
            if (i >= arrsSoFar.length)
                continue;
            if (!Arrays.equals(args[i].getShape(), arrsSoFar[i].shape()))
                throw new ND4JIllegalStateException("Illegal array passed in. Expected shape " + Arrays.toString(args[i].getShape()) + " and received array with shape " + Arrays.toString(arg[i].shape()));
        }
    }
}
Also used : lombok.val(lombok.val) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Example 75 with ND4JIllegalStateException

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

the class BaseSparseNDArray method percentile.

@Override
public INDArray percentile(Number quantile, int... dimension) {
    if (quantile.doubleValue() < 0 || quantile.doubleValue() > 100)
        throw new ND4JIllegalStateException("Percentile value should be in 0...100 range");
    if (isScalar())
        return Nd4j.scalar(this.getDouble(0));
    INDArray sorted = Nd4j.getNDArrayFactory().sort(this.dup(this.ordering()), false, dimension);
    // there's no practical sense doing this on GPU, stride will be just size of TAD.
    INDArray ret = Nd4j.createUninitialized(sorted.tensorssAlongDimension(dimension));
    for (int i = 0; i < ret.length(); i++) {
        ret.putScalar(i, getPercentile(quantile, sorted.tensorAlongDimension(i, dimension)));
    }
    return ret;
}
Also used : 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