Search in sources :

Example 16 with FloatPointer

use of org.bytedeco.javacpp.FloatPointer in project deeplearning4j by deeplearning4j.

the class Hdf5Archive method readDataSet.

/**
     * Read data set as ND4J array from HDF5 group.
     *
     * @param fileGroup     HDF5 file or group (as CommonFG)
     * @param datasetName   Name of data set
     * @return
     * @throws UnsupportedKerasConfigurationException
     */
private INDArray readDataSet(hdf5.CommonFG fileGroup, String datasetName) throws UnsupportedKerasConfigurationException {
    hdf5.DataSet dataset = fileGroup.openDataSet(datasetName);
    hdf5.DataSpace space = dataset.getSpace();
    int nbDims = space.getSimpleExtentNdims();
    long[] dims = new long[nbDims];
    space.getSimpleExtentDims(dims);
    float[] dataBuffer = null;
    FloatPointer fp = null;
    int j = 0;
    INDArray data = null;
    switch(nbDims) {
        case 4:
            /* 2D Convolution weights */
            dataBuffer = new float[(int) (dims[0] * dims[1] * dims[2] * dims[3])];
            fp = new FloatPointer(dataBuffer);
            dataset.read(fp, new hdf5.DataType(hdf5.PredType.NATIVE_FLOAT()));
            fp.get(dataBuffer);
            data = Nd4j.create((int) dims[0], (int) dims[1], (int) dims[2], (int) dims[3]);
            j = 0;
            for (int i1 = 0; i1 < dims[0]; i1++) for (int i2 = 0; i2 < dims[1]; i2++) for (int i3 = 0; i3 < dims[2]; i3++) for (int i4 = 0; i4 < dims[3]; i4++) data.putScalar(i1, i2, i3, i4, dataBuffer[j++]);
            break;
        case 3:
            dataBuffer = new float[(int) (dims[0] * dims[1] * dims[2])];
            fp = new FloatPointer(dataBuffer);
            dataset.read(fp, new hdf5.DataType(hdf5.PredType.NATIVE_FLOAT()));
            fp.get(dataBuffer);
            data = Nd4j.create((int) dims[0], (int) dims[1], (int) dims[2]);
            j = 0;
            for (int i1 = 0; i1 < dims[0]; i1++) for (int i2 = 0; i2 < dims[1]; i2++) for (int i3 = 0; i3 < dims[2]; i3++) data.putScalar(i1, i2, i3, dataBuffer[j++]);
            break;
        case 2:
            /* Dense and Recurrent weights */
            dataBuffer = new float[(int) (dims[0] * dims[1])];
            fp = new FloatPointer(dataBuffer);
            dataset.read(fp, new hdf5.DataType(hdf5.PredType.NATIVE_FLOAT()));
            fp.get(dataBuffer);
            data = Nd4j.create((int) dims[0], (int) dims[1]);
            j = 0;
            for (int i1 = 0; i1 < dims[0]; i1++) for (int i2 = 0; i2 < dims[1]; i2++) data.putScalar(i1, i2, dataBuffer[j++]);
            break;
        case 1:
            /* Bias */
            dataBuffer = new float[(int) dims[0]];
            fp = new FloatPointer(dataBuffer);
            dataset.read(fp, new hdf5.DataType(hdf5.PredType.NATIVE_FLOAT()));
            fp.get(dataBuffer);
            data = Nd4j.create((int) dims[0]);
            j = 0;
            for (int i1 = 0; i1 < dims[0]; i1++) data.putScalar(i1, dataBuffer[j++]);
            break;
        default:
            throw new UnsupportedKerasConfigurationException("Cannot import weights with rank " + nbDims);
    }
    return data;
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) FloatPointer(org.bytedeco.javacpp.FloatPointer) org.bytedeco.javacpp.hdf5(org.bytedeco.javacpp.hdf5)

Example 17 with FloatPointer

use of org.bytedeco.javacpp.FloatPointer in project nd4j by deeplearning4j.

the class HalfOpsTests method testHalfToFloat1.

@Test
public void testHalfToFloat1() throws Exception {
    File tempFile = File.createTempFile("dsadasd", "dsdfasd");
    tempFile.deleteOnExit();
    INDArray array = Nd4j.linspace(1, 100, 100);
    DataOutputStream stream = new DataOutputStream(new FileOutputStream(tempFile));
    Nd4j.write(array, stream);
    DataInputStream dis = new DataInputStream(new FileInputStream(tempFile));
    INDArray restoredFP16 = Nd4j.read(dis);
    // assertEquals(array, restoredFP16);
    DataTypeUtil.setDTypeForContext(DataBuffer.Type.FLOAT);
    assertEquals(DataBuffer.Type.FLOAT, Nd4j.dataType());
    log.error("--------------------");
    dis = new DataInputStream(new FileInputStream(tempFile));
    INDArray expFP32 = Nd4j.linspace(1, 100, 100);
    INDArray restoredFP32 = Nd4j.read(dis);
    CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();
    assertTrue(AtomicAllocator.getInstance().getPointer(expFP32, context) instanceof FloatPointer);
    assertTrue(AtomicAllocator.getInstance().getPointer(restoredFP32, context) instanceof FloatPointer);
    assertEquals(DataBuffer.Type.FLOAT, expFP32.data().dataType());
    assertEquals(DataBuffer.Type.FLOAT, restoredFP32.data().dataType());
    assertEquals(expFP32, restoredFP32);
    DataTypeUtil.setDTypeForContext(DataBuffer.Type.HALF);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) FloatPointer(org.bytedeco.javacpp.FloatPointer) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) Test(org.junit.Test)

Example 18 with FloatPointer

use of org.bytedeco.javacpp.FloatPointer in project nd4j by deeplearning4j.

the class JcublasLevel1 method sscal.

@Override
protected void sscal(int N, float alpha, INDArray X, int incX) {
    if (Nd4j.dataType() != DataBuffer.Type.FLOAT)
        logger.warn("FLOAT scal called");
    Nd4j.getExecutioner().push();
    CudaContext ctx = allocator.getFlowController().prepareAction(X);
    CublasPointer xCPointer = new CublasPointer(X, ctx);
    cublasHandle_t handle = ctx.getHandle();
    synchronized (handle) {
        cublasSetStream_v2(new cublasContext(handle), new CUstream_st(ctx.getOldStream()));
        cublasSscal_v2(new cublasContext(handle), N, new FloatPointer(alpha), (FloatPointer) xCPointer.getDevicePointer(), incX);
    }
    allocator.registerAction(ctx, X);
    OpExecutionerUtil.checkForAny(X);
}
Also used : CUstream_st(org.bytedeco.javacpp.cuda.CUstream_st) org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t(org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t) FloatPointer(org.bytedeco.javacpp.FloatPointer) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) CublasPointer(org.nd4j.linalg.jcublas.CublasPointer)

Example 19 with FloatPointer

use of org.bytedeco.javacpp.FloatPointer in project nd4j by deeplearning4j.

the class JcublasLevel1 method snrm2.

@Override
protected float snrm2(int N, INDArray X, int incX) {
    if (Nd4j.dataType() != DataBuffer.Type.FLOAT)
        logger.warn("FLOAT nrm2 called");
    Nd4j.getExecutioner().push();
    CudaContext ctx = allocator.getFlowController().prepareAction(null, X);
    float ret;
    CublasPointer cAPointer = new CublasPointer(X, ctx);
    cublasHandle_t handle = ctx.getHandle();
    synchronized (handle) {
        cublasSetStream_v2(new cublasContext(handle), new CUstream_st(ctx.getOldStream()));
        FloatPointer resultPointer = new FloatPointer(0.0f);
        cublasSnrm2_v2(new cublasContext(handle), N, (FloatPointer) cAPointer.getDevicePointer(), incX, resultPointer);
        ret = resultPointer.get();
    }
    allocator.registerAction(ctx, null, X);
    return ret;
}
Also used : CUstream_st(org.bytedeco.javacpp.cuda.CUstream_st) org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t(org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t) FloatPointer(org.bytedeco.javacpp.FloatPointer) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) CublasPointer(org.nd4j.linalg.jcublas.CublasPointer)

Example 20 with FloatPointer

use of org.bytedeco.javacpp.FloatPointer in project nd4j by deeplearning4j.

the class JcublasLevel3 method strsm.

@Override
protected void strsm(char Order, char Side, char Uplo, char TransA, char Diag, int M, int N, float alpha, INDArray A, int lda, INDArray B, int ldb) {
    if (Nd4j.dataType() != DataBuffer.Type.FLOAT)
        logger.warn("FLOAT trsm called");
    Nd4j.getExecutioner().push();
    CudaContext ctx = allocator.getFlowController().prepareAction(B, A);
    CublasPointer aPointer = new CublasPointer(A, ctx);
    CublasPointer bPointer = new CublasPointer(B, ctx);
    cublasHandle_t handle = ctx.getHandle();
    synchronized (handle) {
        cublasSetStream_v2(new cublasContext(handle), new CUstream_st(ctx.getOldStream()));
        cublasStrsm_v2(new cublasContext(handle), convertSideMode(Side), convertUplo(Uplo), convertTranspose(TransA), convertDiag(Diag), M, N, new FloatPointer(alpha), (FloatPointer) aPointer.getDevicePointer(), lda, (FloatPointer) bPointer.getDevicePointer(), ldb);
    }
    allocator.registerAction(ctx, B, A);
    OpExecutionerUtil.checkForAny(B);
}
Also used : org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t(org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t) FloatPointer(org.bytedeco.javacpp.FloatPointer) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) CublasPointer(org.nd4j.linalg.jcublas.CublasPointer)

Aggregations

FloatPointer (org.bytedeco.javacpp.FloatPointer)30 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)15 CublasPointer (org.nd4j.linalg.jcublas.CublasPointer)14 IntPointer (org.bytedeco.javacpp.IntPointer)11 INDArray (org.nd4j.linalg.api.ndarray.INDArray)11 DoublePointer (org.bytedeco.javacpp.DoublePointer)9 CUstream_st (org.bytedeco.javacpp.cuda.CUstream_st)9 org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t (org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t)9 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)7 Pointer (org.bytedeco.javacpp.Pointer)6 BlasException (org.nd4j.linalg.api.blas.BlasException)6 BytePointer (org.bytedeco.javacpp.BytePointer)5 ShortPointer (org.bytedeco.javacpp.ShortPointer)5 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)5 org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t (org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t)5 GridExecutioner (org.nd4j.linalg.api.ops.executioner.GridExecutioner)5 ByteBuffer (java.nio.ByteBuffer)4 DoubleBuffer (java.nio.DoubleBuffer)4 FloatBuffer (java.nio.FloatBuffer)4 IntBuffer (java.nio.IntBuffer)4