Search in sources :

Example 11 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class NeuralNetworkOpTests method testConv2d.

@Test
public void testConv2d() {
    String scriptStr = "O = conv2d(image, filter, padding=[padH, padW], stride=[strideH, strideW], input_shape=[N,C,H,W], filter_shape=[K,C,R,S])";
    for (long N : Nlst) {
        for (long C : Clst) {
            for (long H : Hlst) {
                for (long W : Wlst) {
                    for (long K : Klst) {
                        for (long R : Rlst) {
                            for (long S : Slst) {
                                for (long strideH : strideHeightLst) {
                                    for (long strideW : strideWidthLst) {
                                        for (long padH : padHeightLst) {
                                            for (long padW : padWidthLst) {
                                                for (double sparsity : sparsitylst) {
                                                    // Make sure ops fit in GPU memory and within constraints of cudnn
                                                    long imageSize = N * C * H * W * 8l;
                                                    if (// image size
                                                    imageSize > MAX_OP_SIZE)
                                                        continue;
                                                    long filterSize = K * C * R * S * 8l;
                                                    if (// filter size
                                                    filterSize > MAX_OP_SIZE)
                                                        continue;
                                                    // filter is smaller than image + padding
                                                    if (R > (H + padH) || S > (W + padW))
                                                        continue;
                                                    int P = (int) ConvolutionUtils.getP(H, R, strideH, padH);
                                                    int Q = (int) ConvolutionUtils.getQ(W, S, strideW, padW);
                                                    long doutSize = N * K * P * Q * 8l;
                                                    if (// dout/output size
                                                    doutSize > MAX_OP_SIZE)
                                                        continue;
                                                    double imageSizeInMB = imageSize / (1024.0 * 1024.0);
                                                    double filterSizeInMB = filterSize / (1024.0 * 1024.0);
                                                    double doutSizeInMB = doutSize / (1024.0 * 1024.0);
                                                    System.out.format("conv2d, image[%d,%d,%d,%d](%.1fMB), filter[%d,%d,%d,%d](%.1f), dout[%d,%d,%d,%d](%.1fMB), stride[%d,%d], padding[%d,%d]", N, C, H, W, imageSizeInMB, N, C, R, S, filterSizeInMB, N, K, P, Q, doutSizeInMB, strideH, strideW, padH, padW);
                                                    Matrix image = generateInputMatrix(spark, (int) N, (int) (C * H * W), sparsity, seed);
                                                    Matrix filter = generateInputMatrix(spark, (int) K, (int) (C * R * S), sparsity, seed);
                                                    HashMap<String, Object> inputs = new HashMap<>();
                                                    inputs.put("N", N);
                                                    inputs.put("C", C);
                                                    inputs.put("H", H);
                                                    inputs.put("W", W);
                                                    inputs.put("K", K);
                                                    inputs.put("R", R);
                                                    inputs.put("S", S);
                                                    inputs.put("strideH", strideH);
                                                    inputs.put("strideW", strideW);
                                                    inputs.put("padH", padH);
                                                    inputs.put("padW", padW);
                                                    inputs.put("image", image);
                                                    inputs.put("filter", filter);
                                                    List<Object> outCPU = runOnCPU(spark, scriptStr, inputs, Arrays.asList("O"));
                                                    List<Object> outGPU = runOnGPU(spark, scriptStr, inputs, Arrays.asList("O"));
                                                    assertHeavyHitterPresent("gpu_conv2d");
                                                    assertEqualObjects(outCPU.get(0), outGPU.get(0));
                                                    clearGPUMemory();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Matrix(org.apache.sysml.api.mlcontext.Matrix) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 12 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class NeuralNetworkOpTests method testMaxPool.

@Test
public void testMaxPool() {
    String scriptStr = "O = max_pool(image, padding=[padH, padW], stride=[strideH, strideW], input_shape=[N,C,H,W], pool_size=[R,S])";
    for (long N : Nlst) {
        for (long C : Clst) {
            for (long H : Hlst) {
                for (long W : Wlst) {
                    for (long R : Rlst) {
                        for (long S : Slst) {
                            for (long strideH : strideHeightLst) {
                                for (long strideW : strideWidthLst) {
                                    for (long padH : padHeightLst) {
                                        for (long padW : padWidthLst) {
                                            for (double sparsity : sparsitylst) {
                                                // pool is smaller than image + padding
                                                if (R > (H + padH) || S > (W + padW))
                                                    continue;
                                                // Make sure ops fit in GPU memory and within constraints of cudnn
                                                long imageSize = N * C * H * W * 8l;
                                                if (// image size
                                                imageSize > MAX_OP_SIZE)
                                                    continue;
                                                long poolSize = R * S * 8l;
                                                if (// filter size
                                                poolSize > MAX_OP_SIZE)
                                                    continue;
                                                int P = (int) ConvolutionUtils.getP(H, R, strideH, padH);
                                                int Q = (int) ConvolutionUtils.getQ(W, S, strideW, padW);
                                                long doutSize = N * C * P * Q * 8l;
                                                if (// dout/output size
                                                doutSize > MAX_OP_SIZE)
                                                    continue;
                                                double imageSizeInMB = imageSize / (1024.0 * 1024.0);
                                                double poolSizeInMB = poolSize / (1024.0 * 1024.0);
                                                double doutSizeInMB = doutSize / (1024.0 * 1024.0);
                                                System.out.format("max_pool, image[%d,%d,%d,%d](%.1fMB), pool[%d,%d](%.1f), dout[%d,%d,%d,%d](%.1fMB), stride[%d,%d], padding[%d,%d]", N, C, H, W, imageSizeInMB, R, S, poolSizeInMB, N, C, P, Q, doutSizeInMB, strideH, strideW, padH, padW);
                                                Matrix image = generateInputMatrix(spark, (int) N, (int) (C * H * W), sparsity, seed);
                                                HashMap<String, Object> inputs = new HashMap<>();
                                                inputs.put("N", N);
                                                inputs.put("C", C);
                                                inputs.put("H", H);
                                                inputs.put("W", W);
                                                inputs.put("R", R);
                                                inputs.put("S", S);
                                                inputs.put("strideH", strideH);
                                                inputs.put("strideW", strideW);
                                                inputs.put("padH", padH);
                                                inputs.put("padW", padW);
                                                inputs.put("image", image);
                                                List<Object> outCPU = runOnCPU(spark, scriptStr, inputs, Arrays.asList("O"));
                                                List<Object> outGPU = runOnGPU(spark, scriptStr, inputs, Arrays.asList("O"));
                                                assertHeavyHitterPresent("gpu_maxpooling");
                                                assertEqualObjects(outCPU.get(0), outGPU.get(0));
                                                clearGPUMemory();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Matrix(org.apache.sysml.api.mlcontext.Matrix) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class NeuralNetworkOpTests method testConv2dBackwardFilter.

@Test
public void testConv2dBackwardFilter() {
    String scriptStr = "O = conv2d_backward_filter(image, dout, padding=[padH, padW], stride=[strideH, strideW], input_shape=[N,C,H,W], filter_shape=[K,C,R,S])";
    for (long N : Nlst) {
        for (long C : Clst) {
            for (long H : Hlst) {
                for (long W : Wlst) {
                    for (long K : Klst) {
                        for (long R : Rlst) {
                            for (long S : Slst) {
                                for (long strideH : strideHeightLst) {
                                    for (long strideW : strideWidthLst) {
                                        for (long padH : padHeightLst) {
                                            for (long padW : padWidthLst) {
                                                for (double sparsity : sparsitylst) {
                                                    // filter is smaller than image + padding
                                                    if (R > (H + padH) || S > (W + padW))
                                                        continue;
                                                    // Make sure ops fit in GPU memory and within constraints of cudnn
                                                    long imageSize = N * C * H * W * 8l;
                                                    if (// image size
                                                    imageSize > MAX_OP_SIZE)
                                                        continue;
                                                    long filterSize = K * C * R * S * 8l;
                                                    if (// filter size
                                                    filterSize > MAX_OP_SIZE)
                                                        continue;
                                                    int P = (int) ConvolutionUtils.getP(H, R, strideH, padH);
                                                    int Q = (int) ConvolutionUtils.getQ(W, S, strideW, padW);
                                                    long doutSize = N * K * P * Q * 8l;
                                                    if (// dout/output size
                                                    doutSize > MAX_OP_SIZE)
                                                        continue;
                                                    double imageSizeInMB = imageSize / (1024.0 * 1024.0);
                                                    double filterSizeInMB = filterSize / (1024.0 * 1024.0);
                                                    double doutSizeInMB = doutSize / (1024.0 * 1024.0);
                                                    System.out.format("conv2d_backward_filter, image[%d,%d,%d,%d](%.1fMB), filter[%d,%d,%d,%d](%.1f), dout[%d,%d,%d,%d](%.1fMB), stride[%d,%d], padding[%d,%d]", N, C, H, W, imageSizeInMB, N, C, R, S, filterSizeInMB, N, K, P, Q, doutSizeInMB, strideH, strideW, padH, padW);
                                                    Matrix image = generateInputMatrix(spark, (int) N, (int) (C * H * W), sparsity, seed);
                                                    Matrix dout = generateInputMatrix(spark, (int) N, (int) (K * P * Q), sparsity, seed);
                                                    HashMap<String, Object> inputs = new HashMap<>();
                                                    inputs.put("N", N);
                                                    inputs.put("C", C);
                                                    inputs.put("H", H);
                                                    inputs.put("W", W);
                                                    inputs.put("K", K);
                                                    inputs.put("R", R);
                                                    inputs.put("S", S);
                                                    inputs.put("strideH", strideH);
                                                    inputs.put("strideW", strideW);
                                                    inputs.put("padH", padH);
                                                    inputs.put("padW", padW);
                                                    inputs.put("image", image);
                                                    inputs.put("dout", dout);
                                                    List<Object> outCPU = runOnCPU(spark, scriptStr, inputs, Arrays.asList("O"));
                                                    List<Object> outGPU = runOnGPU(spark, scriptStr, inputs, Arrays.asList("O"));
                                                    assertHeavyHitterPresent("gpu_conv2d_backward_filter");
                                                    assertEqualObjects(outCPU.get(0), outGPU.get(0));
                                                    clearGPUMemory();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Matrix(org.apache.sysml.api.mlcontext.Matrix) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 14 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class MatrixMatrixElementWiseOpTests method runMatrixRowVectorTest.

/**
	 * Run O = X op Y where X is a matrix, Y is a row vector
	 *
	 * @param scriptStr         the script string
	 * @param matrixInput       name of the matrix input variable in the script string
	 * @param vectorInput       name of the vector input variable in the script string
	 * @param output            name of the output variable in the script string
	 * @param heavyHitterOpcode the string printed for the unary op heavy hitter when executed on gpu
	 */
private void runMatrixRowVectorTest(String scriptStr, String matrixInput, String vectorInput, String output, String heavyHitterOpcode) {
    int[] rows = new int[] { 64, 130, 1024, 2049 };
    int[] cols = new int[] { 64, 130, 1024, 2049 };
    for (int i = 0; i < rows.length; i++) {
        for (int j = 0; j < cols.length; j++) {
            for (int k = 0; k < sparsities.length; k++) {
                int m = rows[i];
                int n = cols[j];
                double sparsity = sparsities[k];
                Matrix X = generateInputMatrix(spark, m, n, sparsity, seed);
                Matrix Y = generateInputMatrix(spark, 1, n, sparsity, seed);
                HashMap<String, Object> inputs = new HashMap<>();
                inputs.put(matrixInput, X);
                inputs.put(vectorInput, Y);
                System.out.println("Vector[" + m + ", 1] op Matrix[" + m + ", " + n + "], sparsity = " + sparsity);
                List<Object> cpuOut = runOnCPU(spark, scriptStr, inputs, Arrays.asList(output));
                List<Object> gpuOut = runOnGPU(spark, scriptStr, inputs, Arrays.asList(output));
                //assertHeavyHitterPresent(heavyHitterOpcode);
                assertEqualObjects(cpuOut.get(0), gpuOut.get(0));
            }
        }
    }
}
Also used : Matrix(org.apache.sysml.api.mlcontext.Matrix) HashMap(java.util.HashMap)

Example 15 with Matrix

use of org.apache.sysml.api.mlcontext.Matrix in project incubator-systemml by apache.

the class MatrixMultiplicationOpTest method assertMatrixMultiplication.

/**
	 * Assert that matrix multiplication is the same on gpu and cpu
	 *
	 * @param scriptStr script string that has matrix multiplication (eg : O = X %*% Y)
	 * @param rows1     rows in X
	 * @param cols1     cols in X
	 * @param rows2     rows in Y
	 * @param cols2     cols in Y
	 * @param sparsity1 sparsity for X
	 * @param sparsity2 sparsity for Y
	 */
private void assertMatrixMultiplication(String scriptStr, int rows1, int cols1, int rows2, int cols2, double sparsity1, double sparsity2) {
    HashMap<String, Object> inputs = new HashMap<>();
    Matrix X = generateInputMatrix(spark, rows1, cols1, sparsity1, seed);
    Matrix Y = generateInputMatrix(spark, rows2, cols2, sparsity2, seed);
    inputs.put("X", X);
    inputs.put("Y", Y);
    List<Object> cpuOuts = runOnCPU(spark, scriptStr, inputs, Arrays.asList("O"));
    List<Object> gpuOuts = runOnGPU(spark, scriptStr, inputs, Arrays.asList("O"));
    //assertHeavyHitterPresent("gpu_ba+*'");
    assertEqualObjects(cpuOuts.get(0), gpuOuts.get(0));
}
Also used : Matrix(org.apache.sysml.api.mlcontext.Matrix) HashMap(java.util.HashMap)

Aggregations

Matrix (org.apache.sysml.api.mlcontext.Matrix)20 HashMap (java.util.HashMap)15 Test (org.junit.Test)8 Script (org.apache.sysml.api.mlcontext.Script)5 DMLScript (org.apache.sysml.api.DMLScript)3 RUNTIME_PLATFORM (org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM)3 MLContext (org.apache.sysml.api.mlcontext.MLContext)3 SparkSession (org.apache.spark.sql.SparkSession)2 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)2 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)2 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CoordinateMatrix (org.apache.spark.mllib.linalg.distributed.CoordinateMatrix)1 MatrixEntry (org.apache.spark.mllib.linalg.distributed.MatrixEntry)1 Row (org.apache.spark.sql.Row)1 FrameMetadata (org.apache.sysml.api.mlcontext.FrameMetadata)1 MLResults (org.apache.sysml.api.mlcontext.MLResults)1 MatrixMetadata (org.apache.sysml.api.mlcontext.MatrixMetadata)1 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)1