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/02/11 02:59:25 UTC

[4/4] systemml git commit: [SYSTEMML-2143] Fix missing sprop/sigmoid codegen support in row templ

[SYSTEMML-2143] Fix missing sprop/sigmoid codegen support in row templ

This patch adds codegen support for sprop (sample proportion) and
sigmoid in codegen row templates, which requires sparse and dense vector
primitives. So far we only supported scalar sprop and sigmoid in outer,
cell and magg templates.


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

Branch: refs/heads/master
Commit: 744df8139cbacaa5b65323768c099ae87121af3c
Parents: d7e4c7c
Author: Matthias Boehm <mb...@gmail.com>
Authored: Sat Feb 10 18:58:57 2018 -0800
Committer: Matthias Boehm <mb...@gmail.com>
Committed: Sat Feb 10 18:58:57 2018 -0800

----------------------------------------------------------------------
 .../sysml/hops/codegen/cplan/CNodeUnary.java    | 37 ++++++++------
 .../hops/codegen/template/TemplateRow.java      |  4 +-
 .../runtime/codegen/LibSpoofPrimitives.java     | 52 ++++++++++++++++++++
 .../functions/codegen/RowAggTmplTest.java       | 34 ++++++++++++-
 .../scripts/functions/codegen/rowAggPattern37.R | 32 ++++++++++++
 .../functions/codegen/rowAggPattern37.dml       | 27 ++++++++++
 .../scripts/functions/codegen/rowAggPattern38.R | 32 ++++++++++++
 .../functions/codegen/rowAggPattern38.dml       | 27 ++++++++++
 8 files changed, 227 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/744df813/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java
index 83ddd28..a1401c3 100644
--- a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java
+++ b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java
@@ -36,6 +36,7 @@ public class CNodeUnary extends CNode
 		VECT_SIN, VECT_COS, VECT_TAN, VECT_ASIN, VECT_ACOS, VECT_ATAN, 
 		VECT_SINH, VECT_COSH, VECT_TANH,
 		VECT_CUMSUM, VECT_CUMMIN, VECT_CUMMAX,
+		VECT_SPROP, VECT_SIGMOID,
 		EXP, POW2, MULT2, SQRT, LOG, LOG_NZ,
 		ABS, ROUND, CEIL, FLOOR, SIGN, 
 		SIN, COS, TAN, ASIN, ACOS, ATAN, SINH, COSH, TANH,
@@ -81,7 +82,9 @@ public class CNodeUnary extends CNode
 				case VECT_TANH:
 				case VECT_CUMSUM:
 				case VECT_CUMMIN:
-				case VECT_CUMMAX:{
+				case VECT_CUMMAX:
+				case VECT_SPROP:
+				case VECT_SIGMOID: {
 					String vectName = getVectorPrimitiveName();
 					return sparse ? "    double[] %TMP% = LibSpoofPrimitives.vect"+vectName+"Write(%IN1v%, %IN1i%, %POS1%, alen, len);\n" : 
 									"    double[] %TMP% = LibSpoofPrimitives.vect"+vectName+"Write(%IN1%, %POS1%, %LEN%);\n";
@@ -89,20 +92,20 @@ public class CNodeUnary extends CNode
 					
 				case EXP:
 					return "    double %TMP% = FastMath.exp(%IN1%);\n";
-			    case LOOKUP_R:
-			    	return sparse ?
-			    		"    double %TMP% = getValue(%IN1v%, %IN1i%, ai, alen, 0);\n" :
-			    		"    double %TMP% = getValue(%IN1%, rix);\n";
-			    case LOOKUP_C:
-			    	return "    double %TMP% = getValue(%IN1%, n, 0, cix);\n";
-			    case LOOKUP_RC:
-			    	return "    double %TMP% = getValue(%IN1%, n, rix, cix);\n";	
+				case LOOKUP_R:
+					return sparse ?
+						"    double %TMP% = getValue(%IN1v%, %IN1i%, ai, alen, 0);\n" :
+						"    double %TMP% = getValue(%IN1%, rix);\n";
+				case LOOKUP_C:
+					return "    double %TMP% = getValue(%IN1%, n, 0, cix);\n";
+				case LOOKUP_RC:
+					return "    double %TMP% = getValue(%IN1%, n, rix, cix);\n";
 				case LOOKUP0:
-					return "    double %TMP% = %IN1%[0];\n" ;
+					return "    double %TMP% = %IN1%[0];\n";
 				case POW2:
-					return "    double %TMP% = %IN1% * %IN1%;\n" ;
+					return "    double %TMP% = %IN1% * %IN1%;\n";
 				case MULT2:
-					return "    double %TMP% = %IN1% + %IN1%;\n" ;
+					return "    double %TMP% = %IN1% + %IN1%;\n";
 				case ABS:
 					return "    double %TMP% = Math.abs(%IN1%);\n";
 				case SIN:
@@ -155,8 +158,8 @@ public class CNodeUnary extends CNode
 				|| this == VECT_SIN || this == VECT_COS || this == VECT_TAN
 				|| this == VECT_ASIN || this == VECT_ACOS || this == VECT_ATAN
 				|| this == VECT_SINH || this == VECT_COSH || this == VECT_TANH
-				|| this == VECT_CUMSUM || this == VECT_CUMMIN
-				|| this == VECT_CUMMAX;
+				|| this == VECT_CUMSUM || this == VECT_CUMMIN || this == VECT_CUMMAX
+				|| this == VECT_SPROP || this == VECT_SIGMOID;
 		}
 		public UnaryType getVectorAddPrimitive() {
 			return UnaryType.valueOf("VECT_"+getVectorPrimitiveName().toUpperCase()+"_ADD");
@@ -267,7 +270,9 @@ public class CNodeUnary extends CNode
 			case VECT_CUMSUM:
 			case VECT_CUMMIN:
 			case VECT_CUMMAX:
-			case VECT_SIGN: return "u(v"+_type.name().toLowerCase()+")";
+			case VECT_SIGN:
+			case VECT_SIGMOID:
+			case VECT_SPROP:return "u(v"+_type.name().toLowerCase()+")";
 			case LOOKUP_R:  return "u(ixr)";
 			case LOOKUP_C:  return "u(ixc)";
 			case LOOKUP_RC: return "u(ixrc)";
@@ -302,6 +307,8 @@ public class CNodeUnary extends CNode
 			case VECT_CUMSUM:
 			case VECT_CUMMIN:
 			case VECT_CUMMAX:
+			case VECT_SPROP:
+			case VECT_SIGMOID:
 				_rows = _inputs.get(0)._rows;
 				_cols = _inputs.get(0)._cols;
 				_dataType= DataType.MATRIX;

http://git-wip-us.apache.org/repos/asf/systemml/blob/744df813/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 f405516..d133cc4 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
@@ -63,7 +63,7 @@ public class TemplateRow extends TemplateBase
 	private static final Hop.OpOp1[] SUPPORTED_VECT_UNARY = new OpOp1[]{
 			OpOp1.EXP, OpOp1.SQRT, OpOp1.LOG, OpOp1.ABS, OpOp1.ROUND, OpOp1.CEIL, OpOp1.FLOOR, OpOp1.SIGN,
 			OpOp1.SIN, OpOp1.COS, OpOp1.TAN, OpOp1.ASIN, OpOp1.ACOS, OpOp1.ATAN, OpOp1.SINH, OpOp1.COSH, OpOp1.TANH,
-			OpOp1.CUMSUM, OpOp1.CUMMIN, OpOp1.CUMMAX};
+			OpOp1.CUMSUM, OpOp1.CUMMIN, OpOp1.CUMMAX, OpOp1.SPROP, OpOp1.SIGMOID};
 	private static final Hop.OpOp2[] SUPPORTED_VECT_BINARY = new OpOp2[]{
 			OpOp2.MULT, OpOp2.DIV, OpOp2.MINUS, OpOp2.PLUS, OpOp2.POW, OpOp2.MIN, OpOp2.MAX, OpOp2.XOR,
 			OpOp2.EQUAL, OpOp2.NOTEQUAL, OpOp2.LESS, OpOp2.LESSEQUAL, OpOp2.GREATER, OpOp2.GREATEREQUAL};
@@ -272,7 +272,7 @@ public class TemplateRow extends TemplateBase
 				if( cdata1 instanceof CNodeBinary && ((CNodeBinary)cdata1).getType().isVectorScalarPrimitive() )
 					out = new CNodeBinary(cdata1.getInput().get(0), cdata1.getInput().get(1),
 							((CNodeBinary)cdata1).getType().getVectorAddPrimitive());
-				else	
+				else
 					out = cdata1;
 			}
 			else if( ((AggUnaryOp)hop).getDirection() == Direction.RowCol && ((AggUnaryOp)hop).getOp() == AggOp.SUM ) {

http://git-wip-us.apache.org/repos/asf/systemml/blob/744df813/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java b/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java
index 473d849..2f1a4a3 100644
--- a/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java
+++ b/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java
@@ -1504,6 +1504,58 @@ public class LibSpoofPrimitives
 			c[aix[j]] = Math.sqrt(a[j]);
 		return c;
 	}
+
+	//custom sprop
+	
+	public static void vectSpropAdd(double[] a, double[] c, int ai, int ci, int len) {
+		for( int j = ai; j < ai+len; j++, ci++)
+			c[ci] += a[j] * (1 - a[j]);
+	}
+
+	public static void vectSpropAdd(double[] a, double[] c, int[] aix, int ai, int ci, int alen, int len) {
+		for( int j = ai; j < ai+alen; j++ )
+			c[ci + aix[j]] += a[j] * (1 - a[j]);
+	}
+	
+	public static double[] vectSpropWrite(double[] a, int ai, int len) {
+		double[] c = allocVector(len, false);
+		for( int j = 0; j < len; j++, ai++)
+			c[j] = a[j] * (1 - a[j]);
+		return c;
+	}
+
+	public static double[] vectSpropWrite(double[] a, int[] aix, int ai, int alen, int len) {
+		double[] c = allocVector(len, true);
+		for( int j = ai; j < ai+alen; j++ )
+			c[aix[j]] = a[j] * (1 - a[j]);
+		return c;
+	}
+	
+	//custom sigmoid
+	
+	public static void vectSigmoidAdd(double[] a, double[] c, int ai, int ci, int len) {
+		for( int j = ai; j < ai+len; j++, ci++)
+			c[ci] +=  1 / (1 + FastMath.exp(-a[j]));
+	}
+
+	public static void vectSigmoidAdd(double[] a, double[] c, int[] aix, int ai, int ci, int alen, int len) {
+		for( int j = ai; j < ai+alen; j++ )
+			c[ci + aix[j]] += 1 / (1 + FastMath.exp(-a[j]));
+	}
+	
+	public static double[] vectSigmoidWrite(double[] a, int ai, int len) {
+		double[] c = allocVector(len, false);
+		for( int j = 0; j < len; j++, ai++)
+			c[j] = 1 / (1 + FastMath.exp(-a[j]));
+		return c;
+	}
+
+	public static double[] vectSigmoidWrite(double[] a, int[] aix, int ai, int alen, int len) {
+		double[] c = allocVector(len, true, 0.5); //sigmoid(0) = 0.5
+		for( int j = ai; j < ai+alen; j++ )
+			c[aix[j]] = 1 / (1 + FastMath.exp(-a[j]));
+		return c;
+	}
 	
 	//custom vector equal
 	

http://git-wip-us.apache.org/repos/asf/systemml/blob/744df813/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 129178d..bd75c5b 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
@@ -73,6 +73,8 @@ public class RowAggTmplTest extends AutomatedTestBase
 	private static final String TEST_NAME34 = TEST_NAME+"34"; //X / rowSums(X!=0)
 	private static final String TEST_NAME35 = TEST_NAME+"35"; //cbind(X/rowSums(X), Y, Z)
 	private static final String TEST_NAME36 = TEST_NAME+"36"; //xor operation
+	private static final String TEST_NAME37 = TEST_NAME+"37"; //sprop(X/rowSums)
+	private static final String TEST_NAME38 = TEST_NAME+"38"; //sigmoid(X/rowSums)
 	
 	private static final String TEST_DIR = "functions/codegen/";
 	private static final String TEST_CLASS_DIR = TEST_DIR + RowAggTmplTest.class.getSimpleName() + "/";
@@ -84,7 +86,7 @@ public class RowAggTmplTest extends AutomatedTestBase
 	@Override
 	public void setUp() {
 		TestUtils.clearAssertionInformation();
-		for(int i=1; i<=36; i++)
+		for(int i=1; i<=38; i++)
 			addTestConfiguration( TEST_NAME+i, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME+i, new String[] { String.valueOf(i) }) );
 	}
 	
@@ -628,6 +630,36 @@ public class RowAggTmplTest extends AutomatedTestBase
 		testCodegenIntegration( TEST_NAME36, false, ExecType.SPARK );
 	}
 	
+	@Test
+	public void testCodegenRowAggRewrite37CP() {
+		testCodegenIntegration( TEST_NAME37, true, ExecType.CP );
+	}
+
+	@Test
+	public void testCodegenRowAgg37CP() {
+		testCodegenIntegration( TEST_NAME37, false, ExecType.CP );
+	}
+
+	@Test
+	public void testCodegenRowAgg37SP() {
+		testCodegenIntegration( TEST_NAME37, false, ExecType.SPARK );
+	}
+	
+	@Test
+	public void testCodegenRowAggRewrite38CP() {
+		testCodegenIntegration( TEST_NAME38, true, ExecType.CP );
+	}
+
+	@Test
+	public void testCodegenRowAgg38CP() {
+		testCodegenIntegration( TEST_NAME38, false, ExecType.CP );
+	}
+
+	@Test
+	public void testCodegenRowAgg38SP() {
+		testCodegenIntegration( TEST_NAME38, false, ExecType.SPARK );
+	}
+	
 	private void testCodegenIntegration( String testname, boolean rewrites, ExecType instType )
 	{
 		boolean oldFlag = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION;

http://git-wip-us.apache.org/repos/asf/systemml/blob/744df813/src/test/scripts/functions/codegen/rowAggPattern37.R
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/rowAggPattern37.R b/src/test/scripts/functions/codegen/rowAggPattern37.R
new file mode 100644
index 0000000..7cf0047
--- /dev/null
+++ b/src/test/scripts/functions/codegen/rowAggPattern37.R
@@ -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.
+#
+#-------------------------------------------------------------
+
+args<-commandArgs(TRUE)
+options(digits=22)
+library("Matrix")
+library("matrixStats")
+
+X = matrix(seq(1, 6000)/600, 300, 20, byrow=TRUE);
+
+Y = X/(rowSums(X)%*%matrix(1,1,ncol(X)))
+S = Y * (Y-1);
+
+writeMM(as(S, "CsparseMatrix"), paste(args[2], "S", sep=""));

http://git-wip-us.apache.org/repos/asf/systemml/blob/744df813/src/test/scripts/functions/codegen/rowAggPattern37.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/rowAggPattern37.dml b/src/test/scripts/functions/codegen/rowAggPattern37.dml
new file mode 100644
index 0000000..fd6be93
--- /dev/null
+++ b/src/test/scripts/functions/codegen/rowAggPattern37.dml
@@ -0,0 +1,27 @@
+#-------------------------------------------------------------
+#
+# 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, 300, 20);
+
+Y = X/rowSums(X)
+S = Y * (Y-1);
+
+write(S, $1);

http://git-wip-us.apache.org/repos/asf/systemml/blob/744df813/src/test/scripts/functions/codegen/rowAggPattern38.R
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/rowAggPattern38.R b/src/test/scripts/functions/codegen/rowAggPattern38.R
new file mode 100644
index 0000000..fccf1a6
--- /dev/null
+++ b/src/test/scripts/functions/codegen/rowAggPattern38.R
@@ -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.
+#
+#-------------------------------------------------------------
+
+args<-commandArgs(TRUE)
+options(digits=22)
+library("Matrix")
+library("matrixStats")
+
+X = matrix(seq(1, 6000)/600, 300, 20, byrow=TRUE);
+
+Y = X/(rowSums(X)%*%matrix(1,1,ncol(X)))
+S = 1 / (1 + exp(-Y));
+
+writeMM(as(S, "CsparseMatrix"), paste(args[2], "S", sep=""));

http://git-wip-us.apache.org/repos/asf/systemml/blob/744df813/src/test/scripts/functions/codegen/rowAggPattern38.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/rowAggPattern38.dml b/src/test/scripts/functions/codegen/rowAggPattern38.dml
new file mode 100644
index 0000000..b6d46c2
--- /dev/null
+++ b/src/test/scripts/functions/codegen/rowAggPattern38.dml
@@ -0,0 +1,27 @@
+#-------------------------------------------------------------
+#
+# 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, 300, 20);
+
+Y = X/rowSums(X)
+S = 1 / (1 + exp(-Y));
+
+write(S, $1);