This is an automated email from the ASF dual-hosted git repository.

mboehm7 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/systemds.git


The following commit(s) were added to refs/heads/main by this push:
     new 567849f3f1 [SYSTEMDS-3715] Fix robustness for incorrect value types in 
functions
567849f3f1 is described below

commit 567849f3f1fa3a1465b01f05aab84c431ad92b93
Author: e-strauss <lathan...@gmx.de>
AuthorDate: Thu Aug 22 15:32:40 2024 +0200

    [SYSTEMDS-3715] Fix robustness for incorrect value types in functions
    
    Closes #2070.
---
 .../sysds/parser/dml/DmlSyntacticValidator.java    |  4 ++
 .../sysds/test/functions/misc/TypeTyposTest.java   | 79 ++++++++++++++++++++++
 src/test/scripts/functions/misc/WrongDataType.dml  | 27 ++++++++
 src/test/scripts/functions/misc/WrongValueType.dml | 27 ++++++++
 .../functions/tensor/BILSTMBackwardTest.dml        | 13 ++--
 .../scripts/functions/tensor/BILSTMForwardTest.dml | 15 ++--
 6 files changed, 150 insertions(+), 15 deletions(-)

diff --git 
a/src/main/java/org/apache/sysds/parser/dml/DmlSyntacticValidator.java 
b/src/main/java/org/apache/sysds/parser/dml/DmlSyntacticValidator.java
index b80c4d569a..9a1ede09e0 100644
--- a/src/main/java/org/apache/sysds/parser/dml/DmlSyntacticValidator.java
+++ b/src/main/java/org/apache/sysds/parser/dml/DmlSyntacticValidator.java
@@ -622,6 +622,10 @@ public class DmlSyntacticValidator implements DmlListener {
                        //load and add builtin DML-bodied functions
                        String filePath = Builtins.getFilePath(functionName);
                        DMLProgram tmpProg = 
parseAndAddImportedFunctions(namespace, filePath, ctx);
+                       if(tmpProg == null){
+                               //a parse error occured, which was logged in 
the error listener and will be shown in the error message
+                               return;
+                       }
                        FunctionDictionary<FunctionStatementBlock> prog = 
tmpProg.getBuiltinFunctionDictionary();
                        if( prog != null ) { //robustness for existing functions
                                //add builtin functions
diff --git 
a/src/test/java/org/apache/sysds/test/functions/misc/TypeTyposTest.java 
b/src/test/java/org/apache/sysds/test/functions/misc/TypeTyposTest.java
new file mode 100644
index 0000000000..668f061d4c
--- /dev/null
+++ b/src/test/java/org/apache/sysds/test/functions/misc/TypeTyposTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.sysds.test.functions.misc;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.apache.sysds.parser.ParseException;
+import org.apache.sysds.test.AutomatedTestBase;
+import org.apache.sysds.test.TestConfiguration;
+
+public class TypeTyposTest extends AutomatedTestBase
+{
+       private final static String TEST_DIR = "functions/misc/";
+       private final static String TEST_NAME1 = "WrongValueType";
+       private final static String TEST_NAME2 = "WrongDataType";
+       private final static String TEST_CLASS_DIR = TEST_DIR + 
TypeTyposTest.class.getSimpleName() + "/";
+       
+       @Override
+       public void setUp() {
+               addTestConfiguration(TEST_NAME1, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME1, new String[] {}));
+               addTestConfiguration(TEST_NAME2, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME2, new String[] {}));
+       }
+       
+       @Test
+       public void testValueTypes() {
+               runTest( TEST_NAME1, true );
+       }
+       
+       @Test
+       public void testDataTypes() {
+               runTest( TEST_NAME2, true );
+       }
+       
+       private void runTest( String testName, boolean exExp ) {
+               TestConfiguration config = getTestConfiguration(TEST_NAME1);
+               loadTestConfiguration(config);
+               
+               String HOME = SCRIPT_DIR + TEST_DIR;
+               fullDMLScriptName = HOME + testName + ".dml";
+               programArgs = new String[]{"-explain"};
+               
+               PrintStream old = System.err;
+               String out = null;
+               try {
+                       ByteArrayOutputStream buff = new 
ByteArrayOutputStream();
+                       System.setErr(new PrintStream(buff));
+                       runTest(true, exExp, ParseException.class, -1);
+                       out = buff.toString();
+               }
+               finally {
+                       System.setErr(old);
+               }
+               if( testName.equals(TEST_NAME1) )
+                       Assert.assertTrue(out.contains("invalid valuetype"));
+               else if(testName.equals(TEST_NAME2))
+                       Assert.assertTrue(out.contains("incorrect datatype"));
+       }
+}
diff --git a/src/test/scripts/functions/misc/WrongDataType.dml 
b/src/test/scripts/functions/misc/WrongDataType.dml
new file mode 100644
index 0000000000..b1280d6069
--- /dev/null
+++ b/src/test/scripts/functions/misc/WrongDataType.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.
+#
+#-------------------------------------------------------------
+
+fun = function(Matric[Double] X) returns(Matrix[Double] Y) {
+   Y = X + 7;
+}
+
+X = matrix(1, 10, 10);
+print(sum(fun(X)));
diff --git a/src/test/scripts/functions/misc/WrongValueType.dml 
b/src/test/scripts/functions/misc/WrongValueType.dml
new file mode 100644
index 0000000000..07e548f61e
--- /dev/null
+++ b/src/test/scripts/functions/misc/WrongValueType.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.
+#
+#-------------------------------------------------------------
+
+fun = function(Matrix[Doubl] X) return(Matrix[Double] Y) {
+   Y = X + 7;
+}
+
+X = matrix(1, 10, 10);
+print(sum(fun(X)));
diff --git a/src/test/scripts/functions/tensor/BILSTMBackwardTest.dml 
b/src/test/scripts/functions/tensor/BILSTMBackwardTest.dml
index 5ef3ac1c66..036a3219b1 100644
--- a/src/test/scripts/functions/tensor/BILSTMBackwardTest.dml
+++ b/src/test/scripts/functions/tensor/BILSTMBackwardTest.dml
@@ -29,12 +29,11 @@ debug = as.logical(as.integer($5))
 seq = as.logical(as.integer($6))
 
 factor = 0.01
-input_range = matrix("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 
140 141 142 143 144  [...]
-input = input_range*factor
-lstmIn = matrix(input[,1:batch_size*seq_length*num_features], 
rows=batch_size,cols=(seq_length*num_features))
+lstmIn = matrix(seq(0,batch_size*seq_length*num_features - 1)*factor, 
rows=batch_size,cols=(seq_length*num_features))
 
-input = input - (num_features + hidden_size)*hidden_size*factor
-W = matrix(input[,1:(num_features + 
hidden_size)*hidden_size*4],rows=num_features + hidden_size, cols=hidden_size*4)
+W = seq(0, (num_features + hidden_size)*hidden_size*4 - 1)*factor
+W = W - (num_features + hidden_size)*hidden_size*factor
+W = matrix(W,rows=num_features + hidden_size, cols=hidden_size*4)
 b = matrix(1,rows=1, cols=4*hidden_size)*factor
 out0 = matrix(1,rows=batch_size, cols=hidden_size)*factor
 c0 = matrix(0,rows=batch_size, cols=hidden_size)*factor
@@ -46,10 +45,10 @@ c0 = rbind(c0, c0)
 
 dc = matrix(0,rows=batch_size*2,cols=hidden_size)
 if(batch_size == 5){
-  dout = matrix(input_range[,1:batch_size*hidden_size*seq_length*2], 
rows=batch_size, cols=hidden_size*seq_length*2)
+  dout = matrix(seq(0, batch_size*hidden_size*seq_length*2 - 1), 
rows=batch_size, cols=hidden_size*seq_length*2)
 } else if(batch_size == 4) {
   dout = matrix(0, rows=batch_size, cols=hidden_size*2)
-  dc = matrix(input_range[,1:batch_size*hidden_size*2], rows=batch_size*2, 
cols=hidden_size)
+  dc = matrix(seq(0, batch_size*hidden_size*2 - 1), rows=batch_size*2, 
cols=hidden_size)
 } else if(batch_size == 3) {
 
 } else {
diff --git a/src/test/scripts/functions/tensor/BILSTMForwardTest.dml 
b/src/test/scripts/functions/tensor/BILSTMForwardTest.dml
index 772c3bf1a0..e47f841a0c 100644
--- a/src/test/scripts/functions/tensor/BILSTMForwardTest.dml
+++ b/src/test/scripts/functions/tensor/BILSTMForwardTest.dml
@@ -29,16 +29,15 @@ debug = as.logical(as.integer($5))
 seq = as.logical(as.integer($6))
 
 factor = 0.01
-input_range = matrix("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 
140 141 142 143 144  [...]
-input = input_range*factor
-lstmIn = matrix(input[,1:batch_size*seq_length*num_features], 
rows=batch_size,cols=(seq_length*num_features))
+lstmIn = matrix(seq(0,batch_size*seq_length*num_features - 1)*factor, 
rows=batch_size,cols=(seq_length*num_features))
 
-input = input - (num_features + hidden_size)*hidden_size*factor
-W = matrix(input[,1:(num_features + 
hidden_size)*hidden_size*4],rows=num_features + hidden_size, cols=hidden_size*4)
+W = seq(0, (num_features + hidden_size)*hidden_size*4 - 1)*factor
+W = W - (num_features + hidden_size)*hidden_size*factor
+W = matrix(W,rows=num_features + hidden_size, cols=hidden_size*4)
 if(batch_size == 2){
-  b = (matrix(input_range[,1:4*hidden_size], rows=1, cols=4*hidden_size) - 
2*hidden_size)*factor
-  c0 = (matrix(input_range[,1:2*batch_size*hidden_size], rows=batch_size*2, 
cols=hidden_size) - 2*hidden_size)*factor
-  out0 = (matrix(input_range[,1:2*batch_size*hidden_size], rows=batch_size*2, 
cols=hidden_size) + 2*hidden_size)*factor
+  b = (matrix(seq(0,4*hidden_size- 1), rows=1, cols=4*hidden_size) - 
2*hidden_size)*factor
+  c0 = (matrix(seq(0,2*batch_size*hidden_size - 1), rows=batch_size*2, 
cols=hidden_size) - 2*hidden_size)*factor
+  out0 = (matrix(seq(0,2*batch_size*hidden_size - 1), rows=batch_size*2, 
cols=hidden_size) + 2*hidden_size)*factor
 } else {
   b = matrix(1,rows=1, cols=4*hidden_size)*factor
   out0 = matrix(1,rows=batch_size, cols=hidden_size)*factor

Reply via email to