Skip to content

Conversation

jessegrabowski
Copy link
Member

@jessegrabowski jessegrabowski commented Oct 18, 2025

Description

Reorganize the existing sparse code to be a bit more logical. In particular, the sparse module now mirrors the tensor module:

  • sparse/basic.py has constructors and utilities
  • sparse/math.py has math stuff
  • sparse/variable.py has base classes
  • sparse/linalg.py has linear algebra

Other stuff was mostly untouched. My motivation here is to lay the groundwork to get people to come look at and work on sparse. Things were pretty messy before. There was a lot of magic I didn't like. For example, the _sparse_py_operators inherited from _tensor_py_operators, and did some wrapper magic to dispatch a huge list of methods to sparse. I made this a lot more verbose, and put NotImplemented everywhere, so it's obvious to potential helpers what needs to be implemented.

Related Issue

  • Closes #
  • Related to #

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

📚 Documentation preview 📚: https://pytensor--1674.org.readthedocs.build/en/1674/

@ricardoV94
Copy link
Member

I think it was a mistake to inherit from TensorVariable in the first place. They are way too distinct objects

@ricardoV94
Copy link
Member

I believe we should revert aesara-devs/aesara#142, in fact the example given of Subtensor just further proves the point. Indexing in sparsevariables has nothing to do with in indexing in tensorvariables, starting with the fact there are no sparse vectors, only matrices

@jessegrabowski
Copy link
Member Author

mypy is failing because of that inheritance for what its worth

@jessegrabowski
Copy link
Member Author

I believe we should revert aesara-devs/aesara#142, in fact the example given of Subtensor just further proves the point. Indexing in sparsevariables has nothing to do with in indexing in tensorvariables, starting with the fact there are no sparse vectors, only matrices

I agree. The current mixins don't make any sense at all. We have e.g. SparseShared inheriting from two different sources that have the same method twice. So now the order matters, which sucks. I opened an issue to address it. For this PR, I'm just having mypy ignore it.

@jessegrabowski jessegrabowski force-pushed the sparse-tensor-methods branch 6 times, most recently from 3d78e1e to 2e2211c Compare October 18, 2025 19:44
else:
return pytensor.sparse.matrix(format, name, dtype=dtype)

params = product(
Copy link
Member Author

Choose a reason for hiding this comment

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

This leads to 2,304 test cases, which seems slightly excessive.

Copy link

codecov bot commented Oct 18, 2025

Codecov Report

❌ Patch coverage is 81.86728% with 235 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.62%. Comparing base (792bd04) to head (659fec8).

Files with missing lines Patch % Lines
pytensor/sparse/math.py 81.60% 88 Missing and 70 partials ⚠️
pytensor/sparse/variable.py 84.47% 42 Missing and 1 partial ⚠️
pytensor/sparse/basic.py 78.30% 13 Missing and 10 partials ⚠️
pytensor/sparse/rewriting.py 65.38% 2 Missing and 7 partials ⚠️
pytensor/sparse/linalg.py 91.66% 1 Missing and 1 partial ⚠️

❌ Your patch check has failed because the patch coverage (81.86%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1674      +/-   ##
==========================================
- Coverage   81.64%   81.62%   -0.02%     
==========================================
  Files         242      245       +3     
  Lines       53573    53764     +191     
  Branches     9453     9451       -2     
==========================================
+ Hits        43737    43887     +150     
- Misses       7358     7400      +42     
+ Partials     2478     2477       -1     
Files with missing lines Coverage Δ
pytensor/link/jax/dispatch/sparse.py 83.33% <100.00%> (ø)
pytensor/sparse/sharedvar.py 94.11% <100.00%> (ø)
pytensor/tensor/slinalg.py 91.40% <100.00%> (ø)
pytensor/sparse/linalg.py 91.66% <91.66%> (ø)
pytensor/sparse/rewriting.py 75.44% <65.38%> (+0.09%) ⬆️
pytensor/sparse/basic.py 82.86% <78.30%> (+0.25%) ⬆️
pytensor/sparse/variable.py 84.47% <84.47%> (ø)
pytensor/sparse/math.py 81.60% <81.60%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants