Java Code Examples for org.nd4j.linalg.api.memory.MemoryWorkspace#isScopeActive()

The following examples show how to use org.nd4j.linalg.api.memory.MemoryWorkspace#isScopeActive() . 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 deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Assert that no workspaces are currently open
 *
 * @param msg Message to include in the exception, if required
 * @param allowScopedOut If true: don't fail if we have an open workspace but are currently scoped out
 */
public static void assertNoWorkspacesOpen(String msg, boolean allowScopedOut) throws ND4JWorkspaceException {
    if (Nd4j.getWorkspaceManager().anyWorkspaceActiveForCurrentThread()) {

        MemoryWorkspace currWs = Nd4j.getMemoryManager().getCurrentWorkspace();
        if(allowScopedOut && (currWs == null || currWs instanceof DummyWorkspace))
            return; //Open WS but we've scoped out

        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);
    }
}
 
Example 2
Source File: BasicWorkspaceManager.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public boolean anyWorkspaceActiveForCurrentThread(){
    ensureThreadExistense();
    boolean anyActive = false;
    for(MemoryWorkspace ws : backingMap.get().values()){
        if(ws.isScopeActive()){
            anyActive = true;
            break;
        }
    }
    return anyActive;
}
 
Example 3
Source File: DefaultOpExecutioner.java    From nd4j with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: WorkspaceUtils.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: WorkspaceUtils.java    From nd4j with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: BasicWorkspaceManager.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public boolean anyWorkspaceActiveForCurrentThread(){
    ensureThreadExistense();
    boolean anyActive = false;
    for(MemoryWorkspace ws : backingMap.get().values()){
        if(ws.isScopeActive()){
            anyActive = true;
            break;
        }
    }
    return anyActive;
}
 
Example 7
Source File: DefaultOpExecutioner.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public 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;
}
 
Example 8
Source File: WorkspaceUtils.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: InferenceSession.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@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;
}