You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemml.apache.org by mb...@apache.org on 2018/05/15 03:05:47 UTC
[2/3] systemml git commit: [MINOR][SYSTEMML-2067] New codegen tests
for conv2d and bias_add ops
[MINOR][SYSTEMML-2067] New codegen tests for conv2d and bias_add ops
Project: http://git-wip-us.apache.org/repos/asf/systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/94260013
Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/94260013
Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/94260013
Branch: refs/heads/master
Commit: 94260013acad86bccaf864c97f4f21de3b1b0393
Parents: b4dedfe
Author: Matthias Boehm <mb...@gmail.com>
Authored: Mon May 14 18:45:52 2018 -0700
Committer: Matthias Boehm <mb...@gmail.com>
Committed: Mon May 14 18:45:52 2018 -0700
----------------------------------------------------------------------
.../codegen/RowConv2DOperationsTest.java | 136 +++++++++++++++++++
.../scripts/functions/codegen/RowConv2DTest.R | 123 +++++++++++++++++
.../scripts/functions/codegen/RowConv2DTest.dml | 52 +++++++
.../functions/codegen/ZPackageSuite.java | 1 +
4 files changed, 312 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/systemml/blob/94260013/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowConv2DOperationsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowConv2DOperationsTest.java b/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowConv2DOperationsTest.java
new file mode 100644
index 0000000..6910c03
--- /dev/null
+++ b/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowConv2DOperationsTest.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sysml.test.integration.functions.codegen;
+
+import java.io.File;
+import java.util.HashMap;
+
+import org.apache.sysml.api.DMLScript;
+import org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM;
+import org.apache.sysml.hops.OptimizerUtils;
+import org.apache.sysml.lops.LopProperties.ExecType;
+import org.apache.sysml.runtime.matrix.data.MatrixValue.CellIndex;
+import org.apache.sysml.test.integration.AutomatedTestBase;
+import org.apache.sysml.test.integration.TestConfiguration;
+import org.apache.sysml.test.utils.TestUtils;
+import org.junit.Test;
+
+public class RowConv2DOperationsTest extends AutomatedTestBase
+{
+ private final static String TEST_NAME1 = "RowConv2DTest";
+ private final static String TEST_DIR = "functions/codegen/";
+ private final static String TEST_CLASS_DIR = TEST_DIR + RowConv2DOperationsTest.class.getSimpleName() + "/";
+
+ private final static String TEST_CONF = "SystemML-config-codegen.xml";
+ private final static File TEST_CONF_FILE = new File(SCRIPT_DIR + TEST_DIR, TEST_CONF);
+
+ private static final double eps = Math.pow(10, -10);
+
+ @Override
+ public void setUp() {
+ TestUtils.clearAssertionInformation();
+ addTestConfiguration(TEST_NAME1, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1, new String[] {"B"}));
+ }
+
+ @Test
+ public void testConv2DDenseDenseCP() {
+ runConv2DTest(TEST_NAME1, true, 16, 64, 1, 3, 2, 1, 0, false, false, ExecType.CP);
+ }
+
+ @Test
+ public void testConv2DSparseDenseCP() {
+ runConv2DTest(TEST_NAME1, true, 16, 64, 1, 3, 2, 1, 0, true, false, ExecType.CP);
+ }
+
+ @Test
+ public void testConv2DDenseDenseSP() {
+ runConv2DTest(TEST_NAME1, true, 16, 64, 1, 3, 2, 1, 0, false, false, ExecType.SPARK);
+ }
+
+ @Test
+ public void testConv2DSparseDenseSP() {
+ runConv2DTest(TEST_NAME1, true, 16, 64, 1, 3, 2, 1, 0, true, false, ExecType.SPARK);
+ }
+
+ public void runConv2DTest(String testname, boolean rewrites, int imgSize, int numImg, int numChannels,
+ int numFilters, int filterSize, int stride, int pad, boolean sparse1, boolean sparse2, ExecType et)
+ {
+ boolean oldFlag = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION;
+ RUNTIME_PLATFORM platformOld = rtplatform;
+ switch( et ) {
+ case MR: rtplatform = RUNTIME_PLATFORM.HADOOP; break;
+ case SPARK: rtplatform = RUNTIME_PLATFORM.SPARK; break;
+ default: rtplatform = RUNTIME_PLATFORM.HYBRID_SPARK; break;
+ }
+
+ boolean sparkConfigOld = DMLScript.USE_LOCAL_SPARK_CONFIG;
+ if( rtplatform == RUNTIME_PLATFORM.SPARK || rtplatform == RUNTIME_PLATFORM.HYBRID_SPARK )
+ DMLScript.USE_LOCAL_SPARK_CONFIG = true;
+
+ try
+ {
+ String sparseVal1 = String.valueOf(sparse1).toUpperCase();
+ String sparseVal2 = String.valueOf(sparse2).toUpperCase();
+ TestConfiguration config = getTestConfiguration(testname);
+ loadTestConfiguration(config);
+
+ String HOME = SCRIPT_DIR + TEST_DIR;
+ fullDMLScriptName = HOME + testname + ".dml";
+ programArgs = new String[]{"-explain", "recompile_runtime", "-stats", "-args",
+ String.valueOf(imgSize), String.valueOf(numImg), String.valueOf(numChannels),
+ String.valueOf(numFilters), String.valueOf(filterSize), String.valueOf(stride),
+ String.valueOf(pad), output("B"), sparseVal1, sparseVal2 };
+
+ fullRScriptName = HOME + testname + ".R";
+ rCmd = getRCmd(String.valueOf(imgSize), String.valueOf(numImg), String.valueOf(numChannels),
+ String.valueOf(numFilters), String.valueOf(filterSize), String.valueOf(stride),
+ String.valueOf(pad), expectedDir(), sparseVal1, sparseVal2);
+
+ OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = rewrites;
+
+ runTest(true, false, null, -1);
+ runRScript(true);
+
+ //compare matrices
+ HashMap<CellIndex, Double> dmlfile = readDMLMatrixFromHDFS("B");
+ HashMap<CellIndex, Double> rfile = readRMatrixFromFS("B");
+ TestUtils.compareMatrices(dmlfile, rfile, eps, "Stat-DML", "Stat-R");
+ //Assert.assertTrue(heavyHittersContainsSubString("spoofRA")
+ // || heavyHittersContainsSubString("sp_spoofRA"));
+ }
+ finally {
+ rtplatform = platformOld;
+ DMLScript.USE_LOCAL_SPARK_CONFIG = sparkConfigOld;
+ OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = oldFlag;
+ OptimizerUtils.ALLOW_AUTO_VECTORIZATION = true;
+ OptimizerUtils.ALLOW_OPERATOR_FUSION = true;
+ }
+ }
+
+ /**
+ * Override default configuration with custom test configuration to ensure
+ * scratch space and local temporary directory locations are also updated.
+ */
+ @Override
+ protected File getConfigTemplateFile() {
+ // Instrumentation in this test's output log to show custom configuration file used for template.
+ System.out.println("This test case overrides default configuration with " + TEST_CONF_FILE.getPath());
+ return TEST_CONF_FILE;
+ }
+}
http://git-wip-us.apache.org/repos/asf/systemml/blob/94260013/src/test/scripts/functions/codegen/RowConv2DTest.R
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/RowConv2DTest.R b/src/test/scripts/functions/codegen/RowConv2DTest.R
new file mode 100644
index 0000000..070602d
--- /dev/null
+++ b/src/test/scripts/functions/codegen/RowConv2DTest.R
@@ -0,0 +1,123 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+args <- commandArgs(TRUE)
+library("Matrix")
+imgSize=as.integer(args[1])
+numImg=as.integer(args[2])
+numChannels=as.integer(args[3])
+numFilters=as.integer(args[4])
+filterSize=as.integer(args[5])
+stride=as.integer(args[6])
+pad=as.integer(args[7])
+
+# Assumption: NCHW image format
+x=matrix(seq(1, numImg*numChannels*imgSize*imgSize), numImg, numChannels*imgSize*imgSize, byrow=TRUE)
+w=matrix(seq(1, numFilters*numChannels*filterSize*filterSize), numFilters, numChannels*filterSize*filterSize, byrow=TRUE)
+
+if(as.logical(args[9])) {
+ zero_mask = (x - mean(x)*1.5) > 0
+ x = x * zero_mask
+} else {
+ x = x - mean(x)
+}
+if(as.logical(args[10])) {
+ zero_mask = (w - mean(w)*1.5) > 0
+ w = w * zero_mask
+} else {
+ w = w - mean(w)
+}
+pad_image <- function(img, Hin, Win, padh, padw){
+ C = nrow(img)
+ img_padded = matrix(0, C, (Hin+2*padh)*(Win+2*padw), byrow=TRUE) # zeros
+ for (c in 1:C) {
+ img_slice = matrix(img[c,], Hin, Win, byrow=TRUE) # depth slice C reshaped
+ img_padded_slice = matrix(0, Hin+2*padh, Win+2*padw)
+ img_padded_slice[(padh+1):(padh+Hin), (padw+1):(padw+Win)] = img_slice
+ img_padded[c,] = matrix(t(img_padded_slice), 1, (Hin+2*padh)*(Win+2*padw)) # reshape
+ }
+ img_padded
+}
+
+im2col <- function(img, Hin, Win, Hf, Wf, strideh, stridew) {
+ C = nrow(img)
+ Hout = as.integer((Hin - Hf) / strideh + 1)
+ Wout = as.integer((Win - Wf) / stridew + 1)
+
+ img_cols = matrix(0, C*Hf*Wf, Hout*Wout, byrow=TRUE) # zeros
+ for (hout in 1:Hout) { # all output rows
+ hin = (hout-1) * strideh + 1
+ for (wout in 1:Wout) { # all output columns
+ win = (wout-1) * stridew + 1
+ # Extract a local patch of the input image corresponding spatially to the filter sizes.
+ img_patch = matrix(0, C, Hf*Wf, byrow=TRUE) # zeros
+ for (c in 1:C) { # all channels
+ img_slice = matrix(img[c,], Hin, Win, byrow=TRUE) # reshape
+ img_patch[c,] = matrix(t(img_slice[hin:(hin+Hf-1), win:(win+Wf-1)]), 1, Hf*Wf)
+ }
+ img_cols[,(hout-1)*Wout + wout] = matrix(t(img_patch), C*Hf*Wf, 1) # reshape
+ }
+ }
+ img_cols
+}
+
+conv2d <- function(X, W, C, Hin, Win, Hf, Wf, strideh, stridew, padh, padw) {
+ N = nrow(X)
+ F = nrow(W)
+ Hout = as.integer((Hin + 2 * padh - Hf) / strideh + 1)
+ Wout = as.integer((Win + 2 * padw - Wf) / stridew + 1)
+
+ # Create output volume
+ out = matrix(0, N, F*Hout*Wout, byrow=TRUE)
+
+ # Convolution - im2col implementation
+ for (n in 1:N) { # all examples
+ Xn = matrix(X[n,], C, Hin*Win, byrow=TRUE) # reshape
+
+ # Pad image
+ Xn_padded = pad_image(Xn, Hin, Win, padh, padw) # shape (C, (Hin+2*padh)*(Win+2*padw))
+
+ # Extract local image patches into columns with im2col, of shape (C*Hf*Wf, Hout*Wout)
+ Xn_padded_cols = im2col(Xn_padded, Hin+2*padh, Win+2*padw, Hf, Wf, strideh, stridew)
+
+ # Convolve patches with filters
+ outn = W %*% Xn_padded_cols # shape (F, Hout*Wout)
+ out[n,] = matrix(t(outn), 1, F*Hout*Wout) # reshape
+ }
+
+ out
+}
+
+output = conv2d(x, w, numChannels, imgSize, imgSize, filterSize, filterSize, stride, stride, pad, pad);
+Hout = as.integer((imgSize + 2 * pad - filterSize) / stride + 1)
+Wout = Hout
+
+b=matrix(seq(1, numFilters), numFilters, 1, byrow=TRUE)
+for(k in 0:(numFilters-1)) {
+ for(i in 1:nrow(output)) {
+ start = k*Hout*Hout;
+ for(j in 1:(Hout*Hout)) {
+ output[i,start+j] = output[i,start+j] + b[k+1,1]
+ }
+ }
+}
+
+writeMM(as(output,"CsparseMatrix"), paste(args[8], "B", sep=""))
http://git-wip-us.apache.org/repos/asf/systemml/blob/94260013/src/test/scripts/functions/codegen/RowConv2DTest.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/RowConv2DTest.dml b/src/test/scripts/functions/codegen/RowConv2DTest.dml
new file mode 100644
index 0000000..9d51716
--- /dev/null
+++ b/src/test/scripts/functions/codegen/RowConv2DTest.dml
@@ -0,0 +1,52 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+imgSize=$1
+numImg=$2
+numChannels=$3
+numFilters=$4
+filterSize=$5
+stride=$6
+pad=$7
+
+# Assumption: NCHW image format
+x=matrix(seq(1, numImg*numChannels*imgSize*imgSize), rows=numImg, cols=numChannels*imgSize*imgSize)
+w=matrix(seq(1, numFilters*numChannels*filterSize*filterSize), rows=numFilters, cols=numChannels*filterSize*filterSize)
+b=matrix(seq(1, numFilters), rows=numFilters, cols=1)
+
+if($9) {
+ zero_mask = (x - mean(x)*1.5) > 0
+ x = x * zero_mask
+}
+else {
+ x = x - mean(x)
+}
+if($10) {
+ zero_mask = (w - mean(w)*1.5) > 0
+ w = w * zero_mask
+}
+else {
+ w = w - mean(w)
+}
+output = conv2d(x, w, padding=[pad, pad], stride=[stride, stride], input_shape=[numImg, numChannels, imgSize, imgSize], filter_shape=[numFilters, numChannels, filterSize, filterSize], bias=b)
+output = bias_add(output, b)
+
+write(output, $8, format="text")
http://git-wip-us.apache.org/repos/asf/systemml/blob/94260013/src/test_suites/java/org/apache/sysml/test/integration/functions/codegen/ZPackageSuite.java
----------------------------------------------------------------------
diff --git a/src/test_suites/java/org/apache/sysml/test/integration/functions/codegen/ZPackageSuite.java b/src/test_suites/java/org/apache/sysml/test/integration/functions/codegen/ZPackageSuite.java
index 9a78dbc..3857cfa 100644
--- a/src/test_suites/java/org/apache/sysml/test/integration/functions/codegen/ZPackageSuite.java
+++ b/src/test_suites/java/org/apache/sysml/test/integration/functions/codegen/ZPackageSuite.java
@@ -40,6 +40,7 @@ import org.junit.runners.Suite;
MultiAggTmplTest.class,
OuterProdTmplTest.class,
RowAggTmplTest.class,
+ RowConv2DOperationsTest.class,
RowVectorComparisonTest.class,
SparseSideInputTest.class,
SumProductChainTest.class,