Skip to content

ENH Remove hardcoded device choice in _weighted_sum #27232

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

Closed
wants to merge 2 commits into from

Conversation

betatim
Copy link
Member

@betatim betatim commented Aug 30, 2023

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
Copy link

github-actions bot commented Aug 30, 2023

✔️ Linting Passed

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

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

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
Copy link
Contributor

fcharras commented Dec 6, 2023

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

Copy link
Member

@thomasjpfan thomasjpfan left a comment

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.

@fcharras
Copy link
Contributor

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

@betatim
Copy link
Member Author

betatim commented Mar 20, 2024

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