Skip to content

ENH make _loss.link.py array API compatible - #33345

Merged
ogrisel merged 14 commits into
scikit-learn:mainfrom
lorentzenchr:link_array_api
Mar 5, 2026
Merged

ogrisel merged 14 commits into
scikit-learn:mainfrom
lorentzenchr:link_array_api

Conversation

@lorentzenchr

Copy link
Copy Markdown
Member

Reference Issues/PRs

Contributes to #26024.

What does this implement/fix? Explain your changes.

This makes the link functions in the private loss module array API compliant (for recent versions of scipy).

AI usage disclosure

None

Any other comments?

@lorentzenchr

Copy link
Copy Markdown
Member Author

@OmarManzoor ping as you might be interested.

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the PR @lorentzenchr

Comment thread sklearn/_loss/link.py Resolved
Comment thread sklearn/_loss/link.py Resolved
Comment thread sklearn/_loss/link.py Resolved
@OmarManzoor

OmarManzoor commented Feb 23, 2026 •

Copy link
Copy Markdown
Contributor

The tests are failing because we aren't enabling the context manager with config_context(array_api_dispatch=True) so we create torch or array api strict arrays in the tests but because array_api_dispatch is False, the actual inner functions which are being tested return numpy as the namespace. My suggestion would be to either just use array_api_dispatch=True in the modified tests or add a new parameter having both options True and False and when dispatch is False just create numpy arrays for the inputs.

@lorentzenchr

Copy link
Copy Markdown
Member Author

Do you know how to resolve the current CI error?

RuntimeError: Scikit-learn array API support was enabled but scipy's own support is not enabled. Please set the SCIPY_ARRAY_API=1 environment variable before importing sklearn or scipy.

@OmarManzoor

OmarManzoor commented Feb 23, 2026 •

Copy link
Copy Markdown
Contributor

Do you know how to resolve the current CI error?

RuntimeError: Scikit-learn array API support was enabled but scipy's own support is not enabled. Please set the SCIPY_ARRAY_API=1 environment variable before importing sklearn or scipy.

Did you get this error when running on your local system? This seems weird because technically this should be configured in the CI environments already. I think you might need to use the context manager rather than decorating the tests with the array api dispatch config context.

@lorentzenchr

Copy link
Copy Markdown
Member Author

Did you get this error when running on your local system?
No, locally all is green.

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally looks good now. I'll run the CUDA CI

Comment thread sklearn/_loss/tests/test_link.py Resolved
Comment thread sklearn/_loss/tests/test_link.py Resolved
Comment thread sklearn/_loss/link.py Resolved
Comment thread sklearn/_loss/link.py Resolved

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thank you @lorentzenchr

Comment thread sklearn/_loss/tests/test_link.py Resolved
@OmarManzoor

Copy link
Copy Markdown
Contributor

CC: @ogrisel

@lorentzenchr

Copy link
Copy Markdown
Member Author

Or @lucyleeow, maybe.

@ogrisel ogrisel 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.

Thanks for the PR. The core of the change LGTM but I have concerns about how to write the tests (see details below):

Comment thread sklearn/_loss/tests/test_link.py Resolved
Comment thread sklearn/_loss/link.py Resolved
Comment thread sklearn/_loss/link.py Resolved
@ogrisel ogrisel moved this to In Progress in Array API Feb 24, 2026
@ogrisel ogrisel changed the title ENH make _loss.link.py array API compatible ENH make _loss.link.py array API compatible Feb 24, 2026
Comment thread sklearn/_loss/link.py Resolved
Comment thread sklearn/_loss/link.py Resolved
Comment thread sklearn/_loss/link.py Resolved
else:
raw_prediction = rng.uniform(low=-20, high=20, size=(n_samples))

assert_allclose(link.link(link.inverse(raw_prediction)), raw_prediction)

@ogrisel ogrisel Mar 3, 2026 •

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.

The update of this check makes it such was we no longer test for this assertion to hold on NumPy inputs with array_api_dispatch=False.

This is point c. of the usual testing policy exposed in:

https://github.com/scikit-learn/scikit-learn/pull/33345/changes#r2845855747

It makes it extra hard to reviewers of array API support PRs to check that we do not introduce a regression for regular scikit-learn users with array API disabled.

Note that this point will become moot once we move out of experimental and always enabled array API dispatch (see #33444) but as long as it's not the case I would prefer to stick to our policy.

Comment thread sklearn/_loss/link.py Resolved
* This is a test for the Interval, it has nothing to do with arrays.
* test_link_inverse_identity only for numpy
* new test_link_inverse_array_api
@lorentzenchr

Copy link
Copy Markdown
Member Author

Due to @ogrisel's comment, I changed a few things:

  • 47c804a: Inverval only uses Pythoon float and _inclusive_low_high does not need array API support (only floats).
  • feb3459:
    • test_link_inverse_identity reverted to main, only numpy is checked
    • new test_link_inverse_array_api to check array API vs numpy

@OmarManzoor

OmarManzoor commented Mar 5, 2026 •

Copy link
Copy Markdown
Contributor

Okay let's run the CUDA CI now that we have array_api in the test name. Oh you already ran it 😆

@lorentzenchr

Copy link
Copy Markdown
Member Author

Okay let's run the CUDA CI now that we have array_api in the test name. Oh you already ran it 😆

Hihi, yes. Ready to merge from my point of view.

@ogrisel ogrisel 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.

LGTM!

@ogrisel
ogrisel merged commit ba3ea9b into scikit-learn:main Mar 5, 2026
37 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Mar 5, 2026
@ogrisel
ogrisel deleted the link_array_api branch March 5, 2026 10:33
tonybaloney pushed a commit to tonybaloney/swe-complex-scikit-learn that referenced this pull request Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants