Java Code Examples for org.nd4j.linalg.factory.Nd4j#createTypedBufferDetached()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#createTypedBufferDetached() . 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: ConstantBuffersCache.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public DataBuffer getConstantBuffer(int[] array, DataType dataType) {
    ArrayDescriptor descriptor = new ArrayDescriptor(array, dataType);

    if (!buffersCache.containsKey(descriptor)) {
        DataBuffer buffer = Nd4j.createTypedBufferDetached(array, dataType);

        if (counter.get() < MAX_ENTRIES) {
            counter.incrementAndGet();
            buffersCache.put(descriptor, buffer);

            bytes.addAndGet(array.length * Nd4j.sizeOfDataType(dataType));
            AllocationsTracker.getInstance().markAllocated(AllocationKind.CONSTANT, 0, array.length * Nd4j.sizeOfDataType(dataType));
        }
        return buffer;
    }

    return buffersCache.get(descriptor);
}
 
Example 2
Source File: ConstantBuffersCache.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public DataBuffer getConstantBuffer(boolean[] array, DataType dataType) {
    ArrayDescriptor descriptor = new ArrayDescriptor(array, dataType);

    if (!buffersCache.containsKey(descriptor)) {
        DataBuffer buffer = Nd4j.createTypedBufferDetached(array, dataType);

        if (counter.get() < MAX_ENTRIES) {
            counter.incrementAndGet();
            buffersCache.put(descriptor, buffer);

            bytes.addAndGet(array.length * Nd4j.sizeOfDataType(dataType));
            AllocationsTracker.getInstance().markAllocated(AllocationKind.CONSTANT, 0, array.length * Nd4j.sizeOfDataType(dataType));
        }
        return buffer;
    }

    return buffersCache.get(descriptor);
}
 
Example 3
Source File: ConstantBuffersCache.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public DataBuffer getConstantBuffer(double[] array, DataType dataType) {
    ArrayDescriptor descriptor = new ArrayDescriptor(array, dataType);

    if (!buffersCache.containsKey(descriptor)) {
        DataBuffer buffer = Nd4j.createTypedBufferDetached(array, dataType);

        if (counter.get() < MAX_ENTRIES) {
            counter.incrementAndGet();
            buffersCache.put(descriptor, buffer);

            bytes.addAndGet(array.length * Nd4j.sizeOfDataType(dataType));
            AllocationsTracker.getInstance().markAllocated(AllocationKind.CONSTANT, 0, array.length * Nd4j.sizeOfDataType(dataType));
        }
        return buffer;
    }

    return buffersCache.get(descriptor);
}
 
Example 4
Source File: ConstantBuffersCache.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public DataBuffer getConstantBuffer(float[] array, DataType dataType) {
    ArrayDescriptor descriptor = new ArrayDescriptor(array, dataType);

    if (!buffersCache.containsKey(descriptor)) {
        DataBuffer buffer = Nd4j.createTypedBufferDetached(array, dataType);

        if (counter.get() < MAX_ENTRIES) {
            counter.incrementAndGet();
            buffersCache.put(descriptor, buffer);

            bytes.addAndGet(array.length * Nd4j.sizeOfDataType(dataType));
            AllocationsTracker.getInstance().markAllocated(AllocationKind.CONSTANT, 0, array.length * Nd4j.sizeOfDataType(dataType));
        }
        return buffer;
    }

    return buffersCache.get(descriptor);
}
 
Example 5
Source File: DataBufferTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testCreateTypedBuffer() {

        WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
        MemoryWorkspace workspace = Nd4j.getWorkspaceManager().createNewWorkspace(initialConfig, "WorkspaceId");

        for (String sourceType : new String[]{"int", "long", "float", "double", "short", "byte", "boolean"}) {
            for (DataType dt : DataType.values()) {
                if (dt == DataType.UTF8 || dt == DataType.COMPRESSED || dt == DataType.UNKNOWN) {
                    continue;
                }

//                log.info("Testing source [{}]; target: [{}]", sourceType, dt);

                for (boolean useWs : new boolean[]{false, true}) {

                    try (MemoryWorkspace ws = (useWs ? workspace.notifyScopeEntered() : null)) {

                        DataBuffer db1;
                        DataBuffer db2;
                        switch (sourceType) {
                            case "int":
                                db1 = Nd4j.createTypedBuffer(new int[]{1, 2, 3}, dt);
                                db2 = Nd4j.createTypedBufferDetached(new int[]{1, 2, 3}, dt);
                                break;
                            case "long":
                                db1 = Nd4j.createTypedBuffer(new long[]{1, 2, 3}, dt);
                                db2 = Nd4j.createTypedBufferDetached(new long[]{1, 2, 3}, dt);
                                break;
                            case "float":
                                db1 = Nd4j.createTypedBuffer(new float[]{1, 2, 3}, dt);
                                db2 = Nd4j.createTypedBufferDetached(new float[]{1, 2, 3}, dt);
                                break;
                            case "double":
                                db1 = Nd4j.createTypedBuffer(new double[]{1, 2, 3}, dt);
                                db2 = Nd4j.createTypedBufferDetached(new double[]{1, 2, 3}, dt);
                                break;
                            case "short":
                                db1 = Nd4j.createTypedBuffer(new short[]{1, 2, 3}, dt);
                                db2 = Nd4j.createTypedBufferDetached(new short[]{1, 2, 3}, dt);
                                break;
                            case "byte":
                                db1 = Nd4j.createTypedBuffer(new byte[]{1, 2, 3}, dt);
                                db2 = Nd4j.createTypedBufferDetached(new byte[]{1, 2, 3}, dt);
                                break;
                            case "boolean":
                                db1 = Nd4j.createTypedBuffer(new boolean[]{true, false, true}, dt);
                                db2 = Nd4j.createTypedBufferDetached(new boolean[]{true, false, true}, dt);
                                break;
                            default:
                                throw new RuntimeException();
                        }

                        checkTypes(dt, db1, 3);
                        checkTypes(dt, db2, 3);

                        assertEquals(useWs, db1.isAttached());
                        assertFalse(db2.isAttached());

                        if(!sourceType.equals("boolean")){
                            testDBOps(db1);
                            testDBOps(db2);
                        }
                    }
                }
            }
        }
    }