Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#isAttached()
The following examples show how to use
org.nd4j.linalg.api.ndarray.INDArray#isAttached() .
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: WorkspaceUtils.java From nd4j with Apache License 2.0 | 6 votes |
/** * 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()); } } }
Example 2
Source File: BaseWorkspaceMgr.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public INDArray castTo(@NonNull T arrayType, @NonNull DataType dataType, @NonNull INDArray toCast, boolean dupIfCorrectType){ if(toCast.dataType() == dataType){ if(!dupIfCorrectType){ //Check if we can avoid duping... if not in workspace, or already in correct workspace if(!toCast.isAttached() || toCast.data().getParentWorkspace().getId().equals(workspaceNames.get(arrayType))){ return toCast; } } return dup(arrayType, toCast); } else { try(MemoryWorkspace ws = notifyScopeBorrowed(arrayType)){ return toCast.castTo(dataType); } } }
Example 3
Source File: BaseWorkspaceMgr.java From deeplearning4j with Apache License 2.0 | 6 votes |
@Override public INDArray leverageTo(@NonNull T arrayType, @NonNull INDArray array) { if(array == null || !array.isAttached()){ return array; } validateConfig(arrayType); enforceExistsAndActive(arrayType); if(!DISABLE_LEVERAGE){ if(scopeOutOfWs.contains(arrayType)){ return array.detach(); } return array.leverageTo(getWorkspaceName(arrayType), true); } else { if(array.isAttached()){ if(!array.data().getParentWorkspace().getId().equals(getWorkspaceName(arrayType))){ throw new IllegalStateException("Array of type " + arrayType + " is leveraged from " + array.data().getParentWorkspace().getId() + " to " + getWorkspaceName(arrayType) + " but WorkspaceMgn.leverageTo() is currently disabled"); } } return array; } }
Example 4
Source File: WorkspaceUtils.java From deeplearning4j with Apache License 2.0 | 6 votes |
/** * 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()); } } }
Example 5
Source File: DefaultOpExecutioner.java From deeplearning4j with Apache License 2.0 | 6 votes |
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() + "]: Workspace the array was defined in is no longer open.\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() + "]: Workspace array was defined in has been closed and reopened at least once since array creation. Array WS iteration: " + array.data().getGenerationId() + ". Workspace current iteration: " + ws.getGenerationId() + "\nAll open workspaces: " + allOpenWorkspaces() + "\n" + SCOPE_PANIC_MSG); } } }
Example 6
Source File: BaseWorkspaceMgr.java From nd4j with Apache License 2.0 | 6 votes |
@Override public INDArray leverageTo(@NonNull T arrayType, @NonNull INDArray array) { if(array == null || !array.isAttached()){ return array; } validateConfig(arrayType); enforceExistsAndActive(arrayType); if(!DISABLE_LEVERAGE){ if(scopeOutOfWs.contains(arrayType)){ return array.detach(); } return array.leverageTo(getWorkspaceName(arrayType), true); } else { if(array.isAttached()){ if(!array.data().getParentWorkspace().getId().equals(getWorkspaceName(arrayType))){ throw new IllegalStateException("Array of type " + arrayType + " is leveraged from " + array.data().getParentWorkspace().getId() + " to " + getWorkspaceName(arrayType) + " but WorkspaceMgn.leverageTo() is currently disabled"); } } return array; } }
Example 7
Source File: DefaultOpExecutioner.java From nd4j with Apache License 2.0 | 6 votes |
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); } } }
Example 8
Source File: LayerWorkspaceMgr.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public INDArray leverageTo(ArrayType arrayType, INDArray array){ if(noLeverageOverride != null && array.isAttached() && noLeverageOverride.contains(array.data().getParentWorkspace().getId())){ return array; } return super.leverageTo(arrayType, array); }
Example 9
Source File: LayerWorkspaceMgr.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public INDArray validateArrayLocation(@NonNull ArrayType arrayType, @NonNull INDArray array, boolean migrateIfInvalid, boolean exceptionIfDetached) { if(noLeverageOverride != null && array.isAttached() && noLeverageOverride.contains(array.data().getParentWorkspace().getId())){ return array; //OK - leverage override } return super.validateArrayLocation(arrayType, array, migrateIfInvalid, exceptionIfDetached); }
Example 10
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; }
Example 11
Source File: BaseWorkspaceMgr.java From nd4j with Apache License 2.0 | 4 votes |
@Override public INDArray validateArrayLocation(@NonNull T arrayType, @NonNull INDArray array, boolean migrateIfInvalid, boolean exceptionIfDetached) { validateConfig(arrayType); if(scopeOutOfWs.contains(arrayType)){ //Array is supposed to be detached (no workspace) boolean ok = !array.isAttached(); if(!ok){ if(migrateIfInvalid){ return leverageTo(arrayType, array); } 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()); } } else { //Detached array, as expected return array; } } //At this point: we expect the array to be in a workspace String wsNameExpected = getWorkspaceName(arrayType); if(!array.isAttached()){ if(exceptionIfDetached) { throw new ND4JWorkspaceException("Array workspace validation failed: Array of type " + arrayType + " should be in workspace \"" + wsNameExpected + "\" but is detached"); } else { return array; } } String wsNameAct = array.data().getParentWorkspace().getId(); if(!wsNameExpected.equals(wsNameAct)){ if(migrateIfInvalid){ return leverageTo(arrayType, array); } else { throw new ND4JWorkspaceException("Array workspace validation failed: Array of type " + arrayType + " should be in workspace \"" + wsNameExpected + "\" but is in workspace \"" + wsNameAct + "\""); } } //OK - return as-is return array; }
Example 12
Source File: BaseWorkspaceMgr.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override public INDArray validateArrayLocation(@NonNull T arrayType, @NonNull INDArray array, boolean migrateIfInvalid, boolean exceptionIfDetached) { validateConfig(arrayType); if(scopeOutOfWs.contains(arrayType)){ //Array is supposed to be detached (no workspace) boolean ok = !array.isAttached(); if(!ok){ if(migrateIfInvalid){ return leverageTo(arrayType, array); } 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()); } } else { //Detached array, as expected return array; } } //At this point: we expect the array to be in a workspace String wsNameExpected = getWorkspaceName(arrayType); if(!array.isAttached()){ if(exceptionIfDetached) { throw new ND4JWorkspaceException("Array workspace validation failed: Array of type " + arrayType + " should be in workspace \"" + wsNameExpected + "\" but is detached"); } else { return array; } } String wsNameAct = array.data().getParentWorkspace().getId(); if(!wsNameExpected.equals(wsNameAct)){ if(migrateIfInvalid){ return leverageTo(arrayType, array); } else { throw new ND4JWorkspaceException("Array workspace validation failed: Array of type " + arrayType + " should be in workspace \"" + wsNameExpected + "\" but is in workspace \"" + wsNameAct + "\""); } } //OK - return as-is return array; }
Example 13
Source File: SameDiffLayer.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override public INDArray activate(boolean training, LayerWorkspaceMgr workspaceMgr) { assertInputSet(false); try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) { if (sameDiff == null) { doInit(); } } org.deeplearning4j.nn.conf.layers.samediff.SameDiffLayer bl = (org.deeplearning4j.nn.conf.layers.samediff.SameDiffLayer) layerConf(); bl.validateInput(input); Map<String,INDArray> phMap = new HashMap<>(); phMap.put(INPUT_KEY, input); if(maskArray != null){ phMap.put(MASK_KEY, maskArray); } else { phMap.put(MASK_KEY, layerConf().onesMaskForInput(input)); } //Configure memory management for SameDiff instance - use DL4J workspaces String wsNameWorking = workspaceMgr.getWorkspaceName(ArrayType.FF_WORKING_MEM); String wsNameOutput = workspaceMgr.getWorkspaceName(ArrayType.ACTIVATIONS); WorkspaceConfiguration confWorking = workspaceMgr.getConfiguration(ArrayType.FF_WORKING_MEM); WorkspaceConfiguration confOutput = workspaceMgr.getConfiguration(ArrayType.ACTIVATIONS); boolean actScopedOut = workspaceMgr.isScopedOut(ArrayType.ACTIVATIONS); Preconditions.checkState(actScopedOut || wsNameOutput != null, "Activations must have a workspace or must be scoped out"); SessionMemMgr mmgr = new DL4JSameDiffMemoryMgr(wsNameWorking, wsNameOutput, confWorking, confOutput); InferenceSession is = sameDiff.getSessions().get(Thread.currentThread().getId()); if(is == null){ is = new InferenceSession(sameDiff); sameDiff.getSessions().put(Thread.currentThread().getId(), is); } is.setMmgr(mmgr); Map<String,INDArray> out = sameDiff.output(phMap, outputKey); INDArray result = out.get(outputKey); //Edge case - identity activation //TODO there may be a cleaner way to do this... if(!actScopedOut && !result.data().getParentWorkspace().getId().equals(wsNameOutput)){ result = workspaceMgr.dup(ArrayType.ACTIVATIONS, result); } else if(actScopedOut && result.isAttached()){ result = result.detach(); } //Clear placeholders and op inputs to ensure no out-of-scope arrays are still referenced anywhere sameDiff.clearPlaceholders(true); sameDiff.clearOpInputs(); return result; }
Example 14
Source File: SameDiffOutputLayer.java From deeplearning4j with Apache License 2.0 | 4 votes |
private INDArray activateHelper(boolean activations, LayerWorkspaceMgr workspaceMgr){ assertInputSet(false); //Check where the output occurs. If it's a simple loss layer (no params) this could // just be the input! if(activations && INPUT_KEY.equals(layerConf().activationsVertexName())){ return workspaceMgr.leverageTo(ArrayType.ACTIVATIONS, input); } //TODO optimize try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) { if (sameDiff == null) { doInit(); } } //Configure memory management for SameDiff instance - use DL4J workspaces String wsNameWorking = workspaceMgr.getWorkspaceName(ArrayType.FF_WORKING_MEM); String wsNameOutput = workspaceMgr.getWorkspaceName(ArrayType.ACTIVATIONS); WorkspaceConfiguration confWorking = workspaceMgr.getConfiguration(ArrayType.FF_WORKING_MEM); WorkspaceConfiguration confOutput = workspaceMgr.getConfiguration(ArrayType.ACTIVATIONS); boolean actScopedOut = workspaceMgr.isScopedOut(ArrayType.ACTIVATIONS); Preconditions.checkState(actScopedOut || wsNameOutput != null, "Activations must have a workspace or must be scoped out"); SessionMemMgr mmgr = new DL4JSameDiffMemoryMgr(wsNameWorking, wsNameOutput, confWorking, confOutput); InferenceSession is = sameDiff.getSessions().get(Thread.currentThread().getId()); if(is == null){ is = new InferenceSession(sameDiff); sameDiff.getSessions().put(Thread.currentThread().getId(), is); } is.setMmgr(mmgr); Map<String,INDArray> phMap = new HashMap<>(); phMap.put(INPUT_KEY, input); if(!activations && layerConf().labelsRequired() && labels != null) { phMap.put(LABELS_KEY, labels); } String s = activations ? layerConf().activationsVertexName() : outputVar.name(); INDArray out = sameDiff.outputSingle(phMap, s); //Clear placeholders and op inputs to ensure no out-of-scope arrays are still referenced anywhere sameDiff.clearPlaceholders(true); sameDiff.clearOpInputs(); //Edge case: vertex is just an Identity function, for example //TODO there may be a cleaner way to do this... if(!actScopedOut && !out.data().getParentWorkspace().getId().equals(wsNameOutput)){ out = workspaceMgr.dup(ArrayType.ACTIVATIONS, out); } else if(actScopedOut && out.isAttached()){ out = out.detach(); } return out; }
Example 15
Source File: SameDiffOutputLayer.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override public Pair<Gradient, INDArray> backpropGradient(INDArray epsilon, LayerWorkspaceMgr workspaceMgr) { assertInputSet(true); Preconditions.checkState(!layerConf().labelsRequired() || labels != null, "Cannot execute backprop: Labels are not set. " + "If labels are not required for this SameDiff output layer, override SameDiffOutputLayer.labelsRequired()" + " to return false instead"); Gradient g = new DefaultGradient(); INDArray dLdIn; try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) { if (sameDiff == null) { //Usually doInit will be called in forward pass; not necessarily the case in output layers // (for efficiency, we skip output layer forward pass in MultiLayerNetwork/ComputationGraph) doInit(); } if(sameDiff.getFunction("grad") == null) sameDiff.createGradFunction(INPUT_KEY); } //Configure memory management for SameDiff instance - use DL4J workspaces Map<Long,InferenceSession> sessionMap = sameDiff.getFunction("grad").getSessions(); if(!sessionMap.containsKey(Thread.currentThread().getId())){ sessionMap.put(Thread.currentThread().getId(), new InferenceSession(sameDiff.getFunction("grad"))); } String wsNameWorking = workspaceMgr.getWorkspaceName(ArrayType.BP_WORKING_MEM); String wsNameActGrad = workspaceMgr.getWorkspaceName(ArrayType.ACTIVATION_GRAD); WorkspaceConfiguration confWorking = workspaceMgr.getConfiguration(ArrayType.BP_WORKING_MEM); WorkspaceConfiguration confOutput = workspaceMgr.getConfiguration(ArrayType.ACTIVATION_GRAD); boolean actGradScopedOut = workspaceMgr.isScopedOut(ArrayType.ACTIVATION_GRAD); Preconditions.checkState(actGradScopedOut || wsNameActGrad != null, "Activation gradients must have a workspace or be scoped out"); SessionMemMgr mmgr = new DL4JSameDiffMemoryMgr(wsNameWorking, wsNameActGrad, confWorking, confOutput); sessionMap.get(Thread.currentThread().getId()).setMmgr(mmgr); if(!sameDiff.hasGradientFunction()) { //Create when scoped out, to ensure any arrays are not in WS sameDiff.createGradFunction(INPUT_KEY); } List<String> gradVarNames = new ArrayList<>(); gradVarNames.addAll(paramTable.keySet()); gradVarNames.add(INPUT_KEY); Map<String,INDArray> phMap = new HashMap<>(); phMap.put(INPUT_KEY, input); phMap.put(LABELS_KEY, labels); Map<String,INDArray> grads = sameDiff.calculateGradients(phMap, gradVarNames); for(String s : paramTable.keySet() ){ INDArray sdGrad = grads.get(s); INDArray dl4jGrad = gradTable.get(s); dl4jGrad.assign(sdGrad); //TODO OPTIMIZE THIS g.gradientForVariable().put(s, dl4jGrad); if(sdGrad.closeable()){ sdGrad.close(); } } dLdIn = grads.get(INPUT_KEY); //Clear placeholders and op inputs to ensure no out-of-scope arrays are still referenced anywhere sameDiff.clearPlaceholders(true); sameDiff.clearOpInputs(); //TODO there may be a cleaner way to do this... if(!actGradScopedOut && !dLdIn.data().getParentWorkspace().getId().equals(wsNameActGrad)){ dLdIn = workspaceMgr.dup(ArrayType.ACTIVATION_GRAD, dLdIn); } else if(actGradScopedOut && dLdIn.isAttached()){ dLdIn = dLdIn.detach(); } return new Pair<>(g, dLdIn); }
Example 16
Source File: SameDiffGraphVertex.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override public INDArray doForward(boolean training, LayerWorkspaceMgr workspaceMgr) { try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) { if (sameDiff == null) { doInit(); } } Map<String,INDArray> phMap = new HashMap<>(); config.validateInput(inputs); for(int i=0; i<inputs.length; i++ ){ String name = config.getVertexParams().getInputs().get(i); final String maskName = name + "_mask"; phMap.put(name, inputs[i]); if(maskArrays != null && maskArrays[i] != null) { phMap.put(maskName, maskArrays[i]); }else{ phMap.put(maskName, createMask(dataType, inputs[i].shape())); } } //Configure memory management for SameDiff instance - use DL4J workspaces String wsNameWorking = workspaceMgr.getWorkspaceName(ArrayType.FF_WORKING_MEM); String wsNameOutput = workspaceMgr.getWorkspaceName(ArrayType.ACTIVATIONS); WorkspaceConfiguration confWorking = workspaceMgr.getConfiguration(ArrayType.FF_WORKING_MEM); WorkspaceConfiguration confOutput = workspaceMgr.getConfiguration(ArrayType.ACTIVATIONS); boolean actScopedOut = workspaceMgr.isScopedOut(ArrayType.ACTIVATIONS); Preconditions.checkState(actScopedOut || wsNameOutput != null, "Activations must have a workspace or must be scoped out"); SessionMemMgr mmgr = new DL4JSameDiffMemoryMgr(wsNameWorking, wsNameOutput, confWorking, confOutput); InferenceSession is = sameDiff.getSessions().get(Thread.currentThread().getId()); if(is == null){ is = new InferenceSession(sameDiff); sameDiff.getSessions().put(Thread.currentThread().getId(), is); } is.setMmgr(mmgr); INDArray result = sameDiff.outputSingle(phMap, outputKey); //Edge case: "vertex" is just an identity activation, for example //TODO there may be a cleaner way to do this... if(!actScopedOut && !result.data().getParentWorkspace().getId().equals(wsNameOutput)){ result = workspaceMgr.dup(ArrayType.ACTIVATIONS, result); } else if(actScopedOut && result.isAttached()){ result = result.detach(); } //Clear placeholders and op inputs to ensure no out-of-scope arrays are still referenced anywhere sameDiff.clearPlaceholders(true); sameDiff.clearOpInputs(); return workspaceMgr.dup(ArrayType.ACTIVATIONS, result); }
Example 17
Source File: MultiLayerNetwork.java From deeplearning4j with Apache License 2.0 | 4 votes |
/** * Feed-forward through the network - returning all array activations in a list, detached from any workspace. * Note that no workspace should be active externally when calling this method (an exception will be thrown * if a workspace is open externally) * * @param train Training mode (true) or test/inference mode (false) * @param fwdPassType Type of forward pass to perform (STANDARD or RNN_ACTIVATE_WITH_STORED_STATE only) * @param storeLastForTBPTT ONLY used if fwdPassType == FwdPassType.RNN_ACTIVATE_WITH_STORED_STATE * @param layerIndex Index (inclusive) to stop forward pass at. For all layers, use numLayers-1 * @param input Input to the network * @param fMask Feature mask array. May be null. * @param lMask Label mask array. May be null. * @param clearInputs Whether the layer inputs should be cleared * @return List of activations (including the input), detached from any workspace */ protected synchronized List<INDArray> ffToLayerActivationsDetached(boolean train, @NonNull FwdPassType fwdPassType, boolean storeLastForTBPTT, int layerIndex, @NonNull INDArray input, INDArray fMask, INDArray lMask, boolean clearInputs){ setInput(input); setLayerMaskArrays(fMask, lMask); //Verify that no workspace is open externally WorkspaceUtils.assertNoWorkspacesOpen("Expected no workspace active in ffToLayerActivationsDetached"); LayerWorkspaceMgr workspaceMgr; WorkspaceMode wsm = (train ? layerWiseConfigurations.getTrainingWorkspaceMode() : layerWiseConfigurations.getInferenceWorkspaceMode()); if(wsm == WorkspaceMode.NONE){ workspaceMgr = LayerWorkspaceMgr.noWorkspaces(); } else { workspaceMgr = LayerWorkspaceMgr.builder() .noWorkspaceFor(ArrayType.ACTIVATIONS) .with(ArrayType.INPUT, WS_LAYER_WORKING_MEM, WS_LAYER_WORKING_MEM_CONFIG) .with(ArrayType.FF_WORKING_MEM, WS_LAYER_WORKING_MEM, WS_LAYER_WORKING_MEM_CONFIG) .with(ArrayType.RNN_FF_LOOP_WORKING_MEM, WS_RNN_LOOP_WORKING_MEM, WS_RNN_LOOP_WORKING_MEM_CONFIG) .build(); if(input.isAttached()){ //Don't leverage out of async DataSetIterator workspaces workspaceMgr.setNoLeverageOverride(input.data().getParentWorkspace().getId()); } if(!clearInputs){ workspaceMgr.setScopedOutFor(ArrayType.INPUT); } } workspaceMgr.setHelperWorkspacePointers(helperWorkspaces); List<INDArray> out = new ArrayList<>(); out.add(workspaceMgr.leverageTo(ArrayType.INPUT, input)); //Should be unnecessary (and no op), if layer is implemented correctly for( int i=0; i<=layerIndex; i++ ){ try(MemoryWorkspace wsFFWorking = workspaceMgr.notifyScopeEntered(ArrayType.FF_WORKING_MEM)){ if (getLayerWiseConfigurations().getInputPreProcess(i) != null) { input = getLayerWiseConfigurations().getInputPreProcess(i).preProcess(input, getInputMiniBatchSize(), workspaceMgr); //Validation: Exception if invalid (bad preprocessor implementation) validateArrayWorkspaces(workspaceMgr, input, ArrayType.ACTIVATIONS, i, true, "Feed forward to layer (inference)"); } if(fwdPassType == FwdPassType.STANDARD){ input = layers[i].activate(input, train, workspaceMgr); } else if (fwdPassType == FwdPassType.RNN_ACTIVATE_WITH_STORED_STATE) { if (layers[i] instanceof RecurrentLayer) { input = ((RecurrentLayer) layers[i]).rnnActivateUsingStoredState(input, train, storeLastForTBPTT, workspaceMgr); } else if(layers[i] instanceof BaseWrapperLayer && ((BaseWrapperLayer)layers[i]).getUnderlying() instanceof RecurrentLayer) { RecurrentLayer rl = (RecurrentLayer) ((BaseWrapperLayer)layers[i]).getUnderlying(); input = rl.rnnActivateUsingStoredState(input, train,storeLastForTBPTT, workspaceMgr); } else if (layers[i] instanceof MultiLayerNetwork) { List<INDArray> temp = ((MultiLayerNetwork) layers[i]).rnnActivateUsingStoredState(input, train, storeLastForTBPTT); input = temp.get(temp.size() - 1); } else { input = layers[i].activate(input, train, workspaceMgr); } } else { throw new IllegalStateException("Forward pass type not supported for this method: " + fwdPassType); } //Validation: Exception if invalid (bad layer implementation) validateArrayWorkspaces(workspaceMgr, input, ArrayType.ACTIVATIONS, i, false, "Feed forward to layer (inference)"); out.add(input); } if(clearInputs) { layers[i].clear(); } } return out; }
Example 18
Source File: MultiLayerNetwork.java From deeplearning4j with Apache License 2.0 | 4 votes |
/** * Feed-forward through the network at training time - returning a list of all activations in a workspace (WS_ALL_LAYERS_ACT) * if workspaces are enabled for training; or detached if no workspaces are used.<br> * Note: if using workspaces for training, this method requires that WS_ALL_LAYERS_ACT is open externally.<br> * If using NO workspaces, requires that no external workspace is open<br> * Note that this method does NOT clear the inputs to each layer - instead, they are in the WS_ALL_LAYERS_ACT workspace * for use in later backprop. * * @param layerIndex Index (inclusive) to stop forward pass at. For all layers, use numLayers-1 * @param fwdPassType Type of forward pass to perform (STANDARD or RNN_ACTIVATE_WITH_STORED_STATE only) * @param storeLastForTBPTT ONLY used if fwdPassType == FwdPassType.RNN_ACTIVATE_WITH_STORED_STATE * @param input Input to network * @param fMask Feature mask array. May be null * @param lMask Label mask aray. May be null. * @return */ protected synchronized List<INDArray> ffToLayerActivationsInWs(int layerIndex, @NonNull FwdPassType fwdPassType, boolean storeLastForTBPTT, @NonNull INDArray input, INDArray fMask, INDArray lMask){ setInput(input); setLayerMaskArrays(fMask, lMask); LayerWorkspaceMgr workspaceMgr; if(layerWiseConfigurations.getTrainingWorkspaceMode() == WorkspaceMode.NONE){ WorkspaceUtils.assertNoWorkspacesOpen("Expected no workspace active in ffToLayerActivationsInWs when training workspace is set to NONE"); workspaceMgr = LayerWorkspaceMgr.noWorkspaces(); } else { workspaceMgr = LayerWorkspaceMgr.builder() .with(ArrayType.INPUT, WS_ALL_LAYERS_ACT, WS_ALL_LAYERS_ACT_CONFIG) .with(ArrayType.ACTIVATIONS, WS_ALL_LAYERS_ACT, WS_ALL_LAYERS_ACT_CONFIG) .with(ArrayType.FF_WORKING_MEM, WS_LAYER_WORKING_MEM, WS_LAYER_WORKING_MEM_CONFIG) .with(ArrayType.RNN_FF_LOOP_WORKING_MEM, WS_RNN_LOOP_WORKING_MEM, WS_RNN_LOOP_WORKING_MEM_CONFIG) .build(); if(input.isAttached()){ //Don't leverage out of async DataSetIterator workspaces workspaceMgr.setNoLeverageOverride(input.data().getParentWorkspace().getId()); } if(layerWiseConfigurations.getCacheMode() != CacheMode.NONE){ //For now: store cache mode activations in activations workspace workspaceMgr.setWorkspace(ArrayType.FF_CACHE, WS_ALL_LAYERS_ACT, WS_ALL_LAYERS_ACT_CONFIG); workspaceMgr.setWorkspace(ArrayType.BP_WORKING_MEM, WS_LAYER_WORKING_MEM, WS_LAYER_WORKING_MEM_CONFIG); } WorkspaceUtils.assertOpenAndActive(WS_ALL_LAYERS_ACT, "ffToLayerActivationsInWs method requires workspace WS_ALL_LAYERS_ACT to be open"); } workspaceMgr.setHelperWorkspacePointers(helperWorkspaces); List<INDArray> out = new ArrayList<>(); out.add(workspaceMgr.leverageTo(ArrayType.INPUT, input)); //Probably unnecessary usually boolean traceLog = log.isTraceEnabled(); for( int i=0; i<=layerIndex; i++ ){ try(MemoryWorkspace wsFFWorking = workspaceMgr.notifyScopeEntered(ArrayType.FF_WORKING_MEM)){ if (getLayerWiseConfigurations().getInputPreProcess(i) != null) { input = getLayerWiseConfigurations().getInputPreProcess(i).preProcess(input, getInputMiniBatchSize(), workspaceMgr); //Validation: Exception if invalid (bad preprocessor implementation) validateArrayWorkspaces(workspaceMgr, input, ArrayType.ACTIVATIONS, i, true, "Feed forward to layer (training)"); } if(traceLog){ log.trace("About to forward pass: {} - {}", i, layers[i].getClass().getSimpleName()); } if(fwdPassType == FwdPassType.STANDARD){ input = layers[i].activate(input, true, workspaceMgr); } else if(fwdPassType == FwdPassType.RNN_ACTIVATE_WITH_STORED_STATE){ if (layers[i] instanceof RecurrentLayer) { input = ((RecurrentLayer) layers[i]).rnnActivateUsingStoredState(input, true, storeLastForTBPTT, workspaceMgr); }else if(layers[i] instanceof BaseWrapperLayer && ((BaseWrapperLayer)layers[i]).getUnderlying() instanceof RecurrentLayer) { RecurrentLayer rl = (RecurrentLayer) ((BaseWrapperLayer)layers[i]).getUnderlying(); input = rl.rnnActivateUsingStoredState(input, true, storeLastForTBPTT, workspaceMgr); } else if (layers[i] instanceof MultiLayerNetwork) { List<INDArray> temp = ((MultiLayerNetwork) layers[i]).rnnActivateUsingStoredState(input, true, storeLastForTBPTT); input = temp.get(temp.size() - 1); } else { input = layers[i].activate(input, true, workspaceMgr); } } else { throw new IllegalStateException("FwdPassType not supported for this method: " + fwdPassType); } if(input == null){ throw new IllegalStateException("Layer " + i + " returned null activations"); } //Validation: Exception if invalid (bad layer implementation) validateArrayWorkspaces(workspaceMgr, input, ArrayType.ACTIVATIONS, i, false, "Feed forward to layer (training)"); validateArrayWorkspaces(workspaceMgr, layers[i].input(), ArrayType.INPUT, i, false, "Feed forward to layer (training)"); out.add(input); if(traceLog){ log.trace("Completed forward pass: {} - {}", i, layers[i].getClass().getSimpleName()); } } } return out; }