AttributeError: 'builtin_function_or_method' object has no attribute 'overloadpacket'
🐛 Describe the bug
Thanks for building this project! I just tried the example from http://pytorch.org/maskedtensor/main/notebooks/nan_grad.html but got the following error:
Traceback (most recent call last):
File "test.py", line 156, in <module>
y = torch.where(mask, torch.exp(mx), my)
File "/workspace/.conda/envs/main/lib/python3.7/site-packages/maskedtensor/core.py", line 218, in __torch_function__
ret = func(*args, **kwargs)
File "/workspace/.conda/envs/main/lib/python3.7/site-packages/maskedtensor/core.py", line 230, in __torch_dispatch__
func = func.overloadpacket
AttributeError: 'builtin_function_or_method' object has no attribute 'overloadpacket'
Below is my code
import torch
from maskedtensor import masked_tensor
from maskedtensor import as_masked_tensor
x = torch.tensor([-10., -5, 0, 5, 10, 50, 60, 70,
80, 90, 100], requires_grad=True)
mask = x < 0
mx = masked_tensor(x, mask, requires_grad=True)
my = masked_tensor(torch.ones_like(x), ~mask, requires_grad=True)
y = torch.where(mask, torch.exp(mx), my)
s = y.sum()
s.backward()
# Gradient is only provided to selected subset.
# Effectively this changes the gradient of where to mask out elements instead
# of setting them to zero.
print("mx.grad: ", mx.grad)
My maskedtensor version: 0.11.dev2022416 My PyTorch version: 1.11.0+cu102 My Python version: 3.7.11
Any idea how to workaround this?
Hi @lazycal,
Thanks so much for your interest in MaskedTensor!
The problem here is indeed related to: https://github.com/pytorch/maskedtensor/blob/21155ff2e0095d360f1450054655c92f3ae0c62d/maskedtensor/core.py#L229
which is related to a change made in __torch_dispatch__ in torch 1.12. Basically, you're using nightly version and the stable version of PyTorch, but they need to be consistent with each other. The fix here is to:
- Use the
torchnightly and themaskedtensornightly, or - Use the stable maskedtensor from
pip install maskedtensorandtorch 1.11
We recommend #1 since you'll have the most updated features of maskedtensor, but both work! For #1, I've run the example and printed out the versions
torch version 1.12.0.dev20220420
maskedtensor version 0.11.0+21155ff
mx.grad: masked_tensor(
[ 0.0000, 0.0067, --, --, --, --, --, --, --, --, --]
)
If you'd like to do this, you can simply:
pip uninstall torch
pip3 install --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
python setup.py develop
OR
python3 -m pip install --user "git+https://github.com/pytorch/maskedtensor.git"
Please verify in the above that you get something like: Successfully installed torch-1.12.0.dev20220420
I've updated the setup to require torch > 1.11 so that others won't run into this problem, and I'll update the installation instructions to be more clear on this front, since it can certainly be confusing! I hope this makes sense and fixes your problem, and please don't hesitate to continue reaching out! We really appreciate it :)
Also, if you don't mind elaborating, we're curious what your use case of MaskedTensor is and if there are any particular features that you would like most next!
@george-qi Thanks for your reply! My use case is almost identical to the example. I am crafting a piecewise loss function that looks like x if x>0 else torch.exp(x), and then I found my gradients become nan and I was very confused. Then I found this project that both explains the issue and aims to fix it. Currently it fixes my issue perfectly, and will let you know if there is any other features I would like next! Thanks for the project, this is cool!
Awesome to hear :) I'll close this issue for now then; please reach out whenever!