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

MAINT: Add array-api-tests CI stage, add ndarray.__array_namespace__ #25167

Merged
merged 2 commits into from
Nov 28, 2023

Conversation

mtsokol
Copy link
Member

@mtsokol mtsokol commented Nov 16, 2023

Hi @rgommers @asmeurer,

This draft PR introduces array-api-tests CI stage. Here are some important notes:

  • It's the first WIP implementation that I managed to initially run on my fork. I'm sharing it here to start the discussion.
  • I couldn't find array-api-tests on PyPI, so I followed implementation in https://github.com/google/jax/pull/16099/files, where array-api-tests is cloned, and some issues are manually fixed (for now).
  • array-api-skips.txt contains and disables quite a lot of tests - the rationale is to merge it first, and then, as Array API PRs arrive, enable tests gradually.
  • In this PR I added __array_namespace__ to ndarray class and __array_api_version__ to the main namespace.
  • xp.bool is required to run the tests and it can't be XFAILed, so API: Add and redefine numpy.bool [Array API] #25080 needs to be merged before this PR [EDIT] Merged.

@mtsokol mtsokol changed the title MAINT: Add array-api-tests CI stage, add ndarray.__array_namespace__ MAINT: Add array-api-tests CI stage, add ndarray.__array_namespace__ Nov 16, 2023
@mtsokol mtsokol self-assigned this Nov 16, 2023
@mtsokol mtsokol force-pushed the array-api-test-ci branch 2 times, most recently from 0d50cd4 to edd114c Compare November 16, 2023 21:18
Copy link
Member

Choose a reason for hiding this comment

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

Don't know if there's a cleaner place to put this file than the project root.

@asmeurer
Copy link
Member

This could also be used to run tests for numpy.array_api, but I guess that's lower priority.

run: |
python -m pip install -r build_requirements.txt
python -m pip install -r test_requirements.txt
python -m pip install hypothesis!=6.88.4 # 6.88.4 leads to a strange error
Copy link
Contributor

@jakevdp jakevdp Nov 16, 2023

Choose a reason for hiding this comment

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

This needs to be "hypothesis<6.88.4". The problem is warnings from hypothesis, so it's only an issue is if your pytest configuration errors on warnings. (Reported here: data-apis/array-api-tests#211)

Copy link
Contributor

@jakevdp jakevdp Nov 16, 2023

Choose a reason for hiding this comment

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

Oh, I see -W below, so you definitely will need this version bound until that is addressed upstream.

Copy link
Member

Choose a reason for hiding this comment

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

This should be fixed by data-apis/array-api-tests#212, so the !=6.88.4 can probably be dropped by updating to a newer array-api-tests commit?

And if not, should it be < rather than !=, because there are newer hypothesis versions out already and the problem is unlikely to only exist in that exact 6.88.4 version?

Copy link
Member

Choose a reason for hiding this comment

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

Actually, hypothesis is already present in test_requirements.txt, so this line does nothing - I think it can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's right - removed it.

@mtsokol mtsokol marked this pull request as ready for review November 24, 2023 13:20
@mtsokol
Copy link
Member Author

mtsokol commented Nov 24, 2023

Hi @rgommers, I rebased the branch and now array_api_tests stage runs and passes (with skips.txt file and ignored warnings) - it's ready for a review.

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

This is looking pretty good to me, and test is green with a clean test log. All my suggestions are pretty small.

array_api_tests/test_set_functions.py

# newaxis not included in numpy namespace as of v1.26.2
array_api_tests/test_constants.py::test_newaxis
Copy link
Member

Choose a reason for hiding this comment

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

This one can be left out I think, since numpy.newaxis exists.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, removed!

run: |
python -m pip install -r build_requirements.txt
python -m pip install -r test_requirements.txt
python -m pip install hypothesis!=6.88.4 # 6.88.4 leads to a strange error
Copy link
Member

Choose a reason for hiding this comment

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

This should be fixed by data-apis/array-api-tests#212, so the !=6.88.4 can probably be dropped by updating to a newer array-api-tests commit?

And if not, should it be < rather than !=, because there are newer hypothesis versions out already and the problem is unlikely to only exist in that exact 6.88.4 version?

# newaxis not included in numpy namespace as of v1.26.2
array_api_tests/test_constants.py::test_newaxis

# linalg.solve issue in numpy.array_api as of v1.26.2 (see numpy#25146)
Copy link
Member

Choose a reason for hiding this comment

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

This issue has been fixed already, and didn't affect the main namespace. Can this be dropped?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, removed.

# linalg.solve issue in numpy.array_api as of v1.26.2 (see numpy#25146)
array_api_tests/test_linalg.py::test_solve

# https://github.com/numpy/numpy/issues/21373
Copy link
Member

Choose a reason for hiding this comment

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

also fixed already

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, removed.

# missing copy arg
array_api_tests/test_signatures.py::test_func_signature[reshape]

# https://github.com/numpy/numpy/issues/21211
Copy link
Member

Choose a reason for hiding this comment

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

Closed, ultimately fixed by #21218 I believe.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed.

run: |
python -m pip install -r build_requirements.txt
python -m pip install -r test_requirements.txt
python -m pip install hypothesis!=6.88.4 # 6.88.4 leads to a strange error
Copy link
Member

Choose a reason for hiding this comment

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

Actually, hypothesis is already present in test_requirements.txt, so this line does nothing - I think it can be removed.

submodules: 'true'
path: 'array-api-tests'
- name: Fix array-apis bug
# Temporary workaround for https://github.com/data-apis/array-api/issues/631
Copy link
Member

Choose a reason for hiding this comment

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

This should have been fixed yesterday, can you check if things pass now without this fixup with the latest commit from array-api-tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it's fixed now.

with:
submodules: recursive
fetch-depth: 0
- name: Checkout array-api-tests
Copy link
Member

Choose a reason for hiding this comment

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

This is fine for now, but once a bunch of the updates are made we should instead use a regular git submodule for these tests, so that they get run locally. Right now if this job fails, re-running locally will be a bit of a hassle.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right - at some point I will make it a submodule.

return NULL;
}

if (strcmp(array_api_version, "2022.12") != 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Calling ndarray.__array_namespace__(api_version='2021.12') should work too. The newer version is a superset of the older one, so we'd want to support both. And when we upgrade to say 2023.12 in the future, asking for 2022.12 should not break. So adding a test to check this will be useful.

Copy link
Member

Choose a reason for hiding this comment

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

Second suggested regression test:

>>> import numpy as np
>>> xp = np.arange(2).__array_namespace__()
>>> assert xp is np

Copy link
Member Author

Choose a reason for hiding this comment

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

I added test__array_namespace__ for that purpose.


if (strcmp(array_api_version, "2022.12") != 0) {
PyErr_Format(PyExc_ValueError,
"Version \"%s\" of Array API is not supported.",
Copy link
Member

Choose a reason for hiding this comment

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

minor:

Suggested change
"Version \"%s\" of Array API is not supported.",
"Version \"%s\" of the Array API Standard is not supported.",

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated!

@charris charris changed the title MAINT: Add array-api-tests CI stage, add ndarray.__array_namespace__ MAINT: Add array-api-tests CI stage, add ndarray.__array_namespace__ Nov 24, 2023
@mtsokol
Copy link
Member Author

mtsokol commented Nov 24, 2023

Ok, I think it's ready on my end.

@ngoldbaum
Copy link
Member

ngoldbaum commented Nov 28, 2023

All looks good to me, merging to unblock the other array API PRs. Thanks @mtsokol!

Just out of curiosity, how is __array_namespace__ supposed to work for a library that only partially supports the array API?

@ngoldbaum ngoldbaum merged commit 06d7bdf into numpy:main Nov 28, 2023
61 checks passed
@rgommers
Copy link
Member

Just out of curiosity, how is __array_namespace__ supposed to work for a library that only partially supports the array API?

It isn't - adding the method is kinda saying "I fully support this". In practice one has to be pragmatic and some gaps that aren't too important can simply be ignored. But if you're missing lots of functionality, don't add this method. Think of it like a C standard - there's defines to check whether say C99 is used, but not "C99 minus x, y and z".

@mtsokol mtsokol deleted the array-api-test-ci branch November 28, 2023 21:54
@asmeurer
Copy link
Member

To add to that, at least the way array-api-compat is currently written, __array_namespace__ will bypass the compat namespace https://github.com/data-apis/array-api-compat/blob/main/array_api_compat/common/_helpers.py#L76-L77. That should probably be fixed though if NumPy is adding it, because we will probably need to maintain some compat wrappers even after @mtsokol's work because of things that are kept for backwards compatibility.

return NULL;
}

if (strcmp(array_api_version, "2021.12") != 0 &&
Copy link
Member

Choose a reason for hiding this comment

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

Is the point of array_api_version to be forwards compatible, or should this error too?

Copy link
Member

Choose a reason for hiding this comment

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

2022.12 is a superset of 2021.12, so if a user asks for the latter, it's fine to return the former.

@rgommers
Copy link
Member

To add to that, at least the way array-api-compat is currently written, __array_namespace__ will bypass the compat namespace https://github.com/data-apis/array-api-compat/blob/main/array_api_compat/common/_helpers.py#L76-L77. That should probably be fixed though if NumPy is adding it, because we will probably need to maintain some compat wrappers even after @mtsokol's work because of things that are kept for backwards compatibility.

Hmm, we should get to ~100% compatibility. Do you have specific things in mind? If so, let's open an issue now in array-api-compat and fix it soon?

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

Successfully merging this pull request may close these issues.

None yet

5 participants