Skip to content

Supporting full_matrices argument with Dask's svd #3576

Open

Description

@jakirkham

Ran into an issue where NumPy's svd and Dask's svd would provide different shaped arrays. An example of this behavior is included below with details about the environment used to reproduce it.

Example:
In [1]: import numpy as np

In [2]: import dask.array as da

In [3]: m, n = 20, 10

In [4]: a = np.random.rand(m, n)

In [5]: d_a = da.from_array(a, chunks=(3, n), name='A')

In [6]: d_u, d_s, d_vt = da.linalg.svd(d_a)

In [7]: u, s, vt = np.linalg.svd(a)

In [8]: assert d_u.shape == u.shape
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-8-dffa2c855847> in <module>()
----> 1 assert d_u.shape == u.shape

AssertionError: 

In [9]: d_u.shape
Out[9]: (20, 10)

In [10]: u.shape
Out[10]: (20, 20)

Environment:
name: test
channels:
  - conda-forge
  - defaults
dependencies:
  - appnope=0.1.0=py36_0
  - backcall=0.1.0=py_0
  - blas=1.1=openblas
  - bokeh=0.12.16=py36_0
  - ca-certificates=2018.4.16=0
  - certifi=2018.4.16=py36_0
  - click=6.7=py_1
  - cloudpickle=0.5.3=py_0
  - cycler=0.10.0=py36_0
  - cytoolz=0.9.0.1=py36_0
  - dask=0.17.5=py_0
  - dask-core=0.17.5=py_0
  - decorator=4.3.0=py_0
  - distributed=1.21.8=py36_0
  - freetype=2.8.1=0
  - graphviz=2.38.0=7
  - heapdict=1.0.0=py36_0
  - ipython=6.4.0=py36_0
  - ipython_genutils=0.2.0=py36_0
  - jedi=0.12.0=py36_0
  - jinja2=2.10=py36_0
  - jpeg=9b=2
  - kiwisolver=1.0.1=py36_1
  - libgfortran=3.0.0=0
  - libpng=1.6.34=0
  - libtiff=4.0.9=0
  - locket=0.2.0=py36_1
  - markupsafe=1.0=py36_0
  - matplotlib=2.2.2=py36_1
  - msgpack-python=0.5.6=py36h2d50403_2
  - ncurses=5.9=10
  - numpy=1.14.3=py36_blas_openblas_200
  - openblas=0.2.20=8
  - openssl=1.0.2o=0
  - packaging=17.1=py_0
  - pandas=0.23.0=py36_1
  - parso=0.2.1=py_0
  - partd=0.3.8=py36_0
  - pexpect=4.6.0=py36_0
  - pickleshare=0.7.4=py36_0
  - prompt_toolkit=1.0.15=py36_0
  - psutil=5.4.5=py36_0
  - ptyprocess=0.5.2=py36_0
  - pygments=2.2.0=py36_0
  - pyparsing=2.2.0=py36_0
  - python=3.6.5=1
  - python-dateutil=2.7.3=py_0
  - python-graphviz=0.8.3=py36_0
  - pytz=2018.4=py_0
  - pyyaml=3.12=py36_1
  - readline=7.0=0
  - setuptools=39.2.0=py36_0
  - simplegeneric=0.8.1=py36_0
  - six=1.11.0=py36_1
  - sortedcontainers=2.0.2=py36_0
  - sqlite=3.20.1=2
  - tblib=1.3.2=py36_0
  - tk=8.6.7=0
  - toolz=0.9.0=py_0
  - tornado=5.0.2=py36_0
  - traitlets=4.3.2=py36_0
  - wcwidth=0.1.7=py36_0
  - xz=5.2.3=0
  - yaml=0.1.7=0
  - zict=0.1.3=py_0
  - zlib=1.2.11=h470a237_3

Activity

  1. dhirschfeld commented on Jun 8, 2018

    @dhirschfeld

    It appears the shapes are the same if full_matrices=False is passed to np.linalg.svd.

    It also appears that without full_matrices=False you can't directly reconstruct the original array afrom the numpy results:

    >>> u @ np.diag(s) @ vt
    ValueError: shapes (20,20) and (10,10) not aligned: 20 (dim 1) != 10 (dim 0)
    
    >>> np.abs((d_u @ np.diag(d_s) @ d_vt) - d_a).max().compute()
    8.881784197001252e-15
    
  2. jakirkham commented on Jun 8, 2018

    @jakirkham
    MemberAuthor

    Right, we should rename this to da.linalg.svd lacks full_matrices argument.

  3. changed the title [-]Inconsistent shapes between NumPy's and Dask's svd[/-] [+]Supporting `full_matrices` argument with Dask's `svd`[/+] on Jun 8, 2018
  4. jakirkham commented on Jun 10, 2018

    @jakirkham
    MemberAuthor

    As to the other point you raised, @dhirschfeld, the Dask Arrays were misreporting the actual shape of the results. This was fixed with PR ( #3517 ), which should show up in the next release.

  5. mrocklin commented on Oct 24, 2018

    @mrocklin
    Member

    @jakirkham is this resolved?

  6. eric-czech commented on Sep 2, 2020

    @eric-czech
    Contributor

    Ditto that @jakirkham -- I was interested in helping pick this up after running into dask/dask-ml#732 (comment). Is there anything big left to be done/tested?

  7. eric-czech commented on Sep 9, 2020

    @eric-czech
    Contributor

    I found a little bit more on what the state of this seems to be. It looks like for an (M, N) matrix you get the full_matrices=False behavior when M > N and the full_matrices=True behavior when N > M:

    def shapes(r):
        return [x.shape for x in r]
    
    x = da.random.random((10, 100))
    assert shapes(da.linalg.svd(x)) == shapes(np.linalg.svd(np.asarray(x), full_matrices=True))
    x = da.random.random((100, 10))
    assert shapes(da.linalg.svd(x)) == shapes(np.linalg.svd(np.asarray(x), full_matrices=False))

    If it was clear how to get the full_matrices=True behavior for all inputs from tsqr, I think it would be fairly straightforward to fully support the argument. Without that though, it would at least be nice if support for full_matrices=False was consistent. This could be one way to do that:

    1. If the array is unchunked, run np.linalg.svd with full_matrices=False
    2. If the array has tall-skinny chunking, run tsqr
    3. If the array has short-fat chunking, run tsqr on transposed inputs
    4. If the arrays has tall-skinny or short-fat chunking but the shape contradicts the chunking, throw an error trim off the extra vectors returned by tsqr

    How important is full_matrices=True? I came here looking for consistent full_matrices=False behavior and I'll happily work on that, but I'm not sure what practical use cases there are for that being true other than the case @dhirschfeld mentioned in complete reconstruction of an input array.

  8. chkothe commented on Feb 24, 2021

    @chkothe

    If I understood this right, the above merge achieves behavior that's consistent with np.linalg.svd(X, full_matrices=False). However, one cannot currently invoke da.linalg.svd(X, full_matrices=False) since there's no such argument -- i.e. even though you have the right implementation, the API is not yet consistent.

    I guess the simplest fix would be to add that argument without a default value and require that full_matrices be passed in as False (this would flag users' existing code that may have relied on dask's legacy behavior).

    Plus, if there's interest in back compatibility, one could allow using this with something like full_matrices='legacy', which would invoke dask's old behavior (that some users might depend on?). And if you want to avoid breaking any existing code, one could default to full_matrices=None, which would do the same as 'legacy' but should probably emit a deprecation warning that suggests to use either 'legacy' or False explicitly, and that at some future point the default behavior might flip to match numpy's API (i.e., default to full_matrices=True).

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