COO format and `sparse-assembler` fails with Python bindings

Hi!
I’m using MLIR sparse dialect (19.1.0-rc3) with Python bindings and I’m facing difficulties with COO format.

Performing elem-wise addition on COO format results with:

terminate called after throwing an instance of 'std::length_error'
  what():  vector::_M_fill_insert
Aborted (core dumped)

when returning result tensor.

It happens for both sparse-assembler{direct-out=true} style and manual assemble/disassemble.

Here’s a gist that reproduces it with sparse-assembler. CSR and COO tensors are added and CSR is returned:

And here’s github issue where I reported it: [mlir][sparse] Adding COO tensors and returning result fails with Python bindings · Issue #107477 · llvm/llvm-project · GitHub

Thank you for any help!

It looks like the issue was caused by the fact that the SciPy COO array wasn’t sorted. By default scipy.sparse.random_array(..., format="coo") returns an array where coords are unsorted. Calling coo.sum_duplicates() fixed the issue present here.