<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/107477>107477</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir][sparse] Adding two COO tensors and returning result fails with Python bindings
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mtsokol
</td>
</tr>
</table>
<pre>
Hi!
I've been writing a short program with MLIR and Python bindings and I noticed that adding two COO tensors and returning COO result throws an error:
```
terminate called after throwing an instance of 'std::length_error'
what(): vector::_M_fill_insert
Aborted (core dumped)
```
**I'm using LLVM** `19.1.0-rc3`
---
My setup is:
1. I create NumPy arrays `pos` (of dtype `int64`), `index` (of dtype `int64`), and `data` (of dtype `float64`) and pass them to MLIR module that assembles them and returns a pointer to the COO tensor
2. Then I pass that pointer to another MLIR module that adds COO tensors element-wise `add(a, a)` and returns the result as a pointer to the resulting COO.
It fails with an error pasted above. Here's a full python script that reproduces it (less than 100 lines): https://gist.github.com/mtsokol/a1b83817c02353242ae6be4fae10eb95
---
I did spend some time investigating it and I think the culprit is with returning the result COO tensor after addition. If I convert the COO tensor to a dense one before returning (so step 3. let's say) the program works: https://gist.github.com/mtsokol/06e0f5421b190aaa4eed0dcf560dd3bb
Also, it shouldn't be the problem with the way I return the COO tensor. To prove it here's one more gist (mostly copied from previous ones) where I assemble a COO tensor, return a pointer to it and pass it to another module call "disassemble" and return constituent NumPy arrays and successfully compare them with original ones: https://gist.github.com/mtsokol/d4b4c16a440de03f700601e91cd7f745
My suspicion the issue is with the returning COO tensor that is the result of addition directly from the call, which I would classify it as a bug.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVtFu4zoO_Rrlhaghy46dPOShM0UxAaY7g8VgXwvZomPtyJIh0cnm7xe03TYtinvvAIEDSyTFQx4eS6dkTx7xILZfxPZhoyfqQzwMlMLv4DZNMNfDNyt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