Skip to content
Open
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 himalaya/kernel_ridge/_sklearn_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ def fit(self, X, y=None, sample_weight=None):
# Apply sample weight scaling to dual coefficients (sklearn compatibility)
if sample_weight is not None:
# Ensure sw is on the same device as dual_coef_
sw = backend.asarray(sw, device=self.dual_coef_.device if hasattr(self.dual_coef_, 'device') else None)
if not backend.is_in_gpu(self.dual_coef_):
sw = backend.to_cpu(sw)
Comment on lines +247 to +248

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Instead of checking is_in_gpu and calling to_cpu, we can use getattr(self.dual_coef_, "device", "cpu") to get the device of self.dual_coef_ (defaulting to "cpu" for NumPy < 2 which lacks the .device attribute). This is cleaner, avoids branching, and preserves the explicit device-matching behavior of backend.asarray on multi-GPU setups.

Suggested change
if not backend.is_in_gpu(self.dual_coef_):
sw = backend.to_cpu(sw)
device = getattr(self.dual_coef_, "device", "cpu")
sw = backend.asarray(sw, device=device)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since this only ever moves to the CPU, and there are no device IDs for 'cpu', I think the current check is actually safer.

self.dual_coef_ = self.dual_coef_ * sw

if ravel:
Expand Down Expand Up @@ -1173,7 +1174,8 @@ def fit(self, X, y=None, sample_weight=None):
# Apply sample weight scaling to dual coefficients (sklearn compatibility)
if sample_weight is not None:
# Ensure sw is on the same device as dual_coef_
sw = backend.asarray(sw, device=self.dual_coef_.device if hasattr(self.dual_coef_, 'device') else None)
if not backend.is_in_gpu(self.dual_coef_):
sw = backend.to_cpu(sw)
Comment on lines +1177 to +1178

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Instead of checking is_in_gpu and calling to_cpu, we can use getattr(self.dual_coef_, "device", "cpu") to get the device of self.dual_coef_ (defaulting to "cpu" for NumPy < 2 which lacks the .device attribute). This is cleaner, avoids branching, and preserves the explicit device-matching behavior of backend.asarray on multi-GPU setups.

Suggested change
if not backend.is_in_gpu(self.dual_coef_):
sw = backend.to_cpu(sw)
device = getattr(self.dual_coef_, "device", "cpu")
sw = backend.asarray(sw, device=device)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See previous comment.

self.dual_coef_ = self.dual_coef_ * sw

if ravel or self.deltas_.shape[1] != self.dual_coef_.shape[1]:
Expand Down