Lazy submatrix sampling for SparseSkOp#174
Merged
Merged
Conversation
Sample only the requested submatrix of a sparse sketching operator without materializing the full operator, mirroring the DenseSkOp machinery. - fill_sparse_unpacked(D, n_rows_sub, n_cols_sub, ro_s, co_s, ...): samples the requested submatrix directly into COO buffers. Supports a null-pointer workspace query that reports the required buffer length in nnz. - submatrix_as_coo(...): sparse analog of submatrix_as_blackbox(); uses the workspace query to size its buffers. - lskges/rskges generate only the needed submatrix on the S.nnz < 0 path and call left_spmm/right_spmm with offsets (0,0). - fill_sparse now routes through fill_sparse_unpacked. fill_sparse_unpacked_nosub is retained only for backward compatibility in the 1.x series (removal in 2.0). - RTD: document fill_sparse_unpacked, drop fill_sparse_unpacked_nosub. Thread-independence and exact RNG reproducibility are preserved. 435/435 tests pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sample only the requested submatrix of a sparse sketching operator, directly into COO buffers, without ever materializing the full operator. This mirrors the existing lazy-submatrix machinery for
DenseSkOp(fill_dense_unpacked/submatrix_as_blackbox) and bringsSparseSkOpto parity. Resolves #111.Previously, when
lskges/rskgesreceived an unsampled operator (S.nnz < 0), they filled the entire operator and then extracted the requested submatrix out of it. Now they generate only the submatrix that's actually needed.What changed
fill_sparse_unpacked(D, n_rows_sub, n_cols_sub, ro_s, co_s, nnz, vals, rows, cols, seed_state)(sparse_skops.hh) — new primitive that samples then_rows_sub × n_cols_subsubmatrix ofSanchored at(ro_s, co_s)straight into caller-provided COO buffers, with indices shifted to local coordinates. It advances the RNG counter past the unneeded major-axis vectors, samples the needed ones with the same helpers as the full operator (repeated_fisher_yatesfor SASO,sample_indices_iid_uniform+ merge for LASO), then compacts in place to the requested window. Handles both axis orderings and bothAxis::Short/Axis::Longlayouts.vals/rows/colsas null performs no sampling — it just writes the worst-case required buffer length (vec_nnz * num_major_sub) intonnzand returnsseed_stateunchanged, so callers can size buffers as needed.submatrix_as_coo(S, n_rows_sub, n_cols_sub, ro_s, co_s)(RandBLAS::sparse) — sparse analog ofsubmatrix_as_blackbox(). Uses the workspace query to size buffers, samples the submatrix, and returns a memory-owningCOOMatrixwhose dimensions exactly match the submatrix — so passing it toleft_spmm/right_spmmwith offsets(0,0)takes the no-extract fast path.lskges/rskges(skge.hh) — on theS.nnz < 0path, build only the needed submatrix viasubmatrix_as_cooand callleft_spmm/right_spmmwith(0,0)offsets, instead of filling the full operator into a shallow copy.fill_sparsenow routes throughfill_sparse_unpacked.fill_sparse_unpacked_nosubis retained as a thin deprecated wrapper (delegates withro_s = co_s = 0) for backward compatibility in the 1.x series; scheduled for removal in 2.0.Docs: RTD
skops_and_dists.rstnow documentsfill_sparse_unpackedand drops the_nosubentry.Reproducibility
Thread-independence and exact RNG reproducibility are preserved. Because the counter is skipped per-vector and the same sampling helpers are reused, a submatrix sampled lazily is bit-for-bit identical to the corresponding block of the fully-materialized operator.
Testing
test_sparseskop.cc: newfill_unpacked_sub_{saso,laso}tests (plusint32index variants) checking the lazy submatrix against the full fill.test_lskges.cc/test_rskges.cc: new*_SubmatrixPath.equivalence_to_full_filltests sweeping{Short, Long} × {NoTrans, Trans} × {ColMajor, RowMajor} × vec_nnz ∈ {1,2,3}, asserting the lazy path (S.nnz < 0) produces the sameBas fill-then-sketch.