pytorch / maskedtensor

MaskedTensors for PyTorch
https://pytorch.org/maskedtensor
Other
38 stars 10 forks source link

TypeError when running the tutorial code for CSR #70

Closed tvercaut closed 1 year ago

tvercaut commented 1 year ago

🐛 Describe the bug

The tutorial code from https://pytorch.org/maskedtensor/main/notebooks/sparse.html#sparse-csr is failling on google colab.

!pip install maskedtensor-nightly

import torch
import maskedtensor
print(f'Running PyTorch version: {torch.__version__}')
print(f'Running maskedtensor version: {maskedtensor.__version__}')

crow_indices = torch.tensor([0, 2, 4])
col_indices = torch.tensor([0, 1, 0, 1])
values = torch.tensor([1, 2, 3, 4])
mask_values = torch.tensor([True, False, False, True])

csr = torch.sparse_csr_tensor(crow_indices, col_indices, values, dtype=torch.double)
mask = torch.sparse_csr_tensor(crow_indices, col_indices, mask_values, dtype=torch.bool)

mt = maskedtensor.masked_tensor(csr, mask)

leads to

Running PyTorch version: 1.12.1+cu113
Running maskedtensor version: 0.12.dev2022714

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

[<ipython-input-5-8513d08b650e>](https://localhost:8080/#) in <module>
      7 mask = torch.sparse_csr_tensor(crow_indices, col_indices, mask_values, dtype=torch.bool)
      8 
----> 9 mt = maskedtensor.masked_tensor(csr, mask)

1 frames

[/usr/local/lib/python3.7/dist-packages/maskedtensor/core.py](https://localhost:8080/#) in __new__(cls, data, mask, requires_grad)
    225         kwargs["dispatch_sizes_strides_policy"] = "strides"
    226         kwargs["dispatch_layout"] = True
--> 227         return torch.Tensor._make_wrapper_subclass(cls, data.size(), **kwargs)  # type: ignore[attr-defined]
    228 
    229     def _preprocess_data(self, data, mask):

TypeError: _make_wrapper_subclass() got an unexpected keyword argument 'dispatch_sizes_strides_policy'
mgveliko commented 1 year ago

I had a similar issue and was caused by having Pytorch 1.12.1 (the stable version) and the nightly version of maskedtensor. For me the solution was to uninstall maskedtensor-nightly and use !pip install maskedtensor. See this closed issue for more details: https://github.com/pytorch/maskedtensor/issues/50

cpuhrsch commented 1 year ago

cc @george-qi

tvercaut commented 1 year ago

As per https://github.com/pytorch/maskedtensor/issues/71#issuecomment-1286134243, I tried with the upstream pytorch and confirms it works there subject to modifying maskedtensor by torch.masked:

# Try with the nightly version of PyTorch
!pip3 install --pre torch torchvision torchaudio torchtext --extra-index-url https://download.pytorch.org/whl/nightly/cu117 --upgrade

import torch
print(f'Running PyTorch version: {torch.__version__}')

crow_indices = torch.tensor([0, 2, 4])
col_indices = torch.tensor([0, 1, 0, 1])
values = torch.tensor([1, 2, 3, 4])
mask_values = torch.tensor([True, False, False, True])

csr = torch.sparse_csr_tensor(crow_indices, col_indices, values, dtype=torch.double)
mask = torch.sparse_csr_tensor(crow_indices, col_indices, mask_values, dtype=torch.bool)

mt = torch.masked.masked_tensor(csr, mask)