Search in sources :

Example 26 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testMinSize1.

@Test
public void testMinSize1() throws Exception {
    WorkspaceConfiguration conf = WorkspaceConfiguration.builder().minSize(10 * 1024 * 1024).overallocationLimit(1.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policyLearning(LearningPolicy.FIRST_LOOP).policyMirroring(MirroringPolicy.FULL).policySpill(SpillPolicy.EXTERNAL).build();
    try (Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(conf, "WT")) {
        INDArray array = Nd4j.create(100);
        assertEquals(0, workspace.getCurrentSize());
    }
    try (Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(conf, "WT")) {
        INDArray array = Nd4j.create(100);
        assertEquals(10 * 1024 * 1024, workspace.getCurrentSize());
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 27 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class SpecialWorkspaceTests method testVariableTimeSeries1.

@Test
public void testVariableTimeSeries1() throws Exception {
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(0).overallocationLimit(3.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.EXTERNAL).policyLearning(LearningPolicy.FIRST_LOOP).policyReset(ResetPolicy.ENDOFBUFFER_REACHED).build();
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
        Nd4j.create(500);
        Nd4j.create(500);
    }
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS1");
    workspace.enableDebug(true);
    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());
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS1")) {
        Nd4j.create(2000);
    }
    assertEquals(0, workspace.getStepNumber());
    assertEquals(1000 * Nd4j.sizeOfDataType(), workspace.getSpilledSize());
    assertEquals(2000 * Nd4j.sizeOfDataType(), workspace.getPinnedSize());
    assertEquals(0, workspace.getDeviceOffset());
    // FIXME: fix this!
    // assertEquals(0, workspace.getHostOffset());
    assertEquals(0, workspace.getThisCycleAllocations());
    log.info("------------------");
    assertEquals(1, workspace.getNumberOfPinnedAllocations());
    for (int e = 0; e < 4; e++) {
        for (int i = 0; i < 4; i++) {
            try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
                Nd4j.create(500);
                Nd4j.create(500);
            }
            assertEquals("Failed on iteration " + i, (i + 1) * workspace.getInitialBlockSize(), workspace.getDeviceOffset());
        }
        if (e >= 2) {
            assertEquals("Failed on iteration " + e, 0, workspace.getNumberOfPinnedAllocations());
        } else {
            assertEquals("Failed on iteration " + e, 1, workspace.getNumberOfPinnedAllocations());
        }
    }
    assertEquals(0, workspace.getSpilledSize());
    assertEquals(0, workspace.getPinnedSize());
    assertEquals(0, workspace.getNumberOfPinnedAllocations());
    assertEquals(0, workspace.getNumberOfExternalAllocations());
    log.info("Workspace state after first block: ---------------------------------------------------------");
    Nd4j.getWorkspaceManager().printAllocationStatisticsForCurrentThread();
    log.info("--------------------------------------------------------------------------------------------");
    // we just do huge loop now, with pinned stuff in it
    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(1500 * Nd4j.sizeOfDataType(), workspace.getThisCycleAllocations());
        }
    }
    assertEquals(0, workspace.getSpilledSize());
    assertNotEquals(0, workspace.getPinnedSize());
    assertNotEquals(0, workspace.getNumberOfPinnedAllocations());
    assertEquals(0, workspace.getNumberOfExternalAllocations());
    // and we do another clean loo, without pinned stuff in it, to ensure all pinned allocates are gone
    for (int i = 0; i < 100; i++) {
        try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
            Nd4j.create(500);
            Nd4j.create(500);
        }
    }
    assertEquals(0, workspace.getSpilledSize());
    assertEquals(0, workspace.getPinnedSize());
    assertEquals(0, workspace.getNumberOfPinnedAllocations());
    assertEquals(0, workspace.getNumberOfExternalAllocations());
    log.info("Workspace state after second block: ---------------------------------------------------------");
    Nd4j.getWorkspaceManager().printAllocationStatisticsForCurrentThread();
}
Also used : 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 28 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testAllocation1.

@Test
public void testAllocation1() throws Exception {
    INDArray exp = Nd4j.create(new float[] { 1f, 2f, 3f, 4f, 5f });
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "TestAllocation1");
    Nd4j.getMemoryManager().setCurrentWorkspace(workspace);
    assertNotEquals(null, Nd4j.getMemoryManager().getCurrentWorkspace());
    assertEquals(0, workspace.getHostOffset());
    INDArray array = Nd4j.create(new float[] { 1f, 2f, 3f, 4f, 5f });
    // checking if allocation actually happened
    long reqMem = 5 * Nd4j.sizeOfDataType();
    assertEquals(reqMem + reqMem % 8, workspace.getHostOffset());
    assertEquals(exp, array);
    // checking stuff at native side
    double sum = array.sumNumber().doubleValue();
    assertEquals(15.0, sum, 0.01);
    // checking INDArray validity
    assertEquals(1.0, array.getFloat(0), 0.01);
    assertEquals(2.0, array.getFloat(1), 0.01);
    assertEquals(3.0, array.getFloat(2), 0.01);
    assertEquals(4.0, array.getFloat(3), 0.01);
    assertEquals(5.0, array.getFloat(4), 0.01);
    // checking INDArray validity
    assertEquals(1.0, array.getDouble(0), 0.01);
    assertEquals(2.0, array.getDouble(1), 0.01);
    assertEquals(3.0, array.getDouble(2), 0.01);
    assertEquals(4.0, array.getDouble(3), 0.01);
    assertEquals(5.0, array.getDouble(4), 0.01);
    // checking workspace memory space
    INDArray array2 = Nd4j.create(new float[] { 5f, 4f, 3f, 2f, 1f });
    sum = array2.sumNumber().doubleValue();
    assertEquals(15.0, sum, 0.01);
    // 44 = 20 + 4 + 20, 4 was allocated as Op.extraArgs for sum
    // assertEquals(44, workspace.getHostOffset());
    array.addi(array2);
    sum = array.sumNumber().doubleValue();
    assertEquals(30.0, sum, 0.01);
    // checking INDArray validity
    assertEquals(6.0, array.getFloat(0), 0.01);
    assertEquals(6.0, array.getFloat(1), 0.01);
    assertEquals(6.0, array.getFloat(2), 0.01);
    assertEquals(6.0, array.getFloat(3), 0.01);
    assertEquals(6.0, array.getFloat(4), 0.01);
    workspace.close();
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 29 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace 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 30 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testScope1.

@Test
public void testScope1() throws Exception {
    INDArray array = null;
    try (Nd4jWorkspace wsI = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "ITER")) {
        array = Nd4j.create(100);
        assertTrue(array.isInScope());
    }
    assertFalse(array.isInScope());
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

Test (org.junit.Test)51 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)51 Nd4jWorkspace (org.nd4j.linalg.memory.abstracts.Nd4jWorkspace)51 INDArray (org.nd4j.linalg.api.ndarray.INDArray)47 MemoryWorkspace (org.nd4j.linalg.api.memory.MemoryWorkspace)18 WorkspaceConfiguration (org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration)11 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 Ignore (org.junit.Ignore)1 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)1