Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow no-copy construction from SciPy COO arrays. #822

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions sparse/mlir_backend/_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@

return from_constituent_arrays(format=csx_format, arrays=(indptr, indices, data), shape=arr.shape)
case "coo":
if copy is not None and not copy:
raise RuntimeError(f"`scipy.sparse.{type(arr.__name__)}` cannot be zero-copy converted.")
from ._common import _hold_ref

row, col = arr.row, arr.col
if row.dtype != col.dtype:
raise RuntimeError(f"`row` and `col` dtypes must be the same: {row.dtype} != {col.dtype}.")
Expand All @@ -89,10 +89,8 @@
data = arr.data
if copy:
data = data.copy()

# TODO: Make them own the data until https://github.com/llvm/llvm-project/issues/116012 is fixed.
row = row.copy()
col = col.copy()
row = row.copy()
col = col.copy()

Check warning on line 93 in sparse/mlir_backend/_conversions.py

View check run for this annotation

Codecov / codecov/patch

sparse/mlir_backend/_conversions.py#L92-L93

Added lines #L92 - L93 were not covered by tests

coo_format = (
Coo()
Expand All @@ -103,7 +101,10 @@
.build()
)

return from_constituent_arrays(format=coo_format, arrays=(pos, row, col, data), shape=arr.shape)
ret = from_constituent_arrays(format=coo_format, arrays=(pos, row, col, data), shape=arr.shape)
if not copy:
_hold_ref(ret, arr)
hameerabbasi marked this conversation as resolved.
Show resolved Hide resolved
return ret
case _:
raise NotImplementedError(f"No conversion implemented for `scipy.sparse.{type(arr.__name__)}`.")

Expand Down
5 changes: 5 additions & 0 deletions sparse/mlir_backend/_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
def to_ctype(self):
return rt.as_ctype(self.np_dtype)

def __eq__(self, value):
if np.isdtype(value) or isinstance(value, str):
value = asdtype(value)
return super().__eq__(value)

Check warning on line 39 in sparse/mlir_backend/_dtypes.py

View check run for this annotation

Codecov / codecov/patch

sparse/mlir_backend/_dtypes.py#L37-L39

Added lines #L37 - L39 were not covered by tests


@dataclasses.dataclass(eq=True, frozen=True, kw_only=True)
class IeeeRealFloatingDType(DType):
Expand Down
5 changes: 2 additions & 3 deletions sparse/mlir_backend/tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_coo_3d_format(dtype):
@parametrize_dtypes
def test_sparse_vector_format(dtype):
if sparse.asdtype(dtype) in {sparse.complex64, sparse.complex128}:
pytest.xfail("Heisenbug")
pytest.xfail("The sparse_vector format returns incorrect results for complex dtypes.")
format = sparse.formats.Coo().with_ndim(1).with_dtype(dtype).build()

SHAPE = (10,)
Expand Down Expand Up @@ -465,8 +465,7 @@ def test_asformat(rng, src_fmt, dst_fmt):

expected = sps_arr.asformat(dst_fmt)

copy = None if dst_fmt == "coo" else False
actual_fmt = sparse.asarray(expected, copy=copy).format
actual_fmt = sparse.asarray(expected, copy=False).format
actual = sp_arr.asformat(actual_fmt)
actual_sps = sparse.to_scipy(actual)

Expand Down
Loading