Skip to content

Add support for NumPy 1.23. - #8377

Merged
sklam merged 6 commits into
numba:mainfrom
stuartarchibald:wip/np123_support
Aug 31, 2022
Merged

sklam merged 6 commits into
numba:mainfrom
stuartarchibald:wip/np123_support

Conversation

@stuartarchibald

Copy link
Copy Markdown
Contributor

Adds support for NumPy 1.23 to Numba. With thanks to @gmarkall for patches for the CUDA target.

The Azure CI matrix now looks like this:

$ python -c 'from numba.tests.support import print_azure_matrix; print_azure_matrix()'
NumPy | Python | Count
-----------------------
 1.18 |  3.7   |   4
 1.18 |  3.8   |   1
 1.19.2=*_0 |  3.8   |   3
 1.19.2=*_0 |  3.9   |   1
 1.20 |  3.8   |   3
 1.20 |  3.9   |   1
 1.21 |  3.9   |   1
 1.21 |  3.10  |   1
 1.22 |  3.10  |   1
 1.22 |  3.8   |   1
 1.23 |  3.9   |   1
 1.23 |  3.10  |   2

gmarkall and others added 4 commits August 22, 2022 11:22
Since numpy/numpy#21477 the strides of a 0-sized array are 0 instead of
the itemsize. This causes `array_core()` to erroneously attempt to index
the array with numeric indices instead of empty slices.

Since this is a special case (empty arrays), a simple solution seems to
be for `array_core()` to return the array itself when the array is
0-sized. In order to maintain consistency of the `array_core()` function
between the simulator and hardware targets, we make this change to both
implementations.
Changing to a dtype of a different size in a view only requires
contiguity of the last axis from NumPy 1.23 onwards:

https://numpy.org/devdocs/release/1.23.0-notes.html#changing-to-dtype-of-a-different-size-now-requires-contiguity-of-only-the-last-axis

The error message for the case when even the last axis is not contiguous
has now changed accordingly, so we must search for both variants of the
message.
This updates the Numba implementation of array.view to match the
changed behaviour present in NumPy 1.23+.
This was referenced Aug 22, 2022
@stuartarchibald stuartarchibald added this to the Numba 0.56.1 milestone Aug 22, 2022
@stuartarchibald stuartarchibald added the Effort - medium Medium size effort needed label Aug 22, 2022
@gmarkall

Copy link
Copy Markdown
Member

gpuci run tests

@stuartarchibald

Copy link
Copy Markdown
Contributor Author

Am marking this as ready for review as the 32bit build fail on Azure CI is unrelated.

@stuartarchibald
stuartarchibald marked this pull request as ready for review August 22, 2022 11:18
@esc esc added the Pending BuildFarm For PRs that have been reviewed but pending a push through our buildfarm label Aug 23, 2022

@gmarkall gmarkall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the code is good in the sense that it gets the test suite passing with 1.23 in light of the changed behaviour on taking views, but I still think we permit a little more than NumPy does - I'm not sure whether this matters or not.

For example:

import numpy as np
from numba import njit

x = np.zeros((4, 4))
view = x.T[:2:, :2:]


@njit
def f(arr):
    return arr.view(np.int8)

print("Running jitted function...")
f(view)
print("Running Python function...")
f.py_func(view)

gives:

$ python repro.py 
Running jitted function...
Running Python function...
Traceback (most recent call last):
  File "/home/gmarkall/numbadev/issues/8377/repro.py", line 15, in <module>
    f.py_func(view)
  File "/home/gmarkall/numbadev/issues/8377/repro.py", line 10, in f
    return arr.view(np.int8)
ValueError: To change to a dtype of a different size, the last axis must be contiguous

I think this discrepancy comes because we only look at C-/F-contiguity (if flag_f and not flag_c:) rather than looking specifically at the strides as NumPy does:

https://github.com/numpy/numpy/blob/50a74fb65fc752e77a2f9e9e2b7227629c2ba953/numpy/core/src/multiarray/getset.c#L516-L525

self.assertEqual(str(raises.exception),
"new type not compatible with array")
def check_err_noncontig_last_axis(arr, dtype):
# check NumPy interpreted version raises

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On first glance this seems a tiny bit odd to be checking NumPy, but I think the purpose that this serves is to explicitly test / document our own understanding of the NumPy behaviour we replicate in Numba. So I'm happy with this check staying in.

Out of curiousity, do we expect further NumPy changes around this area that we might detect in future releases with the NumPy check?

@stuartarchibald stuartarchibald Aug 24, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am unsure if there will be changes in NumPy in this area in future releases, but put this in to catch NumPy either changing this or the associated error message(s).

@stuartarchibald

Copy link
Copy Markdown
Contributor Author

Thanks for finding the patch breaking example in #8377 (review) @gmarkall, I've added 8b0d0d2 in an attempt to address this. I noticed when writing it that the existing Numba implementation doesn't exactly replicate NumPy either with respect to error messages, but suggest leaving that for another time (an error is raised, it's just not the same as that in NumPy < 1.23).

@gmarkall

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines successfully started running 1 pipeline(s).

@gmarkall

Copy link
Copy Markdown
Member

gpuci run tests

@gmarkall gmarkall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is good to approve - it looks like behaviour is consistent for NumPy 1.23 now as far as I can tell and the code of the checks appears to be equivalent between Numba and NumPy. I did notice that there are still some discrepancies between the messages produced by Numba and those produced by NumPy, but they are only for NumPy < 1.23 so I don't think it's really worth worrying about.

I tried to push a commit that reduced some duplication in the check_err_* methods but it seems I can't push to Stuart's branch so may just add this as a small refactoring PR later.

@gmarkall gmarkall added 4 - Waiting on CI Review etc done, waiting for CI to finish and removed 3 - Ready for Review labels Aug 25, 2022
@sklam sklam added BuildFarm Passed For PRs that have been through the buildfarm and passed 5 - Ready to merge Review and testing done, is ready to merge and removed Pending BuildFarm For PRs that have been reviewed but pending a push through our buildfarm labels Aug 31, 2022
@sklam sklam removed the 4 - Waiting on CI Review etc done, waiting for CI to finish label Aug 31, 2022
@sklam
sklam merged commit d56f9ba into numba:main Aug 31, 2022
This was referenced Aug 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5 - Ready to merge Review and testing done, is ready to merge BuildFarm Passed For PRs that have been through the buildfarm and passed Effort - medium Medium size effort needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants