Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#markAsCompressed()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#markAsCompressed() . 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: CudaThreshold.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray compress(INDArray array) {
    //logger.info("Threshold [{}] compression", threshold);

    Nd4j.getExecutioner().commit();
    //Nd4j.getAffinityManager().ensureLocation(array, AffinityManager.Location.HOST);

    DataBuffer buffer = compress(array.data());
    if (buffer == null)
        return null;

    INDArray dup = Nd4j.createArrayFromShapeBuffer(buffer, array.shapeInfoDataBuffer());
    dup.markAsCompressed(true);

    return dup;
}
 
Example 2
Source File: JCublasNDArrayFactory.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method converts Single/Double precision databuffer to Half-precision databuffer
 *
 * @param typeSrc
 * @param source
 * @param typeDst @return
 */
@Override
public INDArray convertDataEx(DataBuffer.TypeEx typeSrc, INDArray source, DataBuffer.TypeEx typeDst) {
    if (source.isView())
        throw new UnsupportedOperationException("Impossible to compress View. Consider using dup() before. ");

    DataBuffer buffer = convertDataEx(typeSrc, source.data(), typeDst);
    source.setData(buffer);

    if (buffer instanceof CompressedDataBuffer)
        source.markAsCompressed(true);
    else
        source.markAsCompressed(false);

    return source;
}
 
Example 3
Source File: CpuNDArrayFactory.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method converts Single/Double precision databuffer to Half-precision databuffer
 *
 * @param typeSrc
 * @param source
 * @param typeDst @return
 */
@Override
public INDArray convertDataEx(DataBuffer.TypeEx typeSrc, INDArray source, DataBuffer.TypeEx typeDst) {
    if (source.isView())
        throw new UnsupportedOperationException("Impossible to compress View. Consider using dup() before. ");

    DataBuffer buffer = convertDataEx(typeSrc, source.data(), typeDst);
    source.setData(buffer);

    if (buffer instanceof CompressedDataBuffer)
        source.markAsCompressed(true);
    else
        source.markAsCompressed(false);

    return source;
}
 
Example 4
Source File: CpuThreshold.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray compress(INDArray array) {
    //logger.info("Threshold [{}] compression", threshold);

    Nd4j.getExecutioner().commit();
    Nd4j.getAffinityManager().ensureLocation(array, AffinityManager.Location.HOST);

    DataBuffer buffer = compress(array.data());
    if (buffer == null)
        return null;

    INDArray dup = Nd4j.createArrayFromShapeBuffer(buffer, array.shapeInfoDataBuffer());
    dup.markAsCompressed(true);

    return dup;
}
 
Example 5
Source File: CpuThreshold.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray compress(INDArray array) {
    //logger.info("Threshold [{}] compression", threshold);

    Nd4j.getExecutioner().commit();
    Nd4j.getAffinityManager().ensureLocation(array, AffinityManager.Location.HOST);

    DataBuffer buffer = compress(array.data());
    if (buffer == null)
        return null;

    INDArray dup = Nd4j.createArrayFromShapeBuffer(buffer, array.shapeInfoDataBuffer());
    dup.markAsCompressed(true);

    return dup;
}
 
Example 6
Source File: CpuNDArrayFactory.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method converts Single/Double precision databuffer to Half-precision databuffer
 *
 * @param typeSrc
 * @param source
 * @param typeDst @return
 */
@Override
public INDArray convertDataEx(DataTypeEx typeSrc, INDArray source, DataTypeEx typeDst) {
    if (source.isView())
        throw new UnsupportedOperationException("Impossible to compress View. Consider using dup() before. ");

    DataBuffer buffer = convertDataEx(typeSrc, source.data(), typeDst);
    if (nativeOps.lastErrorCode() != 0)
        throw new RuntimeException(nativeOps.lastErrorMessage());

    source.setData(buffer);

    if (buffer instanceof CompressedDataBuffer)
        source.markAsCompressed(true);
    else
        source.markAsCompressed(false);

    return source;
}
 
Example 7
Source File: JCublasNDArrayFactory.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method converts Single/Double precision databuffer to Half-precision databuffer
 *
 * @param typeSrc
 * @param source
 * @param typeDst @return
 */
@Override
public INDArray convertDataEx(DataTypeEx typeSrc, INDArray source, DataTypeEx typeDst) {
    if (source.isView())
        throw new UnsupportedOperationException("Impossible to compress View. Consider using dup() before. ");

    DataBuffer buffer = convertDataEx(typeSrc, source.data(), typeDst);
    source.setData(buffer);

    if (buffer instanceof CompressedDataBuffer)
        source.markAsCompressed(true);
    else
        source.markAsCompressed(false);

    return source;
}
 
Example 8
Source File: AbstractCompressor.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void decompressi(INDArray array) {
    if (!array.isCompressed())
        return;

    array.markAsCompressed(false);
    array.setData(decompress(array.data()));
}
 
Example 9
Source File: AbstractCompressor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void decompressi(INDArray array) {
    if (!array.isCompressed())
        return;

    array.markAsCompressed(false);
    array.setData(decompress(array.data(), ((CompressedDataBuffer)array.data()).getCompressionDescriptor().getOriginalDataType()));
}
 
Example 10
Source File: AbstractCompressor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Inplace compression of INDArray
 *
 * @param array
 */
@Override
public void compressi(INDArray array) {
    // TODO: lift this restriction
    if (array.isView())
        throw new UnsupportedOperationException("Impossible to apply inplace compression on View");

    array.setData(compress(array.data()));
    array.markAsCompressed(true);
}
 
Example 11
Source File: AbstractCompressor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray compress(INDArray array) {
    INDArray dup = array.dup(array.ordering());

    Nd4j.getExecutioner().commit();

    dup.setData(compress(dup.data()));
    dup.markAsCompressed(true);

    return dup;
}
 
Example 12
Source File: JCublasNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray dup(char order) {
    if (this.isCompressed() && this.ordering() == order) {
        INDArray ret = Nd4j.createArrayFromShapeBuffer(data().dup(), this.shapeInfoDataBuffer());
        ret.markAsCompressed(true);
        return ret;
    }
    /*
    if (!isView() && ordering() == order && Shape.strideDescendingCAscendingF(this)) {
        AtomicAllocator allocator = AtomicAllocator.getInstance();
        INDArray array = Nd4j.createUninitialized(shape(), order);
    
        CudaContext context = allocator.getFlowController().prepareAction(array, this);
    
        Configuration configuration = CudaEnvironment.getInstance().getConfiguration();
    
        if (configuration.getMemoryModel() == Configuration.MemoryModel.IMMEDIATE && configuration.getFirstMemory() == AllocationStatus.DEVICE) {
            allocator.memcpyDevice(array.data(), allocator.getPointer(this.data, context), this.data.length() * this.data().getElementSize(), 0, context);
        } else if (configuration.getMemoryModel() == Configuration.MemoryModel.DELAYED || configuration.getFirstMemory() == AllocationStatus.HOST) {
            AllocationPoint pointSrc = allocator.getAllocationPoint(this);
            AllocationPoint pointDst = allocator.getAllocationPoint(array);
    
            if (pointSrc.getAllocationStatus() == AllocationStatus.HOST) {
                NativeOpsHolder.getInstance().getDeviceNativeOps().memcpyAsync(pointDst.getPointers().getHostPointer(), pointSrc.getPointers().getHostPointer(), length * data.getElementSize(), CudaConstants.cudaMemcpyHostToHost, context.getOldStream());
            } else {
                // this code branch is possible only with DELAYED memoryModel and src point being allocated on device
                if (pointDst.getAllocationStatus() != AllocationStatus.DEVICE) {
                    allocator.getMemoryHandler().alloc(AllocationStatus.DEVICE, pointDst, pointDst.getShape(), false);
                }
    
                NativeOpsHolder.getInstance().getDeviceNativeOps().memcpyAsync(pointDst.getPointers().getDevicePointer(), pointSrc.getPointers().getDevicePointer(), length * data.getElementSize(), CudaConstants.cudaMemcpyHostToDevice, context.getOldStream());
            }
        }
    
        allocator.getFlowController().registerAction(context, array, this);
    
        return array;
    } else */return super.dup(order);
}
 
Example 13
Source File: AbstractCompressor.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Inplace compression of INDArray
 *
 * @param array
 */
@Override
public void compressi(INDArray array) {
    // TODO: lift this restriction
    if (array.isView())
        throw new UnsupportedOperationException("Impossible to apply inplace compression on View");

    array.setData(compress(array.data()));
    array.markAsCompressed(true);
}
 
Example 14
Source File: AbstractCompressor.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray compress(INDArray array) {
    INDArray dup = array.dup(array.ordering());

    Nd4j.getExecutioner().commit();

    dup.setData(compress(dup.data()));
    dup.markAsCompressed(true);

    return dup;
}
 
Example 15
Source File: JCublasNDArray.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray dup(char order) {
    if (this.isCompressed() && this.ordering() == order) {
        INDArray ret = Nd4j.createArrayFromShapeBuffer(data().dup(), this.shapeInfoDataBuffer());
        ret.markAsCompressed(true);
        return ret;
    }
    /*
    if (!isView() && ordering() == order && Shape.strideDescendingCAscendingF(this)) {
        AtomicAllocator allocator = AtomicAllocator.getInstance();
        INDArray array = Nd4j.createUninitialized(shape(), order);
    
        CudaContext context = allocator.getFlowController().prepareAction(array, this);
    
        Configuration configuration = CudaEnvironment.getInstance().getConfiguration();
    
        if (configuration.getMemoryModel() == Configuration.MemoryModel.IMMEDIATE && configuration.getFirstMemory() == AllocationStatus.DEVICE) {
            allocator.memcpyDevice(array.data(), allocator.getPointer(this.data, context), this.data.length() * this.data().getElementSize(), 0, context);
        } else if (configuration.getMemoryModel() == Configuration.MemoryModel.DELAYED || configuration.getFirstMemory() == AllocationStatus.HOST) {
            AllocationPoint pointSrc = allocator.getAllocationPoint(this);
            AllocationPoint pointDst = allocator.getAllocationPoint(array);
    
            if (pointSrc.getAllocationStatus() == AllocationStatus.HOST) {
                NativeOpsHolder.getInstance().getDeviceNativeOps().memcpyAsync(pointDst.getPointers().getHostPointer(), pointSrc.getPointers().getHostPointer(), length * data.getElementSize(), CudaConstants.cudaMemcpyHostToHost, context.getOldStream());
            } else {
                // this code branch is possible only with DELAYED memoryModel and src point being allocated on device
                if (pointDst.getAllocationStatus() != AllocationStatus.DEVICE) {
                    allocator.getMemoryHandler().alloc(AllocationStatus.DEVICE, pointDst, pointDst.getShape(), false);
                }
    
                NativeOpsHolder.getInstance().getDeviceNativeOps().memcpyAsync(pointDst.getPointers().getDevicePointer(), pointSrc.getPointers().getDevicePointer(), length * data.getElementSize(), CudaConstants.cudaMemcpyHostToDevice, context.getOldStream());
            }
        }
    
        allocator.getFlowController().registerAction(context, array, this);
    
        return array;
    } else */return super.dup(order);
}
 
Example 16
Source File: JCublasNDArray.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray dup() {
    if (this.isCompressed() && this.ordering() == Nd4j.order().charValue()) {
        INDArray ret = Nd4j.createArrayFromShapeBuffer(data().dup(), this.shapeInfoDataBuffer());
        ret.markAsCompressed(true);
        return ret;
    }
    /*
        Special case for cuda: if we have not a view, and shapes do match - we
    */
    /*
    if (!isView() && ordering() == Nd4j.order() && Shape.strideDescendingCAscendingF(this)) {
        AtomicAllocator allocator = AtomicAllocator.getInstance();
        INDArray array = Nd4j.createUninitialized(shape(), ordering());
    
        CudaContext context = allocator.getFlowController().prepareAction(array, this);
    
        Configuration configuration = CudaEnvironment.getInstance().getConfiguration();
    
        if (configuration.getMemoryModel() == Configuration.MemoryModel.IMMEDIATE && configuration.getFirstMemory() == AllocationStatus.DEVICE) {
    //                log.info("Path 0");
            allocator.memcpyDevice(array.data(), allocator.getPointer(this.data, context), this.data.length() * this.data().getElementSize(), 0, context);
        } else if (configuration.getMemoryModel() == Configuration.MemoryModel.DELAYED || configuration.getFirstMemory() == AllocationStatus.HOST) {
            AllocationPoint pointSrc = allocator.getAllocationPoint(this);
            AllocationPoint pointDst = allocator.getAllocationPoint(array);
    
            if (pointSrc.getAllocationStatus() == AllocationStatus.HOST) {
    //                    log.info("Path A");
                NativeOpsHolder.getInstance().getDeviceNativeOps().memcpyAsync(pointDst.getPointers().getHostPointer(), pointSrc.getPointers().getHostPointer(), length * data.getElementSize(), CudaConstants.cudaMemcpyHostToHost, context.getOldStream());
            } else {
    //                    log.info("Path B. SRC dId: [{}], DST dId: [{}], cId: [{}]", pointSrc.getDeviceId(), pointDst.getDeviceId(), allocator.getDeviceId());
                // this code branch is possible only with DELAYED memoryModel and src point being allocated on device
                if (pointDst.getAllocationStatus() != AllocationStatus.DEVICE) {
                    allocator.getMemoryHandler().alloc(AllocationStatus.DEVICE, pointDst, pointDst.getShape(), false);
                }
    
                NativeOpsHolder.getInstance().getDeviceNativeOps().memcpyAsync(pointDst.getPointers().getDevicePointer(), pointSrc.getPointers().getHostPointer(), length * data.getElementSize(), CudaConstants.cudaMemcpyHostToDevice, context.getOldStream());
            }
        }
    
        allocator.getFlowController().registerAction(context, array, this);
        return array;
    } else */

    val res = super.dup();
    Nd4j.getExecutioner().commit();
    return res;
}
 
Example 17
Source File: JCublasNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray dup() {
    if (this.isCompressed() && this.ordering() == Nd4j.order().charValue()) {
        INDArray ret = Nd4j.createArrayFromShapeBuffer(data().dup(), this.shapeInfoDataBuffer());
        ret.markAsCompressed(true);
        return ret;
    }
    /*
        Special case for cuda: if we have not a view, and shapes do match - we
    */
    /*
    if (!isView() && ordering() == Nd4j.order() && Shape.strideDescendingCAscendingF(this)) {
        AtomicAllocator allocator = AtomicAllocator.getInstance();
        INDArray array = Nd4j.createUninitialized(shape(), ordering());
    
        CudaContext context = allocator.getFlowController().prepareAction(array, this);
    
        Configuration configuration = CudaEnvironment.getInstance().getConfiguration();
    
        if (configuration.getMemoryModel() == Configuration.MemoryModel.IMMEDIATE && configuration.getFirstMemory() == AllocationStatus.DEVICE) {
    //                log.info("Path 0");
            allocator.memcpyDevice(array.data(), allocator.getPointer(this.data, context), this.data.length() * this.data().getElementSize(), 0, context);
        } else if (configuration.getMemoryModel() == Configuration.MemoryModel.DELAYED || configuration.getFirstMemory() == AllocationStatus.HOST) {
            AllocationPoint pointSrc = allocator.getAllocationPoint(this);
            AllocationPoint pointDst = allocator.getAllocationPoint(array);
    
            if (pointSrc.getAllocationStatus() == AllocationStatus.HOST) {
    //                    log.info("Path A");
                NativeOpsHolder.getInstance().getDeviceNativeOps().memcpyAsync(pointDst.getPointers().getHostPointer(), pointSrc.getPointers().getHostPointer(), length * data.getElementSize(), CudaConstants.cudaMemcpyHostToHost, context.getOldStream());
            } else {
    //                    log.info("Path B. SRC dId: [{}], DST dId: [{}], cId: [{}]", pointSrc.getDeviceId(), pointDst.getDeviceId(), allocator.getDeviceId());
                // this code branch is possible only with DELAYED memoryModel and src point being allocated on device
                if (pointDst.getAllocationStatus() != AllocationStatus.DEVICE) {
                    allocator.getMemoryHandler().alloc(AllocationStatus.DEVICE, pointDst, pointDst.getShape(), false);
                }
    
                NativeOpsHolder.getInstance().getDeviceNativeOps().memcpyAsync(pointDst.getPointers().getDevicePointer(), pointSrc.getPointers().getHostPointer(), length * data.getElementSize(), CudaConstants.cudaMemcpyHostToDevice, context.getOldStream());
            }
        }
    
        allocator.getFlowController().registerAction(context, array, this);
        return array;
    } else */return super.dup();
}