Skip to content
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

Improve model round-tripping #6342

Draft
wants to merge 1 commit into
base: branch-25.04
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions python/cuml/cuml/internals/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,11 @@ class UniversalBase(Base):
]
):
for attr in self.get_attr_names():
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?


# check presence of attribute
if hasattr(self._cpu_model, attr) or \
isinstance(getattr(type(self._cpu_model),
Expand Down
5 changes: 4 additions & 1 deletion python/cuml/cuml/tests/test_sklearn_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def assert_estimator_roundtrip(
else:
cuml_model.fit(X)

original_params = cuml_model.get_params()
# Convert to sklearn model
sklearn_model = cuml_model.as_sklearn()
check_is_fitted(sklearn_model)
Expand All @@ -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.


# Ensure roundtrip model is fitted
check_is_fitted(roundtrip_model)

Expand Down Expand Up @@ -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

assert_estimator_roundtrip(original, SkKMeans, X)


Expand Down