Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#elementWiseStride()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#elementWiseStride() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElementWiseStrideTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testEWS1() throws Exception {
    List<Pair<INDArray,String>> list = NDArrayCreationUtil.getAllTestMatricesWithShape(4,5,12345);
    list.addAll(NDArrayCreationUtil.getAll3dTestArraysWithShape(12345,4,5,6));
    list.addAll(NDArrayCreationUtil.getAll4dTestArraysWithShape(12345,4,5,6,7));
    list.addAll(NDArrayCreationUtil.getAll5dTestArraysWithShape(12345,4,5,6,7,8));
    list.addAll(NDArrayCreationUtil.getAll6dTestArraysWithShape(12345,4,5,6,7,8,9));


    for(Pair<INDArray,String> p : list){
        int ewsBefore = Shape.elementWiseStride(p.getFirst().shapeInfo());
        INDArray reshapeAttempt = Shape.newShapeNoCopy(p.getFirst(),new int[]{1,p.getFirst().length()}, Nd4j.order() == 'f');

        if (reshapeAttempt != null && ewsBefore == -1 && reshapeAttempt.elementWiseStride() != -1 ) {
            System.out.println("NDArrayCreationUtil." + p.getSecond());
            System.out.println("ews before: " + ewsBefore);
            System.out.println(p.getFirst().shapeInfoToString());
            System.out.println("ews returned by elementWiseStride(): " + p.getFirst().elementWiseStride());
            System.out.println("ews returned by reshape(): " + reshapeAttempt.elementWiseStride());
            System.out.println();
      //      assertTrue(false);
        } else {
      //      System.out.println("FAILED: " + p.getFirst().shapeInfoToString());
        }
    }
}
 
Example 2
Source File: OpExecutionerUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/** Can we do the op (X = Op(X)) directly on the arrays without breaking X up into 1d tensors first?
 * In general, this is possible if the elements of X are contiguous in the buffer, OR if every element
 * of X is at position offset+i*elementWiseStride in the buffer
 * */
public static boolean canDoOpDirectly(INDArray x) {
    if (x.elementWiseStride() < 1)
        return false;
    if (x.isVector())
        return true;

    //For a single NDArray all we require is that the elements are contiguous in the buffer or every nth element

    //Full buffer -> implies all elements are contiguous (and match)
    long l1 = x.lengthLong();
    long dl1 = x.data().length();
    if (l1 == dl1)
        return true;

    //Strides are same as a zero offset NDArray -> all elements are contiguous (even if not offset 0)
    long[] shape1 = x.shape();
    long[] stridesAsInit =
                    (x.ordering() == 'c' ? ArrayUtil.calcStrides(shape1) : ArrayUtil.calcStridesFortran(shape1));
    boolean stridesSameAsInit = Arrays.equals(x.stride(), stridesAsInit);
    return stridesSameAsInit;
}
 
Example 3
Source File: GaussianDistribution.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public GaussianDistribution(@NonNull INDArray z, @NonNull INDArray means, double stddev) {
    if (z.lengthLong() != means.lengthLong())
        throw new IllegalStateException("Result length should be equal to provided Means length");

    if (means.elementWiseStride() < 1)
        throw new IllegalStateException("Means array can't have negative EWS");

    init(z, means, z, z.lengthLong());
    this.mean = 0.0;
    this.stddev = stddev;
    this.extraArgs = new Object[] {this.mean, this.stddev};
}
 
Example 4
Source File: LogNormalDistribution.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public LogNormalDistribution(@NonNull INDArray z, @NonNull INDArray means, double stddev) {
    super(z,means,z);
    if (z.length() != means.length())
        throw new IllegalStateException("Result length should be equal to provided Means length");

    if (means.elementWiseStride() < 1)
        throw new IllegalStateException("Means array can't have negative EWS");
    this.mean = 0.0;
    this.stddev = stddev;
    this.extraArgs = new Object[] {this.mean, this.stddev};
}
 
Example 5
Source File: BernoulliDistribution.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This op fills Z with bernoulli trial results, so 0, or 1, each element will have it's own success probability defined in prob array
 * @param prob array with probabilities
 * @param z

 */
public BernoulliDistribution(@NonNull INDArray z, @NonNull INDArray prob) {
    super(prob, null, z);
    if (prob.elementWiseStride() != 1)
        throw new ND4JIllegalStateException("Probabilities should have ElementWiseStride of 1");

    if (prob.length() != z.length())
        throw new ND4JIllegalStateException("Length of probabilities array [" + prob.length()
                        + "] doesn't match length of output array [" + z.length() + "]");
    this.prob = 0.0;
    this.extraArgs = new Object[] {this.prob};
}
 
Example 6
Source File: BlasBufferUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get blas stride for the
 * given array
 * @param arr the array
 * @return the blas stride
 */
public static int getBlasStride(INDArray arr) {
    if (arr instanceof IComplexNDArray)
        return arr.elementWiseStride() / 2;

    return arr.elementWiseStride();
}
 
Example 7
Source File: Choice.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public Choice(@NonNull INDArray source, @NonNull INDArray probabilities, @NonNull INDArray z) {
    super(source, probabilities, z);
    Preconditions.checkArgument(source.dataType() == probabilities.dataType() && z.dataType() == source.dataType(), "Data types of all arguments should match");
    Preconditions.checkState(source.length() == probabilities.length(), "From & probabilities length mismatch: %s vs. %s",
                        source.length(), probabilities.length());
    if (probabilities.elementWiseStride() < 1 || source.elementWiseStride() < 1)
        throw new IllegalStateException("Source and probabilities should have element-wise stride >= 1");
    this.extraArgs = new Object[] {0.0};
}
 
Example 8
Source File: GemvParameters.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private INDArray copyIfNecessary(INDArray arr) {
    //See also: Shape.toMmulCompatible - want same conditions here and there
    //Check if matrix values are contiguous in memory. If not: dup
    //Contiguous for c if: stride[0] == shape[1] and stride[1] = 1
    //Contiguous for f if: stride[0] == 1 and stride[1] == shape[0]
    if (arr.ordering() == 'c' && (arr.stride(0) != arr.size(1) || arr.stride(1) != 1))
        return arr.dup();
    else if (arr.ordering() == 'f' && (arr.stride(0) != 1 || arr.stride(1) != arr.size(0)))
        return arr.dup();
    else if (arr.elementWiseStride() < 1)
        return arr.dup();
    return arr;
}
 
Example 9
Source File: BinomialDistribution.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This op fills Z with binomial distribution over given trials with probability for each trial given as probabilities INDArray
 * @param z
 * @param trials
 * @param probabilities array with probability value for each trial
 */
public BinomialDistribution(@NonNull INDArray z, int trials, @NonNull INDArray probabilities) {
    if (trials > probabilities.lengthLong())
        throw new IllegalStateException("Number of trials is > then amount of probabilities provided");

    if (probabilities.elementWiseStride() < 1)
        throw new IllegalStateException("Probabilities array shouldn't have negative elementWiseStride");

    init(z, probabilities, z, z.lengthLong());

    this.trials = trials;
    this.probability = 0.0;
    this.extraArgs = new Object[] {(double) this.trials, this.probability};
}
 
Example 10
Source File: BinomialDistributionEx.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This op fills Z with binomial distribution over given trials with probability for each trial given as probabilities INDArray
 * @param z
 * @param trials
 * @param probabilities array with probability value for each trial
 */
public BinomialDistributionEx(@NonNull INDArray z, int trials, @NonNull INDArray probabilities) {
    if (z.lengthLong() != probabilities.lengthLong())
        throw new IllegalStateException("Length of probabilities array should match length of target array");

    if (probabilities.elementWiseStride() < 1)
        throw new IllegalStateException("Probabilities array shouldn't have negative elementWiseStride");

    init(z, probabilities, z, z.lengthLong());

    this.trials = trials;
    this.probability = 0.0;
    this.extraArgs = new Object[] {(double) this.trials, this.probability};
}
 
Example 11
Source File: BinomialDistribution.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This op fills Z with binomial distribution over given trials with probability for each trial given as probabilities INDArray
 * @param z
 * @param trials
 * @param probabilities array with probability value for each trial
 */
public BinomialDistribution(@NonNull INDArray z, int trials, @NonNull INDArray probabilities) {
    super(z, probabilities, z);
    if (trials > probabilities.length())
        throw new IllegalStateException("Number of trials is > then amount of probabilities provided");

    if (probabilities.elementWiseStride() < 1)
        throw new IllegalStateException("Probabilities array shouldn't have negative elementWiseStride");

    Preconditions.checkArgument(probabilities.dataType() == z.dataType(), "Probabilities and Z operand should have same data type");

    this.trials = trials;
    this.probability = 0.0;
    this.extraArgs = new Object[] {(double) this.trials, this.probability};
}
 
Example 12
Source File: CudaAffinityManager.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method replicates given INDArray, and places it to target device.
 *
 * @param deviceId target deviceId
 * @param array    INDArray to replicate
 * @return
 */
@Override
public synchronized INDArray replicateToDevice(Integer deviceId, INDArray array) {
    if (array == null)
        return null;

    if (array.isView())
        throw new UnsupportedOperationException("It's impossible to replicate View");

    val shape = array.shape();
    val stride = array.stride();
    val elementWiseStride = array.elementWiseStride();
    val ordering = array.ordering();
    val length = array.length();

    // we use this call to get device memory updated
    AtomicAllocator.getInstance().getPointer(array,
                    (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext());

    int currentDeviceId = getDeviceForCurrentThread();

    NativeOpsHolder.getInstance().getDeviceNativeOps().setDevice(new CudaPointer(deviceId));
    attachThreadToDevice(Thread.currentThread().getId(), deviceId);


    DataBuffer newDataBuffer = replicateToDevice(deviceId, array.data());
    DataBuffer newShapeBuffer = Nd4j.getShapeInfoProvider().createShapeInformation(shape, stride, 0,
                    elementWiseStride, ordering).getFirst();
    INDArray result = Nd4j.createArrayFromShapeBuffer(newDataBuffer, newShapeBuffer);

    attachThreadToDevice(Thread.currentThread().getId(), currentDeviceId);
    NativeOpsHolder.getInstance().getDeviceNativeOps().setDevice(new CudaPointer(currentDeviceId));


    return result;
}
 
Example 13
Source File: Choice.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public Choice(@NonNull INDArray source, @NonNull INDArray probabilities, @NonNull INDArray z) {
    if (source.lengthLong() != probabilities.lengthLong())
        throw new IllegalStateException("From & probabilities length mismatch: " + source.lengthLong() + "/"
                        + probabilities.lengthLong());

    if (probabilities.elementWiseStride() < 1 || source.elementWiseStride() < 1)
        throw new IllegalStateException("Source and probabilities should have element-wise stride >= 1");

    init(source, probabilities, z, z.lengthLong());
    this.extraArgs = new Object[] {0.0};
}
 
Example 14
Source File: TruncatedNormalDistribution.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public TruncatedNormalDistribution(@NonNull INDArray z, @NonNull INDArray means, double stddev) {
    if (z.lengthLong() != means.lengthLong())
        throw new IllegalStateException("Result length should be equal to provided Means length");

    if (means.elementWiseStride() < 1)
        throw new IllegalStateException("Means array can't have negative EWS");

    init(z, means, z, z.lengthLong());
    this.mean = 0.0;
    this.stddev = stddev;
    this.extraArgs = new Object[] {this.mean, this.stddev};
}
 
Example 15
Source File: BinomialDistributionEx.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This op fills Z with binomial distribution over given trials with probability for each trial given as probabilities INDArray
 * @param z
 * @param trials
 * @param probabilities array with probability value for each trial
 */
public BinomialDistributionEx(@NonNull INDArray z, long trials, @NonNull INDArray probabilities) {
    super(z, probabilities, z);
    if (z.length() != probabilities.length())
        throw new IllegalStateException("Length of probabilities array should match length of target array");

    if (probabilities.elementWiseStride() < 1)
        throw new IllegalStateException("Probabilities array shouldn't have negative elementWiseStride");

    Preconditions.checkArgument(probabilities.dataType() == z.dataType(), "Probabilities and Z operand should have same data type");

    this.trials = trials;
    this.probability = 0.0;
    this.extraArgs = new Object[] {(double) this.trials, this.probability};
}
 
Example 16
Source File: GemvParameters.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private INDArray copyIfNecessary(INDArray arr) {
    //See also: Shape.toMmulCompatible - want same conditions here and there
    //Check if matrix values are contiguous in memory. If not: dup
    //Contiguous for c if: stride[0] == shape[1] and stride[1] = 1
    //Contiguous for f if: stride[0] == 1 and stride[1] == shape[0]
    if (arr.ordering() == 'c' && (arr.stride(0) != arr.size(1) || arr.stride(1) != 1))
        return arr.dup();
    else if (arr.ordering() == 'f' && (arr.stride(0) != 1 || arr.stride(1) != arr.size(0)))
        return arr.dup();
    else if (arr.elementWiseStride() < 1)
        return arr.dup();
    return arr;
}
 
Example 17
Source File: OpExecutionerUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/** Can we do the transform op (Z = Op(X,Y)) directly on the arrays without breaking them up into 1d tensors first? */
public static boolean canDoOpDirectly(INDArray x, INDArray y, INDArray z) {
    if (x.isVector())
        return true;
    if (x.ordering() != y.ordering() || x.ordering() != z.ordering())
        return false; //other than vectors, elements in f vs. c NDArrays will never line up
    if (x.elementWiseStride() < 1 || y.elementWiseStride() < 1)
        return false;
    //Full buffer + matching strides -> implies all elements are contiguous (and match)
    long l1 = x.lengthLong();
    long dl1 = x.data().length();
    long l2 = y.lengthLong();
    long dl2 = y.data().length();
    long l3 = z.lengthLong();
    long dl3 = z.data().length();
    long[] strides1 = x.stride();
    long[] strides2 = y.stride();
    long[] strides3 = z.stride();
    boolean equalStrides = Arrays.equals(strides1, strides2) && Arrays.equals(strides1, strides3);
    if (l1 == dl1 && l2 == dl2 && l3 == dl3 && equalStrides)
        return true;

    //Strides match + are same as a zero offset NDArray -> all elements are contiguous (and match)
    if (equalStrides) {
        long[] shape1 = x.shape();
        long[] stridesAsInit = (x.ordering() == 'c' ? ArrayUtil.calcStrides(shape1)
                        : ArrayUtil.calcStridesFortran(shape1));
        boolean stridesSameAsInit = Arrays.equals(strides1, stridesAsInit);
        return stridesSameAsInit;
    }

    return false;
}
 
Example 18
Source File: OpExecutionerUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/** Can we do the transform op (X = Op(X,Y)) directly on the arrays without breaking them up into 1d tensors first? */
public static boolean canDoOpDirectly(INDArray x, INDArray y) {
    if (x.isVector())
        return true;
    if (x.ordering() != y.ordering())
        return false; //other than vectors, elements in f vs. c NDArrays will never line up
    if (x.elementWiseStride() < 1 || y.elementWiseStride() < 1)
        return false;
    //Full buffer + matching strides -> implies all elements are contiguous (and match)
    //Need strides to match, otherwise elements in buffer won't line up (i.e., c vs. f order arrays)
    long l1 = x.lengthLong();
    long dl1 = x.data().length();
    long l2 = y.lengthLong();
    long dl2 = y.data().length();
    long[] strides1 = x.stride();
    long[] strides2 = y.stride();
    boolean equalStrides = Arrays.equals(strides1, strides2);
    if (l1 == dl1 && l2 == dl2 && equalStrides)
        return true;

    //Strides match + are same as a zero offset NDArray -> all elements are contiguous (and match)
    if (equalStrides) {
        long[] shape1 = x.shape();
        long[] stridesAsInit = (x.ordering() == 'c' ? ArrayUtil.calcStrides(shape1)
                        : ArrayUtil.calcStridesFortran(shape1));
        boolean stridesSameAsInit = Arrays.equals(strides1, stridesAsInit);
        return stridesSameAsInit;
    }

    return false;
}
 
Example 19
Source File: GemvParameters.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public GemvParameters(INDArray a, INDArray x, INDArray y) {
    a = copyIfNecessary(a);
    x = copyIfNecessaryVector(x);
    this.a = a;
    this.x = x;
    this.y = y;

    if (a.columns() > Integer.MAX_VALUE || a.rows() > Integer.MAX_VALUE)
        throw new ND4JArraySizeException();

    if (x.columns() > Integer.MAX_VALUE || x.rows() > Integer.MAX_VALUE)
        throw new ND4JArraySizeException();


    if (a.ordering() == 'f' && a.isMatrix()) {
        this.m = (int) a.rows();
        this.n = (int) a.columns();
        this.lda = (int) a.rows();
    } else if (a.ordering() == 'c' && a.isMatrix()) {
        this.m = (int) a.columns();
        this.n = (int) a.rows();
        this.lda = (int) a.columns();
        aOrdering = 'T';
    }

    else {
        this.m = (int) a.rows();
        this.n = (int) a.columns();
        this.lda = (int) a.size(0);
    }


    if (x.rank() == 1) {
        incx = 1;
    } else if (x.isColumnVector()) {
        incx = x.stride(0);
    } else {
        incx = x.stride(1);
    }

    this.incy = y.elementWiseStride();

    if (x instanceof IComplexNDArray)
        this.incx /= 2;
    if (y instanceof IComplexNDArray)
        this.incy /= 2;

}
 
Example 20
Source File: ProtectedCudaShapeInfoProviderTest.java    From nd4j with Apache License 2.0 2 votes vote down vote up
@Test
    public void testPurge2() throws Exception {
        INDArray arrayA = Nd4j.create(10, 10);

        DataBuffer shapeInfoA = arrayA.shapeInfoDataBuffer();

        INDArray arrayE = Nd4j.create(10, 10);

        DataBuffer shapeInfoE = arrayE.shapeInfoDataBuffer();

        int[] arrayShapeA = shapeInfoA.asInt();

        assertTrue(shapeInfoA == shapeInfoE);

        ShapeDescriptor descriptor = new ShapeDescriptor(arrayA.shape(), arrayA.stride(), 0, arrayA.elementWiseStride(), arrayA.ordering());
        ConstantProtector protector = ConstantProtector.getInstance();
        AllocationPoint pointA = AtomicAllocator.getInstance().getAllocationPoint(arrayA.shapeInfoDataBuffer());

        assertEquals(true, protector.containsDataBuffer(0, descriptor));

////////////////////////////////////

        Nd4j.getMemoryManager().purgeCaches();

////////////////////////////////////


        assertEquals(false, protector.containsDataBuffer(0, descriptor));

        INDArray arrayB = Nd4j.create(10, 10);

        DataBuffer shapeInfoB = arrayB.shapeInfoDataBuffer();

        assertFalse(shapeInfoA == shapeInfoB);

        AllocationPoint pointB = AtomicAllocator.getInstance().getAllocationPoint(arrayB.shapeInfoDataBuffer());


        assertArrayEquals(arrayShapeA, shapeInfoB.asInt());

        // pointers should be equal, due to offsets reset
        assertEquals(pointA.getPointers().getDevicePointer().address(), pointB.getPointers().getDevicePointer().address());
    }