org.nd4j.linalg.api.memory.enums.AllocationPolicy Java Examples

The following examples show how to use org.nd4j.linalg.api.memory.enums.AllocationPolicy. 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: IntDataBufferTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new int[] {1, 2, 3, 4});
    val old = buffer.asInt();
    assertTrue(buffer.isAttached());
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    val newContent = buffer.asInt();
    assertEquals(6, newContent.length);
    assertArrayEquals(old, Arrays.copyOf(newContent, old.length));
    workspace.close();
}
 
Example #2
Source File: InterleavedDataSetCallback.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
protected void initializeWorkspaces(long size) {
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(size)
                    .overallocationLimit(bufferSize).policyReset(ResetPolicy.ENDOFBUFFER_REACHED)
                    .policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.EXTERNAL)
                    .policyLearning(LearningPolicy.NONE).build();

    int numDevices = Nd4j.getAffinityManager().getNumberOfDevices();
    int cDevice = Nd4j.getAffinityManager().getDeviceForCurrentThread();
    for (int i = 0; i < numDevices; i++) {
        Nd4j.getAffinityManager().unsafeSetDevice(i);
        workspaces.add(Nd4j.getWorkspaceManager().createNewWorkspace(configuration, "IDSC-" + i, i));
    }

    Nd4j.getAffinityManager().unsafeSetDevice(cDevice);
    numWorkspaces = numDevices;
    isInitialized = true;
}
 
Example #3
Source File: MixedDataTypesTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testWorkspaceBool(){
        val conf = WorkspaceConfiguration.builder().minSize(10 * 1024 * 1024)
                .overallocationLimit(1.0).policyAllocation(AllocationPolicy.OVERALLOCATE)
                .policyLearning(LearningPolicy.FIRST_LOOP).policyMirroring(MirroringPolicy.FULL)
                .policySpill(SpillPolicy.EXTERNAL).build();

        val ws = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(conf, "WS");

        for( int i=0; i<10; i++ ) {
            try (val workspace = (Nd4jWorkspace)ws.notifyScopeEntered() ) {
                val bool = Nd4j.create(DataType.BOOL, 1, 10);
                val dbl = Nd4j.create(DataType.DOUBLE, 1, 10);

                val boolAttached = bool.isAttached();
                val doubleAttached = dbl.isAttached();

//                System.out.println(i + "\tboolAttached=" + boolAttached + ", doubleAttached=" + doubleAttached );
                //System.out.println("bool: " + bool);        //java.lang.IllegalStateException: Indexer must never be null
                //System.out.println("double: " + dbl);
            }
        }
    }
 
Example #4
Source File: CyclicWorkspaceTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testBasicMechanics_1() {
        val fShape = new long[]{128, 784};
        val lShape = new long[] {128, 10};
        val prefetchSize = 24;
        val configuration = WorkspaceConfiguration.builder().minSize(10 * 1024L * 1024L)
                .overallocationLimit(prefetchSize + 1).policyReset(ResetPolicy.ENDOFBUFFER_REACHED)
                .policyLearning(LearningPolicy.FIRST_LOOP).policyAllocation(AllocationPolicy.OVERALLOCATE)
                .policySpill(SpillPolicy.REALLOCATE).build();

        for (int e = 0; e < 100; e++) {
            try (val ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "randomNameHere" + 119)) {
                val fArray = Nd4j.create(fShape).assign(e);
                val lArray = Nd4j.create(lShape).assign(e);

//                log.info("Current offset: {}; Current size: {};", ws.getCurrentOffset(), ws.getCurrentSize());
            }
        }
    }
 
Example #5
Source File: FloatDataBufferTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new float[] {1, 2, 3, 4});
    assertTrue(buffer.isAttached());
    float[] old = buffer.asFloat();
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    float[] newBuf = buffer.asFloat();
    assertArrayEquals(old, newBuf, 1e-4F);
    workspace.close();
}
 
Example #6
Source File: DoubleDataBufferTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new double[] {1, 2, 3, 4});
    double[] old = buffer.asDouble();
    assertTrue(buffer.isAttached());
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    assertArrayEquals(old, Arrays.copyOf(buffer.asDouble(), 4), 1e-1);
    workspace.close();

}
 
Example #7
Source File: AccountingTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testWorkspaceAccounting_1() {
    val deviceId = Nd4j.getAffinityManager().getDeviceForCurrentThread();
    val wsConf = WorkspaceConfiguration.builder()
            .initialSize(10 * 1024 * 1024)
            .policyAllocation(AllocationPolicy.STRICT)
            .policyLearning(LearningPolicy.FIRST_LOOP)
            .build();

    val before = Nd4j.getMemoryManager().allocatedMemory(deviceId);

    val workspace = Nd4j.getWorkspaceManager().createNewWorkspace(wsConf, "random_name_here");

    val middle = Nd4j.getMemoryManager().allocatedMemory(deviceId);

    workspace.destroyWorkspace(true);

    val after = Nd4j.getMemoryManager().allocatedMemory(deviceId);

    log.info("Before: {}; Middle: {}; After: {}", before, middle, after);
    assertTrue(middle > before);
    assertTrue(after < middle);
}
 
Example #8
Source File: CudaWorkspaceTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCircularWorkspaceAsymmetry_3() {
    // circular workspace mode
    val configuration = WorkspaceConfiguration.builder().initialSize(10 * 1024 * 1024)
            .policyReset(ResetPolicy.ENDOFBUFFER_REACHED).policyAllocation(AllocationPolicy.STRICT)
            .policySpill(SpillPolicy.FAIL).policyLearning(LearningPolicy.NONE).build();

    val root = Nd4j.create(DataType.FLOAT, 1000000).assign(119);

    for (int e = 0; e < 100; e++) {
        try (val ws = (CudaWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "circular_ws")) {
            val array = Nd4j.create(DataType.FLOAT, root.shape());
            array.assign(root);

            val second = Nd4j.create(DataType.FLOAT, root.shape());

            array.data().getInt(3);
        }
    }
}
 
Example #9
Source File: CudaWorkspaceTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCircularWorkspaceAsymmetry_2() {
    // circular workspace mode
    val configuration = WorkspaceConfiguration.builder().initialSize(10 * 1024 * 1024)
            .policyReset(ResetPolicy.ENDOFBUFFER_REACHED).policyAllocation(AllocationPolicy.STRICT)
            .policySpill(SpillPolicy.FAIL).policyLearning(LearningPolicy.NONE).build();

    val root = Nd4j.create(DataType.FLOAT, 1000000).assign(119);

    for (int e = 0; e < 100; e++) {
        try (val ws = (CudaWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "circular_ws")) {
            val array = Nd4j.create(DataType.FLOAT, root.shape());
            array.assign(root);

            array.data().getInt(3);

            assertEquals(ws.getHostOffset(), ws.getDeviceOffset());
        }
    }
}
 
Example #10
Source File: FloatDataBufferTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new float[] {1, 2, 3, 4});
    assertTrue(buffer.isAttached());
    float[] old = buffer.asFloat();
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    float[] newBuf = buffer.asFloat();
    assertArrayEquals(old, newBuf, 1e-4F);
    workspace.close();
}
 
Example #11
Source File: DoubleDataBufferTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new double[] {1, 2, 3, 4});
    double[] old = buffer.asDouble();
    assertTrue(buffer.isAttached());
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    assertArrayEquals(old, buffer.asDouble(), 1e-1);
    workspace.close();

}
 
Example #12
Source File: CudaWorkspaceTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircularWorkspaceAsymmetry_1() {
    // circular workspace mode
    val configuration = WorkspaceConfiguration.builder().initialSize(10 * 1024 * 1024)
            .policyReset(ResetPolicy.ENDOFBUFFER_REACHED).policyAllocation(AllocationPolicy.STRICT)
            .policySpill(SpillPolicy.FAIL).policyLearning(LearningPolicy.NONE).build();


    try (val ws = (CudaWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "circular_ws")) {
        val array = Nd4j.create(DataType.FLOAT, 10, 10);

        assertEquals(0, ws.getHostOffset());
        assertNotEquals(0, ws.getDeviceOffset());

        // we expect that this array has no data/buffer on HOST side
        assertEquals(AffinityManager.Location.DEVICE, Nd4j.getAffinityManager().getActiveLocation(array));

        // since this array doesn't have HOST buffer - it will allocate one now
        array.getDouble(3L);

        assertEquals(ws.getHostOffset(), ws.getDeviceOffset());
    }

    try (val ws = (CudaWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "circular_ws")) {
        assertEquals(ws.getHostOffset(), ws.getDeviceOffset());
    }

    Nd4j.getWorkspaceManager().destroyAllWorkspacesForCurrentThread();
}
 
Example #13
Source File: SpecialWorkspaceTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testViewDetach_1() throws Exception {
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(10000000).overallocationLimit(3.0)
            .policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.REALLOCATE)
            .policyLearning(LearningPolicy.FIRST_LOOP).policyReset(ResetPolicy.BLOCK_LEFT).build();

    Nd4jWorkspace workspace =
            (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "WS109");

    INDArray row = Nd4j.linspace(1, 10, 10);
    INDArray exp = Nd4j.create(1, 10).assign(2.0);
    INDArray result = null;
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS109")) {
        INDArray matrix = Nd4j.create(10, 10);
        for (int e = 0; e < matrix.rows(); e++)
            matrix.getRow(e).assign(row);


        INDArray column = matrix.getColumn(1);
        assertTrue(column.isView());
        assertTrue(column.isAttached());
        result = column.detach();
    }

    assertFalse(result.isView());
    assertFalse(result.isAttached());
    assertEquals(exp, result);
}
 
Example #14
Source File: AccountingTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testWorkspaceAccounting_2() {
    val deviceId = Nd4j.getAffinityManager().getDeviceForCurrentThread();
    val wsConf = WorkspaceConfiguration.builder()
            .initialSize(0)
            .policyAllocation(AllocationPolicy.STRICT)
            .policyLearning(LearningPolicy.OVER_TIME)
            .cyclesBeforeInitialization(3)
            .build();

    val before = Nd4j.getMemoryManager().allocatedMemory(deviceId);

    long middle1 = 0;
    try (val workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(wsConf, "random_name_here")) {
        val array = Nd4j.create(DataType.DOUBLE, 5, 5);
        middle1 = Nd4j.getMemoryManager().allocatedMemory(deviceId);
    }

    val middle2 = Nd4j.getMemoryManager().allocatedMemory(deviceId);

    Nd4j.getWorkspaceManager().destroyAllWorkspacesForCurrentThread();

    val after = Nd4j.getMemoryManager().allocatedMemory(deviceId);

    log.info("Before: {}; Middle1: {}; Middle2: {}; After: {}", before, middle1, middle2, after);
    assertTrue(middle1 > before);
    assertTrue(after < middle1);
}
 
Example #15
Source File: IntDataBufferTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new int[] {1, 2, 3, 4});
    int[] old = buffer.asInt();
    assertTrue(buffer.isAttached());
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    assertArrayEquals(old, buffer.asInt());
    workspace.close();
}
 
Example #16
Source File: SpecialTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testYoloStyle(){
        WorkspaceConfiguration WS_ALL_LAYERS_ACT_CONFIG = WorkspaceConfiguration.builder()
                .initialSize(0)
                .overallocationLimit(0.05)
                .policyLearning(LearningPolicy.FIRST_LOOP)
                .policyReset(ResetPolicy.BLOCK_LEFT)
                .policySpill(SpillPolicy.REALLOCATE)
                .policyAllocation(AllocationPolicy.OVERALLOCATE)
                .build();



        for( int i=0; i<10; i++ ){
            try(val ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(WS_ALL_LAYERS_ACT_CONFIG, "ws")){
//                System.out.println("STARTING: " + i);

                INDArray objectPresentMask = Nd4j.create(DataType.BOOL, 1,4,4);

                long[] shape = {1,3,2,4,4};
                INDArray noIntMask1 = Nd4j.createUninitialized(DataType.BOOL, shape, 'c');
                INDArray noIntMask2 = Nd4j.createUninitialized(DataType.BOOL, shape, 'c');

                noIntMask1 = Transforms.or(noIntMask1.get(all(), all(), point(0), all(), all()), noIntMask1.get(all(), all(), point(1), all(), all()) );    //Shape: [mb, b, H, W]. Values 1 if no intersection
                noIntMask2 = Transforms.or(noIntMask2.get(all(), all(), point(0), all(), all()), noIntMask2.get(all(), all(), point(1), all(), all()) );
                INDArray noIntMask = Transforms.or(noIntMask1, noIntMask2 );

                Nd4j.getExecutioner().commit();

                INDArray intMask = Transforms.not(noIntMask); //Values 0 if no intersection
                Nd4j.getExecutioner().commit();

                Broadcast.mul(intMask, objectPresentMask, intMask, 0, 2, 3);
                Nd4j.getExecutioner().commit();
//                System.out.println("DONE: " + i);
            }
        }
    }
 
Example #17
Source File: MultiLayerNetwork.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected static WorkspaceConfiguration getLayerWorkingMemWSConfig(int numWorkingMemCycles){
    return WorkspaceConfiguration.builder()
            .initialSize(0)
            .overallocationLimit(0.02)
            .policyLearning(LearningPolicy.OVER_TIME)
            .cyclesBeforeInitialization(numWorkingMemCycles)
            .policyReset(ResetPolicy.BLOCK_LEFT)
            .policySpill(SpillPolicy.REALLOCATE)
            .policyAllocation(AllocationPolicy.OVERALLOCATE)
            .build();
}
 
Example #18
Source File: MultiLayerNetwork.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected static WorkspaceConfiguration getLayerActivationWSConfig(int numLayers){
    //Activations memory: opened once per layer - for every second layer (preprocessors are within the loop).
    //Technically we could set learning to numLayers / 2, but will set to numLayers for simplicity, and also to
    // account for a backward pass
    return WorkspaceConfiguration.builder()
            .initialSize(0)
            .overallocationLimit(0.02)
            .policyLearning(LearningPolicy.OVER_TIME)
            .cyclesBeforeInitialization(numLayers)
            .policyReset(ResetPolicy.BLOCK_LEFT)
            .policySpill(SpillPolicy.REALLOCATE)
            .policyAllocation(AllocationPolicy.OVERALLOCATE)
            .build();
}
 
Example #19
Source File: MtcnnService.java    From mtcnn-java with Apache License 2.0 5 votes vote down vote up
/**
 * Detect faces and related points.
 * @param image3HW input image with dimensions [C x H x W] (e.g. channels first)
 * @return Two INDArray elements representing the Total Boxes found and the related points.
 * @throws IOException
 */
public INDArray[] rawFaceDetection(INDArray image3HW) throws IOException {

	WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder()
			.initialSize(10 * 1024L * 1024L)
			.policyAllocation(AllocationPolicy.STRICT)
			.policyLearning(LearningPolicy.NONE)
			.build();

	try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) {

		Assert.isTrue(image3HW.rank() == 3, "The input image is expected to have [0, Channels, Width, Height] dimensions");
		Assert.isTrue(image3HW.shape()[0] == 3, "The input image is expected to have channel count at dimension 0");

		// Compute the scale pyramid
		int height = (int) image3HW.size(1);
		int width = (int) image3HW.size(2);

		List<Double> scales = MtcnnUtil.computeScalePyramid(height, width, this.minFaceSize, this.scaleFactor);

		// Stage One
		Object[] stageOneResult = this.preparationStage(image3HW, scales);

		// Reorder image dimensions from [3,H,W] to [H,W,3]
		image3HW = image3HW.permute(1, 2, 0);

		// Stage Two
		INDArray totalBoxes = this.refinementStage(image3HW, (INDArray) stageOneResult[0], (MtcnnUtil.PadResult) stageOneResult[1]);

		// Stage Three
		INDArray[] stageThreeResult = this.outputStage(image3HW, totalBoxes);

		return stageThreeResult;
	}
}
 
Example #20
Source File: DataBufferTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    @Ignore("AB 2019/06/03 - CI issue: \"CUDA stream synchronization failed\" - see issue 7657")
    public void testNoArgCreateBufferFromArray() {

        //Tests here:
        //1. Create from JVM array
        //2. Create from JVM array with offset -> does this even make sense?
        //3. Create detached buffer

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

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

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

                //Float
                DataBuffer f = Nd4j.createBuffer(new float[]{1, 2, 3});
                checkTypes(DataType.FLOAT, f, 3);
                assertEquals(useWs, f.isAttached());
                testDBOps(f);

                f = Nd4j.createBuffer(new float[]{1, 2, 3}, 0);
                checkTypes(DataType.FLOAT, f, 3);
                assertEquals(useWs, f.isAttached());
                testDBOps(f);

                f = Nd4j.createBufferDetached(new float[]{1, 2, 3});
                checkTypes(DataType.FLOAT, f, 3);
                assertFalse(f.isAttached());
                testDBOps(f);

                //Double
                DataBuffer d = Nd4j.createBuffer(new double[]{1, 2, 3});
                checkTypes(DataType.DOUBLE, d, 3);
                assertEquals(useWs, d.isAttached());
                testDBOps(d);

                d = Nd4j.createBuffer(new double[]{1, 2, 3}, 0);
                checkTypes(DataType.DOUBLE, d, 3);
                assertEquals(useWs, d.isAttached());
                testDBOps(d);

                d = Nd4j.createBufferDetached(new double[]{1, 2, 3});
                checkTypes(DataType.DOUBLE, d, 3);
                assertFalse(d.isAttached());
                testDBOps(d);

                //Int
                DataBuffer i = Nd4j.createBuffer(new int[]{1, 2, 3});
                checkTypes(DataType.INT, i, 3);
                assertEquals(useWs, i.isAttached());
                testDBOps(i);

                i = Nd4j.createBuffer(new int[]{1, 2, 3});
                checkTypes(DataType.INT, i, 3);
                assertEquals(useWs, i.isAttached());
                testDBOps(i);

                i = Nd4j.createBufferDetached(new int[]{1, 2, 3});
                checkTypes(DataType.INT, i, 3);
                assertFalse(i.isAttached());
                testDBOps(i);

                //Long
                DataBuffer l = Nd4j.createBuffer(new long[]{1, 2, 3});
                checkTypes(DataType.LONG, l, 3);
                assertEquals(useWs, l.isAttached());
                testDBOps(l);

                l = Nd4j.createBuffer(new long[]{1, 2, 3});
                checkTypes(DataType.LONG, l, 3);
                assertEquals(useWs, l.isAttached());
                testDBOps(l);

                l = Nd4j.createBufferDetached(new long[]{1, 2, 3});
                checkTypes(DataType.LONG, l, 3);
                assertFalse(l.isAttached());
                testDBOps(l);

                //byte
//                DataBuffer b = Nd4j.createBuffer(new byte[]{1, 2, 3});
//                checkTypes(DataType.BYTE, b, 3);
//                testDBOps(b);
//
//                b = Nd4j.createBuffer(new byte[]{1, 2, 3}, 0);
//                checkTypes(DataType.BYTE, b, 3);
//                testDBOps(b);
//
//                b = Nd4j.createBufferDetached(new byte[]{1,2,3});
//                checkTypes(DataType.BYTE, b, 3);
//                testDBOps(b);

                //short
                //TODO
            }
        }
    }
 
Example #21
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);
                        }
                    }
                }
            }
        }
    }
 
Example #22
Source File: SpecialWorkspaceTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testVariableTimeSeries2() throws Exception {
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(0).overallocationLimit(3.0)
                    .policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.REALLOCATE)
                    .policyLearning(LearningPolicy.FIRST_LOOP).policyReset(ResetPolicy.ENDOFBUFFER_REACHED).build();

    Nd4jWorkspace workspace =
                    (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "WS1");
    workspace.enableDebug(true);

    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
        Nd4j.create(500);
        Nd4j.create(500);
    }



    assertEquals(0, workspace.getStepNumber());

    long requiredMemory = 1000 * Nd4j.sizeOfDataType();
    long shiftedSize = ((long) (requiredMemory * 1.3)) + (8 - (((long) (requiredMemory * 1.3)) % 8));
    assertEquals(requiredMemory, workspace.getSpilledSize());
    assertEquals(shiftedSize, workspace.getInitialBlockSize());
    assertEquals(workspace.getInitialBlockSize() * 4, workspace.getCurrentSize());


    for (int i = 0; i < 100; i++) {
        try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
            Nd4j.create(500);
            Nd4j.create(500);
            Nd4j.create(500);
        }
    }


    assertEquals(workspace.getInitialBlockSize() * 4, workspace.getCurrentSize());

    assertEquals(0, workspace.getNumberOfPinnedAllocations());
    assertEquals(0, workspace.getNumberOfExternalAllocations());

    assertEquals(0, workspace.getSpilledSize());
    assertEquals(0, workspace.getPinnedSize());

}
 
Example #23
Source File: SpecialTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testYoloS(){
        //Nd4j.getExecutioner().enableDebugMode(true);
        //Nd4j.getExecutioner().enableVerboseMode(true);
        //Nd4j.setDefaultDataTypes(DataType.DOUBLE, DataType.DOUBLE);

        WorkspaceConfiguration WS_ALL_LAYERS_ACT_CONFIG = WorkspaceConfiguration.builder()
                .initialSize(10 * 1024 * 1024)
                .overallocationLimit(0.05)
                .policyLearning(LearningPolicy.FIRST_LOOP)
                .policyReset(ResetPolicy.BLOCK_LEFT)
                .policySpill(SpillPolicy.REALLOCATE)
                .policyAllocation(AllocationPolicy.OVERALLOCATE)
                .build();


        INDArray labels = Nd4j.create(DataType.DOUBLE, 1,7,5,7);

        for( int i=0; i<10; i++ ){
            try(val ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(WS_ALL_LAYERS_ACT_CONFIG, "ws")){
//                System.out.println("STARTING: " + i);

                val nhw = new long[]{1, 5, 7};

                val size1 = labels.size(1);
                INDArray classLabels = labels.get(all(), interval(4,size1), all(), all());   //Shape: [minibatch, nClasses, H, W]
                INDArray maskObjectPresent = classLabels.sum(Nd4j.createUninitialized(DataType.DOUBLE, nhw, 'c'), 1).castTo(DataType.BOOL); //Shape: [minibatch, H, W]

                INDArray labelTLXY = labels.get(all(), interval(0,2), all(), all());
                INDArray labelBRXY = labels.get(all(), interval(2,4), all(), all());

                Nd4j.getExecutioner().commit();

                INDArray labelCenterXY = labelTLXY.add(labelBRXY);
                val m = labelCenterXY.muli(0.5);  //In terms of grid units
                INDArray labelsCenterXYInGridBox = labelCenterXY.dup(labelCenterXY.ordering());         //[mb, 2, H, W]
                Nd4j.getExecutioner().commit();
//                System.out.println("DONE: " + i);
            }
        }
    }
 
Example #24
Source File: Nd4jEnvironmentThread.java    From jstarcraft-ai with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * 构建缓存
 * 
 * @param size
 */
void constructCache(int size) {
    array = new float[size];
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(size).policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    space = Nd4j.getWorkspaceManager().createNewWorkspace(configuration, "ND4J");
}