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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure when converting a sparse boolean tensor to a dense tensor #49977

Closed
NathanHowell opened this issue Dec 30, 2020 · 5 comments
Closed

Failure when converting a sparse boolean tensor to a dense tensor #49977

NathanHowell opened this issue Dec 30, 2020 · 5 comments
Labels
function request A request for a new function or the addition of new arguments/modes to an existing function. module: boolean tensor module: sparse Related to torch.sparse triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@NathanHowell
Copy link
Contributor

NathanHowell commented Dec 30, 2020

馃悰 Bug

Converting a sparse boolean tensor to a dense tensor fails

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-4-183b7da37f3a> in <module>
----> 1 torch.tensor([True, False]).to_sparse().to_dense()

RuntimeError: "add_dense_sparse" not implemented for 'Bool'

To Reproduce

Steps to reproduce the behavior:

import torch

# raises exception
torch.tensor([True, False]).to_sparse().to_dense()
# RuntimeError

# converting via another type works
torch.tensor([True, False]).to_sparse().to(torch.uint8).to_dense().to(torch.bool)
# Out[6]: tensor([ True, False])

Expected behavior

Conversion to succeed

Environment

PyTorch version: 1.7.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 11.1 (x86_64)
GCC version: Could not collect
Clang version: 12.0.0 (clang-1200.0.32.28)
CMake version: version 3.19.2

Python version: 3.9 (64-bit runtime)
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.4
[pip3] torch==1.7.1
[pip3] torchvision==0.8.2
[conda] Could not collect

cc @izdeby @aocsa @nikitaved @pearu @mruberry

@ngimel ngimel added module: sparse Related to torch.sparse module: boolean tensor triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Dec 31, 2020
@mruberry
Copy link
Collaborator

mruberry commented Jan 2, 2021

Thank you for reporting this issue, @NathanHowell. We would accept a PR implementing this behavior.

@mruberry mruberry added the function request A request for a new function or the addition of new arguments/modes to an existing function. label Jan 2, 2021
@NathanHowell
Copy link
Contributor Author

Thank you for reporting this issue, @NathanHowell. We would accept a PR implementing this behavior.

do you have any pointers as to how this can be fixed? I haven't run this through gdb/lldb and quick scan through the aten code didn't show anything obvious.

@mruberry
Copy link
Collaborator

mruberry commented Jan 2, 2021

I would suggest the following:

First, understand the call chain on CPU and CUDA. In this case the error message suggests the issue is from here on CPU:

And if we look at that dispatch macro we can see it doesn't include bool:

#define AT_DISPATCH_ALL_TYPES(TYPE, NAME, ...) \

This suggests a good starting point for inserting debug statements and prints, for example, on both the CPU and in the analogous place on CUDA.

Second, this fix might be as simple as updating the dispatch macro to include the boolean dtype. So you could try that and validate it fixes the issue, then create a test in test_sparse.py to verify the behavior works as expected. If that doesn't work, however, you'll have to dig further into the CPU and CUDA code to understand what's required to support this operation for bools.

@NathanHowell
Copy link
Contributor Author

cool, thx. I'll take a peek in the next day or two

hwangdeyu pushed a commit to hwangdeyu/pytorch that referenced this issue Jan 14, 2021
Summary:
Fixes pytorch#49977

Pull Request resolved: pytorch#50019

Reviewed By: smessmer

Differential Revision: D25782045

Pulled By: ezyang

fbshipit-source-id: a8389cbecb7e79099292a423a6fd8ac28631905b
@KinWaiCheuk
Copy link

I still encounter this bug after using torch.save and torch.load.

This code works:

x = torch.tensor([[[True, False],[True, True],[True, True]]])
x_sparse = x.to_sparse()
x_dense = x_sparse.to_dense()

But after doing torch.save and torch.load, I get the following error

torch.save(x_sparse, 'x.pt') 
x_loaded = torch.load('x.pt')
x_loaded.to_dense()

>>> RuntimeError: "coalesce" not implemented for 'Bool'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
function request A request for a new function or the addition of new arguments/modes to an existing function. module: boolean tensor module: sparse Related to torch.sparse triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants