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

ENH: stats.circ___: add array-API support #20595

Merged
merged 12 commits into from May 3, 2024
Merged

Conversation

mdhaber
Copy link
Contributor

@mdhaber mdhaber commented Apr 27, 2024

Reference issue

gh-20544

What does this implement/fix?

Makes circmean, circstd, and circvar array-API compatible.

@mdhaber mdhaber added scipy.stats enhancement A new feature or improvement labels Apr 27, 2024
@github-actions github-actions bot added scipy.special scipy._lib CI Items related to the CI tools such as CircleCI, GitHub Actions or Azure array types Items related to array API support and input array validation (see gh-18286) labels Apr 27, 2024
Copy link
Contributor Author

@mdhaber mdhaber left a comment

Choose a reason for hiding this comment

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

Some questions and comments.

scipy/_lib/_array_api.py Outdated Show resolved Hide resolved
scipy/_lib/_util.py Outdated Show resolved Hide resolved
scipy/special/tests/test_support_alternative_backends.py Outdated Show resolved Hide resolved
scipy/stats/tests/test_morestats.py Show resolved Hide resolved
scipy/stats/tests/test_morestats.py Outdated Show resolved Hide resolved
scipy/stats/tests/test_morestats.py Outdated Show resolved Hide resolved
scipy/stats/_morestats.py Show resolved Hide resolved
@mdhaber mdhaber mentioned this pull request Apr 27, 2024
69 tasks
@mdhaber mdhaber marked this pull request as ready for review April 27, 2024 18:32
@mdhaber
Copy link
Contributor Author

mdhaber commented Apr 27, 2024

This is getting a little out of hand with lint complaining about UP031 in CI but not locally. I fix one thing and it starts complaining about another : (

@lucascolley
Copy link
Member

Do you have the same ruff version locally (0.4.2)?

@mdhaber
Copy link
Contributor Author

mdhaber commented Apr 27, 2024

Probably not. I thought we would have configured a rule set, though. I guess as ruff has added these, and we don't exclude them.

@lucascolley
Copy link
Member

We enabled all UP rules in gh-19516. UP031 should be in any compatible version of ruff that you have locally, as our minimum is 0.0.292, and the rule was added in 0.0.229. I would guess that it is due to detection differences over versions of ruff due to bug fixes.

@tylerjereddy
Copy link
Contributor

I think the regular CI is already picking some of them up, but here are fails I see locally with GPU:

FAILED scipy/stats/tests/test_axis_nan_policy.py::test_axis_nan_policy_decorated_positional_args - AssertionError: Regex pattern did not match.
FAILED scipy/stats/tests/test_morestats.py::TestCircFuncs::test_nan_propagate_array[circmean-expected0-torch] - AssertionError: Scalars are not close!
FAILED scipy/stats/tests/test_morestats.py::TestCircFuncs::test_circmean_axis[circvar-torch] - AssertionError: Tensor-likes are not close!
FAILED scipy/stats/tests/test_morestats.py::TestCircFuncs::test_circmean_axis[circstd-torch] - AssertionError: Tensor-likes are not close!
FAILED scipy/stats/tests/test_morestats.py::TestCircFuncs::test_circfuncs_array_like[circmean-0.167690146-torch] - AssertionError: Scalars are not close!
FAILED scipy/stats/tests/test_morestats.py::TestCircFuncs::test_circmean_axis[circmean-torch] - AssertionError: Tensor-likes are not close!

@mdhaber
Copy link
Contributor Author

mdhaber commented Apr 27, 2024

Interesting. No, I don't think it is picking up in CI. I am running with CuPy locally, and I wasn't getting those.
Upgrading ruff seems to have broken my in-place build, and pip uninstall scipy is erroring out... I'll test again when I get things fixed.

@mdhaber
Copy link
Contributor Author

mdhaber commented Apr 28, 2024

Thanks @tylerjereddy. Can you give the full failure information (e.g. actual relative error)?

Also, can you reproduce these two:

FAILED scipy/stats/tests/test_morestats.py::TestCircFuncs::test_circfuncs_small[torch] - TypeError: var() received an invalid combination of arguments - got (Tensor, tuple, correction=float), but expected one of:
FAILED scipy/special/tests/test_support_alternative_backends.py::test_dispatch_to_unrecognize_library - TypeError: Cannot interpret 'array_api_strict.float64' as a data type

I can't reproduce them on this machine.

@tylerjereddy
Copy link
Contributor

I can't reproduce those two either. The PR seems fine on CPU, I'll try to add some inline comments re: the failure details on GPU.

scipy/_lib/_array_api.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@mdhaber mdhaber left a comment

Choose a reason for hiding this comment

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

OK, I'm happy with this now. aside from the comments here and the one failure, which I can't reproduce.

=================================== FAILURES ===================================
__________________ TestCircFuncs.test_circfuncs_small[torch] ___________________
scipy/stats/tests/test_morestats.py:2479: in test_circfuncs_small
    V1 = xp_test.var(x*xp.pi/180, correction=xp.asarray(0.0))
        M1         = tensor(19.9571, dtype=torch.float64)
        M2         = tensor(19.9571, dtype=torch.float64)
        self       = <scipy.stats.tests.test_morestats.TestCircFuncs object at 0x7f625bdd31d0>
        x          = tensor([20.0000, 21.0000, 22.0000, 18.0000, 19.0000, 20.5000, 19.2000],
       dtype=torch.float64)
        xp         = <module 'torch' from '/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/torch/__init__.py'>
        xp_test    = <module 'scipy._lib.array_api_compat.torch' from '/home/runner/work/scipy/scipy/build-install/lib/python3.11/site-packages/scipy/_lib/array_api_compat/torch/__init__.py'>
scipy/_lib/array_api_compat/torch/_aliases.py:425: in var
    res = torch.var(x, tuple(range(x.ndim)), correction=correction, **kwargs)
E   TypeError: var() received an invalid combination of arguments - got (Tensor, tuple, correction=Tensor), but expected one of:
E    * (Tensor input, tuple of ints dim, bool unbiased, bool keepdim, *, Tensor out)
E    * (Tensor input, tuple of ints dim, *, int correction, bool keepdim, Tensor out)
E    * (Tensor input, bool unbiased)
E    * (Tensor input, tuple of names dim, bool unbiased, bool keepdim, *, Tensor out)
E    * (Tensor input, tuple of names dim, *, int correction, bool keepdim, Tensor out)
        axis       = None
        correction = tensor(0.)
        keepdims   = False
        x          = tensor([0.3491, 0.3665, 0.3840, 0.3142, 0.3316, 0.3578, 0.3351],
       dtype=torch.float64)

The whole point of calling array_namespace is to get an array-API xp_test so that it has var. Clearly x is a tensor. Technically, correction can just be an int or float, but I think I've tried that, in addition to leaving it at its default value. I would think it's a bug in array_api_compat, but this is using SciPy's vendored version, right?

Well, the message suggests that it should be an int, whereas the default is a 0.0, so let's try that.

Yeah we must have an older version of torch that isn't perfectly compatible with our array-api-compat.

scipy/cluster/tests/test_vq.py Outdated Show resolved Hide resolved
scipy/optimize/tests/test_minpack.py Show resolved Hide resolved
scipy/optimize/tests/test_minpack.py Show resolved Hide resolved
scipy/stats/_morestats.py Show resolved Hide resolved
scipy/stats/_morestats.py Show resolved Hide resolved
scipy/stats/_morestats.py Outdated Show resolved Hide resolved
scipy/stats/tests/test_morestats.py Outdated Show resolved Hide resolved
scipy/stats/tests/test_morestats.py Outdated Show resolved Hide resolved
scipy/stats/tests/test_morestats.py Show resolved Hide resolved
@mdhaber mdhaber requested a review from j-bowhay April 28, 2024 14:54
@mdhaber
Copy link
Contributor Author

mdhaber commented Apr 29, 2024

@tylerjereddy please run these tests once more with TF GPU when you have a moment. Thanks!

Copy link
Member

@j-bowhay j-bowhay 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 good, some minor comments, most of which are for my own understanding

scipy/_lib/_array_api.py Outdated Show resolved Hide resolved
scipy/_lib/_util.py Outdated Show resolved Hide resolved
scipy/stats/_morestats.py Show resolved Hide resolved
scipy/stats/_morestats.py Show resolved Hide resolved
scipy/stats/_morestats.py Show resolved Hide resolved
scipy/stats/_morestats.py Show resolved Hide resolved
scipy/stats/tests/test_morestats.py Show resolved Hide resolved
@tylerjereddy
Copy link
Contributor

please run these tests once more with TF GPU when you have a moment. Thanks!

@mdhaber

SCIPY_DEVICE=cuda python dev.py test -j 32 -b all

  • this branch (230f383): 3 failed, 51023 passed, 11078 skipped, 155 xfailed, 13 xpassed in 55.71s
  • main (4ee0de3): 4 failed, 50958 passed, 11090 skipped, 155 xfailed, 13 xpassed in 60.08s (0:01:00)

So, you're at parity with main in my hands, which seems good (I always get a few thread safety issues with fft locally on GPU). I didn't check the diff myself again, just reporting this result.

@lucascolley
Copy link
Member

I always get a few thread safety issues with fft locally on GPU

Gentle reminder that these should be gone with gh-19900

Copy link
Contributor Author

@mdhaber mdhaber left a comment

Choose a reason for hiding this comment

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

OK If tests pass, I think I'm happy with this, and I think comments have been addressed.

Copy link
Member

@j-bowhay j-bowhay left a comment

Choose a reason for hiding this comment

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

One query but otherwise this is good to go

scipy/_lib/_util.py Show resolved Hide resolved
Copy link
Member

@j-bowhay j-bowhay left a comment

Choose a reason for hiding this comment

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

Thanks @mdhaber LGTM

@j-bowhay j-bowhay added this to the 1.14.0 milestone May 3, 2024
@j-bowhay j-bowhay merged commit 1464ae1 into scipy:main May 3, 2024
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
array types Items related to array API support and input array validation (see gh-18286) CI Items related to the CI tools such as CircleCI, GitHub Actions or Azure enhancement A new feature or improvement scipy._lib scipy.stats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants