Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Sparse] Raise exception when expand is called on sparse tensor #98365

Closed
wants to merge 2 commits into from

Conversation

malfet
Copy link
Contributor

@malfet malfet commented Apr 5, 2023

It's already not working, but this makes error message a bit more readable. I.e. it turns:

% python -c "import torch;x=torch.eye(3).to_sparse().expand(3,3)" 

from

NotImplementedError: Could not run 'aten::as_strided' with arguments from the 'SparseCPU' backend.

to

RuntimeError: Expand is unsupported for Sparse tensors.

cc @alexsamardzic @nikitaved @pearu @cpuhrsch @amjames @bhosmer

It's already not working, but this makes error message a bit more readable.
I.e. it turns:
```
% python -c "import torch;x=torch.eye(3).to_sparse().expand(3,3)" 
```

from
```
NotImplementedError: Could not run 'aten::as_strided' with arguments from the 'SparseCPU' backend.
```
to
```
RuntimeError: Expand is unsupported for Sparse tensors.
```
@malfet malfet added the module: sparse Related to torch.sparse label Apr 5, 2023
@pytorch-bot
Copy link

pytorch-bot bot commented Apr 5, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/98365

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit ef8049c:
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@malfet malfet requested review from pearu and cpuhrsch April 5, 2023 00:01
@malfet
Copy link
Contributor Author

malfet commented Apr 5, 2023

@cpuhrsch do you know why why torch.eye(3).to_sparse_csr().is_sparse() returns false?
Also, why python -c "import torch;print(torch.eye(3).to_sparse().stride())" prints (0, 0), but the same command for sparse_csr tensor raises runtime exception?

@pearu
Copy link
Collaborator

pearu commented Apr 5, 2023

do you know why why torch.eye(3).to_sparse_csr().is_sparse() returns false?

@malfet This is because .is_sparse attribute is for testing COO sparse tensor, not for CSR sparse tensor:

>>> torch.eye(3).to_sparse_csr().is_sparse
False
>>> torch.eye(3).to_sparse_csr().is_sparse_csr
True

(.is_sparse is not called .is_sparse_coo because of BC)

Also, why python -c "import torch;print(torch.eye(3).to_sparse().stride())" prints (0, 0), but the same command for sparse_csr tensor raises runtime exception?

sparse compressed tensor implementation defines sym_strides_custom (that raises the runtime exception) while the sparse coo tensor implementation doesn't. IIRC, there was an edge case where coo stride() result was actually used but I couldn't find it quickly (enabling the exception for coo tensor strides might give a hint). In any case, sparse tensors don't have strides and one should not call the method on sparse tensors in general.

aten/src/ATen/native/TensorShape.cpp Outdated Show resolved Hide resolved
@malfet
Copy link
Contributor Author

malfet commented Apr 5, 2023

do you know why why torch.eye(3).to_sparse_csr().is_sparse() returns false?
(.is_sparse is not called .is_sparse_coo because of BC)

@pearu then perhaps the documentation should be updated, because according to it: Tensor.is_sparse "is True if the Tensor uses sparse storage layout, False otherwise."

sparse compressed tensor implementation defines sym_strides_custom (that raises the runtime exception) while the sparse coo tensor implementation doesn't. IIRC, there was an edge case where coo stride() result was actually used but I couldn't find it quickly (enabling the exception for coo tensor strides might give a hint). In any case, sparse tensors don't have strides and one should not call the method on sparse tensors in general.

Let me dry that in separate PR, as I believe stride() results indeed does not make sense for any type of sparse tensors.

@malfet malfet requested a review from pearu April 5, 2023 14:15
@malfet malfet added release notes: sparse release notes category topic: improvements topic category labels Apr 5, 2023
@amjames
Copy link
Collaborator

amjames commented Apr 5, 2023

do you know why why torch.eye(3).to_sparse_csr().is_sparse() returns false?
(.is_sparse is not called .is_sparse_coo because of BC)

@pearu then perhaps the documentation should be updated, because according to it: Tensor.is_sparse "is True if the Tensor uses sparse storage layout, False otherwise."

IMO: We should break that compatibility. We are still operating under the beta tag, so we can do that. We really just need to strict about BC with changes that affect other APIs (like gradcheck)

The docs also probably need a review to make sure that any place where "sparse layout" refers specifically to "sparse_coo" is clarified.

Copy link
Collaborator

@pearu pearu left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks, @malfet!

@pearu
Copy link
Collaborator

pearu commented Apr 5, 2023

Yes, the docs need an update.

In fact, I think we should just deprecate the usage of .is_sparse and .is_sparse_csr attributes (notice that there are no .is_sparse_csc/bsr/bsc attributes) in favor of using the .layout attribute.

That is, instead of x.is_sparse, use x.layout is torch.sparse_coo, etc.

For example, we often use x.layout in {torch.sparse_coo, torch.sparse_csr, torch.sparse_csc, torch.sparse_bsr, torch.sparse_bsc} to test if x has any of the sparse layouts.

@malfet
Copy link
Contributor Author

malfet commented Apr 6, 2023

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 6, 2023
@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: Approval needed from one of the following:
dhthompson, soumith, jisaacso, osalpekar, asalioufb, ...

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@malfet
Copy link
Contributor Author

malfet commented Apr 6, 2023

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

The merge job was canceled. If you believe this is a mistake,then you can re trigger it through pytorch-bot.

@malfet
Copy link
Contributor Author

malfet commented Apr 6, 2023

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@malfet malfet deleted the malfet-patch-26 branch August 2, 2023 02:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request Merged module: sparse Related to torch.sparse release notes: sparse release notes category topic: improvements topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants