Skip to content

(Final Submission): 987 mnc sketch implemenation #1000

Open
justaprog wants to merge 88 commits into
daphne-project:mainfrom
justaprog:987-mnc-sketch-implemenation
Open

(Final Submission): 987 mnc sketch implemenation #1000
justaprog wants to merge 88 commits into
daphne-project:mainfrom
justaprog:987-mnc-sketch-implemenation

Conversation

@justaprog
Copy link
Copy Markdown

@justaprog justaprog commented Jan 18, 2026

Issues?
#987
What?
Based on the paper: Johanna Sommer, Matthias Boehm, Alexandre V. Evfimievski, Berthold Reinwald, Peter J. Haas (2019). "MNC: Structure-Exploiting Sparsity Estimation for Matrix Expressions". SIGMOD '19: Proceedings of the 2019 International Conference on Management of Data.

MNC Sparsity Estimator Project (Large Scale Data Engineering
This section documents the environment and results for the MNC Sparsity Estimator)

  1. Experimental Setting
    All experiments were conducted using a Docker container to ensure a consistent environment for library linking and reproducibility.
    data.zip

Hardware: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz with 16.0 GB RAM.
OS/Software: Windows 11 Home running through WSL (Windows Subsystem for Linux).
System Type: 64-bit operating system, x64-based processor.
2. Metrics
Dataset: We used real matrices from the SuiteSparse Matrix Collection (e.g., gre__115.mtx) instead of synthetic random data to better reflect real-world distributions.
Metrics: We measured Accuracy Error (the difference between the MNC estimate and actual sparsity) and Decision Correctness.
Variance: Because MNC is a probabilistic sketching method, small variations in results between runs are expected and were managed by using mean values for reporting.
3. Key Results
Decision Logic: Our implementation uses a 0.25 density threshold. It correctly triggers SPARSE storage for workloads measured below this value and DENSE for those above.
Reliability: The prototype passed 2,452 unit tests in the DAPHNE Catch2 suite, confirming it integrates safely with the existing system kernels.

This PR contains:

  • feat: implementations for Mnc sketch data structure
  • feat: builder functions to build the sketch from dense and csr matrix
  • feat: sparsity estimator for matrix multiplication's result
  • feat: sketch propagation for matrix multiplication
  • feat: integrate mnc sketch into daphne compiler as data property of data type matrix
  • feat: support MalmutOP, FillOp, RandOp, SeqOp, Binary elementwise arthmetic, Transpose, ReshapeOp, MatrixConstantOp, ReadOp to infer sparsity during compiling
  • test: integration tests via DaphneDSL scripts/test to ensure that all ops work properly within daphne compiler
  • test: unittest cases

Update after final submission:

Current all tests run:

image - Problems: statuscode and --select-matrix-repr

Update Features

  • feat: support rbind, cbind,diagMatrix ops

Experiments

  • Visualizations: https://github.com/justaprog/daphne-experiment-visualizations
  • Dataset: bcsstk02 (66,66), bp__1000 (822,822), bp_1200 (822,822), football (35,35), G10 (800,800),
    G11 (800,800), G14(800,800), G22 (2000,2000), G27 (2000,2000), gre__115 (115,115), gre__185 (185,185),
    gre__343 (343,343), gre__512 (512,512), str__200 (363,363), str__400 (363,363), lp_scagr7 (129, 185),
    football__115 (115,115), dw256A (512,512), dwt__66 (66,66)
  • Scripts:
  • mncsketch_propa folder: scripts to ensure inferring sparsity via mncsketch works well
  • experiment folder: scripts to run all experiments
  • experiment/result folder: experiments results saved as txt files
  • data folder: data.zip

justaprog and others added 20 commits December 5, 2025 19:16
@pdamme pdamme self-requested a review January 28, 2026 13:53
@pdamme pdamme added student project Suitable for a bachelor/master student's programming project. LDE winter 2025/26 Student project in the course Large-scale Data Engineering at TU Berlin (winter 2025/26). labels Jan 28, 2026
Copy link
Copy Markdown
Collaborator

@pdamme pdamme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR, @justaprog, @laakhdher, et al. Adding support for MNC sketches would make a great contribution to sparsity estimation in DAPHNE. Overall, your code is already in a good initial state, the stand-alone implementation of the MNC sketch and related algorithms as described in the paper looks good to me. Nevertheless, there is still substantial work required before we can merge this PR, most importantly, the current MNC implementation needs to be integrated with the rest of the DAPHNE system, such that we can benefit from MNC when running a DaphneDSL script.

Required points: (need to be addressed before we can merge this PR)

  1. Finish the implementation of the remaining parts of MNC as described in the paper.
    1. Implement the density map estimator over an MNC sketch (your function Edm() is currently a stub).
    2. Implement the propagation of the MNC sketch over reorganization ops and elementwise binary ops as described in Section 4.2 of the paper.
  2. Integrate your implementation of MNC with the rest of the DAPHNE system.
    1. Make an MNC sketch an optional data property of a matrix (see the developer docs on adding new data properties).
    2. Invoke the MNC-based sparsity estimation and MNC sketch propagation during the InferencePass (implement the necessary MLIR interfaces or traits, see the developer docs on data properties again).
    3. Ensure that the MNC sketch of inputs of the data flow graph can be known. Such inputs could be:
      • Matrices read from files (most important).
        • MNC sketch as an optional part of the file meta data, plus some way of creating that file meta data automatically.
        • MNC sketch creation on-the-fly (optional).
      • Matrices created by various source ops (e.g., fill()/FillOp, seq()/SeqOp, rand()/RandMatrixOp, … (implement the inference interface for the MNC sketch for those ops).
      • Matrix literals (MatrixConstantOp).
  3. Please fix the code style issues to make the CI checks pass (see the contribution guidelines).

Optional points: (recommended, but not critical for merging)

  1. Apply some little code improvements (see my comments on individual code lines).
  2. Use the helper function genGivenVals() to shorten the code of your unit test cases. Currently, you initialize CSR matrices by hardcoding the values, colIdxs, and rowOffsets arrays, which leads to long and hard-to-read test code. Instead, you could simply pass the (dense) values array to genGivenVals() and get its representation as a CSRMatrix or DenseMatrix. (For small test matrices, it doesn't hurt if you explicitly write the zeros in the code.) You can take inspiration from many existing unit test cases.
  3. Use the MNC sketch for more accurate, structure-aware sparsity estimation of additional ops, e.g., SliceRowOp/SliceColOp, ExtractRowOp/ExtractColOp, etc.
  4. Implement the MNC sketch propagation for additional ops, e.g., elementwise unary ops, etc.

h.hc.assign(h.n, 0);

const std::size_t *rowOffsets = A.getRowOffsets();
const std::size_t *colIdxs = A.getColIdxs();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use A.getColIdxs(0); otherwise, you may run into errors if A is a view into a larger CSRMatrix.


// --- 3) isDiagonal ---
// We call a matrix "diagonal" if it is square and every non-zero lies on i == j.
if(h.m == h.n && nnzEnd > nnzBegin) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also skip the check for a diagonal matrix if h.maxHr > 1 (or h.maxHc > 1), because a diagonal matrix can have at most one non-zero per row/column.

const std::size_t m = hA.m;
const std::size_t l = hB.n;

double nnz = 0.0;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend not using double for the number of non-zeros, because the number of non-zeros is conceptually an integer (e.g., size_t). Using double could lead to round-off errors (and vastly off numbers when casted to integer) when adding up many small numbers of per-row non-zeros in a large matrix).

This hints also applies to several more such uses of double below.

}

// Case 2: Extended count
else if(!hA.her.empty() || !hB.her.empty()) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the code in the then-branch uses both the extended histograms of hA and hB, I think it should be && instead of || (in contrast to Algorithm 1 in the paper).

else if(!hA.her.empty() || !hB.her.empty()) {

// Exact part
for(std::size_t j = 0; j < hA.n; ++j)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two loops both iterate over the same range (as hA.n is the same as hB.m). If you fuse these loops, you need to scan hA.hec only once, which should be more efficient.

std::size_t p = (hA.nnzRows - hA.rowsEq1) * (hB.nnzCols - hB.colsEq1);

if(p > 0) {
double dens = Edm(hA.hc, hB.hr, p);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to subtract the extended histograms from hA.hc and hB.hr as in Algorithm 1 in the paper.

justaprog and others added 4 commits February 16, 2026 20:57
- Integrated Mncsketch into DaphneIR to support RandMatrixOp, FillOp, SeqOp, MatMulOp, TransposeOp, ReshapeOp, MatrixConstantOp, EwAddOp, EwSubOp, EwMulOp, EwDivOp, ReadOp ops
@justaprog justaprog changed the title (Intial Prototype): 987 mnc sketch implemenation (Final Submission): 987 mnc sketch implemenation Feb 16, 2026
justaprog and others added 24 commits February 21, 2026 17:43
…periments

- Created new result files for matrix multiplication experiments with real data:
  - run_matmul_only_real_data.txt
  - run_matmul_self_transpose.txt
- Updated run_matmul_w.txt with additional results and improved formatting.
- Added new script run_elementwise_mul.sh to execute element-wise multiplication experiments across various datasets.
- Modified run_matmul_only_real_data.sh to include additional datasets and improved execution permissions.
- Created run_matmul_self_transpose.sh for self-transpose matrix multiplication experiments.
- Updated run_write_mnc_md.sh to include more datasets and improved formatting.
Added a Project Spotlight section to the README to document the Matrix Non-zero Count (MNC) sketch implementation. This includes a breakdown of our key technical contributions (compiler integration, sketch propagation, and extended operations), a code navigation guide for our code files in src/ and test/
@abdnadeemm abdnadeemm deleted the 987-mnc-sketch-implemenation branch April 2, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LDE winter 2025/26 Student project in the course Large-scale Data Engineering at TU Berlin (winter 2025/26). student project Suitable for a bachelor/master student's programming project.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants