Search in sources :

Example 11 with WorkspaceConfiguration

use of org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration in project nd4j by deeplearning4j.

the class FloatDataBufferTest method testReallocationWorkspace.

@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();
}
Also used : WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 12 with WorkspaceConfiguration

use of org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration in project nd4j by deeplearning4j.

the class IntDataBufferTests method testReallocationWorkspace.

@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();
}
Also used : WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 13 with WorkspaceConfiguration

use of org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration in project nd4j by deeplearning4j.

the class CompressionTests method testThresholdCompression0.

@Ignore
@Test
public void testThresholdCompression0() throws Exception {
    INDArray initial = Nd4j.rand(new int[] { 1, 150000000 }, 119L);
    log.info("DTYPE: {}", Nd4j.dataType());
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(2 * 1024L * 1024L * 1024L).overallocationLimit(0).policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).policyReset(ResetPolicy.BLOCK_LEFT).build();
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "IIIA")) {
        INDArray compressed = Nd4j.getExecutioner().thresholdEncode(initial.dup(), 0.999);
    }
    long timeS = 0;
    for (int i = 0; i < 100; i++) {
        try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "IIIA")) {
            INDArray d = initial.dup();
            long time1 = System.nanoTime();
            INDArray compressed = Nd4j.getExecutioner().thresholdEncode(d, 0.999);
            long time2 = System.nanoTime();
            timeS += (time2 - time1) / 1000;
        }
    }
    log.info("Elapsed time: {} us", (timeS) / 100);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Ignore(org.junit.Ignore) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 14 with WorkspaceConfiguration

use of org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testOverallocation2.

@Test
public void testOverallocation2() throws Exception {
    WorkspaceConfiguration overallocationConfig = WorkspaceConfiguration.builder().initialSize(0).maxSize(10 * 1024 * 1024).overallocationLimit(1.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policyLearning(LearningPolicy.FIRST_LOOP).policyMirroring(MirroringPolicy.FULL).policySpill(SpillPolicy.EXTERNAL).build();
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().createNewWorkspace(overallocationConfig);
    // Nd4j.getMemoryManager().setCurrentWorkspace(workspace);
    assertEquals(0, workspace.getCurrentSize());
    try (MemoryWorkspace cW = workspace.notifyScopeEntered()) {
        INDArray array = Nd4j.create(100);
    }
    // should be 800 = 100 elements * 4 bytes per element * 2 as overallocation coefficient
    assertEquals(200 * Nd4j.sizeOfDataType(), workspace.getCurrentSize());
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 15 with WorkspaceConfiguration

use of org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testOverallocation3.

@Test
public void testOverallocation3() throws Exception {
    WorkspaceConfiguration overallocationConfig = WorkspaceConfiguration.builder().initialSize(0).maxSize(10 * 1024 * 1024).overallocationLimit(1.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policyLearning(LearningPolicy.OVER_TIME).policyMirroring(MirroringPolicy.FULL).policySpill(SpillPolicy.EXTERNAL).build();
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().createNewWorkspace(overallocationConfig);
    Nd4j.getMemoryManager().setCurrentWorkspace(workspace);
    assertEquals(0, workspace.getCurrentSize());
    for (int x = 10; x <= 100; x += 10) {
        try (MemoryWorkspace cW = workspace.notifyScopeEntered()) {
            INDArray array = Nd4j.create(x);
        }
    }
    assertEquals(0, workspace.getCurrentSize());
    workspace.initializeWorkspace();
    // should be 800 = 100 elements * 4 bytes per element * 2 as overallocation coefficient
    assertEquals(200 * Nd4j.sizeOfDataType(), workspace.getCurrentSize());
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

Test (org.junit.Test)19 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)19 WorkspaceConfiguration (org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration)19 MemoryWorkspace (org.nd4j.linalg.api.memory.MemoryWorkspace)14 INDArray (org.nd4j.linalg.api.ndarray.INDArray)13 Nd4jWorkspace (org.nd4j.linalg.memory.abstracts.Nd4jWorkspace)11 File (java.io.File)2 Ignore (org.junit.Ignore)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 DataSet (org.nd4j.linalg.dataset.DataSet)1 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)1