Skip to content

DOC Add info on 'array-like' array API inputs when array_api_dispatch=False - #32676

Merged
betatim merged 12 commits into
scikit-learn:mainfrom
lucyleeow:doc_aapi_convert
Nov 26, 2025
Merged

betatim merged 12 commits into
scikit-learn:mainfrom
lucyleeow:doc_aapi_convert

Conversation

@lucyleeow

Copy link
Copy Markdown
Member

Reference Issues/PRs

Related #30454
Found out in #32600 (comment)

What does this implement/fix? Explain your changes.

Adds info on what happens when non-NumPy array input occurs with array_api_dispatch=False
I only realised that we do this and I think it would be nice if it was mentioned in the docs.

I thought I'd add a section on the array_api_dispatch as it no longer seemed to fit under the 'Example usage' section

Any other comments?

cc @OmarManzoor @lesteve

@github-actions

github-actions Bot commented Nov 8, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 238869d. Link to the linter CI: here

@lucyleeow

lucyleeow commented Nov 8, 2025 •

Copy link
Copy Markdown
Member Author

Another thought that I am 50/50 on - currently when an array that is not able to be converted via numpy.asarray we get an error like:

RuntimeError: Can not convert array on the 'array_api_strict.Device('device1')' device to a Numpy array.

What do we think about adding something along the lines of - "if you wish to enable array API support and get {namespace} arrays as output, set the configuration array_api_dispatch=True"? If they have input e.g., a cupy array, it is possible that they want to enable array API support but they just forgot/didn't know they need to set array_api_dispatch=True...

At the moment this change would be a bit complicated because we perform the asarray in different places for classification metrics vs regression metrics vs clustering metrics etc, but once we start adding support for mixed array inputs (#31829), I think the error could be raised in move_to, which would be called at the start of functions.

@OmarManzoor

Copy link
Copy Markdown
Contributor

I think it would be beneficial to mention that array API dispatch should be activated. I think we try to handle non NumPy arrays with array_api_dispatch=False by trying to convert to NumPy for metrics but I don't think that is something that should be recommended because we can run into unexpected errors.

@lucyleeow

Copy link
Copy Markdown
Member Author

Yeah I agree, this seems like a niche/odd thing that we do, but we wouldn't really recommend anyone to use it. I've amended the wording.

I think it is important to document, so people know this happens, because it can be quite unexpected to get a different namespaced array than what you've input e.g., you've forgotten to set array_api_dispatch=True.

@lucyleeow lucyleeow changed the title DOC Add info on array API input when array_api_dispatch=False DOC Add info on array API input in metrics when array_api_dispatch=False Nov 10, 2025
@betatim

betatim commented Nov 12, 2025

Copy link
Copy Markdown
Member

I'm not sure if the array API section is the right place for this information. I think we should have this in a more general section of the scikit-learn docs, possibly we already state it there :-/

It has happened that as part of the array API we've changed what happens when you pass torch arrays to scikit-learn. In which case we get reports about it breaking. This makes me think that it is something people rely and is possible on purpose. But I don't fully know the "rule of thumb" for what works and how. I think it is something like "if it looks enough like a Numpy array" and/or if it supports __array_function__

@lucyleeow

lucyleeow commented Nov 13, 2025 •

Copy link
Copy Markdown
Member Author

But I don't fully know the "rule of thumb" for what works and how. I think it is something like "if it looks enough like a Numpy array" and/or if it supports array_function

My naive guess would be arrays on CPU device can be converted. I have tried torch 'cpu' and jax (import jax.numpy as jnp; arr = jnp.array([1, 2, 3])) and both can be converted to numpy with np.asarray.

In which case we get reports about it breaking.

Interesting, can you point me to any references?

I think we should have this in a more general section of the scikit-learn docs, possibly we already state it there :-/

Tricky to look up. In the SVM user guide we state:

The support vector machines in scikit-learn support both dense (numpy.ndarray and convertible to that by numpy.asarray) and sparse (any scipy.sparse) sample vectors as input.

In "Developing scikit-learn estimators" -> "Input validation" we state:

The module sklearn.utils contains various functions for doing input validation and conversion. Sometimes, np.asarray suffices for validation;

I looked for "array " and "asarray" in our .rst files and couldn't find anything else relevant.

I am happy to add a note about this behaviour in the metrics section of the user guide and link it to here?

Comment thread doc/modules/array_api.rst Resolved
@betatim

betatim commented Nov 13, 2025

Copy link
Copy Markdown
Member

#29107 is an example of a bug like this.

@lucyleeow lucyleeow left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've realised that we (probably) do np.array or np.asarray on all inputs that we specify as array-like. Thus I amended the glossary entry for array-like to include pytorch tensors on cpu and JAX arrays as examples. Also amended working in the array API user guide.

I couldn't find another suitable place in the user guide to add this info, so I think I will leave for now and hope the glossary is adequate for now.

Comment thread doc/modules/array_api.rst Outdated
to avoid having to reset it to `False` it at the end of every code snippet, so as to
not affect the rest of the documentation.

Scikit-learn accepts :term:`array-like` inputs for all :mod:`~sklearn.metrics`

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

all :mod:~sklearn.metrics

Is this true..?

Comment thread doc/modules/array_api.rst Outdated
into NumPy arrays using :func:`numpy.asarray` (or :func:`numpy.array`).
While this will successfully convert some array API inputs (e.g., JAX array),
we generally recommend setting `array_api_dispatch=True` when using array API inputs.
Note when `array_api_dispatch=False`, array outputs will be NumPy arrays.

@lucyleeow lucyleeow Nov 15, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This last line is probably redundant, I am happy to remove.

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 it's important to somehow highlight that when array_api_dispatch=False, NumPy conversion can fail, for instance, calling fit on a torch tensor allocated on a GPU will raise an error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a sentence, not 100% happy with it so let me know what you think.

I've also removed the last line, which was redundant, I think thats what @betatim 's thumbs up meant 😄

@lucyleeow lucyleeow changed the title DOC Add info on array API input in metrics when array_api_dispatch=False DOC Add info on 'array-like' array AI inputs when array_api_dispatch=False Nov 17, 2025
@betatim betatim changed the title DOC Add info on 'array-like' array AI inputs when array_api_dispatch=False DOC Add info on 'array-like' array API inputs when array_api_dispatch=False Nov 17, 2025
Comment thread doc/glossary.rst Resolved
Comment thread doc/modules/array_api.rst Resolved
Comment thread doc/modules/array_api.rst Resolved
@lucyleeow lucyleeow moved this to In Progress in Array API Nov 18, 2025
@lucyleeow

Copy link
Copy Markdown
Member Author

@betatim @ogrisel any other comments on this?

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

More than super good enough!

@betatim

betatim commented Nov 26, 2025

Copy link
Copy Markdown
Member

I'll merge it. Olivier can comment or make a follow up PR if there is something we missed

@betatim
betatim merged commit 12fccad into scikit-learn:main Nov 26, 2025
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Array API Nov 26, 2025
@lucyleeow
lucyleeow deleted the doc_aapi_convert branch November 27, 2025 01:18
@lesteve

lesteve commented Nov 27, 2025

Copy link
Copy Markdown
Member

I don't think this is worth doing something about it but

we generally recommend setting array_api_dispatch=True when using array API inputs.

I think the recommendation is quite mild so that's fine but passing a PyTorch cpu tensor has been working for ever and I guess people kind of expect the implicit conversion to numpy array 1.

Something I did not think about until now, is that when array API becomes the default the output will change to a PyTorch tensor and there may be one workflow somewhere that breaks but it's probably quite unlikely to happen?

Footnotes

  1. I agree that for someone working on array API for a while with the namespace_in = namespace_out convention, this can be surprising ... ↩

@betatim

betatim commented Nov 27, 2025

Copy link
Copy Markdown
Member

I think this is something to consider as part of making array API the default. In some way the pytorch on CPU -> Numpy workflow is part of the scikit-learn API, in some way you could also argue that it is undocumented behaviour :-/

@lucyleeow

Copy link
Copy Markdown
Member Author

in some way you could also argue that it is undocumented behaviour

It isn't explicitly stated anywhere, just implied by the glossary entry: "array-like is any type object for which :func:numpy.asarray will produce an array of appropriate shape".

Maybe I should amend this glossary entry to make it more explicit that inputs will be converted to numpy arrays, and array outputs will be numpy arrays...?

@betatim

betatim commented Dec 2, 2025

Copy link
Copy Markdown
Member

I'd not do anything for now

@lorentzenchr

Copy link
Copy Markdown
Member

Hmmm 🤔 I am not 100% happy with the extension of the glossary entry of array-like - maybe I don’t have the full picture.
If we state support for pytorch tensors as array-like, we should test it, e.g., in check_array.
Supporting or not is a major API decision that should involve more core maintainers.

@scikit-learn/core-devs ping

@betatim

betatim commented Mar 2, 2026

Copy link
Copy Markdown
Member

I think the intention here (regarding PyTorch inputs) was to document the status quo and the fact that people already do this. I think you could argue that (until this PR) it was undocumented behaviour and that they were getting lucky that it worked. But then there is also https://xkcd.com/1172/.

Should we put this (the modification of the glossary entry) on the agenda for the next monthly meeting to discuss?

@lucyleeow

lucyleeow commented Mar 2, 2026 •

Copy link
Copy Markdown
Member Author

Should we put this (the modification of the glossary entry) on the agenda for the next monthly meeting to discuss?

+1

Questions we may want to think about:

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.

6 participants