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

The following examples show how to use org.nd4j.linalg.api.memory.MemoryWorkspace#getGenerationId() . 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: BaseDataBuffer.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public BaseDataBuffer(float[] data, boolean copy, MemoryWorkspace workspace) {
    allocationMode = AllocUtil.getAllocationModeFromContext();
    length = data.length;
    underlyingLength = data.length;
    attached = true;
    parentWorkspace = workspace;

    initTypeAndSize();

    //log.info("Allocating FloatPointer from array of {} elements", data.length);

    pointer = workspace.alloc(data.length * getElementSize(), dataType(), false).asFloatPointer().put(data);
    workspaceGenerationId = workspace.getGenerationId();
    setIndexer(FloatIndexer.create((FloatPointer) pointer));
    //wrappedBuffer = pointer.asByteBuffer();
}
 
Example 2
Source File: BaseDataBuffer.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public BaseDataBuffer(double[] data, boolean copy, MemoryWorkspace workspace) {
    allocationMode = AllocUtil.getAllocationModeFromContext();
    length = data.length;
    underlyingLength = data.length;
    attached = true;
    parentWorkspace = workspace;

    initTypeAndSize();

    //log.info("Allocating FloatPointer from array of {} elements", data.length);

    pointer = workspace.alloc(data.length * getElementSize(), dataType(), false).asDoublePointer().put(data);
    workspaceGenerationId = workspace.getGenerationId();
    indexer = DoubleIndexer.create((DoublePointer) pointer);
    //wrappedBuffer = pointer.asByteBuffer();
}
 
Example 3
Source File: BaseDataBuffer.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public BaseDataBuffer(int[] data, boolean copy, MemoryWorkspace workspace) {
    allocationMode = AllocUtil.getAllocationModeFromContext();
    length = data.length;
    underlyingLength = data.length;
    attached = true;
    parentWorkspace = workspace;

    initTypeAndSize();

    //log.info("Allocating FloatPointer from array of {} elements", data.length);

    pointer = workspace.alloc(data.length * getElementSize(), dataType(), false).asIntPointer().put(data);
    workspaceGenerationId = workspace.getGenerationId();
    indexer = IntIndexer.create((IntPointer) pointer);
    //wrappedBuffer = pointer.asByteBuffer();
}
 
Example 4
Source File: BaseCpuDataBuffer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseCpuDataBuffer(float[] data, boolean copy, MemoryWorkspace workspace) {
    allocationMode = AllocUtil.getAllocationModeFromContext();
    length = data.length;
    underlyingLength = data.length;
    attached = true;
    parentWorkspace = workspace;

    initTypeAndSize();

    //log.info("Allocating FloatPointer from array of {} elements", data.length);

    pointer = workspace.alloc(data.length * getElementSize(), dataType(), false).asFloatPointer().put(data);

    ptrDataBuffer = OpaqueDataBuffer.externalizedDataBuffer(length, dataType(), this.pointer, null);
    Nd4j.getDeallocatorService().pickObject(this);

    workspaceGenerationId = workspace.getGenerationId();
    setIndexer(FloatIndexer.create((FloatPointer) pointer));
    //wrappedBuffer = pointer.asByteBuffer();
}
 
Example 5
Source File: BaseCpuDataBuffer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseCpuDataBuffer(double[] data, boolean copy, MemoryWorkspace workspace) {
    allocationMode = AllocUtil.getAllocationModeFromContext();
    length = data.length;
    underlyingLength = data.length;
    attached = true;
    parentWorkspace = workspace;

    initTypeAndSize();

    //log.info("Allocating FloatPointer from array of {} elements", data.length);

    pointer = workspace.alloc(data.length * getElementSize(), dataType(), false).asDoublePointer().put(data);

    ptrDataBuffer = OpaqueDataBuffer.externalizedDataBuffer(length, dataType(), this.pointer, null);
    Nd4j.getDeallocatorService().pickObject(this);

    workspaceGenerationId = workspace.getGenerationId();
    indexer = DoubleIndexer.create((DoublePointer) pointer);
    //wrappedBuffer = pointer.asByteBuffer();
}
 
Example 6
Source File: BaseCpuDataBuffer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseCpuDataBuffer(int[] data, boolean copy, MemoryWorkspace workspace) {
    allocationMode = AllocUtil.getAllocationModeFromContext();
    length = data.length;
    underlyingLength = data.length;
    attached = true;
    parentWorkspace = workspace;

    initTypeAndSize();

    //log.info("Allocating FloatPointer from array of {} elements", data.length);

    pointer = workspace.alloc(data.length * getElementSize(), dataType(), false).asIntPointer().put(data);

    ptrDataBuffer = OpaqueDataBuffer.externalizedDataBuffer(length, dataType(), this.pointer, null);
    Nd4j.getDeallocatorService().pickObject(this);

    workspaceGenerationId = workspace.getGenerationId();
    indexer = IntIndexer.create((IntPointer) pointer);
    //wrappedBuffer = pointer.asByteBuffer();
}
 
Example 7
Source File: BaseCpuDataBuffer.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public BaseCpuDataBuffer(long[] data, boolean copy, MemoryWorkspace workspace) {
    allocationMode = AllocUtil.getAllocationModeFromContext();
    length = data.length;
    underlyingLength = data.length;
    attached = true;
    parentWorkspace = workspace;

    initTypeAndSize();

    //log.info("Allocating FloatPointer from array of {} elements", data.length);

    pointer = workspace.alloc(data.length * getElementSize(), dataType(), false).asLongPointer().put(data);

    ptrDataBuffer = OpaqueDataBuffer.externalizedDataBuffer(length, dataType(), this.pointer, null);
    Nd4j.getDeallocatorService().pickObject(this);

    workspaceGenerationId = workspace.getGenerationId();
    indexer = LongIndexer.create((LongPointer) pointer);
    //wrappedBuffer = pointer.asByteBuffer();
}
 
Example 8
Source File: BaseCudaDataBuffer.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public BaseCudaDataBuffer(long length, int elementSize, boolean initialize, @NonNull MemoryWorkspace workspace) {
    this.allocationMode = AllocationMode.LONG_SHAPE;
    initTypeAndSize();

    this.attached = true;
    this.parentWorkspace = workspace;

    this.allocationPoint = AtomicAllocator.getInstance().allocateMemory(this, new AllocationShape(length, this.elementSize, dataType()), initialize);
    this.length = length;
    //allocationPoint.attachBuffer(this);
    //this.elementSize = elementSize;
    this.trackingPoint = allocationPoint.getObjectId();
    this.offset = 0;
    this.originalOffset = 0;


    if (dataType() == Type.DOUBLE) {
        this.attached = true;
        this.parentWorkspace = workspace;

        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), length, 0).asDoublePointer();
        indexer = DoubleIndexer.create((DoublePointer) pointer);
    } else if (dataType() == Type.FLOAT) {
        this.attached = true;
        this.parentWorkspace = workspace;

        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), length, 0).asFloatPointer();
        indexer = FloatIndexer.create((FloatPointer) pointer);
    } else if (dataType() == Type.INT) {
        this.attached = true;
        this.parentWorkspace = workspace;

        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), length, 0).asIntPointer();
        indexer = IntIndexer.create((IntPointer) pointer);
    } else if (dataType() == Type.HALF) {
        this.attached = true;
        this.parentWorkspace = workspace;

        // FIXME: proper pointer and proper indexer should be used here
        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), length, 0).asShortPointer();
        indexer = HalfIndexer.create((ShortPointer) pointer);
    } else if (dataType() == Type.LONG) {
        this.attached = true;
        this.parentWorkspace = workspace;

        // FIXME: proper pointer and proper indexer should be used here
        this.pointer = new CudaPointer(allocationPoint.getPointers().getHostPointer(), length, 0).asLongPointer();
        indexer = LongIndexer.create((LongPointer) pointer);
    }

    workspaceGenerationId = workspace.getGenerationId();
}
 
Example 9
Source File: BaseDataBuffer.java    From nd4j with Apache License 2.0 4 votes vote down vote up
protected BaseDataBuffer(long length, boolean initialize, MemoryWorkspace workspace) {
    if (length < 1)
        throw new IllegalArgumentException("Length must be >= 1");
    initTypeAndSize();
    this.length = length;
    this.underlyingLength = length;
    allocationMode = AllocUtil.getAllocationModeFromContext();



    if (length < 0)
        throw new IllegalArgumentException("Unable to create a buffer of length <= 0");

    if (dataType() == Type.DOUBLE) {
        attached = true;
        parentWorkspace = workspace;

        pointer = workspace.alloc(length * getElementSize(), dataType(), initialize).asDoublePointer(); //new DoublePointer(length());
        indexer = DoubleIndexer.create((DoublePointer) pointer);

    } else if (dataType() == Type.FLOAT) {
        attached = true;
        parentWorkspace = workspace;

        pointer = workspace.alloc(length * getElementSize(), dataType(), initialize).asFloatPointer(); //new FloatPointer(length());
        setIndexer(FloatIndexer.create((FloatPointer) pointer));

    } else if (dataType() == Type.INT) {
        attached = true;
        parentWorkspace = workspace;

        pointer = workspace.alloc(length * getElementSize(), dataType(), initialize).asIntPointer(); //new FloatPointer(length());
        setIndexer(IntIndexer.create((IntPointer) pointer));

    } else if (dataType() == Type.LONG) {
        attached = true;
        parentWorkspace = workspace;

        pointer = workspace.alloc(length * getElementSize(), dataType(), initialize).asIntPointer(); //new FloatPointer(length());
        setIndexer(LongIndexer.create((LongPointer) pointer));
    }

    workspaceGenerationId = workspace.getGenerationId();

}
 
Example 10
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;
}