org.nd4j.linalg.api.concurrency.AffinityManager Java Examples

The following examples show how to use org.nd4j.linalg.api.concurrency.AffinityManager. 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: BinarySerde.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Setup the given byte buffer
 * for serialization (note that this is for uncompressed INDArrays)
 * 4 bytes int for rank
 * 4 bytes for data opType
 * shape buffer
 * data buffer
 *
 * @param arr the array to setup
 * @param allocated the byte buffer to setup
 * @param rewind whether to rewind the byte buffer or nt
 */
public static void doByteBufferPutUnCompressed(INDArray arr, ByteBuffer allocated, boolean rewind) {
    // ensure we send data to host memory
    Nd4j.getExecutioner().commit();
    Nd4j.getAffinityManager().ensureLocation(arr, AffinityManager.Location.HOST);

    ByteBuffer buffer = arr.data().pointer().asByteBuffer().order(ByteOrder.nativeOrder());
    ByteBuffer shapeBuffer = arr.shapeInfoDataBuffer().pointer().asByteBuffer().order(ByteOrder.nativeOrder());
    //2 four byte ints at the beginning
    allocated.putInt(arr.rank());
    //put data opType next so its self describing
    allocated.putInt(arr.data().dataType().ordinal());
    allocated.put(shapeBuffer);
    allocated.put(buffer);
    if (rewind)
        ((Buffer) allocated).rewind();
}
 
Example #2
Source File: BasicWorkspaceTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCircularWorkspaceAsymmetry_1() {
    // nothing to test on CPU here
    if (Nd4j.getEnvironment().isCPU())
        return;

    // circular workspace mode
    val configuration = WorkspaceConfiguration.builder().initialSize(10 * 1024 * 1024)
            .policyReset(ResetPolicy.ENDOFBUFFER_REACHED).policyAllocation(AllocationPolicy.STRICT)
            .policySpill(SpillPolicy.FAIL).policyLearning(LearningPolicy.NONE).build();


    try (val ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "circular_ws")) {
        val array = Nd4j.create(DataType.FLOAT, 10, 10);

        // we expect that this array has no data/buffer on HOST side
        assertEquals(AffinityManager.Location.DEVICE, Nd4j.getAffinityManager().getActiveLocation(array));

        // since this array doesn't have HOST buffer - it will allocate one now
        array.getDouble(3L);
    }

    Nd4j.getWorkspaceManager().destroyAllWorkspacesForCurrentThread();
}
 
Example #3
Source File: DefaultCallback.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void call(DataSet dataSet) {
    if (dataSet != null) {
        if (dataSet.getFeatures() != null)
            Nd4j.getAffinityManager().ensureLocation(dataSet.getFeatures(), AffinityManager.Location.DEVICE);

        if (dataSet.getLabels() != null)
            Nd4j.getAffinityManager().ensureLocation(dataSet.getLabels(), AffinityManager.Location.DEVICE);

        if (dataSet.getFeaturesMaskArray() != null)
            Nd4j.getAffinityManager().ensureLocation(dataSet.getFeaturesMaskArray(),
                            AffinityManager.Location.DEVICE);

        if (dataSet.getLabelsMaskArray() != null)
            Nd4j.getAffinityManager().ensureLocation(dataSet.getLabelsMaskArray(), AffinityManager.Location.DEVICE);
    }
}
 
Example #4
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 #5
Source File: NumpyArray.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public NumpyArray(INDArray nd4jArray) {
    Nd4j.getAffinityManager().ensureLocation(nd4jArray, AffinityManager.Location.HOST);
    DataBuffer buff = nd4jArray.data();
    address = buff.pointer().address();
    shape = nd4jArray.shape();
    long[] nd4jStrides = nd4jArray.stride();
    strides = new long[nd4jStrides.length];
    int elemSize = buff.getElementSize();
    for (int i = 0; i < strides.length; i++) {
        strides[i] = nd4jStrides[i] * elemSize;
    }
    dtype = nd4jArray.dataType();
    this.nd4jArray = nd4jArray;
    String cacheKey = address + "_" + nd4jArray.length() + "_" + dtype + "_" + ArrayUtils.toString(strides);
    arrayCache.put(cacheKey, nd4jArray);
}
 
Example #6
Source File: DefaultCallback.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void call(MultiDataSet multiDataSet) {
    if (multiDataSet != null) {
        if (multiDataSet.getFeatures() != null)
            for (int i = 0; i < multiDataSet.getFeatures().length; i++)
                Nd4j.getAffinityManager().ensureLocation(multiDataSet.getFeatures()[i],
                                AffinityManager.Location.DEVICE);

        if (multiDataSet.getLabels() != null)
            for (int i = 0; i < multiDataSet.getLabels().length; i++)
                Nd4j.getAffinityManager().ensureLocation(multiDataSet.getLabels()[i],
                                AffinityManager.Location.DEVICE);

        if (multiDataSet.getFeaturesMaskArrays() != null)
            for (int i = 0; i < multiDataSet.getFeaturesMaskArrays().length; i++)
                Nd4j.getAffinityManager().ensureLocation(multiDataSet.getFeaturesMaskArrays()[i],
                                AffinityManager.Location.DEVICE);

        if (multiDataSet.getLabelsMaskArrays() != null)
            for (int i = 0; i < multiDataSet.getLabelsMaskArrays().length; i++)
                Nd4j.getAffinityManager().ensureLocation(multiDataSet.getLabelsMaskArrays()[i],
                                AffinityManager.Location.DEVICE);
    }
}
 
Example #7
Source File: BinarySerde.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Setup the given byte buffer
 * for serialization (note that this is for uncompressed INDArrays)
 * 4 bytes int for rank
 * 4 bytes for data opType
 * shape buffer
 * data buffer
 *
 * @param arr the array to setup
 * @param allocated the byte buffer to setup
 * @param rewind whether to rewind the byte buffer or nt
 */
public static void doByteBufferPutUnCompressed(INDArray arr, ByteBuffer allocated, boolean rewind) {
    // ensure we send data to host memory
    Nd4j.getExecutioner().commit();
    Nd4j.getAffinityManager().ensureLocation(arr, AffinityManager.Location.HOST);

    ByteBuffer buffer = arr.data().pointer().asByteBuffer().order(ByteOrder.nativeOrder());
    ByteBuffer shapeBuffer = arr.shapeInfoDataBuffer().pointer().asByteBuffer().order(ByteOrder.nativeOrder());
    //2 four byte ints at the beginning
    allocated.putInt(arr.rank());
    //put data opType next so its self describing
    allocated.putInt(arr.data().dataType().ordinal());
    allocated.put(shapeBuffer);
    allocated.put(buffer);
    if (rewind)
        allocated.rewind();
}
 
Example #8
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 #9
Source File: CpuThreshold.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[]{1, (int) buffer.length()}).getFirst());
    MatchCondition condition = new MatchCondition(temp, Conditions.absGreaterThanOrEqual(threshold));
    int cntAbs = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);


    //log.info("density ratio: {}", String.format("%.2f", cntAbs * 100.0f / buffer.length()));

    if (cntAbs < 2)
        return null;

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold));
    pointer.put(3, 0);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());



    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.THRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #10
Source File: HistoryProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public void record(INDArray pixelArray) {
    if(isMonitoring()) {
        // before accessing the raw pointer, we need to make sure that array is actual on the host side
        Nd4j.getAffinityManager().ensureLocation(pixelArray, AffinityManager.Location.HOST);

        try {
            videoRecorder.record(pixelArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example #11
Source File: HistoryProcessor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private INDArray transform(INDArray raw) {
    long[] shape = raw.shape();

    // before accessing the raw pointer, we need to make sure that array is actual on the host side
    Nd4j.getAffinityManager().ensureLocation(raw, AffinityManager.Location.HOST);

    Mat ocvmat = new Mat((int)shape[0], (int)shape[1], CV_32FC(3), raw.data().pointer());
    Mat cvmat = new Mat(shape[0], shape[1], CV_8UC(3));
    ocvmat.convertTo(cvmat, CV_8UC(3), 255.0, 0.0);
    cvtColor(cvmat, cvmat, COLOR_RGB2GRAY);
    Mat resized = new Mat(conf.getRescaledHeight(), conf.getRescaledWidth(), CV_8UC(1));
    resize(cvmat, resized, new Size(conf.getRescaledWidth(), conf.getRescaledHeight()));
    //   show(resized);
    //   waitKP();
    //Crop by croppingHeight, croppingHeight
    Mat cropped = resized.apply(new Rect(conf.getOffsetX(), conf.getOffsetY(), conf.getCroppingWidth(),
                    conf.getCroppingHeight()));
    //System.out.println(conf.getCroppingWidth() + " " + cropped.data().asBuffer().array().length);

    INDArray out = null;
    try {
        out = new NativeImageLoader(conf.getCroppingHeight(), conf.getCroppingWidth()).asMatrix(cropped);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //System.out.println(out.shapeInfoToString());
    out = out.reshape(1, conf.getCroppingHeight(), conf.getCroppingWidth());
    INDArray compressed = out.castTo(DataType.UBYTE);
    return compressed;
}
 
Example #12
Source File: NumpyArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Builder
public NumpyArray(long address, long[] shape, long strides[], DataType dtype, boolean copy) {
    this.address = address;
    this.shape = shape;
    this.strides = strides;
    this.dtype = dtype;
    setND4JArray();
    if (copy) {
        nd4jArray = nd4jArray.dup();
        Nd4j.getAffinityManager().ensureLocation(nd4jArray, AffinityManager.Location.HOST);
        this.address = nd4jArray.data().address();
    }
}
 
Example #13
Source File: NumpyArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private void setND4JArray() {

        long size = 1;
        for (long d : shape) {
            size *= d;
        }

        String cacheKey = address + "_" + size + "_" + dtype + "_" + ArrayUtils.toString(strides);
        nd4jArray = arrayCache.get(cacheKey);
        if (nd4jArray == null) {
            Pointer ptr = nativeOps.pointerForAddress(address);
            ptr = ptr.limit(size);
            ptr = ptr.capacity(size);
            DataBuffer buff = Nd4j.createBuffer(ptr, size, dtype);

            int elemSize = buff.getElementSize();
            long[] nd4jStrides = new long[strides.length];
            for (int i = 0; i < strides.length; i++) {
                nd4jStrides[i] = strides[i] / elemSize;
            }

            nd4jArray = Nd4j.create(buff, shape, nd4jStrides, 0, Shape.getOrder(shape, nd4jStrides, 1), dtype);
            arrayCache.put(cacheKey, nd4jArray);
        }
        else{
            if (!Arrays.equals(nd4jArray.shape(), shape)){
                nd4jArray = nd4jArray.reshape(shape);
            }
        }
        Nd4j.getAffinityManager().ensureLocation(nd4jArray, AffinityManager.Location.HOST);
    }
 
Example #14
Source File: CpuFlexibleThreshold.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[]{1, (int) buffer.length()}).getFirst());
    double max = temp.amaxNumber().doubleValue();

    int cntAbs = temp.scan(Conditions.absGreaterThanOrEqual(max - (max * threshold))).intValue();

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold)); // please note, this value will be ovewritten anyway
    pointer.put(3, 0);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());

    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.FTHRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #15
Source File: TestTFKerasModelImport.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private void testModelImportWithKeras(String path) throws Exception{
    Model kerasModel = new Model(path);
    ComputationGraph dl4jModel = KerasModelImport.importKerasModelAndWeights(path);
    Assert.assertEquals(kerasModel.numInputs(), dl4jModel.getNumInputArrays());
    Assert.assertEquals(kerasModel.numOutputs(), dl4jModel.getNumOutputArrays());
    INDArray[] kerasInputArrays = new INDArray[kerasModel.numInputs()];
    INDArray[] dl4jInputArrays = new INDArray[kerasModel.numInputs()];

    for (int i = 0; i < kerasInputArrays.length; i ++) {
        long[] shape = kerasModel.inputShapeAt(i);
        for (int j = 0; j < shape.length; j++) {
            if (shape[j] < 0) {
                shape[j] = 1;
            }
        }

        kerasInputArrays[i] = Nd4j.rand(shape);
    }

    INDArray[] kerasOut = kerasModel.predict(kerasInputArrays);
    INDArray[] dl4jOut = dl4jModel.output(dl4jInputArrays);

    Assert.assertEquals(kerasOut.length, dl4jOut.length);

    for (int i = 0; i < kerasOut.length; i++){
        INDArray kerasOutArr = kerasOut[i];
        kerasOutArr = kerasOutArr.reshape(1, -1);// bit of relaxation on shape
        kerasOutArr= kerasOutArr.castTo(DataType.DOUBLE);
        Nd4j.getAffinityManager().ensureLocation(dl4jOut[i], AffinityManager.Location.HOST);
        INDArray dl4jOutArr = dl4jOut[i].reshape(1, -1);
        System.out.println(kerasOutArr.shapeInfoToString());
        System.out.println(dl4jOutArr.shapeInfoToString());
        Assert.assertEquals(kerasOutArr, dl4jOutArr);
    }
}
 
Example #16
Source File: CudaWorkspaceTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircularWorkspaceAsymmetry_1() {
    // circular workspace mode
    val configuration = WorkspaceConfiguration.builder().initialSize(10 * 1024 * 1024)
            .policyReset(ResetPolicy.ENDOFBUFFER_REACHED).policyAllocation(AllocationPolicy.STRICT)
            .policySpill(SpillPolicy.FAIL).policyLearning(LearningPolicy.NONE).build();


    try (val ws = (CudaWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "circular_ws")) {
        val array = Nd4j.create(DataType.FLOAT, 10, 10);

        assertEquals(0, ws.getHostOffset());
        assertNotEquals(0, ws.getDeviceOffset());

        // we expect that this array has no data/buffer on HOST side
        assertEquals(AffinityManager.Location.DEVICE, Nd4j.getAffinityManager().getActiveLocation(array));

        // since this array doesn't have HOST buffer - it will allocate one now
        array.getDouble(3L);

        assertEquals(ws.getHostOffset(), ws.getDeviceOffset());
    }

    try (val ws = (CudaWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "circular_ws")) {
        assertEquals(ws.getHostOffset(), ws.getDeviceOffset());
    }

    Nd4j.getWorkspaceManager().destroyAllWorkspacesForCurrentThread();
}
 
Example #17
Source File: CpuFlexibleThreshold.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new long[]{1, buffer.length()}, DataType.INT).getFirst());
    double max = temp.amaxNumber().doubleValue();

    int cntAbs = temp.scan(Conditions.absGreaterThanOrEqual(max - (max * threshold))).intValue();

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold)); // please note, this value will be ovewritten anyway
    pointer.put(3, 0);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());

    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataTypeEx.FTHRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #18
Source File: CudaFlexibleThreshold.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[]{1, (int) buffer.length()}));
    double max = temp.amaxNumber().doubleValue();

    int cntAbs = temp.scan(Conditions.absGreaterThanOrEqual(max - (max * threshold))).intValue();

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 3;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold)); // please note, this value will be ovewritten anyway

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());

    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.FTHRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #19
Source File: CpuThreshold.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new long[]{1, buffer.length()}, buffer.dataType()).getFirst());
    MatchCondition condition = new MatchCondition(temp, Conditions.absGreaterThanOrEqual(threshold));
    int cntAbs = Nd4j.getExecutioner().exec(condition).getInt(0);


    //log.info("density ratio: {}", String.format("%.2f", cntAbs * 100.0f / buffer.length()));

    if (cntAbs < 2)
        return null;

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold));
    pointer.put(3, 0);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());



    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataTypeEx.THRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #20
Source File: BaseNativeNDArrayFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public Pointer convertToNumpy(INDArray array) {
    val size = new LongPointer(1);
    Pointer header = NativeOpsHolder
            .getInstance().getDeviceNativeOps()
            .numpyHeaderForNd4j(
                    array.data().pointer(),
                    array.shapeInfoDataBuffer().pointer(),
                    array.data().getElementSize()
                    ,size);

    val headerSize = size.get() - 1;
    header.capacity(headerSize);
    header.position(0);



    BytePointer bytePointer = new BytePointer((int) (headerSize + (array.data().getElementSize() * array.data().length())));
    BytePointer headerCast = new BytePointer(header);
    val indexer = ByteIndexer.create(headerCast);
    int pos = 0;
    bytePointer.position(pos);
    Pointer.memcpy(bytePointer, headerCast,headerCast.capacity());
    pos += (headerCast.capacity());
    bytePointer.position(pos);

    // make sure data is copied to the host memory
    Nd4j.getAffinityManager().ensureLocation(array, AffinityManager.Location.HOST);

    Pointer.memcpy(bytePointer,array.data().pointer(),(array.data().getElementSize() * array.data().length()));
    bytePointer.position(0);
    return bytePointer;
}
 
Example #21
Source File: CaptchaLoader.java    From twse-captcha-solver-dl4j with MIT License 5 votes vote down vote up
public MultiDataSet convertDataSet(int num) throws Exception {
   int batchNumCount = 0;

   INDArray[] featuresMask = null;
   INDArray[] labelMask = null;

   List<MultiDataSet> multiDataSets = new ArrayList<>();

   while (batchNumCount != num && fileIterator.hasNext()) {
     File image = fileIterator.next();
     String imageName = image.getName().substring(0, image.getName().lastIndexOf('.'));
     String[] imageNames = imageName.split("");
     INDArray feature = asMatrix(image);
     INDArray[] features = new INDArray[] {feature};
     INDArray[] labels = new INDArray[5];

     Nd4j.getAffinityManager().ensureLocation(feature, AffinityManager.Location.DEVICE);
     for (int i = 0; i < imageNames.length; i++) {
int digit = labelList.indexOf(imageNames[i]);
labels[i] = Nd4j.zeros(1, labelList.size()).putScalar(new int[] {0, digit}, 1);
     }

     feature = feature.muli(1.0 / 255.0);

     multiDataSets.add(new MultiDataSet(features, labels, featuresMask, labelMask));

     batchNumCount++;
   }

   MultiDataSet result = MultiDataSet.merge(multiDataSets);
   return result;
 }
 
Example #22
Source File: NumpyArray.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray toJava(PythonObject pythonObject) {
    log.info("Converting PythonObject to INDArray...");
    PyObject np = PyImport_ImportModule("numpy");
    PyObject ndarray = PyObject_GetAttrString(np, "ndarray");
    if (PyObject_IsInstance(pythonObject.getNativePythonObject(), ndarray) != 1) {
        Py_DecRef(ndarray);
        Py_DecRef(np);
        throw new PythonException("Object is not a numpy array! Use Python.ndarray() to convert object to a numpy array.");
    }
    Py_DecRef(ndarray);
    Py_DecRef(np);
    PyArrayObject npArr = new PyArrayObject(pythonObject.getNativePythonObject());
    long[] shape = new long[PyArray_NDIM(npArr)];
    SizeTPointer shapePtr = PyArray_SHAPE(npArr);
    if (shapePtr != null)
        shapePtr.get(shape, 0, shape.length);
    long[] strides = new long[shape.length];
    SizeTPointer stridesPtr = PyArray_STRIDES(npArr);
    if (stridesPtr != null)
        stridesPtr.get(strides, 0, strides.length);
    int npdtype = PyArray_TYPE(npArr);

    DataType dtype;
    switch (npdtype) {
        case NPY_DOUBLE:
            dtype = DataType.DOUBLE;
            break;
        case NPY_FLOAT:
            dtype = DataType.FLOAT;
            break;
        case NPY_SHORT:
            dtype = DataType.SHORT;
            break;
        case NPY_INT:
            dtype = DataType.INT32;
            break;
        case NPY_LONG:
            dtype = DataType.INT64;
            break;
        case NPY_UINT:
            dtype = DataType.UINT32;
            break;
        case NPY_BYTE:
            dtype = DataType.INT8;
            break;
        case NPY_UBYTE:
            dtype = DataType.UINT8;
            break;
        case NPY_BOOL:
            dtype = DataType.BOOL;
            break;
        case NPY_HALF:
            dtype = DataType.FLOAT16;
            break;
        case NPY_LONGLONG:
            dtype = DataType.INT64;
            break;
        case NPY_USHORT:
            dtype = DataType.UINT16;
            break;
        case NPY_ULONG:
        case NPY_ULONGLONG:
            dtype = DataType.UINT64;
            break;
        default:
            throw new PythonException("Unsupported array data type: " + npdtype);
    }
    long size = 1;
    for (int i = 0; i < shape.length; size *= shape[i++]) ;

    INDArray ret;
    long address = PyArray_DATA(npArr).address();
    String key = address + "_" + size + "_" + dtype;
    DataBuffer buff = cache.get(key);
    if (buff == null) {
        try (MemoryWorkspace ws = Nd4j.getMemoryManager().scopeOutOfWorkspaces()) {
            Pointer ptr = NativeOpsHolder.getInstance().getDeviceNativeOps().pointerForAddress(address);
            ptr = ptr.limit(size);
            ptr = ptr.capacity(size);
            buff = Nd4j.createBuffer(ptr, size, dtype);
            cache.put(key, buff);
        }
    }
    int elemSize = buff.getElementSize();
    long[] nd4jStrides = new long[strides.length];
    for (int i = 0; i < strides.length; i++) {
        nd4jStrides[i] = strides[i] / elemSize;
    }
    ret = Nd4j.create(buff, shape, nd4jStrides, 0, Shape.getOrder(shape, nd4jStrides, 1), dtype);
    Nd4j.getAffinityManager().tagLocation(ret, AffinityManager.Location.HOST);
    log.info("Done.");
    return ret;


}
 
Example #23
Source File: GraphTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Test
public void testPropagate() throws Exception {
    MathCache factory = new Nd4jCache();

    EnvironmentContext context = EnvironmentFactory.getContext();
    Future<?> task = context.doTask(() -> {
        ComputationGraph oldGraph = getOldFunction();
        Graph graph = getNewFunction(factory, oldGraph);

        int size = 5;
        INDArray oldLeftInputs = Nd4j.zeros(size, 1);
        INDArray oldRightInputs = Nd4j.zeros(size, 1);
        INDArray oldMarks = Nd4j.zeros(size, 1).assign(5);
        for (int point = 0; point < 5; point++) {
            oldLeftInputs.put(point, 0, RandomUtility.randomInteger(5));
            oldRightInputs.put(point, 0, RandomUtility.randomInteger(5));
        }
        for (int index = 0; index < 50; index++) {
            oldGraph.setInputs(oldLeftInputs, oldRightInputs);
            oldGraph.setLabels(oldMarks);
            // 设置fit过程的迭代次数
            for (int iteration = 0; iteration < 2; iteration++) {
                oldGraph.fit();
                double oldScore = oldGraph.score();
                System.out.println(oldScore);
            }
        }
        INDArray oldOutputs = oldGraph.outputSingle(oldLeftInputs, oldRightInputs);
        System.out.println(oldOutputs);

        AffinityManager manager = Nd4j.getAffinityManager();
        manager.attachThreadToDevice(Thread.currentThread(), 0);
        MathMatrix newLeftInputs = getMatrix(factory, oldLeftInputs);
        MathMatrix newRightInputs = getMatrix(factory, oldRightInputs);
        MathMatrix newMarks = getMatrix(factory, oldMarks);
        MathMatrix newOutputs = getMatrix(factory, oldOutputs);

        for (int index = 0; index < 50; index++) {
            double newScore = graph.practice(2, new MathMatrix[] { newLeftInputs, newRightInputs }, new MathMatrix[] { newMarks });
            System.out.println(newScore);
        }

        graph.predict(new MathMatrix[] { newLeftInputs, newRightInputs }, new MathMatrix[] { newOutputs });
        System.out.println(newOutputs);
        Assert.assertTrue(equalMatrix(newOutputs, oldOutputs));
    });
    task.get();
}
 
Example #24
Source File: DataBufferTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testEnsureLocation(){
    //https://github.com/eclipse/deeplearning4j/issues/8783
    Nd4j.create(1);

    BytePointer bp = new BytePointer(5);


    Pointer ptr = NativeOpsHolder.getInstance().getDeviceNativeOps().pointerForAddress(bp.address());
    DataBuffer buff = Nd4j.createBuffer(ptr, 5, DataType.INT8);


    INDArray arr2 = Nd4j.create(buff, new long[]{5}, new long[]{1}, 0, 'c', DataType.INT8);
    long before = arr2.data().pointer().address();
    Nd4j.getAffinityManager().ensureLocation(arr2, AffinityManager.Location.HOST);
    long after = arr2.data().pointer().address();

    assertEquals(before, after);
}
 
Example #25
Source File: DataBufferTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testAsBytes() {
    INDArray orig = Nd4j.linspace(DataType.INT, 0, 10, 1);

    for (DataType dt : new DataType[]{DataType.DOUBLE, DataType.FLOAT, DataType.HALF, DataType.BFLOAT16,
            DataType.LONG, DataType.INT, DataType.SHORT, DataType.BYTE, DataType.BOOL,
            DataType.UINT64, DataType.UINT32, DataType.UINT16, DataType.UBYTE}) {
        INDArray arr = orig.castTo(dt);

        byte[] b = arr.data().asBytes();        //NOTE: BIG ENDIAN

        if(ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
            //Switch from big endian (as defined by asBytes which uses big endian) to little endian
            int w = dt.width();
            if (w > 1) {
                int len = b.length / w;
                for (int i = 0; i < len; i++) {
                    for (int j = 0; j < w / 2; j++) {
                        byte temp = b[(i + 1) * w - j - 1];
                        b[(i + 1) * w - j - 1] = b[i * w + j];
                        b[i * w + j] = temp;
                    }
                }
            }
        }

        INDArray arr2 = Nd4j.create(dt, arr.shape());
        ByteBuffer bb = arr2.data().pointer().asByteBuffer();
        bb.position(0);
        bb.put(b);

        Nd4j.getAffinityManager().tagLocation(arr2, AffinityManager.Location.HOST);

        assertEquals(arr.toString(), arr2.toString());
        assertEquals(arr, arr2);

        //Sanity check on data buffer getters:
        DataBuffer db = arr.data();
        DataBuffer db2 = arr2.data();
        for( int i=0; i<10; i++ ){
            assertEquals(db.getDouble(i), db2.getDouble(i), 0);
            assertEquals(db.getFloat(i), db2.getFloat(i), 0);
            assertEquals(db.getInt(i), db2.getInt(i), 0);
            assertEquals(db.getLong(i), db2.getLong(i), 0);
            assertEquals(db.getNumber(i), db2.getNumber(i));
        }

        assertArrayEquals(db.getDoublesAt(0, 10), db2.getDoublesAt(0, 10), 0);
        assertArrayEquals(db.getFloatsAt(0, 10), db2.getFloatsAt(0, 10), 0);
        assertArrayEquals(db.getIntsAt(0, 10), db2.getIntsAt(0, 10));
        assertArrayEquals(db.getLongsAt(0, 10), db2.getLongsAt(0, 10));
    }
}
 
Example #26
Source File: NumpyArray.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public INDArray getNd4jArray(){
    Nd4j.getAffinityManager().tagLocation(nd4jArray, AffinityManager.Location.HOST);
    return nd4jArray;
}
 
Example #27
Source File: ObjectDetectionRecordReader.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public List<List<Writable>> next(int num) {
    List<File> files = new ArrayList<>(num);
    List<List<ImageObject>> objects = new ArrayList<>(num);

    for (int i = 0; i < num && hasNext(); i++) {
        File f = iter.next();
        this.currentFile = f;
        if (!f.isDirectory()) {
            files.add(f);
            objects.add(labelProvider.getImageObjectsForPath(f.getPath()));
        }
    }

    int nClasses = labels.size();

    INDArray outImg = Nd4j.create(files.size(), channels, height, width);
    INDArray outLabel = Nd4j.create(files.size(), 4 + nClasses, gridH, gridW);

    int exampleNum = 0;
    for (int i = 0; i < files.size(); i++) {
        File imageFile = files.get(i);
        this.currentFile = imageFile;
        try {
            this.invokeListeners(imageFile);
            Image image = this.imageLoader.asImageMatrix(imageFile);
            this.currentImage = image;
            Nd4j.getAffinityManager().ensureLocation(image.getImage(), AffinityManager.Location.DEVICE);

            outImg.put(new INDArrayIndex[]{point(exampleNum), all(), all(), all()}, image.getImage());

            List<ImageObject> objectsThisImg = objects.get(exampleNum);

            label(image, objectsThisImg, outLabel, exampleNum);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        exampleNum++;
    }

    if(!nchw) {
        outImg = outImg.permute(0, 2, 3, 1);        //NCHW to NHWC
        outLabel = outLabel.permute(0, 2, 3, 1);
    }
    return new NDArrayRecordBatch(Arrays.asList(outImg, outLabel));
}
 
Example #28
Source File: NativeOpExecutioner.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray thresholdEncode(INDArray input, double threshold, Integer boundary) {

    MatchCondition condition = new MatchCondition(input, Conditions.absGreaterThanOrEqual(threshold));
    int cntAbs = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);

    if (cntAbs < 2)
        return null;

    if (boundary != null)
        cntAbs = Math.min(cntAbs, boundary);

    DataBuffer buffer = input.data();

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header

    DataBuffer encodedBuffer = Nd4j.getMemoryManager().getCurrentWorkspace() == null ? Nd4j.getDataBufferFactory().createInt(4+cntAbs, false) : Nd4j.getDataBufferFactory().createInt(4+cntAbs, false, Nd4j.getMemoryManager().getCurrentWorkspace());

    encodedBuffer.put(0, cntAbs);
    encodedBuffer.put(1, (int) buffer.length());
    encodedBuffer.put(2, Float.floatToIntBits((float) threshold));

    // format id
    encodedBuffer.put(3, ThresholdCompression.FLEXIBLE_ENCODING);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm("THRESHOLD");
    descriptor.setCompressionType(CompressionType.LOSSLESS);

    //CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(AbstractCompressor.getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.THRESHOLD, encodedBuffer.addressPointer(), buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return Nd4j.createArrayFromShapeBuffer(encodedBuffer, input.shapeInfoDataBuffer());
}
 
Example #29
Source File: ObjectDetectionRecordReader.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public List<List<Writable>> next(int num) {
    List<File> files = new ArrayList<>(num);
    List<List<ImageObject>> objects = new ArrayList<>(num);

    for (int i = 0; i < num && hasNext(); i++) {
        File f = iter.next();
        this.currentFile = f;
        if (!f.isDirectory()) {
            files.add(f);
            objects.add(labelProvider.getImageObjectsForPath(f.getPath()));
        }
    }

    int nClasses = labels.size();

    INDArray outImg = Nd4j.create(files.size(), channels, height, width);
    INDArray outLabel = Nd4j.create(files.size(), 4 + nClasses, gridH, gridW);

    int exampleNum = 0;
    for (int i = 0; i < files.size(); i++) {
        File imageFile = files.get(i);
        this.currentFile = imageFile;
        try {
            this.invokeListeners(imageFile);
            Image image = this.imageLoader.asImageMatrix(imageFile);
            this.currentImage = image;
            Nd4j.getAffinityManager().ensureLocation(image.getImage(), AffinityManager.Location.DEVICE);

            outImg.put(new INDArrayIndex[]{point(exampleNum), all(), all(), all()}, image.getImage());

            List<ImageObject> objectsThisImg = objects.get(exampleNum);

            label(image, objectsThisImg, outLabel, exampleNum);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        exampleNum++;
    }

    return new NDArrayRecordBatch(Arrays.asList(outImg, outLabel));
}