Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt a CSR Representation for Sparse Matrices (Arecibo backport) #232

Merged
merged 4 commits into from
Oct 16, 2023

Conversation

huitseeker
Copy link
Contributor

@huitseeker huitseeker commented Oct 9, 2023

Shifts the sparse matrix representation from COO (Coordinate List) to CSR (Compressed Sparse Row).

Note

COO vs. CSR

In brief, while COO represents a sparse matrix as a list of (row, column, value) tuples, CSR takes a different approach. It makes use of three one-dimensional arrays to store the non-zero elements, the extents of the rows, and the column indices of the elements. The real charm of CSR lies in the contiguity of value representation for the same rows, making it amenable to auto-vectorizers.

The change shows a small 5-10% improvement on recursive proofs, where a matrix-vector product is used in committing to R1CS matrices (and that's on an M2 machine .. on most x86s, more significative benefits are the norm).

group                                                        coo-to-csr                             main
-----                                                        ----------                             ----
CompressedSNARK-Commitments-StepCircuitSize-0/Prove          1.00       6.2±0.03s          1.00       6.1±0.03s
CompressedSNARK-Commitments-StepCircuitSize-0/Verify         1.00    135.0±1.64ms          1.01    135.7±0.97ms
CompressedSNARK-Commitments-StepCircuitSize-121253/Prove     1.00      15.5±0.08s          1.01      15.7±0.07s
CompressedSNARK-Commitments-StepCircuitSize-121253/Verify    1.00    337.3±4.90ms          1.01    340.5±3.28ms
CompressedSNARK-Commitments-StepCircuitSize-22949/Prove      1.00       9.2±0.14s          1.00       9.3±0.14s
CompressedSNARK-Commitments-StepCircuitSize-22949/Verify     1.00    194.7±2.51ms          1.01    196.7±4.07ms
CompressedSNARK-Commitments-StepCircuitSize-252325/Prove     1.00      27.2±0.11s          1.01      27.6±0.08s
CompressedSNARK-Commitments-StepCircuitSize-252325/Verify    1.02   527.1±15.70ms          1.00    519.2±8.87ms
CompressedSNARK-Commitments-StepCircuitSize-55717/Prove      1.01      15.5±0.07s          1.00      15.3±0.15s
CompressedSNARK-Commitments-StepCircuitSize-55717/Verify     1.00    335.7±4.52ms          1.01    339.4±6.36ms
CompressedSNARK-Commitments-StepCircuitSize-6565/Prove       1.00       9.0±0.12s          1.04       9.4±0.32s
CompressedSNARK-Commitments-StepCircuitSize-6565/Verify      1.00    195.1±3.30ms          1.01    196.3±2.11ms
CompressedSNARK-StepCircuitSize-0/Prove                      1.00    619.0±4.70ms          1.01    625.5±5.86ms
CompressedSNARK-StepCircuitSize-0/Verify                     1.00     24.8±0.29ms          1.00     24.8±0.34ms
CompressedSNARK-StepCircuitSize-1038757/Prove                1.02      19.2±0.27s          1.00      18.7±0.07s
CompressedSNARK-StepCircuitSize-1038757/Verify               1.00    530.3±7.85ms          1.00    529.8±3.81ms
CompressedSNARK-StepCircuitSize-121253/Prove                 1.00       2.7±0.03s          1.01       2.8±0.12s
CompressedSNARK-StepCircuitSize-121253/Verify                1.01     88.0±1.56ms          1.00     86.9±1.85ms
CompressedSNARK-StepCircuitSize-22949/Prove                  1.00   931.5±13.86ms          1.00   928.6±10.21ms
CompressedSNARK-StepCircuitSize-22949/Verify                 1.02     34.6±0.46ms          1.00     33.9±0.73ms
CompressedSNARK-StepCircuitSize-252325/Prove                 1.00       4.9±0.02s          1.01       5.0±0.02s
CompressedSNARK-StepCircuitSize-252325/Verify                1.00    161.6±2.03ms          1.00    161.1±2.64ms
CompressedSNARK-StepCircuitSize-514469/Prove                 1.00       9.8±0.04s          1.00       9.8±0.03s
CompressedSNARK-StepCircuitSize-514469/Verify                1.00    321.6±4.50ms          1.01    324.6±3.69ms
CompressedSNARK-StepCircuitSize-55717/Prove                  1.00  1536.6±46.55ms          1.01  1546.5±49.04ms
CompressedSNARK-StepCircuitSize-55717/Verify                 1.03     52.8±2.06ms          1.00     51.5±1.11ms
CompressedSNARK-StepCircuitSize-6565/Prove                   1.00    628.3±4.12ms          1.00    626.4±3.69ms
CompressedSNARK-StepCircuitSize-6565/Verify                  1.02     25.0±0.27ms          1.00     24.6±0.23ms
RecursiveSNARK-StepCircuitSize-0/Prove                       1.00     37.1±0.16ms          1.12     41.4±0.10ms
RecursiveSNARK-StepCircuitSize-0/Verify                      1.00     23.5±0.13ms          1.03     24.3±0.20ms
RecursiveSNARK-StepCircuitSize-1038757/Prove                 1.00   1151.1±7.55ms          1.01   1167.4±8.91ms
RecursiveSNARK-StepCircuitSize-1038757/Verify                1.02   984.3±14.26ms          1.00   968.6±11.56ms
RecursiveSNARK-StepCircuitSize-121253/Prove                  1.00    154.7±3.34ms          1.06    163.8±1.60ms
RecursiveSNARK-StepCircuitSize-121253/Verify                 1.00    127.8±0.73ms          1.00    127.7±2.27ms
RecursiveSNARK-StepCircuitSize-22949/Prove                   1.00     63.3±0.82ms          1.06     67.4±0.23ms
RecursiveSNARK-StepCircuitSize-22949/Verify                  1.00     43.8±0.30ms          1.03     45.0±0.45ms
RecursiveSNARK-StepCircuitSize-252325/Prove                  1.00    280.6±2.54ms          1.05    295.4±5.68ms
RecursiveSNARK-StepCircuitSize-252325/Verify                 1.01    241.8±2.64ms          1.00    239.3±2.13ms
RecursiveSNARK-StepCircuitSize-514469/Prove                  1.00    530.9±3.55ms          1.03    548.2±6.10ms
RecursiveSNARK-StepCircuitSize-514469/Verify                 1.00    432.1±2.60ms          1.11    478.4±3.81ms
RecursiveSNARK-StepCircuitSize-55717/Prove                   1.00     93.0±0.73ms          1.07     99.6±1.14ms
RecursiveSNARK-StepCircuitSize-55717/Verify                  1.00     72.6±0.80ms          1.02     74.2±0.96ms
RecursiveSNARK-StepCircuitSize-6565/Prove                    1.00     44.7±0.27ms          1.10     49.0±0.48ms
RecursiveSNARK-StepCircuitSize-6565/Verify                   1.00     29.4±0.27ms          1.01     29.8±0.17ms

Complete bench run:
https://gist.github.com/huitseeker/63bae83503c61b0cf643f58ccf0a7dad

Credits

argumentcomputer/arecibo#38
Work mostly done by @winston-h-zhang, I'm just the ferryman.

* add csr feature

* remove feature

remove conversions

* cleanup

* avoid iter() and go row-by-row

* cleanup and apply suggestions

* add R1CSShape padding test

* add more suggestions
src/nifs.rs Outdated Show resolved Hide resolved
src/r1cs/mod.rs Outdated Show resolved Hide resolved
@srinathsetty
Copy link
Collaborator

Thanks, @huitseeker and @winston-h-zhang for the PR! This is a nice improvement!

I left some comments.

- Modifies the visibility of `sparse` module from public to private
- Makes `SparseMatrix` class within `sparse` module public
- Updates `SparseMatrix` import across several files to accommodate visibility changes
Cargo.toml Outdated Show resolved Hide resolved
- Shifted `proptest` and `rand` dependencies to dev-dependencies in `Cargo.toml`
- Introduced testing flag for `util` module under `r1cs`
@srinathsetty srinathsetty merged commit 93afdf3 into microsoft:main Oct 16, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants