Skip to content

ENH Remove hardcoded device choice in _weighted_sum - #27232

Closed
betatim wants to merge 2 commits into
scikit-learn:mainfrom
betatim:array_api_weighted_sum
Closed

betatim wants to merge 2 commits into
scikit-learn:mainfrom
betatim:array_api_weighted_sum

Conversation

@betatim

@betatim betatim commented Aug 30, 2023

Copy link
Copy Markdown
Member

What does this implement/fix? Explain your changes.

Some Array API compatible libraries do not have a device called 'cpu'. Instead to deal with the fact that some devices (e.g. MPS) do not have float64 support, we try and detect the lib+device combination that does not support float64.

I also made it so that if weights are passed that array is passed to get_namespace so that both arrays have to be in the same namespace.

In general there is no generic way of specifying a device that all libraries understand. This means the string 'cpu' might or might not be understood. In addition there are libraries like cupy that do not even have a CPU device (no matter what you call it).

I was also wondering what would happen if we have the scores on the CPU, but the weights on a different device.

Any other comments?

This is related to/inspired by reading/xref #27137. That PR also adds tests for _weighted_sum which is why I've not added any here.

For my education: why do we convert the type to float if it isn't? For example if we are doing a sum of an array of integers, without weights, then it seems more natural to stay with ints (to avoid the problem that some ints can't be represented by floats, etc).

I think this shows that we need to figure out a solution to the lack of CI :-/

@github-actions

github-actions Bot commented Aug 30, 2023 •

Copy link
Copy Markdown

✔️ Linting Passed

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

Generated for commit: 0576ec2. Link to the linter CI: here

Comment thread sklearn/utils/_array_api.py Resolved
Some Array API compatible libraries do not have a device called 'cpu'.
Instead we try and detect the lib+device combination that does not
support float64.
@betatim
betatim force-pushed the array_api_weighted_sum branch from 0e9306f to 4cd8bd9 Compare September 8, 2023 06:28
@fcharras

fcharras commented Dec 6, 2023 •

Copy link
Copy Markdown
Contributor

@betatim WDYT of how I've done it in #27904 ? basically using numpy.from_dlpack _convert_to_numpy rather than to("cpu").

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

For my education: why do we convert the type to float if it isn't?

From reading the code there are two issues:

  1. sample_score = sample_score / scale will break in Array API for ints because it does not automatically promote to floats
  2. xp.sum(sample_score) can overflow more easily with ints.

Comment thread sklearn/utils/_array_api.py Resolved
@fcharras

Copy link
Copy Markdown
Contributor

I think this has been addressed in #27904 and can be closed ?

@betatim

betatim commented Mar 20, 2024

Copy link
Copy Markdown
Member Author

Thanks for bumping this Franck. Closing!

@betatim betatim closed this Mar 20, 2024
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.

4 participants