Java Code Examples for org.nd4j.linalg.api.buffer.DataBuffer#AllocationMode

The following examples show how to use org.nd4j.linalg.api.buffer.DataBuffer#AllocationMode . 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: AllocUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the name of the alocation mode
 * @param allocationMode
 * @return
 */
public static String getAllocModeName(DataBuffer.AllocationMode allocationMode) {
    switch (allocationMode) {
        case HEAP:
            return "heap";
        case JAVACPP:
            return "javacpp";
        case DIRECT:
            return "direct";
        default:
            return "javacpp";
    }
}
 
Example 2
Source File: AllocUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get the allocation mode from the context
 * @return
 */
public static DataBuffer.AllocationMode getAllocationModeFromContext(String allocMode) {
    switch (allocMode) {
        case "heap":
            return DataBuffer.AllocationMode.HEAP;
        case "javacpp":
            return DataBuffer.AllocationMode.JAVACPP;
        case "direct":
            return DataBuffer.AllocationMode.DIRECT;
        default:
            return DataBuffer.AllocationMode.JAVACPP;
    }
}
 
Example 3
Source File: AllocUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get the allocation mode from the context
 * @return
 */
public static DataBuffer.AllocationMode getAllocationModeFromContext(String allocMode) {
    switch (allocMode) {
        case "heap":
            return DataBuffer.AllocationMode.HEAP;
        case "javacpp":
            return DataBuffer.AllocationMode.JAVACPP;
        case "direct":
            return DataBuffer.AllocationMode.DIRECT;
        default:
            return DataBuffer.AllocationMode.JAVACPP;
    }
}
 
Example 4
Source File: AllocUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the name of the alocation mode
 * @param allocationMode
 * @return
 */
public static String getAllocModeName(DataBuffer.AllocationMode allocationMode) {
    switch (allocationMode) {
        case HEAP:
            return "heap";
        case JAVACPP:
            return "javacpp";
        case DIRECT:
            return "direct";
        default:
            return "javacpp";
    }
}
 
Example 5
Source File: CudaDataBufferFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer.AllocationMode allocationMode() {
    if (allocationMode == null) {
        String otherAlloc = System.getProperty("alloc");
        if (otherAlloc.equals("heap"))
            setAllocationMode(DataBuffer.AllocationMode.HEAP);
        else if (otherAlloc.equals("direct"))
            setAllocationMode(DataBuffer.AllocationMode.DIRECT);
        else if (otherAlloc.equals("javacpp"))
            setAllocationMode(DataBuffer.AllocationMode.JAVACPP);
    }
    return allocationMode;
}
 
Example 6
Source File: ConvolutionTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testCompareIm2Col() throws Exception {

    int[] miniBatches = {1, 3, 5};
    int[] depths = {1, 3, 5};
    int[] inHeights = {5, 21};
    int[] inWidths = {5, 21};
    int[] strideH = {1, 2};
    int[] strideW = {1, 2};
    int[] sizeW = {1, 2, 3};
    int[] sizeH = {1, 2, 3};
    int[] padH = {0, 1, 2};
    int[] padW = {0, 1, 2};

    DataBuffer.Type[] types = new DataBuffer.Type[] {DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE,
                    DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE};
    DataBuffer.AllocationMode[] modes =
                    new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.HEAP,
                                    DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT};

    String factoryClassName = Nd4j.factory().getClass().toString().toLowerCase();
    if (factoryClassName.contains("jcublas") || factoryClassName.contains("cuda")) {
        //Only test direct for CUDA; test all for CPU
        types = new DataBuffer.Type[] {DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE};
        modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.DIRECT,
                        DataBuffer.AllocationMode.DIRECT};
    }

    DataBuffer.Type inititalType = Nd4j.dataType();
    for (int i = 0; i < types.length; i++) {
        DataBuffer.Type type = types[i];
        DataBuffer.AllocationMode mode = modes[i];

        DataTypeUtil.setDTypeForContext(type);
        Nd4j.alloc = mode;

        for (int m : miniBatches) {
            for (int d : depths) {
                for (int h : inHeights) {
                    for (int w : inWidths) {
                        for (int sh : strideH) {
                            for (int sw : strideW) {
                                for (int kh : sizeH) {
                                    for (int kw : sizeW) {
                                        for (int ph : padH) {
                                            for (int pw : padW) {
                                                System.out.println("Before assertion");
                                                if ((w - kw + 2 * pw) % sw != 0 || (h - kh + 2 * ph) % sh != 0)
                                                    continue; //(w-kp+2*pw)/sw + 1 is not an integer, i.e., number of outputs doesn't fit

                                                INDArray in = Nd4j.rand(new int[] {m, d, h, w});
                                                assertEquals(in.data().allocationMode(), mode);
                                                assertEquals(in.data().dataType(), type);
                                                INDArray im2col = Convolution.im2col(in, kh, kw, sh, sw, ph, pw,
                                                                false); //Cheating, to get correct shape for input

                                                INDArray imgOutOld =
                                                                OldConvolution.col2im(im2col, sh, sw, ph, pw, h, w);
                                                INDArray imgOutNew =
                                                                Convolution.col2im(im2col, sh, sw, ph, pw, h, w);
                                                System.out.println("F order test");
                                                System.out.println(imgOutOld);
                                                System.out.println(imgOutNew);
                                                assertEquals(imgOutOld, imgOutNew);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    DataTypeUtil.setDTypeForContext(inititalType);
}
 
Example 7
Source File: ConvolutionTestsC.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testCompareIm2ColImpl() {

    int[] miniBatches = {1, 3, 5};
    int[] depths = {1, 3, 5};
    int[] inHeights = {5, 21};
    int[] inWidths = {5, 21};
    int[] strideH = {1, 2};
    int[] strideW = {1, 2};
    int[] sizeW = {1, 2, 3};
    int[] sizeH = {1, 2, 3};
    int[] padH = {0, 1, 2};
    int[] padW = {0, 1, 2};
    boolean[] coverall = {false, true};

    DataBuffer.Type[] types = new DataBuffer.Type[] {DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE,
                    DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE};
    DataBuffer.AllocationMode[] modes =
                    new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.HEAP,
                                    DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT};

    String factoryClassName = Nd4j.factory().getClass().toString().toLowerCase();
    if (factoryClassName.contains("jcublas") || factoryClassName.contains("cuda")) {
        //Only test direct for CUDA; test all for CPU
        types = new DataBuffer.Type[] {DataBuffer.Type.FLOAT, DataBuffer.Type.DOUBLE};
        modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.DIRECT,
                        DataBuffer.AllocationMode.DIRECT};
    }

    DataBuffer.Type initialType = Nd4j.dataType();
    for (int i = 0; i < types.length; i++) {
        DataBuffer.Type type = types[i];
        DataBuffer.AllocationMode mode = modes[i];

        DataTypeUtil.setDTypeForContext(type);
        Nd4j.alloc = mode;

        AllocUtil.setAllocationModeForContext(mode);

        for (int m : miniBatches) {
            for (int d : depths) {
                for (int h : inHeights) {
                    for (int w : inWidths) {
                        for (int sh : strideH) {
                            for (int sw : strideW) {
                                for (int kh : sizeH) {
                                    for (int kw : sizeW) {
                                        for (int ph : padH) {
                                            for (int pw : padW) {
                                                if ((w - kw + 2 * pw) % sw != 0 || (h - kh + 2 * ph) % sh != 0)
                                                    continue; //(w-kp+2*pw)/sw + 1 is not an integer,  i.e., number of outputs doesn't fit

                                                System.out.println("Running " + m + " " + d + " " + h + " " + w);
                                                for (boolean cAll : coverall) {

                                                    INDArray in = Nd4j.rand(new int[] {m, d, h, w});
                                                    //assertEquals(in.data().allocationMode(), mode);
                                                    //assertEquals(in.data().dataType(), opType);

                                                    INDArray outOrig = OldConvolution.im2col(in, kh, kw, sh, sw, ph,
                                                                    pw, -1, cAll); //Old implementation
                                                    INDArray outNew = Convolution.im2col(in, kh, kw, sh, sw, ph, pw,
                                                                    cAll); //Current implementation

                                                    assertEquals(outOrig, outNew);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    DataTypeUtil.setDTypeForContext(initialType);
}
 
Example 8
Source File: ConvolutionTestsC.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testCompareIm2ColImpl() {

    int[] miniBatches = {1, 3, 5};
    int[] depths = {1, 3, 5};
    int[] inHeights = {5, 21};
    int[] inWidths = {5, 21};
    int[] strideH = {1, 2};
    int[] strideW = {1, 2};
    int[] sizeW = {1, 2, 3};
    int[] sizeH = {1, 2, 3};
    int[] padH = {0, 1, 2};
    int[] padW = {0, 1, 2};
    boolean[] coverall = {false, true};

    DataType[] types = new DataType[] {DataType.FLOAT, DataType.DOUBLE,
                    DataType.FLOAT, DataType.DOUBLE};
    DataBuffer.AllocationMode[] modes =
                    new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.HEAP,
                                    DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT};

    String factoryClassName = Nd4j.factory().getClass().toString().toLowerCase();
    if (factoryClassName.contains("jcublas") || factoryClassName.contains("cuda")) {
        //Only test direct for CUDA; test all for CPU
        types = new DataType[] {DataType.FLOAT, DataType.DOUBLE};
        modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.DIRECT,
                        DataBuffer.AllocationMode.DIRECT};
    }

    DataType initialType = Nd4j.dataType();
    for (int i = 0; i < types.length; i++) {
        DataType type = types[i];
        DataBuffer.AllocationMode mode = modes[i];

        DataTypeUtil.setDTypeForContext(type);
        Nd4j.alloc = mode;

        AllocUtil.setAllocationModeForContext(mode);

        for (int m : miniBatches) {
            for (int d : depths) {
                for (int h : inHeights) {
                    for (int w : inWidths) {
                        for (int sh : strideH) {
                            for (int sw : strideW) {
                                for (int kh : sizeH) {
                                    for (int kw : sizeW) {
                                        for (int ph : padH) {
                                            for (int pw : padW) {
                                                if ((w - kw + 2 * pw) % sw != 0 || (h - kh + 2 * ph) % sh != 0)
                                                    continue; //(w-kp+2*pW)/sw + 1 is not an integer,  i.e., number of outputs doesn't fit

                                                System.out.println("Running " + m + " " + d + " " + h + " " + w);
                                                for (boolean cAll : coverall) {

                                                    INDArray in = Nd4j.rand(new int[] {m, d, h, w});
                                                    //assertEquals(in.data().allocationMode(), mode);
                                                    //assertEquals(in.data().dataType(), opType);

                                                    INDArray outOrig = OldConvolution.im2col(in, kh, kw, sh, sw, ph,
                                                                    pw, -1, cAll); //Old implementation
                                                    INDArray outNew = Convolution.im2col(in, kh, kw, sh, sw, ph, pw,
                                                                    cAll); //Current implementation

                                                    assertEquals(outOrig, outNew);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    DataTypeUtil.setDTypeForContext(initialType);
}
 
Example 9
Source File: CudaDataBufferFactory.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public void setAllocationMode(DataBuffer.AllocationMode allocationMode) {
    this.allocationMode = allocationMode;
}
 
Example 10
Source File: CudaDataBufferFactory.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void setAllocationMode(DataBuffer.AllocationMode allocationMode) {
    this.allocationMode = allocationMode;
}
 
Example 11
Source File: ConvolutionTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testCompareIm2ColImpl() {

    int[] miniBatches = {1, 3, 5};
    int[] depths = {1, 3, 5};
    int[] inHeights = {5, 21};
    int[] inWidths = {5, 21};
    int[] strideH = {1, 2};
    int[] strideW = {1, 2};
    int[] sizeW = {1, 2, 3};
    int[] sizeH = {1, 2, 3};
    int[] padH = {0, 1, 2};
    int[] padW = {0, 1, 2};
    boolean[] coverall = {false, true};

    DataType[] types = new DataType[] {DataType.FLOAT, DataType.FLOAT,
                    DataType.FLOAT, DataType.FLOAT};
    DataBuffer.AllocationMode[] modes =
                    new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.HEAP,
                                    DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT};

    String factoryClassName = Nd4j.factory().getClass().toString().toLowerCase();
    if (factoryClassName.contains("jcublas") || factoryClassName.contains("cuda")) {
        //Only test direct for CUDA; test all for CPU
        types = new DataType[] {DataType.FLOAT, DataType.FLOAT};
        modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.DIRECT,
                        DataBuffer.AllocationMode.DIRECT};
    }

    DataType initialType = Nd4j.dataType();
    for (int i = 0; i < types.length; i++) {
        DataType type = types[i];
        DataBuffer.AllocationMode mode = modes[i];

        DataTypeUtil.setDTypeForContext(type);
        Nd4j.alloc = mode;

        AllocUtil.setAllocationModeForContext(mode);

        for (int m : miniBatches) {
            for (int d : depths) {
                for (int h : inHeights) {
                    for (int w : inWidths) {
                        for (int sh : strideH) {
                            for (int sw : strideW) {
                                for (int kh : sizeH) {
                                    for (int kw : sizeW) {
                                        for (int ph : padH) {
                                            for (int pw : padW) {
                                                if ((w - kw + 2 * pw) % sw != 0 || (h - kh + 2 * ph) % sh != 0)
                                                    continue; //(w-kp+2*pW)/sw + 1 is not an integer,  i.e., number of outputs doesn't fit

                                                System.out.println("Running " + m + " " + d + " " + h + " " + w);
                                                for (boolean cAll : coverall) {

                                                    INDArray in = Nd4j.rand(new int[] {m, d, h, w});
                                                    //assertEquals(in.data().allocationMode(), mode);
                                                    //assertEquals(in.data().dataType(), opType);

                                                    INDArray outOrig = OldConvolution.im2col(in, kh, kw, sh, sw, ph,
                                                                    pw, -1, cAll); //Old implementation
                                                    INDArray outNew = Convolution.im2col(in, kh, kw, sh, sw, ph, pw,
                                                                    cAll); //Current implementation

                                                    assertArrayEquals(outOrig.data().asFloat(),
                                                                    outNew.data().asFloat(), 0.01f);
                                                    assertEquals(outOrig, outNew);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    DataTypeUtil.setDTypeForContext(initialType);
}
 
Example 12
Source File: ConvolutionTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testCompareIm2Col() {

    int[] miniBatches = {1, 3, 5};
    int[] depths = {1, 3, 5};
    int[] inHeights = {5, 21};
    int[] inWidths = {5, 21};
    int[] strideH = {1, 2};
    int[] strideW = {1, 2};
    int[] sizeW = {1, 2, 3};
    int[] sizeH = {1, 2, 3};
    int[] padH = {0, 1, 2};
    int[] padW = {0, 1, 2};

    DataType[] types = new DataType[] {DataType.FLOAT, DataType.FLOAT,
                    DataType.FLOAT, DataType.FLOAT};
    DataBuffer.AllocationMode[] modes =
                    new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.HEAP, DataBuffer.AllocationMode.HEAP,
                                    DataBuffer.AllocationMode.DIRECT, DataBuffer.AllocationMode.DIRECT};

    String factoryClassName = Nd4j.factory().getClass().toString().toLowerCase();
    if (factoryClassName.contains("jcublas") || factoryClassName.contains("cuda")) {
        //Only test direct for CUDA; test all for CPU
        types = new DataType[] {DataType.FLOAT, DataType.FLOAT};
        modes = new DataBuffer.AllocationMode[] {DataBuffer.AllocationMode.DIRECT,
                        DataBuffer.AllocationMode.DIRECT};
    }

    DataType inititalType = Nd4j.dataType();
    for (int i = 0; i < types.length; i++) {
        DataType type = types[i];
        DataBuffer.AllocationMode mode = modes[i];

        DataTypeUtil.setDTypeForContext(type);
        Nd4j.alloc = mode;

        for (int m : miniBatches) {
            for (int d : depths) {
                for (int h : inHeights) {
                    for (int w : inWidths) {
                        for (int sh : strideH) {
                            for (int sw : strideW) {
                                for (int kh : sizeH) {
                                    for (int kw : sizeW) {
                                        for (int ph : padH) {
                                            for (int pw : padW) {
                                                System.out.println("Before assertion");
                                                if ((w - kw + 2 * pw) % sw != 0 || (h - kh + 2 * ph) % sh != 0)
                                                    continue; //(w-kp+2*pW)/sw + 1 is not an integer, i.e., number of outputs doesn't fit

                                                INDArray in = Nd4j.rand(new int[] {m, d, h, w});
                                                assertEquals(in.data().allocationMode(), mode);
                                                assertEquals(in.data().dataType(), type);
                                                INDArray im2col = Convolution.im2col(in, kh, kw, sh, sw, ph, pw,
                                                                false); //Cheating, to get correct shape for input

                                                INDArray imgOutOld =
                                                                OldConvolution.col2im(im2col, sh, sw, ph, pw, h, w);
                                                INDArray imgOutNew =
                                                                Convolution.col2im(im2col, sh, sw, ph, pw, h, w);
                                                System.out.println("F order test");
                                                System.out.println(imgOutOld);
                                                System.out.println(imgOutNew);
                                                assertEquals(imgOutOld, imgOutNew);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    DataTypeUtil.setDTypeForContext(inititalType);
}
 
Example 13
Source File: AllocUtil.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Set the allocation mode for the nd4j context
 * The value must be one of: heap, java cpp, or direct
 * or an @link{IllegalArgumentException} is thrown
 * @param allocationModeForContext
 */
public static void setAllocationModeForContext(DataBuffer.AllocationMode allocationModeForContext) {
    setAllocationModeForContext(getAllocModeName(allocationModeForContext));
}
 
Example 14
Source File: DataBufferFactory.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Allocation mode for the data buffer
 * @return
 */
DataBuffer.AllocationMode allocationMode();
 
Example 15
Source File: DataBufferFactory.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Setter for the allocation mode
 * @param allocationMode
 */
void setAllocationMode(DataBuffer.AllocationMode allocationMode);
 
Example 16
Source File: AllocUtil.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * get the allocation mode from the context
 * @return
 */
public static DataBuffer.AllocationMode getAllocationModeFromContext() {
    return DataBuffer.AllocationMode.LONG_SHAPE; //getAllocationModeFromContext(Nd4jContext.getInstance().getConf().getProperty("alloc"));
}
 
Example 17
Source File: DataBufferFactory.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Allocation mode for the data buffer
 * @return
 */
DataBuffer.AllocationMode allocationMode();
 
Example 18
Source File: DataBufferFactory.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Setter for the allocation mode
 * @param allocationMode
 */
void setAllocationMode(DataBuffer.AllocationMode allocationMode);
 
Example 19
Source File: AllocUtil.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Set the allocation mode for the nd4j context
 * The value must be one of: heap, java cpp, or direct
 * or an @link{IllegalArgumentException} is thrown
 * @param allocationModeForContext
 */
public static void setAllocationModeForContext(DataBuffer.AllocationMode allocationModeForContext) {
    setAllocationModeForContext(getAllocModeName(allocationModeForContext));
}
 
Example 20
Source File: AllocUtil.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * get the allocation mode from the context
 * @return
 */
public static DataBuffer.AllocationMode getAllocationModeFromContext() {
    return DataBuffer.AllocationMode.MIXED_DATA_TYPES; //getAllocationModeFromContext(Nd4jContext.getInstance().getConf().getProperty("alloc"));
}