Search in sources :

Example 6 with WorkspaceConfiguration

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

the class BasicWorkspaceTests method testOverallocation1.

@Test
public void testOverallocation1() throws Exception {
    WorkspaceConfiguration overallocationConfig = WorkspaceConfiguration.builder().initialSize(1024).maxSize(10 * 1024 * 1024).overallocationLimit(1.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policyLearning(LearningPolicy.NONE).policyMirroring(MirroringPolicy.FULL).policySpill(SpillPolicy.EXTERNAL).build();
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().createNewWorkspace(overallocationConfig);
    assertEquals(2048, workspace.getCurrentSize());
}
Also used : 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 7 with WorkspaceConfiguration

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

the class BasicWorkspaceTests method testMmap2.

@Test
public void testMmap2() throws Exception {
    // we don't support MMAP on cuda yet
    if (Nd4j.getExecutioner().getClass().getName().toLowerCase().contains("cuda"))
        return;
    File tmp = File.createTempFile("tmp", "fdsfdf");
    tmp.deleteOnExit();
    Nd4jWorkspace.fillFile(tmp, 100000);
    WorkspaceConfiguration mmap = WorkspaceConfiguration.builder().policyLocation(LocationPolicy.MMAP).tempFilePath(tmp.getAbsolutePath()).build();
    MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(mmap, "M3");
    INDArray mArray = Nd4j.create(100);
    mArray.assign(10f);
    assertEquals(1000f, mArray.sumNumber().floatValue(), 1e-5);
    ws.notifyScopeLeft();
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) File(java.io.File) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 8 with WorkspaceConfiguration

use of org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration 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 9 with WorkspaceConfiguration

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

the class EndlessWorkspaceTests method endlessTestSerDe1.

@Test
public void endlessTestSerDe1() throws Exception {
    INDArray features = Nd4j.create(32, 3, 224, 224);
    INDArray labels = Nd4j.create(32, 200);
    File tmp = File.createTempFile("12dadsad", "dsdasds");
    float[] array = new float[33 * 3 * 224 * 224];
    DataSet ds = new DataSet(features, labels);
    ds.save(tmp);
    WorkspaceConfiguration wsConf = WorkspaceConfiguration.builder().initialSize(0).policyLearning(LearningPolicy.FIRST_LOOP).build();
    while (true) {
        try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(wsConf, "serde")) {
            /*
                            try (FileOutputStream fos = new FileOutputStream(tmp); BufferedOutputStream bos = new BufferedOutputStream(fos)) {
                SerializationUtils.serialize(array, fos);
                            }
                
                            try (FileInputStream fis = new FileInputStream(tmp); BufferedInputStream bis = new BufferedInputStream(fis)) {
                long time1 = System.currentTimeMillis();
                float[] arrayR = (float[]) SerializationUtils.deserialize(bis);
                long time2 = System.currentTimeMillis();
                
                log.info("Load time: {}", time2 - time1);
                            }
                */
            long time1 = System.currentTimeMillis();
            ds.load(tmp);
            long time2 = System.currentTimeMillis();
            log.info("Load time: {}", time2 - time1);
        }
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) File(java.io.File) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 10 with WorkspaceConfiguration

use of org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration 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)

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