org.nd4j.linalg.api.buffer.DataBuffer Java Examples

The following examples show how to use org.nd4j.linalg.api.buffer.DataBuffer. 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: BaseLevel1.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
    }
}
 
Example #2
Source File: OpExecutionerUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
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)");

}
 
Example #3
Source File: BlasBufferUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * 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;

    }
}
 
Example #4
Source File: OnnxGraphMapper.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public INDArray mapTensorProto(OnnxProto3.TensorProto tensor) {
    if(tensor == null)
        return null;


    DataBuffer.Type type = nd4jTypeFromOnnxType(tensor.getDataType());

    ByteString bytes = tensor.getRawData();
    ByteBuffer byteBuffer = bytes.asReadOnlyByteBuffer().order(ByteOrder.nativeOrder());
    ByteBuffer directAlloc = ByteBuffer.allocateDirect(byteBuffer.capacity()).order(ByteOrder.nativeOrder());
    directAlloc.put(byteBuffer);
    directAlloc.rewind();
    long[] shape = getShapeFromTensor(tensor);
    DataBuffer buffer = Nd4j.createBuffer(directAlloc,type, ArrayUtil.prod(shape));
    INDArray arr = Nd4j.create(buffer).reshape(shape);
    return arr;
}
 
Example #5
Source File: BaseLevel3.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * syrk performs a rank-n update of an n-by-n symmetric matrix c, that is, one of the following operations:
 * c := alpha*a*a' + beta*c  for trans = 'N'or'n'
 * c := alpha*a'*a + beta*c  for trans = 'T'or't','C'or'c',
 * where c is an n-by-n symmetric matrix;
 * a is an n-by-k matrix, if trans = 'N'or'n',
 * a is a k-by-n matrix, if trans = 'T'or't','C'or'c'.
 *  @param Order
 * @param Uplo
 * @param Trans
 * @param alpha
 * @param A
 * @param beta
 * @param C
 */
@Override
public void syrk(char Order, char Uplo, char Trans, double alpha, INDArray A, double beta, INDArray C) {
    if (Nd4j.getExecutioner().getProfilingMode() == OpExecutioner.ProfilingMode.ALL)
        OpProfiler.getInstance().processBlasCall(false, A, C);

    // FIXME: int cast

    if (A.data().dataType() == DataBuffer.Type.DOUBLE) {
        DefaultOpExecutioner.validateDataType(DataBuffer.Type.DOUBLE, A, C);
        dsyrk(Order, Uplo, Trans, (int) C.rows(), 1, alpha, A, (int) A.size(0), beta, C, (int) C.size(0));
    } else {
        DefaultOpExecutioner.validateDataType(DataBuffer.Type.FLOAT, A, C);
        ssyrk(Order, Uplo, Trans, (int) C.rows(), 1, (float) alpha, A, (int) A.size(0), (float) beta, C, (int) C.size(0));
    }

    OpExecutionerUtil.checkForAny(C);
}
 
Example #6
Source File: ArrowSerde.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Create thee databuffer type frm the given type,
 * relative to the bytes in arrow in class:
 * {@link Type}
 * @param type the type to create the nd4j {@link DataBuffer.Type} from
 * @param elementSize the element size
 * @return the data buffer type
 */
public static DataBuffer.Type typeFromTensorType(byte type,int elementSize) {
    if(type == Type.Decimal || type == Type.FloatingPoint) {
        if(elementSize == 4) {
            return DataBuffer.Type.FLOAT;
        }
        else if(elementSize == 8) {
            return DataBuffer.Type.DOUBLE;
        }
    }
    else if(type == Type.Int) {
        if(elementSize == 4) {
            return DataBuffer.Type.INT;
        }
        else if(elementSize == 8) {
            return DataBuffer.Type.LONG;
        }
    }
    else {
        throw new IllegalArgumentException("Only valid types are Type.Decimal and Type.Int");
    }

    throw new IllegalArgumentException("Unable to determine data type");
}
 
Example #7
Source File: ConstantBuffersCache.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public DataBuffer getConstantBuffer(int[] array) {
    ArrayDescriptor descriptor = new ArrayDescriptor(array);

    if (!buffersCache.containsKey(descriptor)) {
        DataBuffer buffer = Nd4j.createBufferDetached(array);

        // we always allow int arrays with length < 3. 99.9% it's just dimension array. we don't want to recreate them over and over
        if (counter.get() < MAX_ENTRIES || array.length < 4) {
            counter.incrementAndGet();
            buffersCache.put(descriptor, buffer);
            bytes.addAndGet(array.length * 4);
        }
        return buffer;
    }

    return buffersCache.get(descriptor);
}
 
Example #8
Source File: SparseBaseLevel1.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Find the index of the element with maximum absolute value
 *
 * @param arr a vector
 * @return the index of the element with maximum absolute value
 * */
@Override
public int iamax(INDArray arr) {
    switch (arr.data().dataType()) {
        case DOUBLE:
            DefaultOpExecutioner.validateDataType(DataBuffer.Type.DOUBLE, arr);
            return idamax(arr.length(), arr, 1);
        case FLOAT:
            DefaultOpExecutioner.validateDataType(DataBuffer.Type.FLOAT, arr);
            return isamax(arr.length(), arr, 1);
        case HALF:
            DefaultOpExecutioner.validateDataType(DataBuffer.Type.HALF, arr);
            return ihamax(arr.length(), arr, 1);
        default:
    }
    throw new UnsupportedOperationException();
}
 
Example #9
Source File: BaseNDArrayFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray create(DataBuffer buffer, int[] shape, int[] stride, char order, long offset) {
    //ensure shapes that wind up being scalar end up with the write shape
    if (shape.length == 1 && shape[0] == 0) {
        shape = new int[] {1, 1};
    }
    return create(buffer, shape, stride, offset, order);
}
 
Example #10
Source File: ProtectedCudaConstantHandler.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private void ensureMaps(Integer deviceId) {
    if (!buffersCache.containsKey(deviceId)) {
        if (flowController == null)
            flowController = AtomicAllocator.getInstance().getFlowController();

        try {
            synchronized (this) {
                if (!buffersCache.containsKey(deviceId)) {

                    // TODO: this op call should be checked
                    //nativeOps.setDevice(new CudaPointer(deviceId));

                    buffersCache.put(deviceId, new ConcurrentHashMap<ArrayDescriptor, DataBuffer>());
                    constantOffsets.put(deviceId, new AtomicLong(0));
                    deviceLocks.put(deviceId, new Semaphore(1));

                    Pointer cAddr = NativeOpsHolder.getInstance().getDeviceNativeOps().getConstantSpace();
                    //                    logger.info("constant pointer: {}", cAddr.address() );

                    deviceAddresses.put(deviceId, cAddr);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example #11
Source File: AtomicAllocator.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method allocates required chunk of memory
 *
 * @param requiredMemory
 */
@Override
public AllocationPoint allocateMemory(DataBuffer buffer, AllocationShape requiredMemory, boolean initialize) {
    // by default we allocate on initial location
    AllocationPoint point = null;

    if (configuration.getMemoryModel() == Configuration.MemoryModel.IMMEDIATE) {
        point = allocateMemory(buffer, requiredMemory, memoryHandler.getInitialLocation(), initialize);
    } else if (configuration.getMemoryModel() == Configuration.MemoryModel.DELAYED) {
        // for DELAYED memory model we allocate only host memory, regardless of firstMemory configuration value
        point = allocateMemory(buffer, requiredMemory, AllocationStatus.HOST, initialize);
    }

    return point;
}
 
Example #12
Source File: BaseBlasWrapper.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public IComplexNDArray geru(IComplexFloat alpha, IComplexNDArray x, IComplexNDArray y, IComplexNDArray a) {
    LinAlgExceptions.assertVector(x, y);
    LinAlgExceptions.assertMatrix(a);

    if (x.data().dataType() == DataBuffer.Type.DOUBLE) {
        return geru(alpha.asDouble(), x, y, a);
    }
    level2().geru('N', alpha, x, y, a);
    return a;
}
 
Example #13
Source File: BaseBlasWrapper.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public IComplexNDArray gerc(IComplexFloat alpha, IComplexNDArray x, IComplexNDArray y, IComplexNDArray a) {
    if (x.data().dataType() == DataBuffer.Type.DOUBLE) {
        return gerc(alpha.asDouble(), x, y, a);
    }
    gerc(alpha, x, y, a);
    return a;
}
 
Example #14
Source File: CudaDataBufferFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer create(DataType dataType, long length, boolean initialize) {
    switch (dataType) {
        case UINT16:
            return new CudaUInt16DataBuffer(length, initialize);
        case UINT32:
            return new CudaUInt32DataBuffer(length, initialize);
        case UINT64:
            return new CudaUInt64DataBuffer(length, initialize);
        case LONG:
            return new CudaLongDataBuffer(length, initialize);
        case INT:
            return new CudaIntDataBuffer(length, initialize);
        case SHORT:
            return new CudaShortDataBuffer(length, initialize);
        case UBYTE:
            return new CudaUByteDataBuffer(length, initialize);
        case BYTE:
            return new CudaByteDataBuffer(length, initialize);
        case DOUBLE:
            return new CudaDoubleDataBuffer(length, initialize);
        case FLOAT:
            return new CudaFloatDataBuffer(length, initialize);
        case BFLOAT16:
            return new CudaBfloat16DataBuffer(length, initialize);
        case HALF:
            return new CudaHalfDataBuffer(length, initialize);
        case BOOL:
            return new CudaBoolDataBuffer(length, initialize);
        case UTF8:
            return new CudaUtf8Buffer(length, true);
        default:
            throw new UnsupportedOperationException("Unknown data type: [" + dataType + "]");
    }
}
 
Example #15
Source File: JCublasNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray convertToDoubles() {
    if (data.dataType() == DataBuffer.Type.DOUBLE)
        return this;

    val factory = Nd4j.getNDArrayFactory();
    val buffer = Nd4j.createBuffer(new long[]{this.length()}, DataBuffer.Type.DOUBLE);

    factory.convertDataEx(convertType(data.dataType()), AtomicAllocator.getInstance().getHostPointer(this.data()), DataBuffer.TypeEx.DOUBLE, AtomicAllocator.getInstance().getHostPointer(buffer), buffer.length());

    AtomicAllocator.getInstance().getAllocationPoint(buffer).tickHostWrite();

    return Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInformation);
}
 
Example #16
Source File: BasicTADManager.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<DataBuffer, DataBuffer> getTADOnlyShapeInfo(INDArray array, int[] dimension) {
    if (dimension != null && dimension.length > 1)
        Arrays.sort(dimension);

    if (dimension == null)
        dimension = new int[] {Integer.MAX_VALUE};

    val pack = Nd4j.getExecutioner().tadShapeInfoAndOffsets(array, dimension);

    //   logger.info("TAD shapeInfo after construction: {}", Arrays.toString(TadDescriptor.dataBufferToArray(outputBuffer)));
    // now we need to copy this buffer to either device global memory or device cache

    return new Pair<>(pack.getTadShapeInfo(), pack.getTadOffsets());
}
 
Example #17
Source File: DataTypeUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Set the allocation mode for the nd4j context
 * The value must be one of: heap, java cpp, or direct
 * or an @link{IllegalArgumentException} is thrown
 * @param allocationModeForContext
 */
public static void setDTypeForContext(DataBuffer.Type allocationModeForContext) {
    try {
        lock.writeLock().lock();

        dtype = allocationModeForContext;

        setDTypeForContext(getDTypeForName(allocationModeForContext));
    } finally {
        lock.writeLock().unlock();
    }
}
 
Example #18
Source File: OperationProfilerTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testBadTad3() {
        INDArray x = Nd4j.create(new int[] {2, 4, 5, 6, 7}, 'f');

        Pair<DataBuffer, DataBuffer> pair =
                        Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(x, 0, 2, 4);

        OpProfiler.PenaltyCause[] causes = OpProfiler.getInstance().processTADOperands(pair.getFirst());

//        log.info("Causes: {}", Arrays.toString(causes));
        assertEquals(1, causes.length);
        assertTrue(ArrayUtils.contains(causes, OpProfiler.PenaltyCause.TAD_NON_EWS_ACCESS));
    }
 
Example #19
Source File: BaseNDArrayFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Create a scalar ndarray with the specified offset
 *
 * @param value  the value to initialize the scalar with
 * @param offset the offset of the ndarray
 * @return the created ndarray
 */
@Override
public INDArray scalar(Number value, long offset) {
    if (Nd4j.dataType() == DataBuffer.Type.DOUBLE)
        return scalar(value.doubleValue(), offset);
    if (Nd4j.dataType() == DataBuffer.Type.FLOAT || Nd4j.dataType() == DataBuffer.Type.HALF)
        return scalar(value.floatValue(), offset);
    if (Nd4j.dataType() == DataBuffer.Type.INT)
        return scalar(value.intValue(), offset);
    throw new IllegalStateException("Illegal data opType " + Nd4j.dataType());
}
 
Example #20
Source File: BasicNDArrayCompressor.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * in place decompression of the given
 * ndarray. If the ndarray isn't compressed
 * this will do nothing
 * @param array the array to decompressed
 *              if it is comprssed
 */
public void decompressi(INDArray array) {
    if (array.data().dataType() != DataBuffer.Type.COMPRESSED)
        return;

    CompressedDataBuffer comp = (CompressedDataBuffer) array.data();
    CompressionDescriptor descriptor = comp.getCompressionDescriptor();

    if (!codecs.containsKey(descriptor.getCompressionAlgorithm()))
        throw new RuntimeException("Non-existent compression algorithm requested: ["
                        + descriptor.getCompressionAlgorithm() + "]");

    codecs.get(descriptor.getCompressionAlgorithm()).decompressi(array);
}
 
Example #21
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param floatBuffer
 * @param order
 */
public BaseNDArray(DataBuffer floatBuffer, char order) {
    this(floatBuffer, new int[] {(int) floatBuffer.length()},
            Nd4j.getStrides(new int[] {(int) floatBuffer.length()}, order), 0, order);
    if (floatBuffer.length() >= Integer.MAX_VALUE)
        throw new IllegalArgumentException("Length of buffer can not be >= Integer.MAX_VALUE");
}
 
Example #22
Source File: ProtectedCudaConstantHandler.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer getConstantBuffer(long[] array) {
    //  logger.info("getConstantBuffer(int[]) called");
    ArrayDescriptor descriptor = new ArrayDescriptor(array);

    Integer deviceId = AtomicAllocator.getInstance().getDeviceId();

    ensureMaps(deviceId);

    if (!buffersCache.get(deviceId).containsKey(descriptor)) {
        // we create new databuffer
        //logger.info("Creating new constant buffer...");
        DataBuffer buffer = Nd4j.createBufferDetached(array);

        if (constantOffsets.get(deviceId).get() + (array.length * 8) < MAX_CONSTANT_LENGTH) {
            buffer.setConstant(true);
            // now we move data to constant memory, and keep happy
            moveToConstantSpace(buffer);

            buffersCache.get(deviceId).put(descriptor, buffer);

            bytes.addAndGet(array.length * 8);
        }
        return buffer;
    } //else logger.info("Reusing constant buffer...");

    return buffersCache.get(deviceId).get(descriptor);
}
 
Example #23
Source File: ProtectedCudaShapeInfoProvider.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<DataBuffer, long[]> createShapeInformation(int[] shape, int[] stride, long offset, int elementWiseStride, char order) {
    // We enforce offset to 0 in shapeBuffer, since we need it for cache efficiency + we don't actually use offset value @ native side
    offset = 0;

    Integer deviceId = AtomicAllocator.getInstance().getDeviceId();

    ShapeDescriptor descriptor = new ShapeDescriptor(shape, stride, offset, elementWiseStride, order);

    if (!protector.containsDataBuffer(deviceId, descriptor)) {
        Pair<DataBuffer, long[]> buffer = null;
        synchronized (this) {
            if (!protector.containsDataBuffer(deviceId, descriptor)) {
                //log.info("Cache miss: {}", descriptor);
                buffer = super.createShapeInformation(shape, stride, offset, elementWiseStride, order);
                buffer.getFirst().setConstant(true);

                if (CudaEnvironment.getInstance().getConfiguration().getMemoryModel() == Configuration.MemoryModel.IMMEDIATE) {
                    Nd4j.getConstantHandler().moveToConstantSpace(buffer.getFirst());
                }

                //deviceCache.get(deviceId).put(descriptor, buffer);
                protector.persistDataBuffer(deviceId, descriptor, buffer);

                bytes.addAndGet(buffer.getFirst().length() * 4 * 2);

                cacheMiss.incrementAndGet();
            } else {
                buffer = protector.getDataBuffer(deviceId, descriptor);
            }
        }
        return buffer;
    } else {
        //       log.info("Cache hit: {}", descriptor);
        cacheHit.incrementAndGet();
    }

    return protector.getDataBuffer(deviceId, descriptor); //deviceCache.get(deviceId).get(descriptor);
}
 
Example #24
Source File: LapackTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCholeskyU() {
    INDArray A = Nd4j.create(new double[] {2, -1, 2, -1, 2, -1, 2, -1, 2,});
    A = A.reshape('f', 3, 3);
    INDArray O = Nd4j.create(A.shape());
    Nd4j.copy(A, O);

    Nd4j.getBlasWrapper().lapack().potrf(A, false);
    A = A.transpose().mmul(A);
    O.subi(A);
    DataBuffer db = O.data();
    for (int i = 0; i < db.length(); i++) {
        assertEquals(0, db.getFloat(i), 1e-5);
    }
}
 
Example #25
Source File: JCusparseNDArrayFactory.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray convertDataEx(DataBuffer.TypeEx typeSrc, INDArray source, DataBuffer.TypeEx typeDst) {
    return null;
}
 
Example #26
Source File: CudaDataBufferFactory.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public DataBuffer createInt(long length) {
    return new CudaIntDataBuffer(length);
}
 
Example #27
Source File: ComplexNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public ComplexNDArray(DataBuffer data, int[] newDims, int[] newStrides, long offset, char ordering) {
    super(data, newDims, newStrides, offset, ordering);
}
 
Example #28
Source File: DataBufferFastCloner.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public Object clone(Object o, IDeepCloner iDeepCloner, Map<Object, Object> map) {
    return ((DataBuffer)o).dup();
}
 
Example #29
Source File: JCublasNDArrayFactory.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public JCublasNDArrayFactory(DataBuffer.Type dtype, char order) {
    super(dtype, order);
    AtomicAllocator.getInstance();
}
 
Example #30
Source File: CudaDataBufferFactory.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public DataBuffer createULong(long length, boolean initialize, MemoryWorkspace workspace) {
    return new CudaUInt64DataBuffer(length, initialize, workspace);
}