Repository: systemml Updated Branches: refs/heads/master 4ad8f7742 -> 0ae2b4f77
[SYSTEMML-1535] Fix codegen rowwise matrix-matrix mult w/ shared reads This patch fixes the recently introduced matrix-matrix multiplication support in rowwise code generation templates to support more complex DAGs with shared reads of the main input. This is important to support, for example, the core inner loop operations of Mlogreg with multiple classes in a single fused row-wise operator: Q = P[,1:K] * (X %*% ssX_V); HV = t(X) %*% (Q - P[,1:K] * rowSums(Q)); Furthermore, this patch also cleans up the constructors of generated outer product operators to consistently call super instead of modifying the base attributes directly. Project: http://git-wip-us.apache.org/repos/asf/systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/0ae2b4f7 Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/0ae2b4f7 Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/0ae2b4f7 Branch: refs/heads/master Commit: 0ae2b4f778f97740db294694d07c28cc332c0f40 Parents: 4ad8f77 Author: Matthias Boehm <mboe...@gmail.com> Authored: Wed Jul 19 00:14:19 2017 -0700 Committer: Matthias Boehm <mboe...@gmail.com> Committed: Wed Jul 19 00:14:19 2017 -0700 ---------------------------------------------------------------------- .../hops/codegen/cplan/CNodeOuterProduct.java | 2 +- .../hops/codegen/template/TemplateRow.java | 6 ++-- .../sysml/hops/rewrite/HopRewriteUtils.java | 4 +++ .../runtime/codegen/SpoofOuterProduct.java | 4 +-- .../functions/codegen/RowAggTmplTest.java | 20 ++++++++++- .../scripts/functions/codegen/rowAggPattern30.R | 35 ++++++++++++++++++++ .../functions/codegen/rowAggPattern30.dml | 32 ++++++++++++++++++ 7 files changed, 97 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/systemml/blob/0ae2b4f7/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeOuterProduct.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeOuterProduct.java b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeOuterProduct.java index 01ca08e..90917f8 100644 --- a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeOuterProduct.java +++ b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeOuterProduct.java @@ -37,7 +37,7 @@ public class CNodeOuterProduct extends CNodeTpl + "\n" + "public final class %TMP% extends SpoofOuterProduct { \n" + " public %TMP%() {\n" - + " _outerProductType = OutProdType.%TYPE%;\n" + + " super(OutProdType.%TYPE%);\n" + " }\n" + " protected void genexecDense(double a, double[] a1, int a1i, double[] a2, int a2i, double[][] b, double[] scalars, double[] c, int ci, int m, int n, int len, int rowIndex, int colIndex) { \n" + "%BODY_dense%" http://git-wip-us.apache.org/repos/asf/systemml/blob/0ae2b4f7/src/main/java/org/apache/sysml/hops/codegen/template/TemplateRow.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/hops/codegen/template/TemplateRow.java b/src/main/java/org/apache/sysml/hops/codegen/template/TemplateRow.java index 5cb016c..8445aeb 100644 --- a/src/main/java/org/apache/sysml/hops/codegen/template/TemplateRow.java +++ b/src/main/java/org/apache/sysml/hops/codegen/template/TemplateRow.java @@ -118,8 +118,10 @@ public class TemplateRow extends TemplateBase return !isClosed() && ((hop instanceof BinaryOp && isValidBinaryOperation(hop) && hop.getDim1() > 1 && input.getDim1()>1) - ||(hop instanceof AggBinaryOp && input.getDim2()==1 - && HopRewriteUtils.isTransposeOperation(hop.getInput().get(0)))); + ||(hop instanceof AggBinaryOp + && HopRewriteUtils.isTransposeOperation(hop.getInput().get(0)) + && (input.getDim2()==1 || (input==hop.getInput().get(1) + && HopRewriteUtils.containsInput(input, hop.getInput().get(0).getInput().get(0)))))); } @Override http://git-wip-us.apache.org/repos/asf/systemml/blob/0ae2b4f7/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java b/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java index 24698f5..ec8e20d 100644 --- a/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java +++ b/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java @@ -831,6 +831,10 @@ public class HopRewriteUtils return ret; } + public static boolean containsInput(Hop current, Hop probe) { + return rContainsInput(current, probe, new HashSet<Long>()); + } + private static boolean rContainsInput(Hop current, Hop probe, HashSet<Long> memo) { if( memo.contains(current.getHopID()) ) return false; http://git-wip-us.apache.org/repos/asf/systemml/blob/0ae2b4f7/src/main/java/org/apache/sysml/runtime/codegen/SpoofOuterProduct.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/codegen/SpoofOuterProduct.java b/src/main/java/org/apache/sysml/runtime/codegen/SpoofOuterProduct.java index 90c7507..9148dd4 100644 --- a/src/main/java/org/apache/sysml/runtime/codegen/SpoofOuterProduct.java +++ b/src/main/java/org/apache/sysml/runtime/codegen/SpoofOuterProduct.java @@ -51,8 +51,8 @@ public abstract class SpoofOuterProduct extends SpoofOperator protected OutProdType _outerProductType; - public SpoofOuterProduct() { - + public SpoofOuterProduct(OutProdType type) { + setOuterProdType(type); } public void setOuterProdType(OutProdType type) { http://git-wip-us.apache.org/repos/asf/systemml/blob/0ae2b4f7/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowAggTmplTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowAggTmplTest.java b/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowAggTmplTest.java index 8f5f03f..59c3ab4 100644 --- a/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowAggTmplTest.java +++ b/src/test/java/org/apache/sysml/test/integration/functions/codegen/RowAggTmplTest.java @@ -65,6 +65,7 @@ public class RowAggTmplTest extends AutomatedTestBase private static final String TEST_NAME27 = TEST_NAME+"27"; //t(X)%*%(X%*%v), w/ mm private static final String TEST_NAME28 = TEST_NAME+"28"; //Kmeans, final eval private static final String TEST_NAME29 = TEST_NAME+"29"; //sum(rowMins(X)) + private static final String TEST_NAME30 = TEST_NAME+"30"; //Mlogreg inner core, multi-class private static final String TEST_DIR = "functions/codegen/"; private static final String TEST_CLASS_DIR = TEST_DIR + RowAggTmplTest.class.getSimpleName() + "/"; @@ -76,7 +77,7 @@ public class RowAggTmplTest extends AutomatedTestBase @Override public void setUp() { TestUtils.clearAssertionInformation(); - for(int i=1; i<=29; i++) + for(int i=1; i<=30; i++) addTestConfiguration( TEST_NAME+i, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME+i, new String[] { String.valueOf(i) }) ); } @@ -515,6 +516,21 @@ public class RowAggTmplTest extends AutomatedTestBase testCodegenIntegration( TEST_NAME29, false, ExecType.SPARK ); } + @Test + public void testCodegenRowAggRewrite30CP() { + testCodegenIntegration( TEST_NAME30, true, ExecType.CP ); + } + + @Test + public void testCodegenRowAgg30CP() { + testCodegenIntegration( TEST_NAME30, false, ExecType.CP ); + } + + @Test + public void testCodegenRowAgg30SP() { + testCodegenIntegration( TEST_NAME30, false, ExecType.SPARK ); + } + private void testCodegenIntegration( String testname, boolean rewrites, ExecType instType ) { boolean oldFlag = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION; @@ -561,6 +577,8 @@ public class RowAggTmplTest extends AutomatedTestBase if( testname.equals(TEST_NAME28) ) Assert.assertTrue(!heavyHittersContainsSubString("spoofRA", 2) && !heavyHittersContainsSubString("sp_spoofRA", 2)); + if( testname.equals(TEST_NAME30) ) + Assert.assertTrue(!heavyHittersContainsSubString("spoofRA", 2)); } finally { rtplatform = platformOld; http://git-wip-us.apache.org/repos/asf/systemml/blob/0ae2b4f7/src/test/scripts/functions/codegen/rowAggPattern30.R ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/codegen/rowAggPattern30.R b/src/test/scripts/functions/codegen/rowAggPattern30.R new file mode 100644 index 0000000..28300e7 --- /dev/null +++ b/src/test/scripts/functions/codegen/rowAggPattern30.R @@ -0,0 +1,35 @@ +#------------------------------------------------------------- +# +# 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") +library("matrixStats") + +X = matrix(seq(1,6000), 600, 10, byrow=TRUE); +ssX_V = matrix(seq(1,40), 10, 4, byrow=TRUE); +P = matrix(seq(1,3000), 600, 5, byrow=TRUE); +K = 4; + +Q = P[,1:K] * (X %*% ssX_V); +R = t(X) %*% (Q - P[,1:K] * rowSums(Q)%*%matrix(1,1,K)); + +writeMM(as(R, "CsparseMatrix"), paste(args[2], "S", sep="")); http://git-wip-us.apache.org/repos/asf/systemml/blob/0ae2b4f7/src/test/scripts/functions/codegen/rowAggPattern30.dml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/codegen/rowAggPattern30.dml b/src/test/scripts/functions/codegen/rowAggPattern30.dml new file mode 100644 index 0000000..0cfbc5d --- /dev/null +++ b/src/test/scripts/functions/codegen/rowAggPattern30.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. +# +#------------------------------------------------------------- + + +X = matrix(seq(1,6000), 600, 10); +ssX_V = matrix(seq(1,40), 10, 4); +P = matrix(seq(1,3000), 600, 5); +K = 4; + +if(1==1){} +Q = P[,1:K] * (X %*% ssX_V); +R = t(X) %*% (Q - P[,1:K] * rowSums(Q)); + +write(R, $1)