GH-50906: [C++] Support 1D tensors in SparseCSR/CSC matrix conversion - #50907
Open
pratyushadk wants to merge 1 commit into
Open
GH-50906: [C++] Support 1D tensors in SparseCSR/CSC matrix conversion#50907pratyushadk wants to merge 1 commit into
pratyushadk wants to merge 1 commit into
Conversation
|
|
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.
Rationale for this change
Calling
SparseCSRMatrix::MakeorSparseCSCMatrix::Makeon a 1D dense tensor currently returns aNotImplementederror ("TODO for ndim <= 1"). The CSR/CSC representation for a 1D vector is well-defined though: it is equivalent to a single-row sparse matrix whereindptr = [0, nnz],indicesholds the positions of non-zero values, andvaluesholds the corresponding values. The COO format already handles 1D tensors, so this fills a straightforward gap.What changes are included in this PR?
cpp/src/arrow/tensor/csx_converter.ccThe
Convert()method inSparseCSXMatrixConverternow handles three cases cleanly.ndim == 0returnsStatus::Invalidsince scalars have no meaningful sparse representation.ndim == 1takes a new path that does a single linear scan of the vector, writesindptr = [0, nnz], and records the positions and values of non-zeros.ndim == 2is the existing path and is completely unchanged.The reverse path (
MakeTensorFromSparseCSXMatrix) had a hardcodedshape[1]access which would be out of bounds for a 1D tensor. That is fixed to useshape.size() > 1 ? shape[1] : shape[0].cpp/src/arrow/sparse_tensor_test.ccThree new tests added to the existing
TestSparseCSRMatrixfixture covering the 1D case.Are these changes tested?
Yes. Three new tests cover the main conversion path, the all-zero edge case, and a full round-trip from dense to sparse and back. All 153 tests in the sparse tensor test suite passed locally with no regressions.
Are there any user-facing changes?
SparseCSRMatrix::MakeandSparseCSCMatrix::Makenow accept 1D tensors instead of returningNotImplemented. There are no breaking changes.