-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[maskedtensor] Add safe softmax tutorial #2045
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
Conversation
[ghstack-poisoned]
|
||
Luckily, :class:`MaskedTensor` has solved this issue: | ||
|
||
>>> data = torch.randn(3, 3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would explain a bit more here or inline what you are doing:
Let's create a Tensor with an "unsafe" column, that is a column of all -inf padding values, for which PyTorch's traditional softmax will result in nans.
Now let's create a safeter MaskedTensor from this Tensor, and show that the resultant values are masked out
|
||
:class:`MaskedTensor` result: | ||
|
||
>>> mt.softmax(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused on this result. The softmax above returns two different values that are getting masked here: 0.0 and nan. Do we really want to mask out both outputs? I assume the idea is that we want to mask all input padding (aka mask all -infs). but in situations of unstable training, do 0's make a difference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the 0's make a difference in unstable training, but maybe I'm not quite understanding the question. In general for MaskedTensor, if the user has deemed a particular index as "unspecified", then it will remain unspecified or masked out in both the forward as well as the backward. And so in this case, while the underlying data that is being masked out would have been different (i.e. 0.0 vs nan), they're both unspecified and so we still want to mask them out.
For training specifically, I agree that leaving the 0's would be identical, but I think it would contradict MaskedTensor semantics and become confusing (e.g. wouldn't know when to expect masked out values vs not.)
Another high level comment similar to other tutorials -- I think this tutorial makes more sense as one component of a larger tutorial that showcases end to end training. At first glance, it's not intuitive to me why I would care to end with a MaskedTensor after softmax vs ending with a tensor. You do reference an issue on unstable training for all nan examples in a batch, but I feel like showing that first hand as part of e2e training would be more powerful. |
Stack from ghstack (oldest at bottom):