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

Add MaskedTensor support to _is_any_true #128574

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nowtryz
Copy link
Contributor

@nowtryz nowtryz commented Jun 13, 2024

Fixes #128557

If there is a better way to detect autograd anomalies consistently, feel free to share your ideas. This is a dirty check.

Copy link

pytorch-bot bot commented Jun 13, 2024

🔗 Helpful Links

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

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 4f0a867 with merge base b4a7b54 (image):
💚 Looks good so far! There are no failures yet. 💚

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

data = _get_data(args[0])
mask = _maybe_get_mask(args[0])
if mask is None:
raise ValueError(f"__torch_dispatch__, {func}: expected a mask tensor")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
raise ValueError(f"__torch_dispatch__, {func}: expected a mask tensor")
raise ValueError(f"__torch_dispatch__, {func}: expected args[0] to be a MaskedTensor")


if func(mask):
return MaskedTensor(func(data & mask), torch.tensor(True))
return MaskedTensor(torch.tensor(0), torch.tensor(False))
Copy link
Contributor

@soulitzer soulitzer Jun 13, 2024

Choose a reason for hiding this comment

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

Could this function just be return func(data & mask)? e.g. does the return necessarily have to be a MaskedTensor?
If everything is masked out, technically "any" of an empty set is just False?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The overload is supposed to return a masked tensor but it is true that if there is no specified true value, then the result is the just False. I'll still wrap the result in a MaskedTensor for coherence, otherwise MaskedTensor.__torch_function__ will still try to wrap the result in a MaskedTensor and fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

otherwise MaskedTensor.torch_function will still try to wrap the result in a MaskedTensor and fail.

Hmm I think its still worth thinking about, as we could simply change the __torch_function__ to relax the condition.

In theory a masked tensor with all True mask should behave as-if it were a plain tensor, but performance-wise maybe we prefer plain tensors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nevermind, torch_function does not enforce subclass, I'll fix this today or tomorrow.

@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

MaskedTensor do not support _is_any_true`
3 participants