Skip to content

Fix array api integration for additive_chi2_kernel with torch mps #29256

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sklearn/metrics/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from ..utils._array_api import (
_find_matching_floating_dtype,
_is_numpy_namespace,
device,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could also use get_namespace_and_device and substitute it to xp, _ = get_namespace(X, Y) below

get_namespace,
)
from ..utils._chunking import get_chunk_n_rows
Expand Down Expand Up @@ -1763,12 +1764,13 @@ def additive_chi2_kernel(X, Y=None):
return result
else:
dtype = _find_matching_floating_dtype(X, Y, xp=xp)
device_ = device(X, Y)
xb = X[:, None, :]
yb = Y[None, :, :]
nom = -((xb - yb) ** 2)
denom = xb + yb
nom = xp.where(denom == 0, xp.asarray(0, dtype=dtype), nom)
denom = xp.where(denom == 0, xp.asarray(1, dtype=dtype), denom)
nom = xp.where(denom == 0, xp.asarray(0, dtype=dtype, device=device_), nom)
denom = xp.where(denom == 0, xp.asarray(1, dtype=dtype, device=device_), denom)
return xp.sum(nom / denom, axis=2)


Expand Down
Loading