/** * 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(); }
public PenaltyCause[] processOperands(INDArray x, INDArray y) { List<PenaltyCause> penalties = new ArrayList<>(); if (x.ordering() != y.ordering()) { penalties.add(PenaltyCause.MIXED_ORDER); } if (x.elementWiseStride() < 1) { penalties.add(PenaltyCause.NON_EWS_ACCESS); } else if (y.elementWiseStride() < 1) { penalties.add(PenaltyCause.NON_EWS_ACCESS); } if (x.elementWiseStride() > 1) { penalties.add(PenaltyCause.STRIDED_ACCESS); } else if (y.elementWiseStride() > 1) { penalties.add(PenaltyCause.STRIDED_ACCESS); } if (penalties.isEmpty()) penalties.add(NONE); return penalties.toArray(new PenaltyCause[0]); }
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}; }
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}; }
/** * 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}; }
public LogNormalDistribution(@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}; }
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}; }
/** * 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}; }
/** Tensor1DStats, used to efficiently iterate through tensors on a matrix (2d NDArray) for element-wise ops * For example, the offset of each 1d tensor can be calculated using only a single tensorAlongDimension method call, * hence is potentially faster than approaches requiring multiple tensorAlongDimension calls.<br> * Note that this can only (generally) be used for 2d NDArrays. For certain 3+d NDArrays, the tensor starts may not * be in increasing order */ public static Tensor1DStats get1DTensorStats(INDArray array, int... dimension) { long tensorLength = array.size(dimension[0]); //As per tensorssAlongDimension: long numTensors = array.tensorssAlongDimension(dimension); //First tensor always starts with the first element in the NDArray, regardless of dimension long firstTensorOffset = array.offset(); //Next: Need to work out the separation between the start (first element) of each 1d tensor long tensorStartSeparation; int elementWiseStride; //Separation in buffer between elements in the tensor if (numTensors == 1) { tensorStartSeparation = -1; //Not applicable elementWiseStride = array.elementWiseStride(); } else { INDArray secondTensor = array.tensorAlongDimension(1, dimension); tensorStartSeparation = secondTensor.offset() - firstTensorOffset; elementWiseStride = secondTensor.elementWiseStride(); } return new Tensor1DStats(firstTensorOffset, tensorStartSeparation, numTensors, tensorLength, elementWiseStride); }
/** * 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) { if (prob.elementWiseStride() != 1) throw new ND4JIllegalStateException("Probabilities should have ElementWiseStride of 1"); if (prob.lengthLong() != z.lengthLong()) throw new ND4JIllegalStateException("Length of probabilities array [" + prob.lengthLong() + "] doesn't match length of output array [" + z.lengthLong() + "]"); init(prob, null, z, z.lengthLong()); this.prob = 0.0; this.extraArgs = new Object[] {this.prob}; }
/** 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; }
/** 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; }
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;
public GridPointers(Op op, int... dimensions) { this.type = BaseOp.getOpType(op); this.dtype = op.x().data().dataType(); this.opNum = op.opNum(); this.opX = op.x(); this.opZ = op.z(); this.xLength = op.x().length(); this.zLength = op.z().length(); this.xOrder = op.x().ordering(); this.zOrder = op.z().ordering(); this.xStride = op.x().elementWiseStride(); this.zStride = op.z().elementWiseStride(); if (op.y() != null) { this.yStride = op.y().elementWiseStride(); this.yLength = op.y().length(); this.yOrder = op.y().ordering(); this.opY = op.y(); } if (dimensions != null) this.dimensionsLength = dimensions.length; } }
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; }
/** * Get a copy of a row. * * @param r the row to get */ @Override public INDArray getRow(long r) { if (isRowVector() && r == 0) return this; else if (isRowVector() && r > 0) throw new IllegalArgumentException("Illegal index for row"); INDArray result = get(NDArrayIndex.point(r), NDArrayIndex.all()); // FIXME: this is bad if (!this.isView() && this.ordering() == 'c' && result.elementWiseStride() == 1 && result.ordering() != 'c') { ((BaseNDArray) result).setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(result.shape(), result.stride(), 0, 1, 'c')); } return result; }
private INDArray copyIfNeccessary(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 (!Nd4j.allowsSpecifyOrdering() && 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() < 0) return arr.dup(); return arr; } }
this.incy = y.elementWiseStride();
} else { if (rank() == 2 && elementWiseStride() == 1 && ordering() == 'c' && columnVector.elementWiseStride() == 1) { switch (operation) { case 'a': {
if (rank() == 2 && elementWiseStride() == 1 && ordering() == 'f' && rowVector.elementWiseStride() == 1) { switch (operation) { case 'a': {