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;
}
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);
}
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);
}
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;
}
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);
}
Aggregations