use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class SpecialWorkspaceTests method testViewDetach_1.
@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);
}
use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class SpecialWorkspaceTests method testVariableTimeSeries2.
@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());
}
use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class OperationProfilerTests method testScopePanicPerf.
@Test
public void testScopePanicPerf() {
try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS121")) {
INDArray x = Nd4j.create(1000, 1000).assign(1.0);
INDArray y = Nd4j.create(1000, 1000).assign(1.0);
for (int e = 0; e < 10000; e++) {
x.addi(y);
}
Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.SCOPE_PANIC);
val nanosC = System.nanoTime();
for (int e = 0; e < 10000; e++) {
x.addi(y);
}
val nanosD = System.nanoTime();
val avgB = (nanosD - nanosC) / 10000;
Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.DISABLED);
val nanosA = System.nanoTime();
for (int e = 0; e < 10000; e++) {
x.addi(y);
}
val nanosB = System.nanoTime();
val avgA = (nanosB - nanosA) / 10000;
log.info("A: {}; B: {}", avgA, avgB);
}
}
use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class OperationProfilerTests method testScopePanic2.
@Test(expected = ND4JIllegalStateException.class)
public void testScopePanic2() throws Exception {
Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.SCOPE_PANIC);
INDArray array;
try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS120")) {
array = Nd4j.create(10);
assertTrue(array.isAttached());
assertEquals(1, workspace.getGenerationId());
}
try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS120")) {
assertEquals(2, workspace.getGenerationId());
array.add(1.0);
assertTrue(array.isAttached());
}
}
use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testMemcpy1.
@Ignore
@Test
public void testMemcpy1() {
INDArray warmUp = Nd4j.create(100000);
for (int x = 0; x < 5000; x++) {
warmUp.addi(0.1);
}
WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().policyMirroring(MirroringPolicy.HOST_ONLY).initialSize(1024L * 1024L * 1024L).policyLearning(LearningPolicy.NONE).build();
INDArray array = Nd4j.createUninitialized(150000000);
MemoryWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().createNewWorkspace(configuration, "HOST");
workspace.notifyScopeEntered();
INDArray memcpy = array.unsafeDuplication(false);
workspace.notifyScopeLeft();
}
Aggregations