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 2016/03/28 19:04:06 UTC

[3/4] incubator-systemml git commit: [SYSTEMML-510] New rewrite 'canonicalize matmult-add-scalar', incl tests

[SYSTEMML-510] New rewrite 'canonicalize matmult-add-scalar', incl tests

This new static simplification rewrite is a pure canonicalization in
order to bring patterns like U%*%V+eps, eps+U%*%V, and U%*%V-eps into
the normalized representation U%*%V+s which simplifies subsequent
complex rewrites (e.g., wdivmm or wcemm with epsilon). The potential
additional plus operation for U%*%V+(-eps) is unproblematic as this
happens on scalars.

Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/b1e6f162
Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/b1e6f162
Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/b1e6f162

Branch: refs/heads/master
Commit: b1e6f162b1c3dd22c751bccc5036401dc889d626
Parents: 1b8a027
Author: Matthias Boehm <mb...@us.ibm.com>
Authored: Sun Mar 27 20:45:24 2016 -0700
Committer: Matthias Boehm <mb...@us.ibm.com>
Committed: Sun Mar 27 20:45:24 2016 -0700

----------------------------------------------------------------------
 .../RewriteAlgebraicSimplificationStatic.java   | 49 +++++++++++++++++++-
 .../quaternary/WeightedDivMatrixMultTest.java   | 21 ++++++++-
 .../quaternary/WeightedDivMMLeftEps2.R          | 38 +++++++++++++++
 .../quaternary/WeightedDivMMLeftEps2.dml        | 32 +++++++++++++
 .../quaternary/WeightedDivMMLeftEps3.R          | 38 +++++++++++++++
 .../quaternary/WeightedDivMMLeftEps3.dml        | 32 +++++++++++++
 6 files changed, 206 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/b1e6f162/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java b/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
index 37a0dc2..0cc9f40 100644
--- a/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
+++ b/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
@@ -137,6 +137,7 @@ public class RewriteAlgebraicSimplificationStatic extends HopRewriteRule
 			hi = fuseDatagenAndBinaryOperation(hop, hi, i);      //e.g., rand(min=-1,max=1)*7 -> rand(min=-7,max=7)
 			hi = fuseDatagenAndMinusOperation(hop, hi, i);       //e.g., -(rand(min=-2,max=1)) -> rand(min=-1,max=2)
  			hi = simplifyBinaryToUnaryOperation(hop, hi, i);     //e.g., X*X -> X^2 (pow2), X+X -> X*2, (X>0)-(X<0) -> sign(X)
+ 			hi = canonicalizeMatrixMultScalarAdd(hi);            //e.g., eps+U%*%t(V) -> U%*%t(V)+eps, U%*%t(V)-eps -> U%*%t(V)+(-eps) 
  			hi = simplifyReverseOperation(hop, hi, i);           //e.g., table(seq(1,nrow(X),1),seq(nrow(X),1,-1)) %*% X -> rev(X)
 			hi = simplifyMultiBinaryToBinaryOperation(hi);       //e.g., 1-X*Y -> X 1-* Y
  			hi = simplifyDistributiveBinaryOperation(hop, hi, i);//e.g., (X-Y*X) -> (1-Y)*X
@@ -473,8 +474,8 @@ public class RewriteAlgebraicSimplificationStatic extends HopRewriteRule
 	}
 	
 	/**
-	 * handle simplification of binary operations
-	 * (relies on previous common subexpression elimination)
+	 * Handle simplification of binary operations (relies on previous common subexpression elimination).
+	 * At the same time this servers as a canonicalization for more complex rewrites. 
 	 * 
 	 * X+X -> X*2, X*X -> X^2, (X>0)-(X<0) -> sign(X)
 	 * @throws HopsException 
@@ -543,6 +544,50 @@ public class RewriteAlgebraicSimplificationStatic extends HopRewriteRule
 		return hi;
 	}
 	
+	/**
+	 * Rewrite to canonicalize all patterns like U%*%V+eps, eps+U%*%V, and
+	 * U%*%V-eps into the common representation U%*%V+s which simplifies 
+	 * subsequent rewrites (e.g., wdivmm or wcemm with epsilon).   
+	 * 
+	 * @param parent
+	 * @param hi
+	 * @param pos
+	 * @return
+	 * @throws HopsException
+	 */
+	private Hop canonicalizeMatrixMultScalarAdd( Hop hi ) 
+		throws HopsException
+	{
+		//pattern: binary operation (+ or -) of matrix mult and scalar 		
+		if( hi instanceof BinaryOp )
+		{
+			BinaryOp bop = (BinaryOp)hi;
+			Hop left = hi.getInput().get(0);
+			Hop right = hi.getInput().get(1);
+			
+			//pattern: (eps + U%*%V) -> (U%*%V+eps)
+			if( left.getDataType().isScalar() && right instanceof AggBinaryOp
+				&& bop.getOp()==OpOp2.PLUS )
+			{
+				HopRewriteUtils.removeAllChildReferences(bop);
+				HopRewriteUtils.addChildReference(bop, right, 0);
+				HopRewriteUtils.addChildReference(bop, left, 1);
+				LOG.debug("Applied canonicalizeMatrixMultScalarAdd1 (line "+hi.getBeginLine()+").");
+			}
+			//pattern: (U%*%V - eps) -> (U%*%V + (-eps))
+			else if( right.getDataType().isScalar() && left instanceof AggBinaryOp
+					&& bop.getOp() == OpOp2.MINUS )
+			{
+				bop.setOp(OpOp2.PLUS);
+				HopRewriteUtils.removeChildReferenceByPos(bop, right, 1);
+				HopRewriteUtils.addChildReference(bop, 
+						HopRewriteUtils.createBinary(new LiteralOp(0), right, OpOp2.MINUS), 1);				
+				LOG.debug("Applied canonicalizeMatrixMultScalarAdd2 (line "+hi.getBeginLine()+").");
+			}
+		}
+		
+		return hi;
+	}
 
 	/**
 	 * NOTE: this would be by definition a dynamic rewrite; however, we apply it as a static

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/b1e6f162/src/test/java/org/apache/sysml/test/integration/functions/quaternary/WeightedDivMatrixMultTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/quaternary/WeightedDivMatrixMultTest.java b/src/test/java/org/apache/sysml/test/integration/functions/quaternary/WeightedDivMatrixMultTest.java
index aa7e667..6ff8de6 100644
--- a/src/test/java/org/apache/sysml/test/integration/functions/quaternary/WeightedDivMatrixMultTest.java
+++ b/src/test/java/org/apache/sysml/test/integration/functions/quaternary/WeightedDivMatrixMultTest.java
@@ -58,6 +58,8 @@ public class WeightedDivMatrixMultTest extends AutomatedTestBase
 	private final static String TEST_NAME9 = "WeightedDivMM4MultMinusRight";
 	private final static String TEST_NAME10 = "WeightedDivMMLeftEps";
 	private final static String TEST_NAME11 = "WeightedDivMMRightEps";
+	private final static String TEST_NAME12 = "WeightedDivMMLeftEps2";
+	private final static String TEST_NAME13 = "WeightedDivMMLeftEps3";
 	private final static String TEST_DIR = "functions/quaternary/";
 	private final static String TEST_CLASS_DIR = TEST_DIR + WeightedDivMatrixMultTest.class.getSimpleName() + "/";
 	
@@ -85,7 +87,9 @@ public class WeightedDivMatrixMultTest extends AutomatedTestBase
 		addTestConfiguration(TEST_NAME9,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME9,new String[]{"R"}));
 		addTestConfiguration(TEST_NAME10,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME10,new String[]{"R"}));
 		addTestConfiguration(TEST_NAME11,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME11,new String[]{"R"}));
-	
+		addTestConfiguration(TEST_NAME12,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME12,new String[]{"R"}));
+		addTestConfiguration(TEST_NAME13,new TestConfiguration(TEST_CLASS_DIR, TEST_NAME13,new String[]{"R"}));
+		
 		if (TEST_CACHE_ENABLED) {
 			setOutAndExpectedDeletionDisabled(true);
 		}
@@ -552,6 +556,18 @@ public class WeightedDivMatrixMultTest extends AutomatedTestBase
 		runWeightedDivMMTest(TEST_NAME11, false, true, true, ExecType.SPARK);
 	}
 
+	//d) testcases for wdivmm w/ DIVIDE LEFT/RIGHT with Epsilon
+	
+	@Test
+	public void testWeightedDivMMLeftEpsCanonicalized1SparseCP() {
+		runWeightedDivMMTest(TEST_NAME12, true, true, false, ExecType.CP);
+	}
+	
+	@Test
+	public void testWeightedDivMMLeftEpsCanonicalized2SparseCP() {
+		runWeightedDivMMTest(TEST_NAME13, true, true, false, ExecType.CP);
+	}
+	
 	/**
 	 * 
 	 * @param sparseM1
@@ -582,7 +598,8 @@ public class WeightedDivMatrixMultTest extends AutomatedTestBase
 			boolean basic = testname.equals(TEST_NAME3);
 			boolean left = testname.equals(TEST_NAME1) || testname.equals(TEST_NAME4) 
 					|| testname.equals(TEST_NAME6) || testname.equals(TEST_NAME8)
-					|| testname.equals(TEST_NAME10);
+					|| testname.equals(TEST_NAME10) || testname.equals(TEST_NAME12)
+					|| testname.equals(TEST_NAME13);
 			double sparsity = (sparse) ? spSparse : spDense;
 			String TEST_NAME = testname;
 			String TEST_CACHE_DIR = TEST_CACHE_ENABLED ? 

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/b1e6f162/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps2.R
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps2.R b/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps2.R
new file mode 100644
index 0000000..8c21496
--- /dev/null
+++ b/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps2.R
@@ -0,0 +1,38 @@
+#-------------------------------------------------------------
+#
+# 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)
+options(digits=22)
+
+library("Matrix")
+
+W = as.matrix(readMM(paste(args[1], "W.mtx", sep="")))
+U = as.matrix(readMM(paste(args[1], "U.mtx", sep="")))
+V = as.matrix(readMM(paste(args[1], "V.mtx", sep="")))
+
+x = as.numeric(args[3])
+
+R = t(t(U) %*% (W/(x + U%*%t(V))));
+
+writeMM(as(R, "CsparseMatrix"), paste(args[2], "R", sep="")); 
+
+

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/b1e6f162/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps2.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps2.dml b/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps2.dml
new file mode 100644
index 0000000..4dbab66
--- /dev/null
+++ b/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps2.dml
@@ -0,0 +1,32 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+
+
+W = read($1);
+U = read($2);
+V = read($3);
+
+x = $5;
+
+R = t(t(U) %*% (W/(x + U%*%t(V))));
+
+write(R, $4);

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/b1e6f162/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps3.R
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps3.R b/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps3.R
new file mode 100644
index 0000000..1c014d3
--- /dev/null
+++ b/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps3.R
@@ -0,0 +1,38 @@
+#-------------------------------------------------------------
+#
+# 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)
+options(digits=22)
+
+library("Matrix")
+
+W = as.matrix(readMM(paste(args[1], "W.mtx", sep="")))
+U = as.matrix(readMM(paste(args[1], "U.mtx", sep="")))
+V = as.matrix(readMM(paste(args[1], "V.mtx", sep="")))
+
+x = as.numeric(args[3])
+
+R = t(t(U) %*% (W/(U%*%t(V)-x)));
+
+writeMM(as(R, "CsparseMatrix"), paste(args[2], "R", sep="")); 
+
+

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/b1e6f162/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps3.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps3.dml b/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps3.dml
new file mode 100644
index 0000000..520aeaa
--- /dev/null
+++ b/src/test/scripts/functions/quaternary/WeightedDivMMLeftEps3.dml
@@ -0,0 +1,32 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+
+
+W = read($1);
+U = read($2);
+V = read($3);
+
+x = $5;
+
+R = t(t(U) %*% (W/(U%*%t(V)-x)));
+
+write(R, $4);