Skip to content

Unbounded memory usage in tensordot #6916

Closed

Description

@tomwhite

What happened:

Following #6846 the memory usage of tensordot (and dot, which delegates to tensordot in Dask) is much higher than before, and grows as a function of array size.

What you expected to happen:

The memory usage should be related to chunk size (as it was previously), not the size of the array. This was achieved previously by avoiding concatenate=True in the call to blockwise from tensordot. As a general rule, concatenate=True should be avoided since it causes these memory issues for very large inputs.

Minimal Complete Verifiable Example:

The "Multiplication Only" part of this notebook - with X.T @ Y replaced with da.dot(X.T, Y) - demonstrates the problem. The memory usage should be flat, not growing with the size of the array.

Anything else we need to know?:

#6874 is a related issue that aims to remove the same memory issue from matmul.

Environment:

  • Dask version: latest head (unreleased)
  • Python version: 3.7.6
  • Operating System: MacOS
  • Install method (conda, pip, source): source

Activity

  1. zklaus commented on Dec 1, 2020

    @zklaus

    Note that there seems to have been a hidden bug in dask's tensordot for a long time that was coincidentally resolved by #6846. For a few more details see #6907.

  2. jakirkham commented on Dec 3, 2020

    @jakirkham
    Member

    Good to know. Maybe we should include a test then? Would you be willing to submit a PR? 🙂

  3. ravwojdyla commented on Dec 3, 2020

    @ravwojdyla
    Contributor

    From #6846 (comment):

    We could have a special case for sparse array (based on type) that introduces the contraction in _tensordot only for sparse arrays? But I wonder if there is a more generic solution 🤔

    Afaiu #6924 also adds a special case but just for {1,2}d. Should I give it a try to produce a PR for the sparse array special case, to compare? Wdyt @tomwhite and the rest?

  4. zklaus commented on Dec 4, 2020

    @zklaus

    Good to know. Maybe we should include a test then? Would you be willing to submit a PR? slightly_smiling_face

    I would. The problem is that I am not completely sure what is going on. In

    dask.config.set(scheduler='single-threaded')
    x = da.random.random((4,4), chunks=(2,2))
    x[x < 0.65] = 0
    y = x.persist()
    r1 = da.dot(x, x).compute()
    r2 = da.dot(y, y).compute()

    r1 is wrong and r2 is correct (with a dask version prior to fbccc4e). This is not due to special values; when I take the problematic values and create an array from a numpy array with them, the problem does not occur. The problem is also not with the (tensor)dot implementation of the blocks, but rather that they get fed wrong blocks. But I was not able yet to track down why and therefore I am not sure how to set up the test. I think having random stuff in tests should rather be avoided.

    What do you think? Include a test with random as above? Track down the problem in more detail before writing the test?

  5. mrocklin commented on Dec 4, 2020

    @mrocklin
    Member

    Can I ask folks to try this with the single-threaded scheduler .compute(scheduler="single-threaded")

    If it's fine there then I suspect that this is the numpy/glibc threaded leak issue.

  6. zklaus commented on Dec 4, 2020

    @zklaus

    Sorry I didn't mention this here before, but in the full script I had a dask.config.set(scheduler='single-threaded') at the beginning, so the error does occur with the single-threaded scheduler. (I now edited the snippet above to include the scheduler change.)

  7. mrocklin commented on Aug 3, 2021

    @mrocklin
    Member

    @GenevieveBuckley and I ran into this today. My apologies for only understanding what had happened until now.

    Short term, we can revert the previous change. I think that tensordot on numpy is more important than tensordot on *sparse.

    A solution that it a little better would be to look at the _meta of the input arrays and special casing those with sparse in the type name.

    However a best solution would be to find a way to use the sum trick with sparse arrays. I think that this trick is necessary for memory efficient operation at large scale.

  8. mrocklin commented on Aug 3, 2021

    @mrocklin
    Member

    I think that @GenevieveBuckley is taking a look at this now. Genevieve, I recommending spending an hour or two to see if you can respect the current concatenate choice for sparse arrays, and if not then just revert the previous commit.

  9. GenevieveBuckley commented on Aug 3, 2021

    @GenevieveBuckley
    Contributor

    Yes, I am looking at it (well, eating lunch and then back to it). What you've commented above is what I had in mind. I think it should be relatively straightforward to keep the current situation with sparse arrays, and use sum for other array types. Once that's done we can look at a better long term plan using the sum trick for sparse arrays too.

  10. dcherian commented on Mar 24, 2022

    @dcherian
    Collaborator

    I think this can be closed now.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions