Skip to content

Disallow np.array(COO) #218

Closed
Closed

Description

@hameerabbasi

Currently, we have a mix of when to allow dense inputs and when not to allow them. Currently, we have the __array__ protocol which converts the sparse array into a dense one when using np.array(COO) or similar.

This would be fine if we knew that we were densifying, but in practice, we really don't. Think of something that calls np.asarray internally, this would densify, and in many cases fill up memory and raise a MemoryError.

In practice, what we want most of the time is to disallow np.[as]array(COO) and provide an explicit escape hatch in the form of COO.todense().

Recently, I added an environment variable to control this, so this discussion is mostly about changing the default behaviour.

Thoughts, @rgommers, @shoyer, @mrocklin, @perimosocordiae?

Activity

  1. asmeurer commented on Dec 12, 2018

    @asmeurer
    Member

    Having worked with the environment variable enabled for the past few weeks, I can confirm that it often densifies in unexpected places. For instance, an algorithm that uses einsum will densify because einsum isn't implemented for sparse.

  2. mrocklin commented on Dec 12, 2018

    @mrocklin
    Contributor

    CuPy explicitly doesn't implement the __array__ protocol for this reason. Dask.array still does. Both seem defensible. No strong thoughts either way.

  3. scopatz commented on Dec 12, 2018

    @scopatz

    I agree with @asmeurer here. The purpose of sparse seems to be to create, manage, and operate on sparse arrays. I don't expect that default behavior is to densify as frequently as it does.

  4. shoyer commented on Dec 12, 2018

    @shoyer
    Member

    __array_function__ will help here, since NumPy functions won't automatically densify anymore.

    I agree that either choice is defensible.

    In the long term, it would be nice to have a protocol for coercion to NumPy arrays even if the operation could be very expensive.

  5. asmeurer commented on Dec 12, 2018

    @asmeurer
    Member

    I think @hameerabbasi didn't want to add the flag to the Python level because of thread safety (right now it only exists as an environment variable that must be set before importing sparse). See the discussion at #210.

  6. perimosocordiae commented on Dec 13, 2018

    @perimosocordiae

    I'm +1 for disallowing implicit densification. That's the approach taken by scipy.sparse, and I think it has been effective in guiding users away from unexpected performance traps.

    Users often test their code using small inputs, so problems caused by densification have a bad habit of only showing up in production.

  7. hameerabbasi commented on Dec 13, 2018

    @hameerabbasi
    CollaboratorAuthor

    It seems all comments are either in favor or neutral. I've implemented this in #220.

    In the long term, it would be nice to have a protocol for coercion to NumPy arrays even if the operation could be very expensive.

    This already exists as a method rather than a protocol, .todense(), the problem with __array__ seems to be that it makes densification implicit, with a lot of NumPy and other library functions calling it.

    __array_function__ will help here, since NumPy functions won't automatically densify anymore.

    __array_function__ will help, but only internal NumPy functions are guaranteed to use it. So unless we allow overriding np.asarray, it isn't perfect.

  8. shoyer commented on Dec 13, 2018

    @shoyer
    Member

    This already exists as a method rather than a protocol, .todense()

    Ideally the method would also be available on non-sparse arrays

  9. asmeurer commented on Feb 20, 2019

    @asmeurer
    Member

    I agree with @shoyer. At the very least it would be nice to have sparse.todense() as a function so that we can call it universally on sparse and dense inputs.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions