Skip to content
New issue

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

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

Already on GitHub? # to your account

Output with empty matrix produces invalid data #28

Closed
projekter opened this issue Nov 4, 2024 · 0 comments · Fixed by #29
Closed

Output with empty matrix produces invalid data #28

projekter opened this issue Nov 4, 2024 · 0 comments · Fixed by #29

Comments

@projekter
Copy link
Contributor

projekter commented Nov 4, 2024

Before carrying out the multiplication, _sparse_dot_dense and _sparse_dot_sparse check whether the result is zero and take a shortcut. However, if the out parameter is specified, this shortcut will just return the output matrix unchanged instead of setting it to zero.
Edit: Basically, the out_scalar parameter is completely ignored.
Second edit: Actually, there's another issue due to the inconsistent way in which output arrays are handled. spmm does not accept an out parameter, so correctly an exception is thrown if one is provided. spmmd does allow for one; however, it always acts as if out_scalar=0 (so the default None corresponds to 0 in this case). sparse_dot just ignores the out_scalar parameter. sparse_?_mm and sparse_?_mv also allows for the output, and this case the out_scalar is converted using _mkl_scalar, i.e., the default None corresponds to 1 in this case. I think there are only two ways to resolve this inconsistency in the general dot_product_mkl interface:

  • The backwards-compatible one is to check for out_scalar different from None or 0 in the spmmd case and throw an exception, plus add this to the documentation.
  • The consistency-oriented one is to change the default value of out_scalar from None to 0 and add the exception conditioned on it not being 0.
import sparse_dot_mkl, scipy.sparse, numpy

output = numpy.random.rand(3, 3)
sparse_dot_mkl._sparse_dense._sparse_dot_dense(
    numpy.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]),
    scipy.sparse.csr_matrix((3, 3)),
    out=output,
    out_scalar=0
)
# now out is still the random matrix
sparse_dot_mkl._sparse_sparse._sparse_dot_sparse(
    scipy.sparse.csr_matrix((3, 3)),
    scipy.sparse.csr_matrix((3, 3)),
    out=output,
    dense=True,
    out_scalar=0
)
# and also now
projekter added a commit to projekter/sparse_dot that referenced this issue Nov 6, 2024
@projekter projekter mentioned this issue Nov 6, 2024
asistradition added a commit that referenced this issue Nov 7, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant