From 5471a777221d44cda2d51d88ca0fa94900efbe31 Mon Sep 17 00:00:00 2001 From: Frxms Date: Tue, 25 Feb 2025 12:44:55 +0100 Subject: [PATCH 01/10] create simple Tests --- .../functions/codegen/rowAggPattern47.R | 35 +++++++++++++++++++ .../functions/codegen/rowAggPattern47.dml | 28 +++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/test/scripts/functions/codegen/rowAggPattern47.R create mode 100644 src/test/scripts/functions/codegen/rowAggPattern47.dml diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.R b/src/test/scripts/functions/codegen/rowAggPattern47.R new file mode 100644 index 00000000000..329d6778ba4 --- /dev/null +++ b/src/test/scripts/functions/codegen/rowAggPattern47.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") + +rowVars <- function(X) { + apply(X, 1, var) +} + +X = matrix(seq(1,6000)/6000, 600, 10, byrow=TRUE); + +R = as.matrix(sum(rowVars(X))); + +writeMM(as(R, "CsparseMatrix"), paste(args[2], "S", sep="")); diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.dml b/src/test/scripts/functions/codegen/rowAggPattern47.dml new file mode 100644 index 00000000000..b0c4bea9ce8 --- /dev/null +++ b/src/test/scripts/functions/codegen/rowAggPattern47.dml @@ -0,0 +1,28 @@ +#------------------------------------------------------------- +# +# 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)/6000, 600, 10); +while(FALSE){} + +R = as.matrix(sum(rowVars(X))); + +write(R, $1) From 382565cdd34b44e79de1cd25fe588671a406d6ac Mon Sep 17 00:00:00 2001 From: Frxms Date: Tue, 25 Feb 2025 12:45:55 +0100 Subject: [PATCH 02/10] add Tests to config files --- .../sysds/test/functions/codegen/RowAggTmplTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/apache/sysds/test/functions/codegen/RowAggTmplTest.java b/src/test/java/org/apache/sysds/test/functions/codegen/RowAggTmplTest.java index d3c9edf8e8c..cf695961805 100644 --- a/src/test/java/org/apache/sysds/test/functions/codegen/RowAggTmplTest.java +++ b/src/test/java/org/apache/sysds/test/functions/codegen/RowAggTmplTest.java @@ -87,6 +87,7 @@ public class RowAggTmplTest extends AutomatedTestBase private static final String TEST_NAME44 = TEST_NAME+"44"; //maxpool(X - mean(X)) + 7; private static final String TEST_NAME45 = TEST_NAME+"45"; //vector allocation; private static final String TEST_NAME46 = TEST_NAME+"46"; //conv2d(X - mean(X), F1) + conv2d(X - mean(X), F2); + private static final String TEST_NAME47 = TEST_NAME+"47"; //sum(rowVar(X)) private static final String TEST_DIR = "functions/codegen/"; private static final String TEST_CLASS_DIR = TEST_DIR + RowAggTmplTest.class.getSimpleName() + "/"; @@ -98,7 +99,7 @@ public class RowAggTmplTest extends AutomatedTestBase @Override public void setUp() { TestUtils.clearAssertionInformation(); - for(int i=1; i<=46; i++) + for(int i=1; i<=47; i++) addTestConfiguration( TEST_NAME+i, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME+i, new String[] { String.valueOf(i) }) ); } @@ -795,6 +796,11 @@ public void testCodegenRowAgg46SP() { testCodegenIntegration( TEST_NAME46, false, ExecType.SPARK ); } + @Test + public void testCodegenRowAgg47() { + testCodegenIntegration(TEST_NAME47, false, ExecType.CP); + } + private void testCodegenIntegration( String testname, boolean rewrites, ExecType instType ) { boolean oldFlag = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION; @@ -807,7 +813,7 @@ private void testCodegenIntegration( String testname, boolean rewrites, ExecType String HOME = SCRIPT_DIR + TEST_DIR; fullDMLScriptName = HOME + testname + ".dml"; - programArgs = new String[]{"-stats", "-args", output("S") }; + programArgs = new String[]{"-explain", "codegen", "-stats", "-args", output("S") }; fullRScriptName = HOME + testname + ".R"; rCmd = getRCmd(inputDir(), expectedDir()); From 53484be93246105044d1130e253b13b3ec330b69 Mon Sep 17 00:00:00 2001 From: Frxms Date: Tue, 25 Feb 2025 17:09:04 +0100 Subject: [PATCH 03/10] implement rowVar into the compiler --- .../sysds/hops/codegen/cplan/CNodeUnary.java | 4 +++- .../sysds/hops/codegen/cplan/java/Unary.java | 2 +- .../sysds/hops/codegen/template/TemplateRow.java | 2 +- .../sysds/runtime/codegen/LibSpoofPrimitives.java | 14 +++++++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeUnary.java b/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeUnary.java index 93cdb2f6612..fe67995b6b5 100644 --- a/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeUnary.java +++ b/src/main/java/org/apache/sysds/hops/codegen/cplan/CNodeUnary.java @@ -33,7 +33,7 @@ public class CNodeUnary extends CNode public enum UnaryType { LOOKUP_R, LOOKUP_C, LOOKUP_RC, LOOKUP0, //codegen specific ROW_SUMS, ROW_SUMSQS, ROW_COUNTNNZS, //codegen specific - ROW_MEANS, ROW_MINS, ROW_MAXS, + ROW_MEANS, ROW_MINS, ROW_MAXS, ROW_VARS, VECT_EXP, VECT_POW2, VECT_MULT2, VECT_SQRT, VECT_LOG, VECT_ABS, VECT_ROUND, VECT_CEIL, VECT_FLOOR, VECT_SIGN, VECT_SIN, VECT_COS, VECT_TAN, VECT_ASIN, VECT_ACOS, VECT_ATAN, @@ -139,6 +139,7 @@ public String toString() { case ROW_MINS: return "u(Rmin)"; case ROW_MAXS: return "u(Rmax)"; case ROW_MEANS: return "u(Rmean)"; + case ROW_VARS: return "u(Rvar)"; case ROW_COUNTNNZS: return "u(Rnnz)"; case VECT_EXP: case VECT_POW2: @@ -210,6 +211,7 @@ public void setOutputDims() { case ROW_MINS: case ROW_MAXS: case ROW_MEANS: + case ROW_VARS: case ROW_COUNTNNZS: case EXP: case LOOKUP_R: diff --git a/src/main/java/org/apache/sysds/hops/codegen/cplan/java/Unary.java b/src/main/java/org/apache/sysds/hops/codegen/cplan/java/Unary.java index 50ea2bace86..d8a1085df58 100644 --- a/src/main/java/org/apache/sysds/hops/codegen/cplan/java/Unary.java +++ b/src/main/java/org/apache/sysds/hops/codegen/cplan/java/Unary.java @@ -32,12 +32,12 @@ public String getTemplate(UnaryType type, boolean sparse) { case ROW_MINS: case ROW_MAXS: case ROW_MEANS: + case ROW_VARS: case ROW_COUNTNNZS: { String vectName = StringUtils.capitalize(type.name().substring(4, type.name().length()-1).toLowerCase()); return sparse ? " double %TMP% = LibSpoofPrimitives.vect"+vectName+"(%IN1v%, %IN1i%, %POS1%, alen, len);\n": " double %TMP% = LibSpoofPrimitives.vect"+vectName+"(%IN1%, %POS1%, %LEN%);\n"; } - case VECT_EXP: case VECT_POW2: case VECT_MULT2: diff --git a/src/main/java/org/apache/sysds/hops/codegen/template/TemplateRow.java b/src/main/java/org/apache/sysds/hops/codegen/template/TemplateRow.java index 4ac38b1487c..82faf9b12ec 100644 --- a/src/main/java/org/apache/sysds/hops/codegen/template/TemplateRow.java +++ b/src/main/java/org/apache/sysds/hops/codegen/template/TemplateRow.java @@ -67,7 +67,7 @@ public class TemplateRow extends TemplateBase { - private static final AggOp[] SUPPORTED_ROW_AGG = new AggOp[]{AggOp.SUM, AggOp.MIN, AggOp.MAX, AggOp.MEAN}; + private static final AggOp[] SUPPORTED_ROW_AGG = new AggOp[]{AggOp.SUM, AggOp.MIN, AggOp.MAX, AggOp.MEAN, AggOp.VAR}; private static final 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, diff --git a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java index 6497b6f321f..4672e99c224 100644 --- a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java +++ b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java @@ -2151,7 +2151,19 @@ public static double[] vectConv2dmmWrite(double[] a, double[] b, int ai, int bi, new DenseBlockFP64(new int[]{K, PQ}, c), PQ, CRS, 0, K, 0, PQ); return c; } - + + public static double vectVar(double[] a, int ai, int len) { + double meanVal = vectMean(a, ai, len); + double[] aSqr = vectPow2Write(a, ai, len); + return ((double) 1 /len)*(vectSum(aSqr, ai, len)-meanVal); + } + + public static double vectVar(double[] avals, int[] aix, int ai, int alen, int len) { + double meanVal = vectMean(avals, aix, ai, alen, len); + double[] avalsSqr = vectPow2Write(avals, aix, ai, alen, len); + return ((double) 1 /len)*(vectSum(avalsSqr, ai, len)-meanVal); + } + //complex builtin functions that are not directly generated //(included here in order to reduce the number of imports) From 38735536febf380a051091f43dd331be1355e973 Mon Sep 17 00:00:00 2001 From: Frxms Date: Wed, 26 Feb 2025 23:30:44 +0100 Subject: [PATCH 04/10] fix out of bounds exception --- .../apache/sysds/runtime/codegen/LibSpoofPrimitives.java | 8 ++++---- src/test/scripts/functions/codegen/rowAggPattern47.R | 3 ++- src/test/scripts/functions/codegen/rowAggPattern47.dml | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java index 4672e99c224..baf1f844d0a 100644 --- a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java +++ b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java @@ -2153,15 +2153,15 @@ public static double[] vectConv2dmmWrite(double[] a, double[] b, int ai, int bi, } public static double vectVar(double[] a, int ai, int len) { - double meanVal = vectMean(a, ai, len); + double meanVal = Math.pow(vectMean(a, ai, len), 2); double[] aSqr = vectPow2Write(a, ai, len); - return ((double) 1 /len)*(vectSum(aSqr, ai, len)-meanVal); + return ((double) 1 /len)*(vectSum(aSqr, 0, len)-meanVal); } public static double vectVar(double[] avals, int[] aix, int ai, int alen, int len) { - double meanVal = vectMean(avals, aix, ai, alen, len); + double meanVal = Math.pow(vectMean(avals, aix, ai, alen, len), 2); double[] avalsSqr = vectPow2Write(avals, aix, ai, alen, len); - return ((double) 1 /len)*(vectSum(avalsSqr, ai, len)-meanVal); + return ((double) 1 /len)*(vectSum(avalsSqr, 0, alen)-meanVal); } //complex builtin functions that are not directly generated diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.R b/src/test/scripts/functions/codegen/rowAggPattern47.R index 329d6778ba4..073fe7707a0 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern47.R +++ b/src/test/scripts/functions/codegen/rowAggPattern47.R @@ -28,7 +28,8 @@ rowVars <- function(X) { apply(X, 1, var) } -X = matrix(seq(1,6000)/6000, 600, 10, byrow=TRUE); +# X = matrix(seq(1,6000)/6000, 600, 10, byrow=TRUE); +X = matrix(2, 10, 10); R = as.matrix(sum(rowVars(X))); diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.dml b/src/test/scripts/functions/codegen/rowAggPattern47.dml index b0c4bea9ce8..e33de1a57d8 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern47.dml +++ b/src/test/scripts/functions/codegen/rowAggPattern47.dml @@ -19,8 +19,9 @@ # #------------------------------------------------------------- +#X = matrix(seq(1,6000)/6000, 600, 10); +X = matrix(2, 10, 10) -X = matrix(seq(1,6000)/6000, 600, 10); while(FALSE){} R = as.matrix(sum(rowVars(X))); From 19b12eb21b214ffa1b8a5ea5bc56718c488a8064 Mon Sep 17 00:00:00 2001 From: Frxms Date: Thu, 27 Feb 2025 14:51:44 +0100 Subject: [PATCH 05/10] debug vectVar --- .../org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java | 4 ++-- src/test/scripts/functions/codegen/rowAggPattern47.R | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java index baf1f844d0a..8b6dd834a81 100644 --- a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java +++ b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java @@ -2155,13 +2155,13 @@ public static double[] vectConv2dmmWrite(double[] a, double[] b, int ai, int bi, public static double vectVar(double[] a, int ai, int len) { double meanVal = Math.pow(vectMean(a, ai, len), 2); double[] aSqr = vectPow2Write(a, ai, len); - return ((double) 1 /len)*(vectSum(aSqr, 0, len)-meanVal); + return ((double) 1 /len)*vectSum(aSqr, 0, len)-meanVal; } public static double vectVar(double[] avals, int[] aix, int ai, int alen, int len) { double meanVal = Math.pow(vectMean(avals, aix, ai, alen, len), 2); double[] avalsSqr = vectPow2Write(avals, aix, ai, alen, len); - return ((double) 1 /len)*(vectSum(avalsSqr, 0, alen)-meanVal); + return ((double) 1 /len)*vectSum(avalsSqr, 0, alen)-meanVal; } //complex builtin functions that are not directly generated diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.R b/src/test/scripts/functions/codegen/rowAggPattern47.R index 073fe7707a0..0efa05ad96f 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern47.R +++ b/src/test/scripts/functions/codegen/rowAggPattern47.R @@ -25,7 +25,7 @@ library("Matrix") library("matrixStats") rowVars <- function(X) { - apply(X, 1, var) + apply(X, 1, function(x) sum((x - mean(x))^2) / length(x)) } # X = matrix(seq(1,6000)/6000, 600, 10, byrow=TRUE); From af60070c10215ec68ece9466c7a3ce7bf79f9b53 Mon Sep 17 00:00:00 2001 From: Frxms Date: Mon, 3 Mar 2025 09:39:30 +0100 Subject: [PATCH 06/10] sparse vectVar fix --- .../org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java index 8b6dd834a81..28db83a8634 100644 --- a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java +++ b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java @@ -2161,7 +2161,7 @@ public static double vectVar(double[] a, int ai, int len) { public static double vectVar(double[] avals, int[] aix, int ai, int alen, int len) { double meanVal = Math.pow(vectMean(avals, aix, ai, alen, len), 2); double[] avalsSqr = vectPow2Write(avals, aix, ai, alen, len); - return ((double) 1 /len)*vectSum(avalsSqr, 0, alen)-meanVal; + return ((double) 1 /len)*vectSum(avalsSqr, 0, len)-meanVal; } //complex builtin functions that are not directly generated From 85d20c2eb0c50487654903bcfd1f06ecf702c01d Mon Sep 17 00:00:00 2001 From: Frxms Date: Fri, 7 Mar 2025 06:59:30 +0100 Subject: [PATCH 07/10] test files --- .../functions/codegen/rowAggPattern47.R | 4 +-- .../functions/codegen/rowAggPattern47.dml | 4 +-- .../functions/codegen/rowAggPattern48.R | 27 +++++++++++++++++++ .../functions/codegen/rowAggPattern48.dml | 26 ++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/test/scripts/functions/codegen/rowAggPattern48.R create mode 100644 src/test/scripts/functions/codegen/rowAggPattern48.dml diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.R b/src/test/scripts/functions/codegen/rowAggPattern47.R index 0efa05ad96f..998b8ed96fb 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern47.R +++ b/src/test/scripts/functions/codegen/rowAggPattern47.R @@ -28,8 +28,8 @@ rowVars <- function(X) { apply(X, 1, function(x) sum((x - mean(x))^2) / length(x)) } -# X = matrix(seq(1,6000)/6000, 600, 10, byrow=TRUE); -X = matrix(2, 10, 10); +X = matrix(seq(7, 100*100+6), 100, 100, byrow=TRUE); +# X = matrix(2, 10, 10); R = as.matrix(sum(rowVars(X))); diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.dml b/src/test/scripts/functions/codegen/rowAggPattern47.dml index e33de1a57d8..d2f7aa4f9b7 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern47.dml +++ b/src/test/scripts/functions/codegen/rowAggPattern47.dml @@ -19,8 +19,8 @@ # #------------------------------------------------------------- -#X = matrix(seq(1,6000)/6000, 600, 10); -X = matrix(2, 10, 10) +X = matrix(seq(7, 100*100+6), 100, 100); +#X = matrix(2, 10, 10) while(FALSE){} diff --git a/src/test/scripts/functions/codegen/rowAggPattern48.R b/src/test/scripts/functions/codegen/rowAggPattern48.R new file mode 100644 index 00000000000..ca812a0fa61 --- /dev/null +++ b/src/test/scripts/functions/codegen/rowAggPattern48.R @@ -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. +# +#------------------------------------------------------------- + + +while(FALSE){} + +R = as.matrix(sum(rowVars(X))); + +write(R, $1) diff --git a/src/test/scripts/functions/codegen/rowAggPattern48.dml b/src/test/scripts/functions/codegen/rowAggPattern48.dml new file mode 100644 index 00000000000..2ff5003838b --- /dev/null +++ b/src/test/scripts/functions/codegen/rowAggPattern48.dml @@ -0,0 +1,26 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +while(FALSE){} + +R = as.matrix(sum(rowVars(X))); + +write(R, $1) \ No newline at end of file From e14c35b0993bf935b3a85d72ea6f99792ac4d4dc Mon Sep 17 00:00:00 2001 From: Frxms Date: Tue, 18 Mar 2025 16:23:32 +0100 Subject: [PATCH 08/10] write tests --- .../scripts/functions/codegen/rowAggPattern47.R | 12 ++++++------ .../scripts/functions/codegen/rowAggPattern47.dml | 6 +++--- .../scripts/functions/codegen/rowAggPattern48.R | 13 +++++++++++-- .../scripts/functions/codegen/rowAggPattern48.dml | 4 ++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.R b/src/test/scripts/functions/codegen/rowAggPattern47.R index 998b8ed96fb..9d6d4bc9f65 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern47.R +++ b/src/test/scripts/functions/codegen/rowAggPattern47.R @@ -24,13 +24,13 @@ options(digits=22) library("Matrix") library("matrixStats") -rowVars <- function(X) { - apply(X, 1, function(x) sum((x - mean(x))^2) / length(x)) -} +# rowVars <- function(X) { +# apply(X, 1, function(x) sum((x - mean(x))^2) / length(x)) +# } -X = matrix(seq(7, 100*100+6), 100, 100, byrow=TRUE); -# X = matrix(2, 10, 10); +X = matrix(seq(7, 50*10+6), 50, 10, byrow=TRUE); +z = seq(1,50) -R = as.matrix(sum(rowVars(X))); +R = as.matrix(sum(X + rowVars(X))); writeMM(as(R, "CsparseMatrix"), paste(args[2], "S", sep="")); diff --git a/src/test/scripts/functions/codegen/rowAggPattern47.dml b/src/test/scripts/functions/codegen/rowAggPattern47.dml index d2f7aa4f9b7..e3ee077fc14 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern47.dml +++ b/src/test/scripts/functions/codegen/rowAggPattern47.dml @@ -19,11 +19,11 @@ # #------------------------------------------------------------- -X = matrix(seq(7, 100*100+6), 100, 100); -#X = matrix(2, 10, 10) +X = matrix(seq(7, 50*10+6), 50, 10); +z = seq(1,50) while(FALSE){} -R = as.matrix(sum(rowVars(X))); +R = as.matrix(sum(X + rowVars(X))); write(R, $1) diff --git a/src/test/scripts/functions/codegen/rowAggPattern48.R b/src/test/scripts/functions/codegen/rowAggPattern48.R index ca812a0fa61..bec1427d619 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern48.R +++ b/src/test/scripts/functions/codegen/rowAggPattern48.R @@ -18,10 +18,19 @@ # under the License. # #------------------------------------------------------------- +args<-commandArgs(TRUE) +options(digits=22) +library("Matrix") +library("matrixStats") +# rowVars <- function(X) { +# apply(X, 1, function(x) sum((x - mean(x))^2) / length(x)) +# } -while(FALSE){} +Z = matrix(seq(1,10), 1, 10) +Y = matrix(0, 10, 10) +X = rbind(Y, Z, Y) R = as.matrix(sum(rowVars(X))); -write(R, $1) +writeMM(as(R, "CsparseMatrix"), paste(args[2], "S", sep="")); diff --git a/src/test/scripts/functions/codegen/rowAggPattern48.dml b/src/test/scripts/functions/codegen/rowAggPattern48.dml index 2ff5003838b..c367a359cdf 100644 --- a/src/test/scripts/functions/codegen/rowAggPattern48.dml +++ b/src/test/scripts/functions/codegen/rowAggPattern48.dml @@ -19,6 +19,10 @@ # #------------------------------------------------------------- +Z = matrix(seq(1,10), 1, 10) +Y = matrix(0, 10, 10) +X = rbind(Y, Z, Y) + while(FALSE){} R = as.matrix(sum(rowVars(X))); From 341cb80cadbf38510d2588a86f8760d1823f5528 Mon Sep 17 00:00:00 2001 From: Frxms Date: Tue, 18 Mar 2025 16:23:51 +0100 Subject: [PATCH 09/10] adjust var function --- .../org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java index 28db83a8634..6c0dc395c3f 100644 --- a/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java +++ b/src/main/java/org/apache/sysds/runtime/codegen/LibSpoofPrimitives.java @@ -2155,13 +2155,13 @@ public static double[] vectConv2dmmWrite(double[] a, double[] b, int ai, int bi, public static double vectVar(double[] a, int ai, int len) { double meanVal = Math.pow(vectMean(a, ai, len), 2); double[] aSqr = vectPow2Write(a, ai, len); - return ((double) 1 /len)*vectSum(aSqr, 0, len)-meanVal; + return (vectSum(aSqr, 0, len)-len*meanVal)/(len-1); } public static double vectVar(double[] avals, int[] aix, int ai, int alen, int len) { double meanVal = Math.pow(vectMean(avals, aix, ai, alen, len), 2); double[] avalsSqr = vectPow2Write(avals, aix, ai, alen, len); - return ((double) 1 /len)*vectSum(avalsSqr, 0, len)-meanVal; + return (vectSum(avalsSqr, 0, len)-len*meanVal)/(len-1); } //complex builtin functions that are not directly generated From c925ed9020ad81c870bd04e7796c09574b4eb60d Mon Sep 17 00:00:00 2001 From: Frxms Date: Tue, 18 Mar 2025 16:30:23 +0100 Subject: [PATCH 10/10] create and run test configs --- .../functions/codegen/RowAggTmplTest.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/apache/sysds/test/functions/codegen/RowAggTmplTest.java b/src/test/java/org/apache/sysds/test/functions/codegen/RowAggTmplTest.java index cf695961805..b89f3007b4a 100644 --- a/src/test/java/org/apache/sysds/test/functions/codegen/RowAggTmplTest.java +++ b/src/test/java/org/apache/sysds/test/functions/codegen/RowAggTmplTest.java @@ -87,8 +87,9 @@ public class RowAggTmplTest extends AutomatedTestBase private static final String TEST_NAME44 = TEST_NAME+"44"; //maxpool(X - mean(X)) + 7; private static final String TEST_NAME45 = TEST_NAME+"45"; //vector allocation; private static final String TEST_NAME46 = TEST_NAME+"46"; //conv2d(X - mean(X), F1) + conv2d(X - mean(X), F2); - private static final String TEST_NAME47 = TEST_NAME+"47"; //sum(rowVar(X)) - + private static final String TEST_NAME47 = TEST_NAME+"47"; //sum(X + rowVars(X)) + private static final String TEST_NAME48 = TEST_NAME+"48"; //sum(rowVars(X)) + private static final String TEST_DIR = "functions/codegen/"; private static final String TEST_CLASS_DIR = TEST_DIR + RowAggTmplTest.class.getSimpleName() + "/"; private final static String TEST_CONF = "SystemDS-config-codegen.xml"; @@ -99,7 +100,7 @@ public class RowAggTmplTest extends AutomatedTestBase @Override public void setUp() { TestUtils.clearAssertionInformation(); - for(int i=1; i<=47; i++) + for(int i=1; i<=48; i++) addTestConfiguration( TEST_NAME+i, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME+i, new String[] { String.valueOf(i) }) ); } @@ -797,8 +798,33 @@ public void testCodegenRowAgg46SP() { } @Test - public void testCodegenRowAgg47() { - testCodegenIntegration(TEST_NAME47, false, ExecType.CP); + public void testCodegenRowAggRewrite47CP() { + testCodegenIntegration( TEST_NAME47, true, ExecType.CP ); + } + + @Test + public void testCodegenRowAgg47CP() { + testCodegenIntegration( TEST_NAME47, false, ExecType.CP ); + } + + @Test + public void testCodegenRowAgg47SP() { + testCodegenIntegration( TEST_NAME47, false, ExecType.SPARK ); + } + + @Test + public void testCodegenRowAggRewrite48CP() { + testCodegenIntegration( TEST_NAME48, true, ExecType.CP ); + } + + @Test + public void testCodegenRowAgg48CP() { + testCodegenIntegration( TEST_NAME48, false, ExecType.CP ); + } + + @Test + public void testCodegenRowAgg48SP() { + testCodegenIntegration( TEST_NAME48, false, ExecType.SPARK ); } private void testCodegenIntegration( String testname, boolean rewrites, ExecType instType )