/** * This method destroys given workspace * * @param workspace */ @Override public void destroyWorkspace(MemoryWorkspace workspace) { if (workspace == null || workspace instanceof DummyWorkspace) return; //workspace.destroyWorkspace(); backingMap.get().remove(workspace.getId()); }
public GarbageWorkspaceReference(MemoryWorkspace referent, ReferenceQueue<? super MemoryWorkspace> queue) { super(referent, queue); this.pointersPair = ((Nd4jWorkspace) referent).workspace; this.id = referent.getId(); this.threadId = referent.getThreadId(); this.pinnedPointers = ((Nd4jWorkspace) referent).pinnedAllocations; this.externalPointers = ((Nd4jWorkspace) referent).externalAllocations; this.key = id + "_" + threadId; } }
private static List<String> allOpenWorkspaces(){ List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread(); List<String> workspaces = new ArrayList<>(l.size()); for( MemoryWorkspace ws : l){ if(ws.isScopeActive()) { workspaces.add(ws.getId()); } } return workspaces; }
private static List<String> allOpenWorkspaces(){ List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread(); List<String> workspaces = new ArrayList<>(l.size()); for( MemoryWorkspace ws : l){ if(ws.isScopeActive()) { workspaces.add(ws.getId()); } } return workspaces; } }
@Override public void assertCurrentWorkspace(@NonNull T arrayType, String msg) { validateConfig(arrayType); MemoryWorkspace curr = Nd4j.getMemoryManager().getCurrentWorkspace(); if(!scopeOutOfWs.contains(arrayType) && (curr == null || !getWorkspaceName(arrayType).equals(curr.getId()))){ throw new ND4JWorkspaceException("Assertion failed: expected current workspace to be \"" + getWorkspaceName(arrayType) + "\" (for array type " + arrayType + ") - actual current workspace is " + (curr == null ? null : curr.getId()) + (msg == null ? "" : ": " + msg)); }; }
/** * Assert that the specified workspace is open, active, and is the current workspace * * @param ws Name of the workspace to assert open/active/current * @param errorMsg Message to include in the exception, if required */ public static void assertOpenActiveAndCurrent(@NonNull String ws, @NonNull String errorMsg) throws ND4JWorkspaceException { if (!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(ws)) { throw new ND4JWorkspaceException(errorMsg + " - workspace is not open and active"); } MemoryWorkspace currWs = Nd4j.getMemoryManager().getCurrentWorkspace(); if (currWs == null || !ws.equals(currWs.getId())) { throw new ND4JWorkspaceException(errorMsg + " - not the current workspace (current workspace: " + (currWs == null ? null : currWs.getId())); } }
/** * Assert that no workspaces are currently open * * @param msg Message to include in the exception, if required */ public static void assertNoWorkspacesOpen(String msg) throws ND4JWorkspaceException { if (Nd4j.getWorkspaceManager().anyWorkspaceActiveForCurrentThread()) { List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread(); List<String> workspaces = new ArrayList<>(l.size()); for (MemoryWorkspace ws : l) { if(ws.isScopeActive()) { workspaces.add(ws.getId()); } } throw new ND4JWorkspaceException(msg + " - Open/active workspaces: " + workspaces); } }
protected void checkWorkspace(String opName, INDArray array) { if (array.isAttached()) { val ws = array.data().getParentWorkspace(); if (ws.getWorkspaceType() != MemoryWorkspace.Type.CIRCULAR) { if (!ws.isScopeActive()) { throw new ND4JIllegalStateException("Op [" + opName + "] X argument uses leaked workspace pointer from workspace [" + ws.getId() + "]\nAll open workspaces: " + allOpenWorkspaces() + "\n" + SCOPE_PANIC_MSG); } if (ws.getGenerationId() != array.data().getGenerationId()) throw new ND4JIllegalStateException("Op [" + opName + "] X argument uses outdated workspace pointer from workspace [" + ws.getId() + "]\nAll open workspaces: " + allOpenWorkspaces() + "\n" + SCOPE_PANIC_MSG); } } }
} else { throw new ND4JWorkspaceException("Array workspace validation failed: Array of type " + arrayType + " should be detached (no workspace) but is in workspace: " + array.data().getParentWorkspace().getId()); String wsNameAct = array.data().getParentWorkspace().getId(); if(!wsNameExpected.equals(wsNameAct)){ if(migrateIfInvalid){
/** * Assert that the specified array is valid, in terms of workspaces: i.e., if it is attached (and not in a circular * workspace), assert that the workspace is open, and that the data is not from an old generation. * @param array Array to check * @param msg Message (prefix) to include in the exception, if required. May be null */ public static void assertValidArray(INDArray array, String msg){ if(array == null || !array.isAttached()){ return; } val ws = array.data().getParentWorkspace(); if (ws.getWorkspaceType() != MemoryWorkspace.Type.CIRCULAR) { if (!ws.isScopeActive()) { throw new ND4JWorkspaceException( (msg == null ? "" : msg + ": ") + "Array uses leaked workspace pointer " + "from workspace " + ws.getId() + "\nAll open workspaces: " + allOpenWorkspaces()); } if (ws.getGenerationId() != array.data().getGenerationId()) { throw new ND4JWorkspaceException( (msg == null ? "" : msg + ": ") + "Array outdated workspace pointer " + "from workspace " + ws.getId() + " (array generation " + array.data().getGenerationId() + ", current workspace generation " + ws.getGenerationId() + ")\nAll open workspaces: " + allOpenWorkspaces()); } } }
/** * This method destroys given workspace * * @param workspace */ @Override public void destroyWorkspace(MemoryWorkspace workspace) { if (workspace == null || workspace instanceof DummyWorkspace) return; //workspace.destroyWorkspace(); backingMap.get().remove(workspace.getId()); }
public GarbageWorkspaceReference(MemoryWorkspace referent, ReferenceQueue<? super MemoryWorkspace> queue) { super(referent, queue); this.pointersPair = ((Nd4jWorkspace) referent).workspace; this.id = referent.getId(); this.threadId = referent.getThreadId(); this.pinnedPointers = ((Nd4jWorkspace) referent).pinnedAllocations; this.externalPointers = ((Nd4jWorkspace) referent).externalAllocations; this.key = id + "_" + threadId; } }
@Override public MemoryWorkspace createNewWorkspace(@NonNull WorkspaceConfiguration configuration) { ensureThreadExistense(); MemoryWorkspace workspace = new CudaWorkspace(configuration); backingMap.get().put(workspace.getId(), workspace); pickReference(workspace); return workspace; }
@Override public MemoryWorkspace createNewWorkspace() { ensureThreadExistense(); MemoryWorkspace workspace = new CudaWorkspace(defaultConfiguration); backingMap.get().put(workspace.getId(), workspace); pickReference(workspace); return workspace; }
@Override public MemoryWorkspace createNewWorkspace(@NonNull WorkspaceConfiguration configuration) { ensureThreadExistense(); MemoryWorkspace workspace = newWorkspace(configuration); backingMap.get().put(workspace.getId(), workspace); if (Nd4j.getWorkspaceManager().getDebugMode() != DebugMode.BYPASS_EVERYTHING) pickReference(workspace); return workspace; }
@Override public MemoryWorkspace createNewWorkspace() { ensureThreadExistense(); MemoryWorkspace workspace = newWorkspace(defaultConfiguration); backingMap.get().put(workspace.getId(), workspace); if (Nd4j.getWorkspaceManager().getDebugMode() != DebugMode.BYPASS_EVERYTHING) pickReference(workspace); return workspace; }
.getCurrentWorkspace().getId().equals(ComputationGraph.workspaceExternal) ? Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread( ComputationGraph.workspaceConfigurationLSTM,