Add sparse matrix interface - #349
Conversation
@inducer I added a loopy kernel fallback for eager (code), and it's working when the array being multiplied is 1D, but I'm running into some trouble with array strides for higher dimensions. I set up the input array like this: u1=actx.np.where(actx.np.less_equal(x, (a+b)/2), 0*x+1, 0*x)
u2=actx.np.where(actx.np.greater_equal(x, (a+b)/2), 0*x+1, 0*x)
# u = actx.freeze_thaw(u1) # 1Du=actx.freeze_thaw(actx.np.stack([u1, u2]).T) # 2DThe strides are no longer C-like after transposing. Lazy doesn't seem to care about this, but eager doesn't like it; it makes my 1D diffusion solver blow up instantly. If I add Edit: I guess the strides of |
majosm
commented
Jan 29, 2026
FWIW, using a dense matrix + |
majosm
commented
Feb 2, 2026
Upon closer inspection, it looks like the kernels may actually be OK. The problem occurs later in the test driver, when I do this: u=actx.freeze_thaw(u+dt*compiled_rhs(t, u))(here |
9200e53 to
cf9dc7bComparemajosm
commented
Feb 5, 2026
I added some limited support for applying sparse matrices to array containers. Currently it's restricted to the cases of
@inducer I think this is ready for a first look. I still have some FIXMEs scattered throughout the changes for things I could use some input on. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
7b336f2 to
ca2ef59Compare…_matmul instead of using derived matrix classes
There was a problem hiding this comment.
Pull request overview
This PR introduces a new sparse matrix API to arraycontext, centered on a generic SparseMatrix base type and a CSRMatrix implementation, along with actx.make_csr_matrix and actx.sparse_matmul/__matmul__ support across selected backends.
Changes:
- Add
SparseMatrix/CSRMatrixtypes and CSR matmul support to the coreArrayContextinterface. - Implement sparse matmul for NumPy (via SciPy) and Pytato (via
pytato.make_csr_matrix); explicitly mark JAX variants as not supported. - Add CSR matmul tests and introduce a
sparseoptional dependency group (SciPy), plus CI typing support (scipy-stubs).
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
arraycontext/context.py | Adds SparseMatrix/CSRMatrix, CSR construction, and a Loopy-based CSR matmul implementation in the base ArrayContext. |
arraycontext/impl/numpy/__init__.py | Implements sparse_matmul for CSRMatrix using scipy.sparse.csr_matrix. |
arraycontext/impl/pytato/__init__.py | Implements sparse_matmul for CSRMatrix using Pytato CSR support; adds overrides for JAX-based Pytato context to raise NotImplementedError. |
arraycontext/impl/jax/__init__.py | Adds explicit NotImplementedError for sparse matrix creation/matmul. |
test/test_arraycontext.py | Adds end-to-end CSR matmul tests over arrays, object arrays, and array containers. |
pyproject.toml | Adds sparse = ["scipy"] optional dependency group. |
.github/workflows/ci.yml | Adds scipy-stubs to the type-checking install list. |
arraycontext/__init__.py | Exports SparseMatrix and CSRMatrix at the package top level. |
.basedpyright/baseline.json | Updates type-checking baseline. |
Comments suppressed due to low confidence (1)
pyproject.toml:58
- The new
sparseoptional dependency addsscipy, but thetestextra does not include it even though tests now require sparse matmul to work forNumpyArrayContext. This will make default test installs (.[test]) fail unless SciPy happens to be present. Consider addingscipy(orarraycontext[sparse]) to thetestextra, or making the tests/implementation gracefully skip when SciPy is not installed.
sparse = [
"scipy",
]
test = [
"basedpyright",
"pytest",
"ruff",
# for type checking
"pymbolic",
]
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
inducer
commented
Mar 16, 2026
These look like reasonable concerns except as noted. @majosm, could you take a look? |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
inducer
commented
Mar 17, 2026
Thx! |
Adds a generic interface for operations involving sparse CSR matrices.
cc @lukeolson