Repository navigation
Add some sparse ops and rename others to have more explicit names - #2469
tomicapretto wants to merge 9 commits into
Conversation
3fe0ca2 to
b7c4de9
Compare
|
|
||
| @pytest.mark.parametrize("format", ["csr", "csc"]) | ||
| def test_inplace(self, format): | ||
| x = getattr(sparse, format + "_matrix")("x", dtype="float64") |
There was a problem hiding this comment.
why don't you pass it directly in the parametrize, instead of retrieving by getattr?
There was a problem hiding this comment.
It's updated now
| self.inplace = inplace | ||
| if self.inplace: | ||
| self.view_map = {0: [0]} | ||
| self.destroy_map = {0: [0]} |
There was a problem hiding this comment.
But is it a view when not inplace?
There was a problem hiding this comment.
Nope, .sorted_indices() returns a copy https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.sorted_indices.html.
There was a problem hiding this comment.
then it's never inplace / aliased either?
There was a problem hiding this comment.
When inplace, it uses .sort_indices(), which mutates x inplace, and it then returns x. When not inplace it used sorted_indices(), which returns a copy. It's like list.sort and sorted(list), right?
There was a problem hiding this comment.
omg sort vs sorted is the difference. ok
| self.op = ensure_sorted_indices | ||
|
|
||
| @pytest.mark.parametrize("format", ["csr", "csc"]) | ||
| def test_inplace(self, format): |
There was a problem hiding this comment.
how is this testing inplace (mutation of the original input)?
There was a problem hiding this comment.
I've updated it now
| verify_grad_sparse(self.op, data, structured=False) | ||
|
|
||
|
|
||
| class TestSumDuplicates(utt.InferShapeTester): |
There was a problem hiding this comment.
missing a perform test (gradient tests perform, but can't validate it by itself?)
8de2d0d to
9c63961
Compare
|
can you add a summary of this PR in the top message. It seems it's not just renaming but creating new Ops as well? |
Yes, but it's not done yet (that's why I am keeping it as a draft). I'll add a summary and mark as ready once it's good for review. But of course thanks for the feedback already given! |
f3d3bd5 to
9552550
Compare
…ving explicit zeros.
…parseSparseData, and AddStructuredSparseDenseVector in Numba
9552550 to
c79b358
Compare
Description
This PR adds two Ops for sparse matrices, implements those and a few others in Numba, and gives more explicit names to several Ops.
Renames
Renamed all Ops that contained names such as
AddSSorAddSDto be more explicit, now they areAddSparseSparseandAddSparseDense, respectively.Implementation
HasCanonicalFormat, a new Op for sparse matrices that determines whether indices are unique and sorted. It is used to then implement thehas_canonical_formatattribute in_sparse_py_operatorswhich is inherited by sparse matrices in PyTensor. This attribute is used in the Numba backend to compute the sum between two sparse mamtrices. However, I am not 100% sure a new Op should be added for this need in one of the backends. Thus I'm raising the question here.SumDuplicates, a new Op for sparse matrices that eliminates entries by adding them together. It is not used when the matrix is already in the canonical format. In SciPy this is an in-place operation, but we return a new one. This Op is used in a few places in the Numba backend to support the implementations of the Ops mentioned below.AddSparseSparse,MultiplySparseSparse,AddSparseDense,AddSparseSparseData,StructuredAddSparseDenseVector.Comment about numba implementations: I've discovered that one can get more performance if:
In a following PR (or this one?) I can incorporate these ideas in the previously implemented ops.
Other
Updated
test_sparse_creation_refcountandtest_sparse_passthrough_refcountintests/link/numba/sparse/test_basic.pyto use the effective initial reference count, which can differ between Python versions. Right now it's hardcoded to 3, but it is 2 in Python 3.14+.Checklist
Type of change