Java Code Examples for org.nd4j.linalg.api.buffer.DataType#BFLOAT16

The following examples show how to use org.nd4j.linalg.api.buffer.DataType#BFLOAT16 . 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: PythonNumpyTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Parameterized.Parameters(name = "{index}: Testing with DataType={0}")
public static DataType[] data() {
    return new DataType[] {
            DataType.BOOL,
            DataType.FLOAT16,
            DataType.BFLOAT16,
            DataType.FLOAT,
            DataType.DOUBLE,
            DataType.INT8,
            DataType.INT16,
            DataType.INT32,
            DataType.INT64,
            DataType.UINT8,
            DataType.UINT16,
            DataType.UINT32,
            DataType.UINT64
    };
}
 
Example 2
Source File: PythonNumpyJobTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Parameterized.Parameters(name = "{index}: Testing with DataType={0}")
public static DataType[] params() {
    return new DataType[]{
            DataType.BOOL,
            DataType.FLOAT16,
            DataType.BFLOAT16,
            DataType.FLOAT,
            DataType.DOUBLE,
            DataType.INT8,
            DataType.INT16,
            DataType.INT32,
            DataType.INT64,
            DataType.UINT8,
            DataType.UINT16,
            DataType.UINT32,
            DataType.UINT64
    };
}
 
Example 3
Source File: PythonNumpyBasicTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecution(){
    List<PythonVariable> inputs = new ArrayList<>();
    INDArray x = Nd4j.ones(dataType, shape);
    INDArray y = Nd4j.zeros(dataType, shape);
    INDArray z = (dataType == DataType.BOOL)?x:x.mul(y.add(2));
    z = (dataType == DataType.BFLOAT16)? z.castTo(DataType.FLOAT): z;
    PythonType<INDArray> arrType = PythonTypes.get("numpy.ndarray");
    inputs.add(new PythonVariable<>("x", arrType, x));
    inputs.add(new PythonVariable<>("y", arrType, y));
    List<PythonVariable> outputs = new ArrayList<>();
    PythonVariable<INDArray> output = new PythonVariable<>("z", arrType);
    outputs.add(output);
    String code = (dataType == DataType.BOOL)?"z = x":"z = x * (y + 2)";
    if (shape.length == 0){ // scalar special case
        code += "\nimport numpy as np\nz = np.asarray(float(z), dtype=x.dtype)";
    }
    PythonExecutioner.exec(code, inputs, outputs);
    INDArray z2 = output.getValue();

    Assert.assertEquals(z.dataType(), z2.dataType());
    Assert.assertEquals(z, z2);

}
 
Example 4
Source File: ArrayOptionsHelper.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public static DataType dataType(long opt) {
    if (hasBitSet(opt, DTYPE_COMPRESSED_BIT))
        return DataType.COMPRESSED;
    else if (hasBitSet(opt, DTYPE_HALF_BIT))
        return DataType.HALF;
    else if (hasBitSet(opt, DTYPE_BFLOAT16_BIT))
        return DataType.BFLOAT16;
    else if (hasBitSet(opt, DTYPE_FLOAT_BIT))
        return DataType.FLOAT;
    else if (hasBitSet(opt, DTYPE_DOUBLE_BIT))
        return DataType.DOUBLE;
    else if (hasBitSet(opt, DTYPE_INT_BIT))
        return hasBitSet(opt, DTYPE_UNSIGNED_BIT) ? DataType.UINT32 : DataType.INT;
    else if (hasBitSet(opt, DTYPE_LONG_BIT))
        return hasBitSet(opt, DTYPE_UNSIGNED_BIT) ? DataType.UINT64 : DataType.LONG;
    else if (hasBitSet(opt, DTYPE_BOOL_BIT))
        return DataType.BOOL;
    else if (hasBitSet(opt, DTYPE_BYTE_BIT)) {
        return hasBitSet(opt, DTYPE_UNSIGNED_BIT) ? DataType.UBYTE : DataType.BYTE;     //Byte bit set for both UBYTE and BYTE
    } else if (hasBitSet(opt, DTYPE_SHORT_BIT))
        return hasBitSet(opt, DTYPE_UNSIGNED_BIT) ? DataType.UINT16 : DataType.SHORT;
    else if (hasBitSet(opt, DTYPE_UTF8_BIT))
        return DataType.UTF8;
    else
        throw new ND4JUnknownDataTypeException("Unknown extras set: [" + opt + "]");
}
 
Example 5
Source File: SameDiffTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRngSanityCheck(){
    Nd4j.getRandom().setSeed(12345);
    for(DataType dt : new DataType[]{DataType.FLOAT, DataType.DOUBLE,DataType.BFLOAT16}) {
        if (!dt.isNumerical())
            continue;
        SameDiff sameDiff = SameDiff.create();
        INDArray indaShape = Nd4j.createFromArray(3, 10);
        SDVariable sdShape = sameDiff.constant(indaShape);
        SDVariable random = sameDiff.random().uniform("data", 0.0, 10.0, dt, 3, 10);
        INDArray out = random.eval();
        String s = out.toString();
    }
}
 
Example 6
Source File: PythonNumpyJobTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNumpyJobReturnAllVariables(){
    PythonContextManager.deleteNonMainContexts();
    List<PythonVariable> inputs = new ArrayList<>();
    INDArray x = Nd4j.ones(dataType, 2, 3);
    INDArray y = Nd4j.zeros(dataType, 2, 3);
    INDArray z = (dataType == DataType.BOOL)?x:x.mul(y.add(2));
    PythonType<INDArray> arrType = PythonTypes.get("numpy.ndarray");
    inputs.add(new PythonVariable<>("x", arrType, x));
    inputs.add(new PythonVariable<>("y", arrType, y));
    String code = (dataType == DataType.BOOL)?"z = x":"z = x * (y + 2)";

    PythonJob job = new PythonJob("job1", code, false);
    List<PythonVariable> outputs = job.execAndReturnAllVariables(inputs);

    INDArray x2 = (INDArray) outputs.get(0).getValue();
    INDArray y2 = (INDArray) outputs.get(1).getValue();
    INDArray z2 = (INDArray) outputs.get(2).getValue();

    if (dataType == DataType.BFLOAT16){
        x = x.castTo(DataType.FLOAT);
        y = y.castTo(DataType.FLOAT);
        z = z.castTo(DataType.FLOAT);
    }
    Assert.assertEquals(x, x2);
    Assert.assertEquals(y, y2);
    Assert.assertEquals(z, z2);

}
 
Example 7
Source File: PythonNumpyJobTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNumpyJobBasic(){
    PythonContextManager.deleteNonMainContexts();
    List<PythonVariable> inputs = new ArrayList<>();
    INDArray x = Nd4j.ones(dataType, 2, 3);
    INDArray y = Nd4j.zeros(dataType, 2, 3);
    INDArray z = (dataType == DataType.BOOL)?x:x.mul(y.add(2));
    z = (dataType == DataType.BFLOAT16)? z.castTo(DataType.FLOAT): z;
    PythonType<INDArray> arrType = PythonTypes.get("numpy.ndarray");
    inputs.add(new PythonVariable<>("x", arrType, x));
    inputs.add(new PythonVariable<>("y", arrType, y));
    List<PythonVariable> outputs = new ArrayList<>();
    PythonVariable<INDArray> output = new PythonVariable<>("z", arrType);
    outputs.add(output);
    String code = (dataType == DataType.BOOL)?"z = x":"z = x * (y + 2)";

    PythonJob job = new PythonJob("job1", code, false);

    job.exec(inputs, outputs);

    INDArray z2 = output.getValue();

    if (dataType == DataType.BFLOAT16){
        z2  = z2.castTo(DataType.FLOAT);
    }

    Assert.assertEquals(z, z2);

}
 
Example 8
Source File: PythonNumpyBasicTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters(name = "{index}: Testing with DataType={0}, shape={2}")
public static Collection params() {
    DataType[] types = new DataType[] {
            DataType.BOOL,
            DataType.FLOAT16,
            DataType.BFLOAT16,
            DataType.FLOAT,
            DataType.DOUBLE,
            DataType.INT8,
            DataType.INT16,
            DataType.INT32,
            DataType.INT64,
            DataType.UINT8,
            DataType.UINT16,
            DataType.UINT32,
            DataType.UINT64
    };

    long[][] shapes = new long[][]{
         new long[]{2, 3},
         new long[]{3},
         new long[]{1},
            new long[]{} // scalar
    };


    List<Object[]> ret = new ArrayList<>();
    for (DataType type: types){
        for (long[] shape: shapes){
            ret.add(new Object[]{type, shape, Arrays.toString(shape)});
        }
    }
    return ret;
}
 
Example 9
Source File: CustomOpsTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSizeTypes(){
    List<DataType> failed = new ArrayList<>();
    for(DataType dt : new DataType[]{DataType.LONG, DataType.INT, DataType.SHORT, DataType.BYTE,
            DataType.UINT64, DataType.UINT32, DataType.UINT16, DataType.UBYTE,
            DataType.DOUBLE, DataType.FLOAT, DataType.HALF, DataType.BFLOAT16}) {

        INDArray in = Nd4j.create(DataType.FLOAT, 100);
        INDArray out = Nd4j.scalar(dt, 0);
        INDArray e = Nd4j.scalar(dt, 100);

        DynamicCustomOp op = DynamicCustomOp.builder("size")
                .addInputs(in)
                .addOutputs(out)
                .build();

        try {
            Nd4j.exec(op);

            assertEquals(e, out);
        } catch (Throwable t){
            failed.add(dt);
        }
    }

    if(!failed.isEmpty()){
        fail("Failed datatypes: " + failed.toString());
    }
}
 
Example 10
Source File: PythonNumpyBasicTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testInplaceExecution(){
    if (dataType == DataType.BOOL || dataType == DataType.BFLOAT16)return;
    if (shape.length == 0) return;
    List<PythonVariable> inputs = new ArrayList<>();
    INDArray x = Nd4j.ones(dataType, shape);
    INDArray y = Nd4j.zeros(dataType, shape);
    INDArray z = x.mul(y.add(2));
   // Nd4j.getAffinityManager().ensureLocation(z, AffinityManager.Location.HOST);
    PythonType<INDArray> arrType = PythonTypes.get("numpy.ndarray");
    inputs.add(new PythonVariable<>("x", arrType, x));
    inputs.add(new PythonVariable<>("y", arrType, y));
    List<PythonVariable> outputs = new ArrayList<>();
    PythonVariable<INDArray> output = new PythonVariable<>("x", arrType);
    outputs.add(output);
    String code = "x *= y + 2";
    PythonExecutioner.exec(code, inputs, outputs);
    INDArray z2 = output.getValue();
    Assert.assertEquals(x.dataType(), z2.dataType());
    Assert.assertEquals(z.dataType(), z2.dataType());
    Assert.assertEquals(x, z2);
    Assert.assertEquals(z, z2);
    Assert.assertEquals(x.data().pointer().address(), z2.data().pointer().address());
    if("CUDA".equalsIgnoreCase(Nd4j.getExecutioner().getEnvironmentInformation().getProperty("backend"))){
        Assert.assertEquals(getDeviceAddress(x), getDeviceAddress(z2));
    }


}
 
Example 11
Source File: PythonNumpyJobTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleNumpyJobsParallel(){
    PythonContextManager.deleteNonMainContexts();
    String code1 =(dataType == DataType.BOOL)?"z = x":"z = x + y";
    PythonJob job1 = new PythonJob("job1", code1, false);

    String code2 =(dataType == DataType.BOOL)?"z = y":"z = x - y";
    PythonJob job2 = new PythonJob("job2", code2, false);

    List<PythonVariable> inputs = new ArrayList<>();
    INDArray x = Nd4j.ones(dataType, 2, 3);
    INDArray y = Nd4j.zeros(dataType, 2, 3);
    INDArray z1 = (dataType == DataType.BOOL)?x:x.add(y);
    z1 = (dataType == DataType.BFLOAT16)? z1.castTo(DataType.FLOAT): z1;
    INDArray z2 = (dataType == DataType.BOOL)?y:x.sub(y);
    z2 = (dataType == DataType.BFLOAT16)? z2.castTo(DataType.FLOAT): z2;
    PythonType<INDArray> arrType = PythonTypes.get("numpy.ndarray");
    inputs.add(new PythonVariable<>("x", arrType, x));
    inputs.add(new PythonVariable<>("y", arrType, y));


    List<PythonVariable> outputs = new ArrayList<>();

    outputs.add(new PythonVariable<>("z", arrType));

    job1.exec(inputs, outputs);

    assertEquals(z1, outputs.get(0).getValue());


    job2.exec(inputs, outputs);

    assertEquals(z2, outputs.get(0).getValue());

}
 
Example 12
Source File: PythonNumpyBasicTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testConversion(){
    INDArray arr = Nd4j.zeros(dataType, shape);
    PythonObject npArr = PythonTypes.convert(arr);
    INDArray arr2 = PythonTypes.<INDArray>getPythonTypeForPythonObject(npArr).toJava(npArr);
    if (dataType == DataType.BFLOAT16){
        arr = arr.castTo(DataType.FLOAT);
    }
    Assert.assertEquals(arr,arr2);
}
 
Example 13
Source File: BaseCpuDataBuffer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public void actualizePointerAndIndexer() {
    val cptr = ptrDataBuffer.primaryBuffer();

    // skip update if pointers are equal
    if (cptr != null && pointer != null && cptr.address() == pointer.address())
        return;

    val t = dataType();
    if (t == DataType.BOOL) {
        pointer = new PagedPointer(cptr, length).asBoolPointer();
        setIndexer(BooleanIndexer.create((BooleanPointer) pointer));
    } else if (t == DataType.UBYTE) {
        pointer = new PagedPointer(cptr, length).asBytePointer();
        setIndexer(UByteIndexer.create((BytePointer) pointer));
    } else if (t == DataType.BYTE) {
        pointer = new PagedPointer(cptr, length).asBytePointer();
        setIndexer(ByteIndexer.create((BytePointer) pointer));
    } else if (t == DataType.UINT16) {
        pointer = new PagedPointer(cptr, length).asShortPointer();
        setIndexer(UShortIndexer.create((ShortPointer) pointer));
    } else if (t == DataType.SHORT) {
        pointer = new PagedPointer(cptr, length).asShortPointer();
        setIndexer(ShortIndexer.create((ShortPointer) pointer));
    } else if (t == DataType.UINT32) {
        pointer = new PagedPointer(cptr, length).asIntPointer();
        setIndexer(UIntIndexer.create((IntPointer) pointer));
    } else if (t == DataType.INT) {
        pointer = new PagedPointer(cptr, length).asIntPointer();
        setIndexer(IntIndexer.create((IntPointer) pointer));
    } else if (t == DataType.UINT64) {
        pointer = new PagedPointer(cptr, length).asLongPointer();
        setIndexer(LongIndexer.create((LongPointer) pointer));
    } else if (t == DataType.LONG) {
        pointer = new PagedPointer(cptr, length).asLongPointer();
        setIndexer(LongIndexer.create((LongPointer) pointer));
    } else if (t == DataType.BFLOAT16) {
        pointer = new PagedPointer(cptr, length).asShortPointer();
        setIndexer(Bfloat16Indexer.create((ShortPointer) pointer));
    } else if (t == DataType.HALF) {
        pointer = new PagedPointer(cptr, length).asShortPointer();
        setIndexer(HalfIndexer.create((ShortPointer) pointer));
    } else if (t == DataType.FLOAT) {
        pointer = new PagedPointer(cptr, length).asFloatPointer();
        setIndexer(FloatIndexer.create((FloatPointer) pointer));
    } else if (t == DataType.DOUBLE) {
        pointer = new PagedPointer(cptr, length).asDoublePointer();
        setIndexer(DoubleIndexer.create((DoublePointer) pointer));
    } else if (t == DataType.UTF8) {
        pointer = new PagedPointer(cptr, length()).asBytePointer();
        setIndexer(ByteIndexer.create((BytePointer) pointer));
    } else
        throw new IllegalArgumentException("Unknown datatype: " + dataType());
}
 
Example 14
Source File: BaseCpuDataBuffer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param length
 * @param elementSize
 */
public BaseCpuDataBuffer(long length, int elementSize) {
    if (length < 1)
        throw new IllegalArgumentException("Length must be >= 1");
    initTypeAndSize();
    allocationMode = AllocUtil.getAllocationModeFromContext();
    this.length = length;
    this.underlyingLength = length;
    this.elementSize = (byte) elementSize;

    if (dataType() != DataType.UTF8)
        ptrDataBuffer = OpaqueDataBuffer.allocateDataBuffer(length, dataType(), false);

    if (dataType() == DataType.DOUBLE) {
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asDoublePointer();

        indexer = DoubleIndexer.create((DoublePointer) pointer);
    } else if (dataType() == DataType.FLOAT) {
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asFloatPointer();

        setIndexer(FloatIndexer.create((FloatPointer) pointer));
    } else if (dataType() == DataType.INT32) {
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asIntPointer();

        setIndexer(IntIndexer.create((IntPointer) pointer));
    } else if (dataType() == DataType.LONG) {
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asLongPointer();

        setIndexer(LongIndexer.create((LongPointer) pointer));
    } else if (dataType() == DataType.SHORT) {
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asShortPointer();

        setIndexer(ShortIndexer.create((ShortPointer) pointer));
    } else if (dataType() == DataType.BYTE) {
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asBytePointer();

        setIndexer(ByteIndexer.create((BytePointer) pointer));
    } else if (dataType() == DataType.UBYTE) {
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asBytePointer();

        setIndexer(UByteIndexer.create((BytePointer) pointer));
    } else if (dataType() == DataType.UTF8) {
        ptrDataBuffer = OpaqueDataBuffer.allocateDataBuffer(length, INT8, false);
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asBytePointer();

        setIndexer(ByteIndexer.create((BytePointer) pointer));
    } else if(dataType() == DataType.FLOAT16){
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asShortPointer();
        setIndexer(HalfIndexer.create((ShortPointer) pointer));
    } else if(dataType() == DataType.BFLOAT16){
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asShortPointer();
        setIndexer(Bfloat16Indexer.create((ShortPointer) pointer));
    } else if(dataType() == DataType.BOOL){
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asBoolPointer();
        setIndexer(BooleanIndexer.create((BooleanPointer) pointer));
    } else if(dataType() == DataType.UINT16){
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asShortPointer();
        setIndexer(UShortIndexer.create((ShortPointer) pointer));
    } else if(dataType() == DataType.UINT32){
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asIntPointer();
        setIndexer(UIntIndexer.create((IntPointer) pointer));
    } else if (dataType() == DataType.UINT64) {
        pointer = new PagedPointer(ptrDataBuffer.primaryBuffer(), length).asLongPointer();
        setIndexer(LongIndexer.create((LongPointer) pointer));
    }

    Nd4j.getDeallocatorService().pickObject(this);
}
 
Example 15
Source File: ExecDebuggingListener.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
private static String createString(INDArray arr){
    StringBuilder sb = new StringBuilder();

    if(arr.isEmpty()){
        sb.append("Nd4j.empty(DataType.").append(arr.dataType()).append(");");
    } else {
        sb.append("Nd4j.createFromArray(");

        DataType dt = arr.dataType();
        switch (dt){
            case DOUBLE:
                double[] dArr = arr.dup().data().asDouble();
                sb.append(Arrays.toString(dArr).replaceAll("[\\[\\]]", ""));
                break;
            case FLOAT:
            case HALF:
            case BFLOAT16:
                float[] fArr = arr.dup().data().asFloat();
                sb.append(Arrays.toString(fArr)
                        .replaceAll(",", "f,")
                        .replaceAll("]", "f")
                        .replaceAll("[\\[\\]]", ""));
                break;
            case LONG:
            case UINT32:
            case UINT64:
                long[] lArr = arr.dup().data().asLong();
                sb.append(Arrays.toString(lArr)
                        .replaceAll(",", "L,")
                        .replaceAll("]", "L")
                        .replaceAll("[\\[\\]]", ""));
                break;
            case INT:
            case SHORT:
            case UBYTE:
            case BYTE:
            case UINT16:
            case BOOL:
                int[] iArr = arr.dup().data().asInt();
                sb.append(Arrays.toString(iArr).replaceAll("[\\[\\]]", ""));
                break;
            case UTF8:
                break;
            case COMPRESSED:
            case UNKNOWN:
                break;
        }

        sb.append(").reshape(").append(Arrays.toString(arr.shape()).replaceAll("[\\[\\]]", ""))
                .append(")");

        if(dt == DataType.HALF || dt == DataType.BFLOAT16 || dt == DataType.UINT32 || dt == DataType.UINT64 ||
                dt == DataType.SHORT || dt == DataType.UBYTE || dt == DataType.BYTE || dt == DataType.UINT16 || dt == DataType.BOOL){
            sb.append(".cast(DataType.").append(arr.dataType()).append(")");
        }
    }

    return sb.toString();
}
 
Example 16
Source File: BFloat16Buffer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the opType of this buffer
 */
@Override
protected void initTypeAndSize() {
    type = DataType.BFLOAT16;
    elementSize = 2;
}
 
Example 17
Source File: BaseCudaDataBuffer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public void actualizePointerAndIndexer() {
    val cptr = ptrDataBuffer.primaryBuffer();

    // skip update if pointers are equal
    if (cptr != null && pointer != null && cptr.address() == pointer.address())
        return;

    val t = dataType();
    if (t == DataType.BOOL) {
        pointer = new PagedPointer(cptr, length).asBoolPointer();
        setIndexer(BooleanIndexer.create((BooleanPointer) pointer));
    } else if (t == DataType.UBYTE) {
        pointer = new PagedPointer(cptr, length).asBytePointer();
        setIndexer(UByteIndexer.create((BytePointer) pointer));
    } else if (t == DataType.BYTE) {
        pointer = new PagedPointer(cptr, length).asBytePointer();
        setIndexer(ByteIndexer.create((BytePointer) pointer));
    } else if (t == DataType.UINT16) {
        pointer = new PagedPointer(cptr, length).asShortPointer();
        setIndexer(UShortIndexer.create((ShortPointer) pointer));
    } else if (t == DataType.SHORT) {
        pointer = new PagedPointer(cptr, length).asShortPointer();
        setIndexer(ShortIndexer.create((ShortPointer) pointer));
    } else if (t == DataType.UINT32) {
        pointer = new PagedPointer(cptr, length).asIntPointer();
        setIndexer(UIntIndexer.create((IntPointer) pointer));
    } else if (t == DataType.INT) {
        pointer = new PagedPointer(cptr, length).asIntPointer();
        setIndexer(IntIndexer.create((IntPointer) pointer));
    } else if (t == DataType.UINT64) {
        pointer = new PagedPointer(cptr, length).asLongPointer();
        setIndexer(LongIndexer.create((LongPointer) pointer));
    } else if (t == DataType.LONG) {
        pointer = new PagedPointer(cptr, length).asLongPointer();
        setIndexer(LongIndexer.create((LongPointer) pointer));
    } else if (t == DataType.BFLOAT16) {
        pointer = new PagedPointer(cptr, length).asShortPointer();
        setIndexer(Bfloat16Indexer.create((ShortPointer) pointer));
    } else if (t == DataType.HALF) {
        pointer = new PagedPointer(cptr, length).asShortPointer();
        setIndexer(HalfIndexer.create((ShortPointer) pointer));
    } else if (t == DataType.FLOAT) {
        pointer = new PagedPointer(cptr, length).asFloatPointer();
        setIndexer(FloatIndexer.create((FloatPointer) pointer));
    } else if (t == DataType.DOUBLE) {
        pointer = new PagedPointer(cptr, length).asDoublePointer();
        setIndexer(DoubleIndexer.create((DoublePointer) pointer));
    } else if (t == DataType.UTF8) {
        pointer = new PagedPointer(cptr, length()).asBytePointer();
        setIndexer(ByteIndexer.create((BytePointer) pointer));
    } else
        throw new IllegalArgumentException("Unknown datatype: " + dataType());
}
 
Example 18
Source File: GlobalPoolingMaskingTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMaskLayerDataTypes(){

    for(DataType dt : new DataType[]{DataType.FLOAT16, DataType.BFLOAT16, DataType.FLOAT, DataType.DOUBLE,
            DataType.INT8, DataType.INT16, DataType.INT32, DataType.INT64,
            DataType.UINT8, DataType.UINT16, DataType.UINT32, DataType.UINT64}){
        INDArray mask = Nd4j.rand(DataType.FLOAT, 2, 10).addi(0.3).castTo(dt);

        for(DataType networkDtype : new DataType[]{DataType.FLOAT16, DataType.BFLOAT16, DataType.FLOAT, DataType.DOUBLE}){

            INDArray in = Nd4j.rand(networkDtype, 2, 5, 10);
            INDArray label1 = Nd4j.rand(networkDtype, 2, 5);
            INDArray label2 = Nd4j.rand(networkDtype, 2, 5, 10);

            for(PoolingType pt : PoolingType.values()) {
                //System.out.println("Net: " + networkDtype + ", mask: " + dt + ", pt=" + pt);

                MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                        .list()
                        .layer(new GlobalPoolingLayer(pt))
                        .layer(new OutputLayer.Builder().nIn(5).nOut(5).activation(Activation.TANH).lossFunction(LossFunctions.LossFunction.MSE).build())
                        .build();

                MultiLayerNetwork net = new MultiLayerNetwork(conf);
                net.init();

                net.output(in, false, mask, null);
                net.output(in, false, mask, null);


                MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder()

                        .list()
                        .layer(new RnnOutputLayer.Builder().nIn(5).nOut(5).activation(Activation.TANH).lossFunction(LossFunctions.LossFunction.MSE).build())
                        .build();

                MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
                net2.init();

                net2.output(in, false, mask, mask);
                net2.output(in, false, mask, mask);

                net.fit(in, label1, mask, null);
                net2.fit(in, label2, mask, mask);
            }
        }
    }
}
 
Example 19
Source File: CudaBfloat16DataBuffer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the opType of this buffer
 */
@Override
protected void initTypeAndSize() {
    elementSize = 2;
    type = DataType.BFLOAT16;
}
 
Example 20
Source File: PythonObject.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public PythonObject(NumpyArray npArray) {
    int numpyType;
    INDArray indArray = npArray.getNd4jArray();
    DataType dataType = indArray.dataType();

    switch (dataType) {
        case DOUBLE:
            numpyType = NPY_DOUBLE;
            break;
        case FLOAT:
        case BFLOAT16:
            numpyType = NPY_FLOAT;
            break;
        case SHORT:
            numpyType = NPY_SHORT;
            break;
        case INT:
            numpyType = NPY_INT;
            break;
        case LONG:
            numpyType = NPY_INT64;
            break;
        case UINT16:
            numpyType = NPY_USHORT;
            break;
        case UINT32:
            numpyType = NPY_UINT;
            break;
        case UINT64:
            numpyType = NPY_UINT64;
            break;
        case BOOL:
            numpyType = NPY_BOOL;
            break;
        case BYTE:
            numpyType = NPY_BYTE;
            break;
        case UBYTE:
            numpyType = NPY_UBYTE;
            break;
        case HALF:
            numpyType = NPY_HALF;
            break;
        default:
            throw new RuntimeException("Unsupported dtype: " + npArray.getDtype());
    }

    long[] shape = indArray.shape();
    INDArray inputArray = indArray;
    if(dataType == DataType.BFLOAT16) {
        log.warn("\n\nThe given nd4j array \n\n{}\n\n is of BFLOAT16 datatype. " +
                "Casting a copy of it to FLOAT and creating the respective numpy array from it.\n", indArray);
        inputArray = indArray.castTo(DataType.FLOAT);
    }

    //Sync to host memory in the case of CUDA, before passing the host memory pointer to Python
    if(inputArray.data() instanceof BaseDataBuffer){
        ((BaseDataBuffer)inputArray.data()).syncToPrimary();
    }

    nativePythonObject = PyArray_New(PyArray_Type(), shape.length, new SizeTPointer(shape),
            numpyType, null,
            inputArray.data().addressPointer(),
            0, NPY_ARRAY_CARRAY, null);

}