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

[stdlib] Enhance Handling of Infinity and NaN in assert_almost_equal #2375

Conversation

leandrolcampos
Copy link
Contributor

@leandrolcampos leandrolcampos commented Apr 22, 2024

This PR enhances the assert_almost_equal function to correctly handle cases involving infinity and NaN.

According to test_assert_almost_equal added to /test/testing/test_assertion.mojo, the current implementation of assert_almost_equal results in errors in the following cases:

    alias float_type = DType.float32
    alias _inf = inf[float_type]()
    alias _nan = nan[float_type]()
    ...
    _should_succeed(
        SIMD[float_type, 2](-_inf, _inf), SIMD[float_type, 2](-_inf, _inf)
    )
    ...
    _should_fail(
        SIMD[float_type, 2](-_inf, 0.0),
        SIMD[float_type, 2](_inf, 0.0),
        rtol=0.1,
    )
    _should_fail(
        SIMD[float_type, 2](_inf, 0.0),
        SIMD[float_type, 2](0.0, 0.0),
        rtol=0.1,
    )
    ...
    _should_fail(
        SIMD[float_type, 2](_nan, 0.0),
        SIMD[float_type, 2](0.0, 0.0),
        equal_nan=True,
    )

This PR also:

  • Eliminates the use of and and or in the _isclose function due to the issue outlined in [BUG] The and and or operators produce incorrect results with SIMD inputs #2374.
  • Explicitly reduces boolean vectors to boolean scalar values instead of counting on implicit conversions for clarity.
  • Avoids arithmetic operations in _isclose and assert_almost_equal when the type is boolean, as these operations are not supported in this case.
  • Clarifies the behavior of assert_almost_equal in the docstring, highlighting differences from similar functions such as numpy.testing.assert_allclose.
  • Adds the inf function to utils/_numerics along with corresponding tests in test/utils/test_numerics.mojo.

@leandrolcampos leandrolcampos requested a review from a team as a code owner April 22, 2024 02:04
@leandrolcampos leandrolcampos force-pushed the fix/infinity-handling-assert branch 3 times, most recently from 031ccbd to d314a48 Compare April 27, 2024 15:36
@leandrolcampos
Copy link
Contributor Author

Hello @JoeLoser,

I hope this message finds you well. I wanted to check in to see if there's anything additional you need from my side to facilitate the review process.

Thank you for your time and attention.

All the best,

Copy link
Contributor

@abduld abduld left a comment

Choose a reason for hiding this comment

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

Nice . Thanks for this

@JoeLoser
Copy link
Collaborator

JoeLoser commented May 1, 2024

Hello @JoeLoser,

I hope this message finds you well. I wanted to check in to see if there's anything additional you need from my side to facilitate the review process.

Thank you for your time and attention.

All the best,

Sorry for the delay in reviewing this - I was on PTO/out of office last week and swamped with meetings yesterday. I just imported this internally and it'll be merged soon! Thanks so much for the contribution (and additional review from @abduld!). 🎉

@JoeLoser JoeLoser added imported-internally Signals that a given pull request has been imported internally. merged-internally Indicates that this pull request has been merged internally labels May 1, 2024
@JoeLoser
Copy link
Collaborator

JoeLoser commented May 1, 2024

✅🟣 This contribution has been merged 🟣✅

Hey @leandrolcampos,

Thanks so much for the contribution! 🎉

We're moving to a new infrastructure for merging contributions to Mojo (we're using a tool called Copybara), and your contribution has now been merged into our internal copy of the Mojo Standard Library. I've added the "merged-internally" label on this PR.

The changes in this PR will appear here in the mojo repo nightly branch when we do our next outbound synchronization at the time that the next Mojo nightly is released. That should happen within the next 24 hours.

Please let me know if you have any questions or concerns.

@JoeLoser JoeLoser removed the merged-internally Indicates that this pull request has been merged internally label May 1, 2024
@JoeLoser
Copy link
Collaborator

JoeLoser commented May 1, 2024

Note to self: this just got reverted due to an internal breakage on one test; we'll look into it today, but FYI.

@leandrolcampos
Copy link
Contributor Author

Please let me know if there is anything I can do to help.

JoeLoser pushed a commit that referenced this pull request May 3, 2024
…lmost_equal` (#38991)

[External] [stdlib] Enhance Handling of Infinity and NaN in
`assert_almost_equal`

This PR enhances the `assert_almost_equal` function to correctly handle
cases involving infinity and NaN.

According to `test_assert_almost_equal` added to
`/test/testing/test_assertion.mojo`, the current implementation of
`assert_almost_equal` results in errors in the following cases:

```mojo
    alias float_type = DType.float32
    alias _inf = inf[float_type]()
    alias _nan = nan[float_type]()
    ...
    _should_succeed(
        SIMD[float_type, 2](-_inf, _inf), SIMD[float_type, 2](-_inf, _inf)
    )
    ...
    _should_fail(
        SIMD[float_type, 2](-_inf, 0.0),
        SIMD[float_type, 2](_inf, 0.0),
        rtol=0.1,
    )
    _should_fail(
        SIMD[float_type, 2](_inf, 0.0),
        SIMD[float_type, 2](0.0, 0.0),
        rtol=0.1,
    )
    ...
    _should_fail(
        SIMD[float_type, 2](_nan, 0.0),
        SIMD[float_type, 2](0.0, 0.0),
        equal_nan=True,
    )
```

This PR also:
- Eliminates the use of `and` and `or` in the `_isclose` function due to
the issue outlined in #2374.
- Explicitly reduces boolean vectors to boolean scalar values instead of
counting on implicit conversions for clarity.
- Avoids arithmetic operations in `_isclose` and `assert_almost_equal`
when the type is boolean, as these operations are not supported in this
case.
- Clarifies the behavior of `assert_almost_equal` in the docstring,
highlighting differences from similar functions such as
`numpy.testing.assert_allclose`.
- Adds the `inf` function to `utils/_numerics` along with corresponding
tests in `test/utils/test_numerics.mojo`.

ORIGINAL_AUTHOR=Leandro Augusto Lacerda Campos
<15185896+leandrolcampos@users.noreply.github.com>
PUBLIC_PR_LINK=#2375

Co-authored-by: Leandro Augusto Lacerda Campos <15185896+leandrolcampos@users.noreply.github.com>
Closes #2375
MODULAR_ORIG_COMMIT_REV_ID: 2e8b24461dd3279bc841877cc0167acaa104f273
@JoeLoser
Copy link
Collaborator

JoeLoser commented May 3, 2024

Please let me know if there is anything I can do to help.

Will do! I didn't have time to look into this yet, but hoping to soon (got tied up into things for the MAX 24.3 release earlier today). I suspect it's an issue you're uncovering in some of our internal code, but I'll need to root cause it in order to land this for you internally. I'll keep you updated if there's anything, but I'm not expecting anything right now 😄

@leandrolcampos
Copy link
Contributor Author

Ok. I'll resolve the conflicts in the branch 🙏

Signed-off-by: Leandro Augusto Lacerda Campos <15185896+leandrolcampos@users.noreply.github.com>
Signed-off-by: Leandro Augusto Lacerda Campos <15185896+leandrolcampos@users.noreply.github.com>
Signed-off-by: Leandro Augusto Lacerda Campos <15185896+leandrolcampos@users.noreply.github.com>
…function

Signed-off-by: Leandro Augusto Lacerda Campos <15185896+leandrolcampos@users.noreply.github.com>
@JoeLoser JoeLoser self-assigned this May 5, 2024
@ematejska ematejska added the mojo-repo Tag all issues with this label label May 6, 2024
@leandrolcampos
Copy link
Contributor Author

Sorry for bothering you again. You may also need to update the implementation of math.isclose.

@JoeLoser
Copy link
Collaborator

JoeLoser commented May 7, 2024

Sorry for bothering you again. You may also need to update the implementation of math.isclose.

Don't feel bad at all! I'm sorry I haven't been able to land this yet internally - it's on my list to get to though. :)

@modularbot
Copy link
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added the merged-internally Indicates that this pull request has been merged internally label May 8, 2024
@JoeLoser
Copy link
Collaborator

JoeLoser commented May 8, 2024

@leandrolcampos this just merged again internally. Thanks for pushing on this and your patience! Your PR actually exposed a real bug with handling of inf in some internal code that @dukebw just fixed this morning. Appreciate it!

@leandrolcampos
Copy link
Contributor Author

@JoeLoser and @dukebw, I'm happy to be able to help you and contribute to Mojo. Thanks!

JoeLoser pushed a commit that referenced this pull request May 8, 2024
…lmost_equal` (#39452)

[External] [stdlib] Enhance Handling of Infinity and NaN in
`assert_almost_equal`

This PR enhances the `assert_almost_equal` function to correctly handle
cases involving infinity and NaN.

According to `test_assert_almost_equal` added to
`/test/testing/test_assertion.mojo`, the current implementation of
`assert_almost_equal` results in errors in the following cases:

```mojo
    alias float_type = DType.float32
    alias _inf = inf[float_type]()
    alias _nan = nan[float_type]()
    ...
    _should_succeed(
        SIMD[float_type, 2](-_inf, _inf), SIMD[float_type, 2](-_inf, _inf)
    )
    ...
    _should_fail(
        SIMD[float_type, 2](-_inf, 0.0),
        SIMD[float_type, 2](_inf, 0.0),
        rtol=0.1,
    )
    _should_fail(
        SIMD[float_type, 2](_inf, 0.0),
        SIMD[float_type, 2](0.0, 0.0),
        rtol=0.1,
    )
    ...
    _should_fail(
        SIMD[float_type, 2](_nan, 0.0),
        SIMD[float_type, 2](0.0, 0.0),
        equal_nan=True,
    )
```

This PR also:
- Eliminates the use of `and` and `or` in the `_isclose` function due to
the issue outlined in #2374.
- Explicitly reduces boolean vectors to boolean scalar values instead of
counting on implicit conversions for clarity.
- Avoids arithmetic operations in `_isclose` and `assert_almost_equal`
when the type is boolean, as these operations are not supported in this
case.
- Clarifies the behavior of `assert_almost_equal` in the docstring,
highlighting differences from similar functions such as
`numpy.testing.assert_allclose`.
- Adds the `inf` function to `utils/_numerics` along with corresponding
tests in `test/utils/test_numerics.mojo`.

ORIGINAL_AUTHOR=Leandro Lacerda Campos
<15185896+leandrolcampos@users.noreply.github.com>
PUBLIC_PR_LINK=#2375

Co-authored-by: Leandro Lacerda Campos <15185896+leandrolcampos@users.noreply.github.com>
Closes #2375
MODULAR_ORIG_COMMIT_REV_ID: 8e0c2394d01d12015816f242e98d6f457af9cdfd
@JoeLoser JoeLoser added the merged-externally Merged externally in public mojo repo label May 8, 2024
@JoeLoser JoeLoser closed this May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally mojo-repo Tag all issues with this label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants