Skip to content

Improve model round-tripping #6342

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

Draft
wants to merge 1 commit into
base: branch-25.04
Choose a base branch
from

Conversation

betatim
Copy link
Member

@betatim betatim commented Feb 20, 2025

This PR is the result of investigating #6142

A short version of the story is that this roundtrip doesn't work:

import cuml
from sklearn import cluster

km = cluster.KMeans(n_clusters=13)
ckm = cuml.KMeans.from_sklearn(km)

print(ckm.n_clusters)  # -> 8, should be 13

This PR kinda fixes things, but really we need the hyper-parameter translator from the 0cc accelerator.

cc @dantegd

Copy link

copy-pr-bot bot commented Feb 20, 2025

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@github-actions github-actions bot added the Cython / Python Cython or Python issue label Feb 20, 2025
cuml_param_names = self._get_param_names()
for param, value in self._cpu_model.get_params().items():
if param in cuml_param_names:
self.set_params(**{param: value})
Copy link
Member Author

Choose a reason for hiding this comment

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

We can make this nicer, left it like this for now to illustrate the point. I think we need some translation mechanism, not just blindly copying things.

In the GPU -> CPU direction attributes are already copied, but probably also needs adjusting?

@@ -81,6 +82,8 @@ def assert_estimator_roundtrip(
# Convert back
roundtrip_model = type(cuml_model).from_sklearn(sklearn_model)

assert original_params == roundtrip_model.get_params()
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this should be true. Otherwise calling fit, which is possible (or should we disallow it? A la FrozenEstimator), will lead to very weird results.

@@ -115,7 +118,7 @@ def test_kmeans(random_state):
X, _ = make_blobs(
n_samples=50, n_features=2, centers=3, random_state=random_state
)
original = KMeans(n_clusters=3, random_state=random_state)
original = KMeans(n_clusters=13, random_state=random_state)
Copy link
Member Author

Choose a reason for hiding this comment

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

Just a random value that isn't the default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cython / Python Cython or Python issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant