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

adding a nan sanitizer similar to our int sanitizer #6009

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

Conversation

adamomainz
Copy link

@adamomainz adamomainz commented Feb 24, 2025

Hey All - We have had a few debugging issues with nan values popping up that are extremely difficult to find. Adding a nan sanitizer to make everyones life a bit easier.

New contributor declaration

  • [x ] I am not making a trivial change, such as fixing a typo in a comment.

  • [x ] I have written a PR description following these
    rules.

  • [x ] I have run pre-commit run --from-ref origin/main --to-ref HEAD.

  • Select one of the following.

    • [ x] I have added tests.
      • /test for lit tests
      • /unittest for C++ tests
      • /python/test for end-to-end tests
    • [x ] This PR does not need a test because ``.
  • Select one of the following.

    • [ x] I have not added any lit tests.
    • The lit tests I have added follow these best practices,
      including the "tests should be minimal" section. (Usually running Python code
      and using the instructions it generates is not minimal.)

@@ -195,7 +208,10 @@ def _convert_float(input, input_dtype, output_dtype, rounding_mode):
significand[subnormal_index] = (significand[subnormal_index] << bit_pos[subnormal_index]) & (
(1 << input_dtype.fp_mantissa_width) - 1)
# Prevent overflow and underflow
exponent_output = np.maximum(0, np.minimum((exponent - bias_input + bias_output), (1 << output_exponent_width) - 1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Too many diffs. Maybe you should run pre-commit

Copy link
Contributor

Choose a reason for hiding this comment

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

Apologies for the extra changes in here as well pre commit decided to do more linting than I like

That's not expected actually

Copy link
Author

Choose a reason for hiding this comment

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

agree let me remove pre-commit and try again

Copy link
Author

Choose a reason for hiding this comment

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

@Jokeren apologies for the delay! This is finally cleaned up

"""
if not builder.options.sanitize_nan:
return
input_cond = and_(equal(lhs,lhs,builder), equal(rhs,rhs,builder), builder)
Copy link
Contributor

Choose a reason for hiding this comment

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

Neat trick to detect NaN values!

return tl.tensor(builder.create_fsub(input.handle, other.handle), input.type)
# int - int
elif scalar_ty.is_int():
if sanitize_nan:
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably not needed for ints?

Copy link
Author

Choose a reason for hiding this comment

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

yeah good point :)

input, other = binary_op_type_checking_impl(input, other, builder)
scalar_ty = input.type.scalar
if sanitize_nan:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why this check is in common path while in other functions it is after checking for int/float type?

Copy link
Author

Choose a reason for hiding this comment

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

this is the only path where we dont have a ptr check so I thought it would be cleaner to just do the check here seeing as though in both cases below we are currently checking

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to put it everywhere under if float for consistency?

Copy link
Author

@adamomainz adamomainz Feb 26, 2025

Choose a reason for hiding this comment

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

agreed especially after your comment about not needing this for ints. Also considering moving this to some ops that have a higher chance of NaN ie exponentials, sqrt, pow etc. what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, the set of ops that may require this is most likely different than for overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants