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

fixing out of bound access for nll_loss #1752

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

jjsjann123
Copy link
Collaborator

@jjsjann123 jjsjann123 commented Feb 7, 2025

Fixes #1744

See torch.nn.functional.nll_loss definition here: https://pytorch.org/docs/stable/generated/torch.nn.functional.nll_loss.html

Because we use prims.take_along_axis, which doesn't cap out-of-bound access. For ignore_index that's out side of [0, C), we need to:

  1. cap the entry in target (so we are not going to be accessing arbitrary memory);
  2. pad inputs with a new row of all-zero, so we can fill the ignore entry with all-zero.

@@ -5111,12 +5111,18 @@ def _nll_loss_helper(
bcast_weight = reshape(weight, [num_class] + [1 for _ in range(2, a.ndim)])
out = out * bcast_weight

assert isinstance(ignore_index, Number)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If ignore_index is a NumberProxy are the constraints created as expected for >= 0 and < num_class comparisons with the symbolic values cache mode?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For today with constant constraints, I think that's right. But then we could have the same out of bound access issue with NumberProxy for ignore_index.
If we do need to support that, I think for now we need to use < 0 or >= num_class to handle it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

BTW, added the support.

For some reason thunderfx does indeed hit give ignore_index as NumberProxy instead, you probably know this better than I do.

@jjsjann123 jjsjann123 marked this pull request as ready for review February 8, 2025 00:45
@jjsjann123 jjsjann123 changed the title fixing out of bound access for cross_entropy fixing out of bound access for nll_loss Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants