Java Code Examples for org.nd4j.linalg.api.buffer.DataBuffer#dataType()

The following examples show how to use org.nd4j.linalg.api.buffer.DataBuffer#dataType() . 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: CudaDataBufferFactory.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public DataBuffer create(DataBuffer underlyingBuffer, long offset, long length) {
    if (underlyingBuffer.dataType() == DataBuffer.Type.DOUBLE) {
        return new CudaDoubleDataBuffer(underlyingBuffer, length, offset);
    } else if (underlyingBuffer.dataType() == DataBuffer.Type.FLOAT) {
        return new CudaFloatDataBuffer(underlyingBuffer, length, offset);

    } else if (underlyingBuffer.dataType() == DataBuffer.Type.INT) {
        return new CudaIntDataBuffer(underlyingBuffer, length, offset);
    } else if (underlyingBuffer.dataType() == DataBuffer.Type.HALF) {
        return new CudaHalfDataBuffer(underlyingBuffer, length, offset);
    } else if (underlyingBuffer.dataType() == DataBuffer.Type.LONG) {
        return new CudaLongDataBuffer(underlyingBuffer, length, offset);
    }

    throw new ND4JIllegalStateException("Unknown data buffer type: " + underlyingBuffer.dataType().toString());
}
 
Example 2
Source File: BaseLevel1.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void axpy(long n, double alpha, DataBuffer x, int offsetX, int incrX, DataBuffer y, int offsetY, int incrY) {
    if (supportsDataBufferL1Ops()) {
        if (x.dataType() == DataBuffer.Type.DOUBLE) {
            daxpy(n, alpha, x, offsetX, incrX, y, offsetY, incrY);
        } else if (x.dataType() == DataBuffer.Type.FLOAT) {
            saxpy(n, (float) alpha, x, offsetX, incrX, y, offsetY, incrY);
        } else {
            haxpy(n, (float) alpha, x, offsetX, incrX, y, offsetY, incrY);
        }
    } else {
        long[] shapex = {1, n};
        long[] shapey = {1, n};
        long[] stridex = {incrX, incrX};
        long[] stridey = {incrY, incrY};
        INDArray arrX = Nd4j.create(x, shapex, stridex, offsetX, 'c');
        INDArray arrY = Nd4j.create(x, shapey, stridey, offsetY, 'c');
        axpy(n, alpha, arrX, arrY);
    }
}
 
Example 3
Source File: BaseLevel1.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void axpy(long n, double alpha, DataBuffer x, int offsetX, int incrX, DataBuffer y, int offsetY, int incrY) {
    if (supportsDataBufferL1Ops()) {
        if (x.dataType() == DataType.DOUBLE) {
            daxpy(n, alpha, x, offsetX, incrX, y, offsetY, incrY);
        } else if (x.dataType() == DataType.FLOAT) {
            saxpy(n, (float) alpha, x, offsetX, incrX, y, offsetY, incrY);
        } else {
            haxpy(n, (float) alpha, x, offsetX, incrX, y, offsetY, incrY);
        }
    } else {
        long[] shapex = {1, n};
        long[] shapey = {1, n};
        long[] stridex = {incrX, incrX};
        long[] stridey = {incrY, incrY};
        INDArray arrX = Nd4j.create(x, shapex, stridex, offsetX, 'c');
        INDArray arrY = Nd4j.create(x, shapey, stridey, offsetY, 'c');
        axpy(n, alpha, arrX, arrY);
    }
}
 
Example 4
Source File: BaseLevel1.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public double asum(long n, DataBuffer x, int offsetX, int incrX) {
    if (supportsDataBufferL1Ops()) {
        if (x.dataType() == DataBuffer.Type.FLOAT) {
            return sasum(n, x, offsetX, incrX);
        } else if (x.dataType() == DataBuffer.Type.DOUBLE) {
            return dasum(n, x, offsetX, incrX);
        } else {
            return hasum(n, x, offsetX, incrX);
        }
    } else {
        long[] shapex = {1, n};
        long[] stridex = {incrX, incrX};
        INDArray arrX = Nd4j.create(x, shapex, stridex, offsetX, 'c');
        return asum(arrX);
    }
}
 
Example 5
Source File: BaseLevel1.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public double dot(long n, DataBuffer x, int offsetX, int incrX, DataBuffer y, int offsetY, int incrY) {
    if (supportsDataBufferL1Ops()) {
        if (x.dataType() == DataBuffer.Type.FLOAT) {
            return sdot(n, x, offsetX, incrX, y, offsetY, incrY);
        } else if (x.dataType() == DataBuffer.Type.DOUBLE) {
            return ddot(n, x, offsetX, incrX, y, offsetY, incrY);
        } else {
            return hdot(n, x, offsetX, incrX, y, offsetY, incrY);
        }
    } else {
        long[] shapex = {1, n};
        long[] shapey = {1, n};
        long[] stridex = {incrX, incrX};
        long[] stridey = {incrY, incrY};
        INDArray arrX = Nd4j.create(x, shapex, stridex, offsetX, 'c');
        INDArray arrY = Nd4j.create(x, shapey, stridey, offsetY, 'c');
        return dot(n, 0.0, arrX, arrY);
    }
}
 
Example 6
Source File: DataBufferStruct.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Create a data buffer struct within
 * the passed in {@link FlatBufferBuilder}
 * @param bufferBuilder the existing flatbuffer
 *                      to use to serialize the {@link DataBuffer}
 * @param create the databuffer to serialize
 * @return an int representing the offset of the buffer
 */
public static int createDataBufferStruct(FlatBufferBuilder bufferBuilder,DataBuffer create) {
    bufferBuilder.prep(create.getElementSize(), (int) create.length() * create.getElementSize());
    for(int i = (int) (create.length() - 1); i >= 0; i--) {
        switch(create.dataType()) {
            case DOUBLE:
                double putDouble = create.getDouble(i);
                bufferBuilder.putDouble(putDouble);
                break;
            case FLOAT:
                float putFloat = create.getFloat(i);
                bufferBuilder.putFloat(putFloat);
                break;
            case INT:
                int putInt = create.getInt(i);
                bufferBuilder.putInt(putInt);
                break;
            case LONG:
                long putLong = create.getLong(i);
                bufferBuilder.putLong(putLong);
        }
    }

    return bufferBuilder.offset();

}
 
Example 7
Source File: TADTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * this method compares rank, shape and stride for two given shapeBuffers
 * @param shapeA
 * @param shapeB
 * @return
 */
protected boolean compareShapes(@NonNull DataBuffer shapeA, @NonNull DataBuffer shapeB) {
    if (shapeA.dataType() != DataType.INT)
        throw new IllegalStateException("ShapeBuffer should have dataType of INT");

    if (shapeA.dataType() != shapeB.dataType())
        return false;

    int rank = shapeA.getInt(0);
    if (rank != shapeB.getInt(0))
        return false;

    for (int e = 1; e <= rank * 2; e++) {
        if (shapeA.getInt(e) != shapeB.getInt(e))
            return false;
    }

    return true;
}
 
Example 8
Source File: CudaDataBufferFactory.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method will create new DataBuffer of the same dataType & same length
 *
 * @param buffer
 * @param workspace
 * @return
 */
@Override
public DataBuffer createSame(DataBuffer buffer, boolean init, MemoryWorkspace workspace) {
    switch (buffer.dataType()) {
        case INT:
            return createInt(buffer.length(), init, workspace);
        case FLOAT:
            return createFloat(buffer.length(), init, workspace);
        case DOUBLE:
            return createDouble(buffer.length(), init, workspace);
        case HALF:
            return createHalf(buffer.length(), init, workspace);
        default:
            throw new UnsupportedOperationException("Unknown dataType: " + buffer.dataType());
    }
}
 
Example 9
Source File: CudaDataBufferFactory.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method will create new DataBuffer of the same dataType & same length
 *
 * @param buffer
 * @return
 */
@Override
public DataBuffer createSame(DataBuffer buffer, boolean init) {
    switch (buffer.dataType()) {
        case INT:
            return createInt(buffer.length(), init);
        case FLOAT:
            return createFloat(buffer.length(), init);
        case DOUBLE:
            return createDouble(buffer.length(), init);
        case HALF:
            return createHalf(buffer.length(), init);
        default:
            throw new UnsupportedOperationException("Unknown dataType: " + buffer.dataType());
    }
}
 
Example 10
Source File: CudaZeroHandler.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * PLEASE NOTE: This method always returns pointer within OS memory space
 *
 * @param buffer
 * @return
 */
@Override
public org.bytedeco.javacpp.Pointer getHostPointer(DataBuffer buffer) {
    AllocationPoint dstPoint = ((BaseCudaDataBuffer) buffer).getAllocationPoint();

    // return pointer with offset if needed. length is specified for constructor compatibility purposes
    if (dstPoint.getHostPointer() == null) {
        return null;
    }

    synchronizeThreadDevice(Thread.currentThread().getId(), dstPoint.getDeviceId(), dstPoint);

    CudaPointer p = new CudaPointer(dstPoint.getHostPointer(), buffer.length(), 0);

    switch (buffer.dataType()) {
        case DOUBLE:
            return p.asDoublePointer();
        case FLOAT:
            return p.asFloatPointer();
        case UINT32:
        case INT:
            return p.asIntPointer();
        case SHORT:
        case UINT16:
        case BFLOAT16:
        case HALF:
            return p.asShortPointer();
        case UINT64:
        case LONG:
            return p.asLongPointer();
        default:
            return p;
    }
}
 
Example 11
Source File: BaseLevel1.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public int iamax(long n, DataBuffer x, int offsetX, int incrX) {
    if (supportsDataBufferL1Ops()) {
        if (x.dataType() == DataType.FLOAT) {
            return isamax(n, x, offsetX, incrX);
        } else {
            return isamax(n, x, offsetX, incrX);
        }
    } else {
        long[] shapex = {1, n};
        long[] stridex = {incrX, incrX};
        INDArray arrX = Nd4j.create(x, shapex, stridex, offsetX, 'c');
        return iamax(n, arrX, incrX);
    }
}
 
Example 12
Source File: DataBufferLogEntry.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public DataBufferLogEntry(DataBuffer buffer, String status) {
    this.length = buffer.length();
    this.dataType = buffer.dataType() == DataBuffer.Type.DOUBLE ? "double" : "float";
    this.stackTraceElements = Thread.currentThread().getStackTrace();
    this.references = buffer.references();
    timestamp = System.currentTimeMillis();
    this.status = status;
}
 
Example 13
Source File: BaseCudaDataBuffer.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public BaseCudaDataBuffer(@NonNull DataBuffer underlyingBuffer, long length, long offset) {
    //this(length, underlyingBuffer.getElementSize(), offset);
    this.allocationMode = AllocationMode.LONG_SHAPE;
    initTypeAndSize();
    this.wrappedDataBuffer = underlyingBuffer;
    this.originalBuffer = underlyingBuffer.originalDataBuffer() == null ? underlyingBuffer
                    : underlyingBuffer.originalDataBuffer();
    this.length = length;
    this.offset = offset;
    this.originalOffset = offset;
    this.trackingPoint = underlyingBuffer.getTrackingPoint();
    this.elementSize = (byte) underlyingBuffer.getElementSize();
    this.allocationPoint = ((BaseCudaDataBuffer) underlyingBuffer).allocationPoint;

    if (underlyingBuffer.dataType() == Type.DOUBLE) {
        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), originalBuffer.length()).asDoublePointer();
        indexer = DoubleIndexer.create((DoublePointer) pointer);
    } else if (underlyingBuffer.dataType() == Type.FLOAT) {
        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), originalBuffer.length()).asFloatPointer();
        indexer = FloatIndexer.create((FloatPointer) pointer);
    } else if (underlyingBuffer.dataType() == Type.INT) {
        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), originalBuffer.length()).asIntPointer();
        indexer = IntIndexer.create((IntPointer) pointer);
    } else if (underlyingBuffer.dataType() == Type.HALF) {
        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), originalBuffer.length()).asShortPointer();
        indexer = HalfIndexer.create((ShortPointer) pointer);
    } else if (underlyingBuffer.dataType() == Type.LONG) {
        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), originalBuffer.length()).asLongPointer();
        indexer = LongIndexer.create((LongPointer) pointer);
    }
}
 
Example 14
Source File: BaseLevel1.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public int iamax(long n, DataBuffer x, int offsetX, int incrX) {
    if (supportsDataBufferL1Ops()) {
        if (x.dataType() == DataBuffer.Type.FLOAT) {
            return isamax(n, x, offsetX, incrX);
        } else {
            return isamax(n, x, offsetX, incrX);
        }
    } else {
        long[] shapex = {1, n};
        long[] stridex = {incrX, incrX};
        INDArray arrX = Nd4j.create(x, shapex, stridex, offsetX, 'c');
        return iamax(n, arrX, incrX);
    }
}
 
Example 15
Source File: CudaDataBufferFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer create(DataBuffer underlyingBuffer, long offset, long length) {
    switch (underlyingBuffer.dataType()) {
        case DOUBLE:
            return new CudaDoubleDataBuffer(underlyingBuffer, length, offset);
        case FLOAT:
            return new CudaFloatDataBuffer(underlyingBuffer, length, offset);
        case HALF:
            return new CudaHalfDataBuffer(underlyingBuffer, length, offset);
        case BFLOAT16:
            return new CudaBfloat16DataBuffer(underlyingBuffer, length, offset);
        case UINT64:
            return new CudaUInt64DataBuffer(underlyingBuffer, length, offset);
        case LONG:
            return new CudaLongDataBuffer(underlyingBuffer, length, offset);
        case UINT32:
            return new CudaUInt32DataBuffer(underlyingBuffer, length, offset);
        case INT:
            return new CudaIntDataBuffer(underlyingBuffer, length, offset);
        case UINT16:
            return new CudaUInt16DataBuffer(underlyingBuffer, length, offset);
        case SHORT:
            return new CudaShortDataBuffer(underlyingBuffer, length, offset);
        case UBYTE:
            return new CudaUByteDataBuffer(underlyingBuffer, length, offset);
        case BYTE:
            return new CudaByteDataBuffer(underlyingBuffer, length, offset);
        case BOOL:
            return new CudaBoolDataBuffer(underlyingBuffer, length, offset);
        case UTF8:
            return new CudaUtf8Buffer(underlyingBuffer, length, offset);
        default:
            throw new ND4JIllegalStateException("Unknown data buffer type: " + underlyingBuffer.dataType().toString());
    }
}
 
Example 16
Source File: JCublasNDArrayFactory.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray create(DataBuffer data, int[] shape, int[] stride, long offset) {
    return new JCublasNDArray(data, ArrayUtil.toLongArray(shape), ArrayUtil.toLongArray(stride), Nd4j.order(), data.dataType());
}
 
Example 17
Source File: JCublasNDArrayFactory.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray create(DataBuffer data, long[] shape, long[] stride, long offset) {
    return new JCublasNDArray(data, shape, stride, offset, Nd4j.order(), data.dataType());
}
 
Example 18
Source File: AbstractCompressor.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public static DataBuffer.TypeEx getBufferTypeEx(DataBuffer buffer) {
    DataBuffer.Type type = buffer.dataType();

    return convertType(type);
}
 
Example 19
Source File: JCublasNDArrayFactory.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray create(DataBuffer data, long rows, long columns, int[] stride, long offset) {
    // FIXME: int cast
    return new JCublasNDArray(data, new long[] {rows, columns}, ArrayUtil.toLongArray(stride), Nd4j.order(), data.dataType());
}
 
Example 20
Source File: CudaZeroHandler.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * PLEASE NOTE: Specific implementation, on systems without special devices can return HostPointer here
 *
 * @param buffer
 * @return
 */
@Override
public org.bytedeco.javacpp.Pointer getDevicePointer(DataBuffer buffer, CudaContext context) {
    // TODO: It would be awesome to get rid of typecasting here
    AllocationPoint dstPoint = ((BaseCudaDataBuffer) buffer).getAllocationPoint();

    // if that's device state, we probably might want to update device memory state
    if (dstPoint.getAllocationStatus() == AllocationStatus.DEVICE) {
        if (!dstPoint.isActualOnDeviceSide()) {
            //relocate(AllocationStatus.HOST, AllocationStatus.DEVICE, dstPoint, dstPoint.getShape(), context);
            throw new UnsupportedOperationException("Pew-pew");
        }
    }

    if (dstPoint.getDevicePointer() == null)
        return null;


    // return pointer. length is specified for constructor compatibility purposes. Offset is accounted at C++ side
    val p = new CudaPointer(dstPoint.getDevicePointer(), buffer.length(), 0);

    if (OpProfiler.getInstance().getConfig().isCheckLocality())
         NativeOpsHolder.getInstance().getDeviceNativeOps().tryPointer(context.getOldStream(), p, 1);

    switch (buffer.dataType()) {
        case DOUBLE:
            return p.asDoublePointer();
        case FLOAT:
            return p.asFloatPointer();
        case UINT32:
        case INT:
            return p.asIntPointer();
        case SHORT:
        case UINT16:
        case HALF:
        case BFLOAT16:
            return p.asShortPointer();
        case UINT64:
        case LONG:
            return p.asLongPointer();
        case UTF8:
        case UBYTE:
        case BYTE:
            return p.asBytePointer();
        case BOOL:
            return p.asBooleanPointer();
        default:
            return p;
    }
}