You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemds.apache.org by yw...@apache.org on 2026/07/21 09:32:18 UTC
(systemds) branch main updated: [SYSTEMDS-3922] Fix distributed (SPARK/FED) quantile pick instruction
This is an automated email from the ASF dual-hosted git repository.
ywcb00 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 5d81e3d76e [SYSTEMDS-3922] Fix distributed (SPARK/FED) quantile pick instruction
5d81e3d76e is described below
commit 5d81e3d76e686dd5ed6235d17ef011019366a684
Author: MegaByteTron <y2...@privaterelay.appleid.com>
AuthorDate: Tue Jul 21 11:23:09 2026 +0200
[SYSTEMDS-3922] Fix distributed (SPARK/FED) quantile pick instruction
Closes #2497
---
.../fed/QuantilePickFEDInstruction.java | 7 ++--
.../spark/QuantilePickSPInstruction.java | 48 ++++++++++++----------
.../sysds/runtime/matrix/data/MatrixBlock.java | 1 +
.../test/functions/binary/matrix/QuantileTest.java | 32 ++++++++++-----
.../primitives/part2/FederatedQuantileTest.java | 11 +----
.../scripts/functions/binary/matrix/MedianBug.R | 34 +++++++++++++++
.../scripts/functions/binary/matrix/MedianBug.dml | 25 +++++++++++
7 files changed, 114 insertions(+), 44 deletions(-)
diff --git a/src/main/java/org/apache/sysds/runtime/instructions/fed/QuantilePickFEDInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/fed/QuantilePickFEDInstruction.java
index e70f1dffa6..d16e1e16ff 100644
--- a/src/main/java/org/apache/sysds/runtime/instructions/fed/QuantilePickFEDInstruction.java
+++ b/src/main/java/org/apache/sysds/runtime/instructions/fed/QuantilePickFEDInstruction.java
@@ -234,7 +234,7 @@ public class QuantilePickFEDInstruction extends BinaryFEDInstruction {
public <T> void processRowQPick(ExecutionContext ec) {
MatrixObject in = ec.getMatrixObject(input1);
FederationMap fedMap = in.getFedMapping();
- boolean average = _type == OperationTypes.MEDIAN;
+ boolean average = _type == OperationTypes.MEDIAN || _type == OperationTypes.VALUEPICK;
double[] quantiles = input2 != null ? (input2.isMatrix() ? ec.getMatrixInput(input2).getDenseBlockValues() :
input2.isScalar() ? new double[] {ec.getScalarInput(input2).getDoubleValue()} : null) :
@@ -749,16 +749,17 @@ public class QuantilePickFEDInstruction extends BinaryFEDInstruction {
super(new long[] {input});
_quantiles = quantiles;
}
+
@Override
public FederatedResponse execute(ExecutionContext ec, Data... data) {
MatrixBlock mb = ((MatrixObject)data[0]).acquireReadAndRelease();
MatrixBlock picked;
if (_quantiles.getLength() == 1) {
return new FederatedResponse(FederatedResponse.ResponseType.SUCCESS,
- new Object[] {mb.pickValue(_quantiles.get(0, 0))});
+ new Object[] {mb.pickValue(_quantiles.get(0, 0), mb.getLength() % 2 == 0)});
}
else {
- picked = mb.pickValues(_quantiles, new MatrixBlock());
+ picked = mb.pickValues(_quantiles, new MatrixBlock(), mb.getLength() % 2 == 0);
return new FederatedResponse(FederatedResponse.ResponseType.SUCCESS,
new Object[] {picked});
}
diff --git a/src/main/java/org/apache/sysds/runtime/instructions/spark/QuantilePickSPInstruction.java b/src/main/java/org/apache/sysds/runtime/instructions/spark/QuantilePickSPInstruction.java
index a30fbc456c..75f8488247 100644
--- a/src/main/java/org/apache/sysds/runtime/instructions/spark/QuantilePickSPInstruction.java
+++ b/src/main/java/org/apache/sysds/runtime/instructions/spark/QuantilePickSPInstruction.java
@@ -115,12 +115,12 @@ public class QuantilePickSPInstruction extends BinarySPInstruction {
if( input2.isScalar() ) {
ScalarObject quantile = ec.getScalarInput(input2);
double[] wt = getWeightedQuantileSummary(in, mc,
- new double[]{quantile.getDoubleValue()});
+ new double[] {quantile.getDoubleValue()}, true);
ec.setScalarOutput(output.getName(), new DoubleObject(wt[3]));
}
else {
- double[] wt = getWeightedQuantileSummary(in, mc, DataConverter
- .convertToDoubleVector(ec.getMatrixInput(input2.getName())));
+ double[] wt = getWeightedQuantileSummary(in, mc,
+ DataConverter.convertToDoubleVector(ec.getMatrixInput(input2.getName())), true);
ec.releaseMatrixInput(input2.getName());
int qlen = wt.length/3;
MatrixBlock out = new MatrixBlock(qlen,1,false);
@@ -130,20 +130,20 @@ public class QuantilePickSPInstruction extends BinarySPInstruction {
}
break;
}
-
+
case MEDIAN: {
- double[] wt = getWeightedQuantileSummary(in, mc, new double[]{0.5});
+ double[] wt = getWeightedQuantileSummary(in, mc, new double[] {0.5}, true);
ec.setScalarOutput(output.getName(), new DoubleObject(wt[3]));
break;
}
-
+
case IQM: {
- double[] wt = getWeightedQuantileSummary(in, mc, new double[]{0.25,0.75});
- long key25 = (long)Math.ceil(wt[1]);
- long key75 = (long)Math.ceil(wt[2]);
- JavaPairRDD<MatrixIndexes,MatrixBlock> out = in
- .filter(new FilterFunction(key25+1,key75,mc.getBlocksize()))
- .mapToPair(new ExtractAndSumFunction(key25+1, key75, mc.getBlocksize()));
+ double[] wt = getWeightedQuantileSummary(in, mc, new double[] {0.25, 0.75}, false);
+ long key25 = (long) Math.ceil(wt[1]);
+ long key75 = (long) Math.ceil(wt[2]);
+ JavaPairRDD<MatrixIndexes, MatrixBlock> out = in
+ .filter(new FilterFunction(key25 + 1, key75, mc.getBlocksize()))
+ .mapToPair(new ExtractAndSumFunction(key25 + 1, key75, mc.getBlocksize()));
double sum = RDDAggregateUtils.sumStable(out).get(0, 0);
double val = MatrixBlock.computeIQMCorrection(
sum, wt[0], wt[3], wt[5], wt[4], wt[6]);
@@ -165,10 +165,10 @@ public class QuantilePickSPInstruction extends BinarySPInstruction {
* @param quantiles one or more quantiles between 0 and 1.
* @return a summary of weighted quantiles
*/
- private static double[] getWeightedQuantileSummary(JavaPairRDD<MatrixIndexes,MatrixBlock> w, DataCharacteristics mc, double[] quantiles)
- {
- double[] ret = new double[3*quantiles.length + 1];
- if( mc.getCols()==2 ) //weighted
+ private static double[] getWeightedQuantileSummary(JavaPairRDD<MatrixIndexes, MatrixBlock> w,
+ DataCharacteristics mc, double[] quantiles, boolean average) {
+ double[] ret = new double[3 * quantiles.length + 1];
+ if(mc.getCols() == 2) // weighted
{
//sort blocks (values sorted but blocks and partitions are not)
w = w.sortByKey();
@@ -213,11 +213,17 @@ public class QuantilePickSPInstruction extends BinarySPInstruction {
}
else {
ret[0] = mc.getRows();
- for( int i=0; i<quantiles.length; i++ ){
- ret[i+1] = quantiles[i] * mc.getRows();
- ret[i+quantiles.length+1] = Math.ceil(ret[i+1])-ret[i+1];
- ret[i+2*quantiles.length+1] = lookupKey(w,
- (long)Math.ceil(ret[i+1]), mc.getBlocksize());
+ for(int i = 0; i < quantiles.length; i++) {
+ ret[i + 1] = quantiles[i] * mc.getRows();
+ ret[i + quantiles.length + 1] = Math.ceil(ret[i + 1]) - ret[i + 1];
+ long key = (long) Math.ceil(ret[i + 1]);
+ ret[i + 2 * quantiles.length + 1] = lookupKey(w, key, mc.getBlocksize());
+
+ // average w/ next value for even-length arrays (mirrors CP QuantilePickCPInstruction)
+ if(average && mc.getRows() % 2 == 0 && key < (mc.getRows() - 1)) {
+ ret[i + 2 * quantiles.length + 1] += lookupKey(w, key + 1, mc.getBlocksize());
+ ret[i + 2 * quantiles.length + 1] /= 2;
+ }
}
}
diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/MatrixBlock.java b/src/main/java/org/apache/sysds/runtime/matrix/data/MatrixBlock.java
index 361d190bd0..18bc30094c 100644
--- a/src/main/java/org/apache/sysds/runtime/matrix/data/MatrixBlock.java
+++ b/src/main/java/org/apache/sysds/runtime/matrix/data/MatrixBlock.java
@@ -4784,6 +4784,7 @@ public class MatrixBlock extends MatrixValue implements CacheBlock<MatrixBlock>,
output.reset(qs.rlen, qs.clen, false);
for ( int i=0; i < qs.rlen; i++ ) {
+ // FIXME: [SYSTEMDS-3953] consider the average parameter here
output.set(i, 0, this.pickValue(qs.get(i,0)) );
}
diff --git a/src/test/java/org/apache/sysds/test/functions/binary/matrix/QuantileTest.java b/src/test/java/org/apache/sysds/test/functions/binary/matrix/QuantileTest.java
index cd10151007..489b31080e 100644
--- a/src/test/java/org/apache/sysds/test/functions/binary/matrix/QuantileTest.java
+++ b/src/test/java/org/apache/sysds/test/functions/binary/matrix/QuantileTest.java
@@ -35,6 +35,7 @@ public class QuantileTest extends AutomatedTestBase
private final static String TEST_NAME2 = "Median";
private final static String TEST_NAME3 = "IQM";
private final static String TEST_NAME4 = "QuantileBug";
+ private final static String TEST_NAME5 = "MedianBug";
private final static String TEST_DIR = "functions/binary/matrix/";
private final static String TEST_CLASS_DIR = TEST_DIR + QuantileTest.class.getSimpleName() + "/";
@@ -49,14 +50,16 @@ public class QuantileTest extends AutomatedTestBase
public void setUp()
{
TestUtils.clearAssertionInformation();
- addTestConfiguration(TEST_NAME1,
+ addTestConfiguration(TEST_NAME1,
new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1, new String[] { "R" }) );
- addTestConfiguration(TEST_NAME2,
+ addTestConfiguration(TEST_NAME2,
new TestConfiguration(TEST_CLASS_DIR, TEST_NAME2, new String[] { "R" }) );
- addTestConfiguration(TEST_NAME3,
+ addTestConfiguration(TEST_NAME3,
new TestConfiguration(TEST_CLASS_DIR, TEST_NAME3, new String[] { "R" }) );
- addTestConfiguration(TEST_NAME4,
+ addTestConfiguration(TEST_NAME4,
new TestConfiguration(TEST_CLASS_DIR, TEST_NAME4, new String[] { "R" }) );
+ addTestConfiguration(TEST_NAME5,
+ new TestConfiguration(TEST_CLASS_DIR, TEST_NAME5, new String[] { "R" }) );
}
@Test
@@ -164,12 +167,21 @@ public class QuantileTest extends AutomatedTestBase
runQuantileTest(TEST_NAME4, 0.5, false, ExecType.CP);
}
-// TODO reimplement distributed value pick logic
-// @Test
-// public void testQuantileBugSP() {
-// runQuantileTest(TEST_NAME4, 0.5, false, ExecType.SPARK);
-// }
-
+ @Test
+ public void testQuantileBugSP() {
+ runQuantileTest(TEST_NAME4, 0.5, false, ExecType.SPARK);
+ }
+
+ @Test
+ public void testMedianBugCP() {
+ runQuantileTest(TEST_NAME5, -1, false, ExecType.CP);
+ }
+
+ @Test
+ public void testMedianBugSP() {
+ runQuantileTest(TEST_NAME5, -1, false, ExecType.SPARK);
+ }
+
private void runQuantileTest( String TEST_NAME, double p, boolean sparse, ExecType et)
{
ExecMode platformOld = setExecMode(et);
diff --git a/src/test/java/org/apache/sysds/test/functions/federated/primitives/part2/FederatedQuantileTest.java b/src/test/java/org/apache/sysds/test/functions/federated/primitives/part2/FederatedQuantileTest.java
index 6f13fce81b..20ad6eb1f0 100644
--- a/src/test/java/org/apache/sysds/test/functions/federated/primitives/part2/FederatedQuantileTest.java
+++ b/src/test/java/org/apache/sysds/test/functions/federated/primitives/part2/FederatedQuantileTest.java
@@ -30,7 +30,6 @@ import org.apache.sysds.test.AutomatedTestBase;
import org.apache.sysds.test.TestConfiguration;
import org.apache.sysds.test.TestUtils;
import org.junit.Assert;
-import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -56,9 +55,7 @@ public class FederatedQuantileTest extends AutomatedTestBase {
@Parameterized.Parameters
public static Collection<Object[]> data() {
- return Arrays.asList(new Object[][] {
- // {1000, 1, false},
- {128, 1, true}});
+ return Arrays.asList(new Object[][] {{1000, 1, false}, {128, 1, true}});
}
@Override
@@ -71,19 +68,16 @@ public class FederatedQuantileTest extends AutomatedTestBase {
}
@Test
- @Ignore
public void federatedQuantile1CP() {
federatedQuartile(Types.ExecMode.SINGLE_NODE, TEST_NAME1, 0.25);
}
@Test
- @Ignore
public void federatedQuantile2CP() {
federatedQuartile(Types.ExecMode.SINGLE_NODE, TEST_NAME1, 0.5);
}
@Test
- @Ignore
public void federatedQuantile3CP() {
federatedQuartile(Types.ExecMode.SINGLE_NODE, TEST_NAME1, 0.75);
}
@@ -104,19 +98,16 @@ public class FederatedQuantileTest extends AutomatedTestBase {
}
@Test
- @Ignore
public void federatedQuantile1SP() {
federatedQuartile(Types.ExecMode.SPARK, TEST_NAME1, 0.25);
}
@Test
- @Ignore
public void federatedQuantile2SP() {
federatedQuartile(Types.ExecMode.SPARK, TEST_NAME1, 0.5);
}
@Test
- @Ignore
public void federatedQuantile3SP() {
federatedQuartile(Types.ExecMode.SPARK, TEST_NAME1, 0.75);
}
diff --git a/src/test/scripts/functions/binary/matrix/MedianBug.R b/src/test/scripts/functions/binary/matrix/MedianBug.R
new file mode 100644
index 0000000000..f868b65275
--- /dev/null
+++ b/src/test/scripts/functions/binary/matrix/MedianBug.R
@@ -0,0 +1,34 @@
+#-------------------------------------------------------------
+#
+# 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")
+
+A = as.matrix(c(1,5,7,10))
+
+s = median(A);
+m = as.matrix(s);
+
+writeMM(as(m, "CsparseMatrix"), paste(args[3], "R", sep=""));
+
+
diff --git a/src/test/scripts/functions/binary/matrix/MedianBug.dml b/src/test/scripts/functions/binary/matrix/MedianBug.dml
new file mode 100644
index 0000000000..21dbb15fe4
--- /dev/null
+++ b/src/test/scripts/functions/binary/matrix/MedianBug.dml
@@ -0,0 +1,25 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+A = as.matrix(list(1,5,7,10));
+s = median(A);
+m = as.matrix(s);
+write(m, $3, format="text");