Skip to content

[RFC] basic support for the sparse tensor dialect - #3198

Draft
simeonschaub wants to merge 2 commits into
EnzymeAD:mainfrom
simeonschaub:sds/sparse_csr
Draft

simeonschaub wants to merge 2 commits into
EnzymeAD:mainfrom
simeonschaub:sds/sparse_csr

Conversation

@simeonschaub

Copy link
Copy Markdown
Contributor

Introduces Reactant.CSRMatrix, hooks it up to https://mlir.llvm.org/docs/Dialects/SparseTensorOps/ and adds the ability to lower simple spmv and spmm ops to either cuSPARSE or hipSPARSE. I verified the cuSPARSE path locally.

Putting this up for comments, since I'm not sure this is the right approach, but this would be really useful to us in CoolPDLP.jl!

simeonschaub and others added 2 commits August 19, 2026 09:46
…ipSPARSE

Introduces an opaque `Reactant.CSRMatrix` (convertible from SparseMatrixCSC
via the SparseArrays ext). Inside traced code `A * x`, `A * B`, and `mul!`
emit `sparse_tensor.assemble` producing a CSR-encoded tensor consumed by
`stablehlo.dot_general`; `Compiler.lower_sparse_ops!` rewrites the pair to
`stablehlo.custom_call @reactant_csr_matmul` on the raw buffers before any
pass pipeline runs, so XLA only ever sees dense types. The custom call is
served by new cuSPARSE ("CUDA") and hipSPARSE ("ROCM") typed-FFI handlers
in ReactantExtra (requires a local jll rebuild for execution).

`@code_hlo optimize=:none` keeps the sparse_tensor IR for inspection.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Pangoraw

Copy link
Copy Markdown
Collaborator

Hey Simeon! Thanks for the PR. Would it be possible to port the lowerings from this PR in Enzyme-JAX. That way we can also include other missing pieces such as AD rules for the sparse ops. You can create a subdialect enzymexla.sparse instead of matching into sparse.assemble+stablehlo.dot_general if that's more convenient.

@simeonschaub

Copy link
Copy Markdown
Contributor Author

Sure, I can give that a go!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants