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

divide and comparisons allow a greater range of Python integer and integer array combinations #1771

Merged
merged 4 commits into from
Jul 31, 2024

Conversation

ndgrigorian
Copy link
Collaborator

@ndgrigorian ndgrigorian commented Jul 29, 2024

This PR closes #1711 by aligning divide with Numpy 2.0 behavior.

When a Python integer is out-of-bounds for a (strong, array) integer data type of the other input, _resolve_weak_types_all_py_ints uses np.min_scalar_type to find the minimum type that will fit it.

This does not affect the output type of comparisons or divide, and therefore aligns with NEP-50 while permitting such workflows as:

In [2]: dpt.divide(-4, dpt.asarray(3, dtype="u4"))
Out[2]: usm_ndarray(-1.3333334, dtype=float32)

This also fixes a bug introduced in #1650 where comparisons between uint64 data type arrays and Python integers larger than the maximum for the default integral data type no longer worked.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • If this PR is a work in progress, are you opening the PR as a draft?

Copy link

github-actions bot commented Jul 29, 2024

Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. 🤞

Copy link

Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_186 ran successfully.
Passed: 880
Failed: 15
Skipped: 119

@coveralls
Copy link
Collaborator

coveralls commented Jul 30, 2024

Coverage Status

coverage: 87.614% (+0.005%) from 87.609%
when pulling 655a5d9 on resolve-gh-1711-fix-comparisons
into a40263d on master.

Copy link

Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_187 ran successfully.
Passed: 894
Failed: 1
Skipped: 119

@oleksandr-pavlyk
Copy link
Collaborator

This example from gh-1711 still give unexpected result:

In [4]: dpt.floor_divide(dpt.asarray(7, dtype='u4'), -2)
Out[4]: usm_ndarray(0, dtype=uint32)

@ndgrigorian
Copy link
Collaborator Author

ndgrigorian commented Jul 30, 2024

This example from gh-1711 still give unexpected result:

In [4]: dpt.floor_divide(dpt.asarray(7, dtype='u4'), -2)
Out[4]: usm_ndarray(0, dtype=uint32)

It won't after Numpy 2, because -2 will no longer be permitted to promote to uint32.

In general it may be a good idea to go back and disallow overflows from Python integers in asarray explicitly, just as Numpy has done.

But arguably, this result is "correct" in that it follows the NEP-50 rule (Python scalar value does not determine output dtype), it's just undefined behavior. The alternative would be to cast to int64, but this wouldn't match how it's handled in Numpy 2.

@oleksandr-pavlyk
Copy link
Collaborator

Indeed, with NumPy 2.0 in run-time environment, I am getting

OverflowError: Python integer -2 out of bounds for uint32

@ndgrigorian ndgrigorian marked this pull request as ready for review July 30, 2024 23:20
Copy link

Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_193 ran successfully.
Passed: 894
Failed: 1
Skipped: 119

@ndgrigorian ndgrigorian force-pushed the resolve-gh-1711-fix-comparisons branch from c3283f2 to 655a5d9 Compare July 31, 2024 01:20
Copy link

Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_193 ran successfully.
Passed: 894
Failed: 1
Skipped: 119

Copy link
Collaborator

@oleksandr-pavlyk oleksandr-pavlyk left a comment

Choose a reason for hiding this comment

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

Thank you @ndgrigorian ! LGTM

@ndgrigorian ndgrigorian merged commit c7af3a0 into master Jul 31, 2024
26 of 35 checks passed
@ndgrigorian ndgrigorian deleted the resolve-gh-1711-fix-comparisons branch July 31, 2024 18:56
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.

Unexpected result from divide-like ufuncs with unsigned integer array and negative integer scalar as inputs
3 participants