Skip to content

Commit 417bf3e

Browse files
committed
[SYSTEMDS-3833] Fix robustness seq() length computation
Size inference and runtime operations all utilize the same primitive for computing the seq length. However, this primitive must be able to handle non-integer (from,to,incr) values and as such is prone to round-off-errors. We now added a compensation that detects such round off errors and corrects the resulting size.
1 parent 4e62522 commit 417bf3e

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/main/java/org/apache/sysds/runtime/util/UtilFunctions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,17 @@ public static long getSeqLength(double from, double to, double incr, boolean che
438438
//a very small increment. Hence, we use a different formulation
439439
//that exhibits better numerical stability by avoiding the subtraction
440440
//of numbers of different magnitude.
441+
//Additionally we check the resulting length and add 1 if this check
442+
//allows inferring that round-off errors happened.
441443
if( (isSpecial(from) || isSpecial(to) || isSpecial(incr)
442444
|| (from > to && incr > 0) || (from < to && incr < 0)) ) {
443445
if( check )
444446
throw new RuntimeException("Invalid seq parameters: ("+from+", "+to+", "+incr+")");
445447
else
446448
return 0; // invalid loop configuration
447449
}
448-
return 1L + (long) Math.floor(to/incr - from/incr);
450+
long tmp = 1L + (long) Math.floor(to/incr - from/incr);
451+
return tmp + ((from+tmp*incr <= to) ? 1 : 0);
449452
}
450453

451454
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.sysds.test.functions.io;
21+
22+
import org.junit.Test;
23+
import org.apache.sysds.test.AutomatedTestBase;
24+
import org.apache.sysds.test.TestConfiguration;
25+
import org.apache.sysds.test.TestUtils;
26+
27+
public class SeqSizeTest extends AutomatedTestBase {
28+
29+
private final static String TEST_NAME = "SeqSizeTest";
30+
private final static String TEST_DIR = "functions/io/";
31+
private final static String TEST_CLASS_DIR = TEST_DIR + SeqSizeTest.class.getSimpleName() + "/";
32+
33+
private final static double eps = 1e-9;
34+
35+
@Override
36+
public void setUp() {
37+
TestUtils.clearAssertionInformation();
38+
addTestConfiguration(TEST_NAME,
39+
new TestConfiguration(TEST_CLASS_DIR, TEST_NAME, new String[] { "Rout" }) );
40+
}
41+
42+
@Test
43+
public void runSizeTest() {
44+
TestConfiguration config = getTestConfiguration(TEST_NAME);
45+
loadTestConfiguration(config);
46+
String HOME = SCRIPT_DIR + TEST_DIR;
47+
fullDMLScriptName = HOME + TEST_NAME + ".dml";
48+
programArgs = new String[]{"-explain", "-args", output("R")};
49+
runTest(true, false, null, -1);
50+
double dmlScalar = TestUtils.readDMLScalar(output("R"));
51+
TestUtils.compareScalars(dmlScalar, 5, eps);
52+
}
53+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#-------------------------------------------------------------
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
#
20+
#-------------------------------------------------------------
21+
22+
x = length(seq(1,1001,250))
23+
write(x, $1);

0 commit comments

Comments
 (0)