-
Notifications
You must be signed in to change notification settings - Fork 19
FIX sklearn sample_weights checks for cupy and numpy<2 #102
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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) | ||||||||||
| self.dual_coef_ = self.dual_coef_ * sw | ||||||||||
|
|
||||||||||
| if ravel: | ||||||||||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of checking
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]: | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of checking
is_in_gpuand callingto_cpu, we can usegetattr(self.dual_coef_, "device", "cpu")to get the device ofself.dual_coef_(defaulting to"cpu"for NumPy < 2 which lacks the.deviceattribute). This is cleaner, avoids branching, and preserves the explicit device-matching behavior ofbackend.asarrayon multi-GPU setups.There was a problem hiding this comment.
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.