Search in sources :

Example 41 with MemoryWorkspace

use of org.nd4j.linalg.api.memory.MemoryWorkspace 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 42 with MemoryWorkspace

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

the class Nd4jWorkspace method notifyScopeEntered.

@Override
public MemoryWorkspace notifyScopeEntered() {
    // we should block stuff since we're going to invalidate spilled allocations
    // TODO: block on spilled allocations probably?
    MemoryWorkspace prev = Nd4j.getMemoryManager().getCurrentWorkspace();
    // if we're opening the same workspace - just increase counter, and skip everything else
    if (prev == this && isOpen.get()) {
        tagScope.incrementAndGet();
        return this;
    }
    // we'll need this in close() call, to restore previous workspace (if any)
    previousWorkspace = prev;
    Nd4j.getMemoryManager().setCurrentWorkspace(this);
    isOpen.set(true);
    // resetting workspace to 0 offset (if anything), not applicable to circular mode, sure
    if (workspaceConfiguration.getPolicyReset() == ResetPolicy.BLOCK_LEFT) {
        reset();
    }
    // if we have any spilled allocations left from last cycle - purge them.
    if (externalCount.get() > 0 && (workspaceConfiguration.getPolicyReset() == ResetPolicy.BLOCK_LEFT || resetPlanned.get())) {
        clearExternalAllocations();
        resetPlanned.set(false);
    }
    cycleAllocations.set(0);
    disabledCounter.set(0);
    generationId.incrementAndGet();
    return this;
}
Also used : MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace)

Example 43 with MemoryWorkspace

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

the class OperationProfilerTests method testScopePanic1.

@Test(expected = ND4JIllegalStateException.class)
public void testScopePanic1() throws Exception {
    Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.SCOPE_PANIC);
    INDArray array;
    try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS119")) {
        array = Nd4j.create(10);
        assertTrue(array.isAttached());
    }
    array.add(1.0);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Test(org.junit.Test)

Example 44 with MemoryWorkspace

use of org.nd4j.linalg.api.memory.MemoryWorkspace 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 45 with MemoryWorkspace

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

MemoryWorkspace (org.nd4j.linalg.api.memory.MemoryWorkspace)62 Test (org.junit.Test)39 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)35 INDArray (org.nd4j.linalg.api.ndarray.INDArray)35 Nd4jWorkspace (org.nd4j.linalg.memory.abstracts.Nd4jWorkspace)18 WorkspaceConfiguration (org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration)14 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 AllocationPoint (org.nd4j.jita.allocator.impl.AllocationPoint)4 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)3 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Ignore (org.junit.Ignore)2 IOException (java.io.IOException)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1