Skip to content

Fixed SVD ignoring "some/full_matrices" flag for empty inputs#51109

Closed
IvanYashchuk wants to merge 1 commit intopytorch:masterfrom
IvanYashchuk:fix-svd-empty-matrix
Closed

Fixed SVD ignoring "some/full_matrices" flag for empty inputs#51109
IvanYashchuk wants to merge 1 commit intopytorch:masterfrom
IvanYashchuk:fix-svd-empty-matrix

Conversation

@IvanYashchuk
Copy link
Copy Markdown
Collaborator

For empty inputs torch.svd (and torch.linalg.svd) was returning incorrect results for some=True (full_matrices=False).
Behaviour on master branch:

In [1]: import torch
In [2]: a = torch.randn(0, 7)
In [3]: a.svd()
Out[3]: 
torch.return_types.svd(
U=tensor([], size=(0, 0)),
S=tensor([]),
V=tensor([[0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.]]))
In [4]: a.svd(some=False)
Out[4]: 
torch.return_types.svd(
U=tensor([], size=(0, 0)),
S=tensor([]),
V=tensor([[0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.]]))

some flag is ignored and 7x7 V matrix is returned in both cases. V should have 7x0 shape when some=True.

This PR fixes that.

@IvanYashchuk IvanYashchuk added the module: linear algebra Issues related to specialized linear algebra operations in PyTorch; includes matrix multiply matmul label Jan 26, 2021
@IvanYashchuk IvanYashchuk requested a review from mruberry January 26, 2021 14:50
@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Jan 26, 2021

💊 CI failures summary and remediations

As of commit a877b8c (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 26, 2021

Codecov Report

Merging #51109 (a877b8c) into master (f7b339d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master   #51109   +/-   ##
=======================================
  Coverage   80.91%   80.91%           
=======================================
  Files        1926     1926           
  Lines      210014   210013    -1     
=======================================
  Hits       169942   169942           
+ Misses      40072    40071    -1     

@heitorschueroff heitorschueroff added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jan 26, 2021
Copy link
Copy Markdown
Collaborator

@mruberry mruberry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay in review, @IvanYashchuk. Thanks for the fix!

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@mruberry merged this pull request in 5e09ec6.

laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
…h#51109)

Summary:
For empty inputs `torch.svd` (and `torch.linalg.svd`) was returning incorrect results for `some=True` (`full_matrices=False`).
Behaviour on master branch:
```python
In [1]: import torch
In [2]: a = torch.randn(0, 7)
In [3]: a.svd()
Out[3]:
torch.return_types.svd(
U=tensor([], size=(0, 0)),
S=tensor([]),
V=tensor([[0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.]]))
In [4]: a.svd(some=False)
Out[4]:
torch.return_types.svd(
U=tensor([], size=(0, 0)),
S=tensor([]),
V=tensor([[0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0.]]))
```
`some` flag is ignored and 7x7 `V` matrix is returned in both cases. `V` should have 7x0 shape when `some=True`.

This PR fixes that.

Pull Request resolved: pytorch#51109

Reviewed By: ngimel

Differential Revision: D26170897

Pulled By: mruberry

fbshipit-source-id: 664c09ca27bb375fabef2a046d0a09ca57b01aac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed Merged module: linear algebra Issues related to specialized linear algebra operations in PyTorch; includes matrix multiply matmul open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants