Skip to content

Commit

Permalink
[MINOR] Improved code coverage and fixes sparsity estimators
Browse files Browse the repository at this point in the history
  • Loading branch information
mboehm7 committed Nov 25, 2024
1 parent 01586a4 commit d726705
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public static long getSelfProductOutputNnz(MatrixBlock m1) {
double[] avals = a.values(i);
int aix = a.pos(i);
Arrays.fill(tmp, 0); //reset
for( int k=aix; k<aix+n; k++ ) {
double aval = avals[k];
for( int k=0; k<n; k++ ) {
double aval = avals[aix+k];
if( aval == 0 ) continue;
double[] bvals = a.values(k);
int bix = a.pos(k);
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/apache/sysds/hops/estim/EstimatorBitsetMM.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
*/
public class EstimatorBitsetMM extends SparsityEstimator
{
private final int _type;

public EstimatorBitsetMM() {
this(-1);
}

public EstimatorBitsetMM(int type) {
_type = type;
}

@Override
public DataCharacteristics estim(MMNode root) {
BitsetMatrix m1Map = getCachedSynopsis(root.getLeft());
Expand Down Expand Up @@ -205,14 +215,14 @@ public BitsetMatrix transpose() {
//protected abstract BitsetMatrix reshape(int rows, int cols, boolean byrow);
}

public static BitsetMatrix createBitset(int m, int n) {
return (long)m*n < Integer.MAX_VALUE ?
public BitsetMatrix createBitset(int m, int n) {
return ((long)m*n < Integer.MAX_VALUE && _type != 2) ?
new BitsetMatrix1(m, n) : //linearized long array
new BitsetMatrix2(m, n); //bitset per row
}

public static BitsetMatrix createBitset(MatrixBlock in) {
return in.getLength() < Integer.MAX_VALUE ?
public BitsetMatrix createBitset(MatrixBlock in) {
return (in.getLength() < Integer.MAX_VALUE && _type != 2) ?
new BitsetMatrix1(in) : //linearized long array
new BitsetMatrix2(in); //bitset per row
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import org.apache.sysds.hops.estim.EstimatorBasicWorst;
import org.apache.sysds.hops.estim.EstimatorBitsetMM;
import org.apache.sysds.hops.estim.EstimatorDensityMap;
import org.apache.sysds.hops.estim.EstimatorLayeredGraph;
import org.apache.sysds.hops.estim.EstimatorMatrixHistogram;
import org.apache.sysds.hops.estim.EstimatorSample;
import org.apache.sysds.hops.estim.EstimatorSampleRa;
import org.apache.sysds.hops.estim.SparsityEstimator;
import org.apache.sysds.runtime.instructions.InstructionUtils;
import org.apache.sysds.runtime.matrix.data.MatrixBlock;
Expand All @@ -37,8 +39,10 @@
public class SelfProductTest extends AutomatedTestBase
{
private final static int m = 2500;
private final static double sparsity1 = 0.0001;
private final static double sparsity2 = 0.000001;
private final static double sparsity0 = 0.5;
private final static double sparsity1 = 0.1;
private final static double sparsity2 = 0.0001;
private final static double sparsity3 = 0.000001;
private final static double eps1 = 0.05;
private final static double eps2 = 1e-4;
private final static double eps3 = 0;
Expand All @@ -50,97 +54,114 @@ public void setUp() {
}

@Test
public void testBasicAvgCase1() {
runSparsityEstimateTest(new EstimatorBasicAvg(), m, sparsity1);
}

@Test
public void testBasicAvgCase2() {
public void testBasicAvgCase() {
runSparsityEstimateTest(new EstimatorBasicAvg(), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorBasicAvg(), m/2, sparsity1);
runSparsityEstimateTest(new EstimatorBasicAvg(), m, sparsity2);
runSparsityEstimateTest(new EstimatorBasicAvg(), m, sparsity3);
}

@Test
public void testDensityMapCase1() {
runSparsityEstimateTest(new EstimatorDensityMap(), m, sparsity1);
}

@Test
public void testDensityMapCase2() {
public void testDensityMapCase() {
runSparsityEstimateTest(new EstimatorDensityMap(), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorDensityMap(), m/2, sparsity1);
runSparsityEstimateTest(new EstimatorDensityMap(), m, sparsity2);
runSparsityEstimateTest(new EstimatorDensityMap(), m, sparsity3);
}

@Test
public void testDensityMap7Case1() {
runSparsityEstimateTest(new EstimatorDensityMap(7), m, sparsity1);
}

@Test
public void testDensityMap7Case2() {
public void testDensityMap7Case() {
runSparsityEstimateTest(new EstimatorDensityMap(7), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorDensityMap(7), m/2, sparsity1);
runSparsityEstimateTest(new EstimatorDensityMap(7), m, sparsity2);
runSparsityEstimateTest(new EstimatorDensityMap(7), m, sparsity3);
}

@Test
public void testBitsetMatrixCase1() {
runSparsityEstimateTest(new EstimatorBitsetMM(), m, sparsity1);
public void testBitsetMatrixCase() {
runSparsityEstimateTest(new EstimatorBitsetMM(), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorBitsetMM(), m/2, sparsity1);
runSparsityEstimateTest(new EstimatorBitsetMM(), m, sparsity2);
runSparsityEstimateTest(new EstimatorBitsetMM(), m, sparsity3);
}

@Test
public void testBitsetMatrixCase2() {
runSparsityEstimateTest(new EstimatorBitsetMM(), m, sparsity2);
public void testBitset2MatrixCase() {
runSparsityEstimateTest(new EstimatorBitsetMM(2), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorBitsetMM(2), m/2, sparsity1);
runSparsityEstimateTest(new EstimatorBitsetMM(2), m, sparsity2);
runSparsityEstimateTest(new EstimatorBitsetMM(2), m, sparsity3);
}

@Test
public void testMatrixHistogramCase1() {
runSparsityEstimateTest(new EstimatorMatrixHistogram(false), m, sparsity1);
public void testMatrixHistogramCase() {
runSparsityEstimateTest(new EstimatorMatrixHistogram(false), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorMatrixHistogram(false), m/2, sparsity1);
runSparsityEstimateTest(new EstimatorMatrixHistogram(false), m, sparsity2);
runSparsityEstimateTest(new EstimatorMatrixHistogram(false), m, sparsity3);
}

@Test
public void testMatrixHistogramCase2() {
runSparsityEstimateTest(new EstimatorMatrixHistogram(false), m, sparsity2);
public void testMatrixHistogramExceptCase() {
runSparsityEstimateTest(new EstimatorMatrixHistogram(true), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorMatrixHistogram(true), m/2, sparsity1);
runSparsityEstimateTest(new EstimatorMatrixHistogram(true), m, sparsity2);
runSparsityEstimateTest(new EstimatorMatrixHistogram(true), m, sparsity3);
}

@Test
public void testMatrixHistogramExceptCase1() {
runSparsityEstimateTest(new EstimatorMatrixHistogram(true), m, sparsity1);
public void testSamplingDefCase() {
runSparsityEstimateTest(new EstimatorSample(), m, sparsity2);
runSparsityEstimateTest(new EstimatorSample(), m, sparsity3);
}

@Test
public void testMatrixHistogramExceptCase2() {
runSparsityEstimateTest(new EstimatorMatrixHistogram(true), m, sparsity2);
public void testSampling20Case() {
runSparsityEstimateTest(new EstimatorSample(0.2), m, sparsity2);
runSparsityEstimateTest(new EstimatorSample(0.2), m, sparsity3);
}

@Test
public void testSamplingDefCase1() {
runSparsityEstimateTest(new EstimatorSample(), m, sparsity1);
public void testSamplingRaDefCase() {
runSparsityEstimateTest(new EstimatorSampleRa(), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorSampleRa(), m, sparsity2);
runSparsityEstimateTest(new EstimatorSampleRa(), m, sparsity3);
}

@Test
public void testSamplingDefCase2() {
runSparsityEstimateTest(new EstimatorSample(), m, sparsity2);
public void testSamplingRa20Case() {
runSparsityEstimateTest(new EstimatorSampleRa(0.2), m/4, sparsity0);
runSparsityEstimateTest(new EstimatorSampleRa(0.2), m, sparsity2);
runSparsityEstimateTest(new EstimatorSampleRa(0.2), m, sparsity3);
}

@Test
public void testSampling20Case1() {
runSparsityEstimateTest(new EstimatorSample(0.2), m, sparsity1);
public void testLayeredGraphDefCase() {
runSparsityEstimateTest(new EstimatorLayeredGraph(), m, sparsity2);
runSparsityEstimateTest(new EstimatorLayeredGraph(), m, sparsity3);
}

@Test
public void testSampling20Case2() {
runSparsityEstimateTest(new EstimatorSample(0.2), m, sparsity2);
public void testLayeredGraph64Case() {
runSparsityEstimateTest(new EstimatorLayeredGraph(64), m, sparsity2);
runSparsityEstimateTest(new EstimatorLayeredGraph(64), m, sparsity3);
}

private static void runSparsityEstimateTest(SparsityEstimator estim, int n, double sp) {
MatrixBlock m1 = MatrixBlock.randOperations(m, n, sp, 1, 1, "uniform", 3);
MatrixBlock m1 = MatrixBlock.randOperations(n, n, sp, 1, 1, "uniform", 3);
MatrixBlock m3 = m1.aggregateBinaryOperations(m1, m1,
new MatrixBlock(), InstructionUtils.getMatMultOperator(1));
double spExact = OptimizerUtils.getSparsity(m, m,
double spExact1 = OptimizerUtils.getSparsity(n, n,
EstimationUtils.getSelfProductOutputNnz(m1));
double spExact2 = sp<0.4 ? OptimizerUtils.getSparsity(n, n,
EstimationUtils.getSparseProductOutputNnz(m1, m1)) : spExact1;

//compare estimated and real sparsity
double est = estim.estim(m1, m1);
TestUtils.compareScalars(est, m3.getSparsity(),
(estim instanceof EstimatorBitsetMM) ? eps3 : //exact
(estim instanceof EstimatorBasicWorst) ? eps1 : eps2);
TestUtils.compareScalars(m3.getSparsity(), spExact, eps3);
TestUtils.compareScalars(m3.getSparsity(), spExact1, eps3);
TestUtils.compareScalars(m3.getSparsity(), spExact2, eps3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.sysds.hops.estim.EstimatorBasicWorst;
import org.apache.sysds.hops.estim.EstimatorBitsetMM;
import org.apache.sysds.hops.estim.EstimatorDensityMap;
import org.apache.sysds.hops.estim.EstimatorLayeredGraph;
import org.apache.sysds.hops.estim.EstimatorMatrixHistogram;
import org.apache.sysds.hops.estim.MMNode;
import org.apache.sysds.hops.estim.SparsityEstimator;
Expand Down Expand Up @@ -126,6 +127,16 @@ public void testMatrixHistogramExceptCase2() {
runSparsityEstimateTest(new EstimatorMatrixHistogram(true), m, k, n, n2, case2);
}

@Test
public void testLayeredGraphCase1() {
runSparsityEstimateTest(new EstimatorLayeredGraph(32), m, k, n, n2, case1);
}

@Test
public void testLayeredGraphCase2() {
runSparsityEstimateTest(new EstimatorLayeredGraph(32), m, k, n, n2, case2);
}

private static void runSparsityEstimateTest(SparsityEstimator estim, int m, int k, int n, int n2, double[] sp) {
MatrixBlock m1 = MatrixBlock.randOperations(m, k, sp[0], 1, 1, "uniform", 1);
MatrixBlock m2 = MatrixBlock.randOperations(k, n, sp[1], 1, 1, "uniform", 2);
Expand Down

0 comments on commit d726705

Please sign in to comment.