@Override public void geqrf(INDArray A, INDArray R) { // FIXME: int cast if (A.rows() > Integer.MAX_VALUE || A.columns() > Integer.MAX_VALUE) throw new ND4JArraySizeException(); int m = (int) A.rows(); int n = (int) A.columns(); INDArray INFO = Nd4j.createArrayFromShapeBuffer(Nd4j.getDataBufferFactory().createInt(1), Nd4j.getShapeInfoProvider().createShapeInformation(new int[] {1, 1}).getFirst()); if (R.rows() != A.columns() || R.columns() != A.columns()) { throw new Error("geqrf: R must be N x N (n = columns in A)"); } if (A.data().dataType() == DataBuffer.Type.DOUBLE) { dgeqrf(m, n, A, R, INFO); } else if (A.data().dataType() == DataBuffer.Type.FLOAT) { sgeqrf(m, n, A, R, INFO); } else { throw new UnsupportedOperationException(); } if (INFO.getInt(0) < 0) { throw new Error("Parameter #" + INFO.getInt(0) + " to getrf() was not valid"); } }
/** * Returns the double data * for this ndarray. * If possible (the offset is 0 representing the whole buffer) * it will return a direct reference to the underlying array * @param buf the ndarray to get the data for * @return the double data for this ndarray */ public static double[] getDoubleData(INDArray buf) { if (buf.data().dataType() != DataBuffer.Type.DOUBLE) throw new IllegalArgumentException("Double data must be obtained from a double buffer"); if (buf.data().allocationMode() == DataBuffer.AllocationMode.HEAP) { return buf.data().asDouble(); } else { double[] ret = new double[(int) buf.length()]; INDArray linear = buf.linearView(); for (int i = 0; i < buf.length(); i++) ret[i] = linear.getDouble(i); return ret; } }
/** * Returns the float data * for this ndarray. * If possible (the offset is 0 representing the whole buffer) * it will return a direct reference to the underlying array * @param buf the ndarray to get the data for * @return the float data for this ndarray */ public static float[] getFloatData(INDArray buf) { if (buf.data().dataType() != DataBuffer.Type.FLOAT) throw new IllegalArgumentException("Float data must be obtained from a float buffer"); if (buf.data().allocationMode() == DataBuffer.AllocationMode.HEAP) { return buf.data().asFloat(); } else { float[] ret = new float[(int) buf.length()]; INDArray linear = buf.linearView(); for (int i = 0; i < buf.length(); i++) ret[i] = linear.getFloat(i); return ret; } }
/** * Create a compression descriptor from the given * data buffer elements * @param buffer the databuffer to create * the compression descriptor from */ public CompressionDescriptor(DataBuffer buffer) { this.originalLength = buffer.length() * buffer.getElementSize(); this.numberOfElements = buffer.length(); this.originalElementSize = buffer.getElementSize(); }
/** * Returns the double data * for this buffer. * If possible (the offset is 0 representing the whole buffer) * it will return a direct reference to the underlying array * @param buf the ndarray to get the data for * @return the double data for this buffer */ public static double[] getDoubleData(DataBuffer buf) { if (buf.allocationMode() == DataBuffer.AllocationMode.HEAP) return buf.asDouble(); else { double[] ret = new double[(int) buf.length()]; for (int i = 0; i < buf.length(); i++) ret[i] = buf.getDouble(i); return ret; } }
public static INDArray tailor3d2d(@NonNull INDArray data, INDArray mask) { if (data.size(0) != mask.size(0) || data.size(2) != mask.size(1)) { throw new IllegalArgumentException( "Invalid mask array/data combination: got data with shape [minibatch, vectorSize, timeSeriesLength] = " if (data.ordering() != 'f' || data.isView() || !Shape.strideDescendingCAscendingF(data)) { data = data.dup('f'); if (mask.ordering() != 'f' || mask.isView() || !Shape.strideDescendingCAscendingF(mask)) { mask = mask.dup('f'); float[] floatMask1d = mask1d.data().asFloat(); int currCount = 0; for (int i = 0; i < floatMask1d.length; i++) { INDArray subset = Nd4j.pullRows(as2d, 1, rowsToPull); //Tensor along dimension 1 == rows return subset;
@Override public INDArray pullRows(INDArray source, INDArray destination, int sourceDimension, int[] indexes) { if (Nd4j.getExecutioner() instanceof GridExecutioner) ((GridExecutioner) Nd4j.getExecutioner()).flushQueue(); Pointer zShape = AtomicAllocator.getInstance().getPointer(ret.shapeInfoDataBuffer(), context); PointerPointer extras = new PointerPointer(AddressRetriever.retrieveHostPointer(ret.shapeInfoDataBuffer()), context.getOldStream(), allocator.getDeviceIdPointer()); TADManager tadManager = Nd4j.getExecutioner().getTADManager(); Pointer tadShapeInfo = AtomicAllocator.getInstance().getPointer(tadBuffers.getFirst(), context); Pointer zTadShapeInfo = AtomicAllocator.getInstance().getPointer(zTadBuffers.getFirst(), context); DataBuffer offsets = tadBuffers.getSecond(); Pointer tadOffsets = AtomicAllocator.getInstance().getPointer(offsets, context); Pointer zTadOffsets = AtomicAllocator.getInstance().getPointer(zTadBuffers.getSecond(), context); if (ret.data().dataType() == DataBuffer.Type.DOUBLE) { nativeOps.pullRowsDouble(extras, (DoublePointer) x, (LongPointer) xShape, (DoublePointer) z, (LongPointer) zShape, indexes.length, (LongPointer) pIndex, (LongPointer) tadShapeInfo, new LongPointerWrapper(tadOffsets), (LongPointer) zTadShapeInfo, new LongPointerWrapper(zTadOffsets)); } else if (ret.data().dataType() == DataBuffer.Type.FLOAT) { nativeOps.pullRowsFloat(extras, (FloatPointer) x, (LongPointer) xShape, (FloatPointer) z, (LongPointer) zShape, indexes.length, (LongPointer) pIndex, (LongPointer) tadShapeInfo,
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)"); }
/** * ?tbsv solves a system of linear equations whose coefficients are in a triangular band matrix. * * @param order * @param Uplo * @param TransA * @param Diag * @param A * @param X */ @Override public void tbsv(char order, char Uplo, char TransA, char Diag, INDArray A, INDArray X) { if (Nd4j.getExecutioner().getProfilingMode() == OpExecutioner.ProfilingMode.ALL) OpProfiler.getInstance().processBlasCall(false, A, X); // FIXME: int cast if (X.data().dataType() == DataBuffer.Type.DOUBLE) { DefaultOpExecutioner.validateDataType(DataBuffer.Type.DOUBLE, A, X); dtbsv(order, Uplo, TransA, Diag, (int) X.length(), (int) A.columns(), A, (int) A.size(0), X, X.majorStride()); } else { DefaultOpExecutioner.validateDataType(DataBuffer.Type.FLOAT, A, X); stbsv(order, Uplo, TransA, Diag, (int) X.length(), (int) A.columns(), A, (int) A.size(0), X, X.majorStride()); } }
Nd4j.getExecutioner().push(); Pair<DataBuffer, DataBuffer> tadBuffers = Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(x, dimension); .getPointer(AtomicAllocator.getInstance().getConstantBuffer(dimension), context); if (x.data().dataType() == DataBuffer.Type.FLOAT) { nativeOps.sortTadFloat(extraz, (FloatPointer) AtomicAllocator.getInstance().getPointer(x, context), (IntPointer) dimensionPointer, dimension.length, (LongPointer) AtomicAllocator.getInstance().getPointer(tadBuffers.getFirst(), context), new LongPointerWrapper(AtomicAllocator.getInstance().getPointer(tadBuffers.getSecond(), context)), descending ); } else if (x.data().dataType() == DataBuffer.Type.DOUBLE) { nativeOps.sortTadDouble(extraz, (DoublePointer) AtomicAllocator.getInstance().getPointer(x, context), ); } else if (x.data().dataType() == DataBuffer.Type.HALF) { nativeOps.sortTadHalf(extraz, (ShortPointer) AtomicAllocator.getInstance().getPointer(x, context), ); } else { throw new UnsupportedOperationException("Unknown dataType " + x.data().dataType());
public static void printNDArrayHeader(INDArray array) { System.out.println(array.data().dataType() + " - order=" + array.ordering() + ", offset=" + array.offset() + ", shape=" + Arrays.toString(array.shape()) + ", stride=" + Arrays.toString(array.stride()) + ", length=" + array.length() + ", data().length()=" + array.data().length()); }
Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(this, dimension); DataBuffer shapeInfo = tadInfo.getFirst(); val shape = Shape.shape(shapeInfo); val stride = Shape.stride(shapeInfo).asLong(); long offset = offset() + tadInfo.getSecond().getLong(index); INDArray toTad = Nd4j.create(data(), shape, stride, offset); BaseNDArray baseNDArray = (BaseNDArray) toTad; char newOrder = Shape.getOrder(shape, stride, 1); int ews = baseNDArray.shapeInfoDataBuffer().getInt(baseNDArray.shapeInfoDataBuffer().length() - 2); Nd4j.getShapeInfoProvider().createShapeInformation(shape, stride, 0, ews, newOrder));
/** * finds the element of a * vector that has the largest absolute value. * * @param arr * @return */ @Override public int iamax(INDArray arr) { if (arr.isSparse()) { return Nd4j.getSparseBlasWrapper().level1().iamax(arr); } if (Nd4j.getExecutioner().getProfilingMode() == OpExecutioner.ProfilingMode.ALL) OpProfiler.getInstance().processBlasCall(false, arr); if (arr.data().dataType() == DataBuffer.Type.DOUBLE) { DefaultOpExecutioner.validateDataType(DataBuffer.Type.DOUBLE, arr); return idamax(arr.length(), arr, BlasBufferUtil.getBlasStride(arr)); } else { DefaultOpExecutioner.validateDataType(DataBuffer.Type.FLOAT, arr); return isamax(arr.length(), arr, BlasBufferUtil.getBlasStride(arr)); } }
public static Pair<INDArray, INDArray> extractNonMaskedTimeSteps(INDArray labels, INDArray predicted, INDArray outputMask) { if (labels.rank() != 3 || predicted.rank() != 3) { throw new IllegalArgumentException("Invalid data: expect rank 3 arrays. Got arrays with shapes labels=" + Arrays.toString(labels.shape()) + ", predictions=" + Arrays.toString(predicted.shape())); return new Pair<>(labels2d, predicted2d); float[] f = oneDMask.dup().data().asFloat(); int[] rowsToPull = new int[f.length]; int usedCount = 0; labels2d = Nd4j.pullRows(labels2d, 1, rowsToPull); predicted2d = Nd4j.pullRows(predicted2d, 1, rowsToPull); return new Pair<>(labels2d, predicted2d);
return target.assign(arrays[0]); Nd4j.getExecutioner().push(); long len = target.lengthLong(); PointerPointer x = new PointerPointer(AtomicAllocator.getInstance().getPointer(tempX, context)); if (target.data().dataType() == DataBuffer.Type.DOUBLE) { nativeOps.accumulateDouble(extras, x, (DoublePointer) z, arrays.length, len); } else if (target.data().dataType() == DataBuffer.Type.FLOAT) { nativeOps.accumulateFloat(extras, x, (FloatPointer) z, arrays.length, len); } else { long len = target.lengthLong(); Nd4j.getExecutioner().commit(); Nd4j.getCompressor().autoDecompress(arrays[i]); if (arrays[i].lengthLong() != len) if (target.data().dataType() == DataBuffer.Type.DOUBLE) { nativeOps.accumulateDouble(extras, dataPointers, (DoublePointer) AtomicAllocator.getInstance().getHostPointer(target), arrays.length, len); } else if (target.data().dataType() == DataBuffer.Type.FLOAT) { nativeOps.accumulateFloat(extras, dataPointers, (FloatPointer) AtomicAllocator.getInstance().getHostPointer(target), arrays.length, len); } else {
/** * Convert an ndarray to a byte array * @param arr the array to convert * @return the converted byte array * @throws IOException */ public static byte[] toByteArray(INDArray arr) throws IOException { if (arr.length() * arr.data().getElementSize() > Integer.MAX_VALUE) throw new ND4JIllegalStateException(""); ByteArrayOutputStream bos = new ByteArrayOutputStream((int) (arr.length() * arr.data().getElementSize())); DataOutputStream dos = new DataOutputStream(bos); write(arr, dos); byte[] ret = bos.toByteArray(); return ret; }