Java Code Examples for org.nd4j.linalg.api.memory.MemoryWorkspace#getId()
The following examples show how to use
org.nd4j.linalg.api.memory.MemoryWorkspace#getId() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Nd4jWorkspace.java From nd4j with Apache License 2.0 | 5 votes |
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; }
Example 2
Source File: WorkspaceUtils.java From nd4j with Apache License 2.0 | 5 votes |
/** * 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())); } }
Example 3
Source File: BaseWorkspaceMgr.java From nd4j with Apache License 2.0 | 5 votes |
@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)); }; }
Example 4
Source File: WorkspaceUtils.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * 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())); } }
Example 5
Source File: BaseWorkspaceMgr.java From deeplearning4j with Apache License 2.0 | 5 votes |
@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)); }; }
Example 6
Source File: InferenceSession.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override protected Map<String, INDArray> preprocessPlaceholders(Map<String, INDArray> placeholders, At at) { arrayUseTracker.clear(); //We'll also use this method as a "pre execution" hook-in, to mark variables as something we should never deallocate //This occurs by never marking these "ConstantDep" and "VariableDep" instances as satisfied, so there's always // an unsatisfied dependency for them in the array use tracker //TODO we shouldn't be clearing this on every single iteration, in 99.5% of cases variables will be same as last iteration... for (SDVariable v : sameDiff.variables()) { if (v.getVariableType() == VariableType.CONSTANT) { arrayUseTracker.addDependency(v.getArr(), new ConstantDep(v.name())); } else if (v.getVariableType() == VariableType.VARIABLE) { arrayUseTracker.addDependency(v.getArr(), new VariableDep(v.name())); } } //Workaround for some TF/Keras based models that require explicit train/test as a placeholder boolean kerasWorkaround = false; List<String> phs = sameDiff.inputs(); if (phs != null && !phs.isEmpty()) { for (String s : phs) { if (s.endsWith(KERAS_TRAIN_TEST) && !placeholders.containsKey(s)) { // The behaviour of some Keras layers (like GRU) differs depending on whether the model is training. // We provide this value directly, unless the user has provided this manually INDArray scalar = mmgr.allocate(false, DataType.BOOL).assign(at.operation().isTrainingPhase()); placeholders = new HashMap<>(placeholders); //Array might be singleton, or otherwise unmodifiable placeholders.put(s, scalar); kerasWorkaround = true; } } } if (placeholders == null || placeholders.isEmpty()) { return placeholders; } //Handle casting of the input array automatically. //The idea here is to avoid unexpected errors if the user (for example) tries to perform inference with a double // array for a float placeholder //TODO eventually we might have ops that support multiple input types, and hence won't need this casting Map<String, INDArray> out = new HashMap<>(); for (Map.Entry<String, INDArray> e : placeholders.entrySet()) { Preconditions.checkState(sameDiff.hasVariable(e.getKey()), "Invalid placeholder passed for execution: " + "No variable/placeholder with name %s exists", e.getKey()); INDArray arr = e.getValue(); //First: check workspaces if (arr.isAttached()) { MemoryWorkspace ws = arr.data() == null ? null : arr.data().getParentWorkspace(); if (ws != null && ws.getWorkspaceType() != MemoryWorkspace.Type.CIRCULAR) { if (!ws.isScopeActive()) { throw new ND4JIllegalStateException("Placeholder \"" + e.getKey() + "\" array uses leaked workspace pointer from workspace [" + ws.getId() + "]: Workspace the array was defined in is no longer open.\nAll open workspaces: " + DefaultOpExecutioner.allOpenWorkspaces() + "\n" + SCOPE_PANIC_MSG); } if (ws.getGenerationId() != arr.data().getGenerationId()) throw new ND4JIllegalStateException("Placeholder \"" + e.getKey() + "\" array uses outdated workspace pointer from workspace [" + ws.getId() + "]: Workspace array was defined in has been closed and reopened at least once since array creation. Array WS iteration: " + arr.data().getGenerationId() + ". Workspace current iteration: " + ws.getGenerationId() + "\nAll open workspaces: " + DefaultOpExecutioner.allOpenWorkspaces() + "\n" + SCOPE_PANIC_MSG); } } //Second: cast the input to the required type //TODO For the casting case, we SHOULD actually deallocate this when we're done with it, which is usually sooner than "exec done" DataType dt = sameDiff.getVariable(e.getKey()).dataType(); if (kerasWorkaround && e.getKey().endsWith(KERAS_TRAIN_TEST)) { arrayUseTracker.addDependency(arr, new ExecDoneDep()); } else if (arr.dataType() == dt) { //Mark as a placeholder array in the array use tracker, so we never deallocate this array... arrayUseTracker.addDependency(e.getValue(), new PlaceholderDep(e.getKey())); } else { INDArray cast = mmgr.allocate(false, dt, arr.shape()); cast.assign(arr); arr = cast; //This array CAN be deallocated once consumed, because of the cast //TODO we can likely close this sooner arrayUseTracker.addDependency(arr, new ExecDoneDep()); } out.put(e.getKey(), arr); } return out; }