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

feat(hyperparameters): get_hyperparameters as staticmethod #2164

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 18 additions & 7 deletions sklearnex/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import logging
import os
import sys
import warnings
from abc import ABC

Expand Down Expand Up @@ -106,14 +105,26 @@ def register_hyperparameters(hyperparameters_map):
Adds `get_hyperparameters` method to class.
"""

def wrap_class(estimator_class):
def get_hyperparameters(self, op):
return hyperparameters_map[op]
def decorator(cls):
"""Add `get_hyperparameters()` static method"""

estimator_class.get_hyperparameters = get_hyperparameters
return estimator_class
class StaticHyperparametersAccessor:
"""Like a @staticmethod, but additionally raises a Warning when called on an instance."""

return wrap_class
def __get__(self, instance, _):
if instance is not None:
warnings.warn(
"Hyperparameters are static variables and can not be modified per instance."
)
return self.get_hyperparameters

def get_hyperparameters(self, op):
return hyperparameters_map[op]

cls.get_hyperparameters = StaticHyperparametersAccessor()
return cls

return decorator


# This abstract class is meant to generate a clickable doc link for classses
Expand Down
2 changes: 1 addition & 1 deletion sklearnex/ensemble/tests/test_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_sklearnex_import_rf_classifier(dataframe, queue, block, trees, rows, sc
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
y = _convert_to_dataframe(y, sycl_queue=queue, target_df=dataframe)
rf = RandomForestClassifier(max_depth=2, random_state=0).fit(X, y)
hparams = rf.get_hyperparameters("infer")
hparams = RandomForestClassifier.get_hyperparameters("infer")
if hparams and block is not None:
hparams.block_size = block
hparams.min_trees_for_threading = trees
Expand Down
8 changes: 4 additions & 4 deletions sklearnex/linear_model/tests/test_incremental_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_sklearnex_fit_on_gold_data(dataframe, queue, fit_intercept, macro_block

inclin = IncrementalLinearRegression(fit_intercept=fit_intercept)
if macro_block is not None:
hparams = inclin.get_hyperparameters("fit")
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block
inclin.fit(X_df, y_df)
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_sklearnex_partial_fit_on_gold_data(

inclin = IncrementalLinearRegression()
if macro_block is not None:
hparams = inclin.get_hyperparameters("fit")
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block
for i in range(2):
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_sklearnex_partial_fit_multitarget_on_gold_data(

inclin = IncrementalLinearRegression()
if macro_block is not None:
hparams = inclin.get_hyperparameters("fit")
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block
for i in range(2):
Expand Down Expand Up @@ -176,7 +176,7 @@ def test_sklearnex_partial_fit_on_random_data(

inclin = IncrementalLinearRegression(fit_intercept=fit_intercept)
if macro_block is not None:
hparams = inclin.get_hyperparameters("fit")
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block
for i in range(num_blocks):
Expand Down
2 changes: 1 addition & 1 deletion sklearnex/linear_model/tests/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_sklearnex_import_linear(

linreg = LinearRegression()
if daal_check_version((2024, "P", 0)) and macro_block is not None:
hparams = linreg.get_hyperparameters("fit")
hparams = LinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block

Expand Down
2 changes: 1 addition & 1 deletion sklearnex/preview/covariance/tests/test_covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_sklearnex_import_covariance(dataframe, queue, macro_block, assume_cente
X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
empcov = EmpiricalCovariance(assume_centered=assume_centered)
if daal_check_version((2024, "P", 0)) and macro_block is not None:
hparams = empcov.get_hyperparameters("fit")
hparams = EmpiricalCovariance.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
result = empcov.fit(X)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ def test_incremental_linear_regression_fit_spmd_gold(
inclin = IncrementalLinearRegression(fit_intercept=fit_intercept)

if macro_block is not None:
hparams = inclin.get_hyperparameters("fit")
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block

hparams_spmd = inclin_spmd.get_hyperparameters("fit")
hparams_spmd = IncrementalLinearRegression_SPMD.get_hyperparameters("fit")
hparams_spmd.cpu_macro_block = macro_block
hparams_spmd.gpu_macro_block = macro_block

Expand Down Expand Up @@ -159,11 +159,11 @@ def test_incremental_linear_regression_partial_fit_spmd_gold(
inclin = IncrementalLinearRegression(fit_intercept=fit_intercept)

if macro_block is not None:
hparams = inclin.get_hyperparameters("fit")
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block

hparams_spmd = inclin_spmd.get_hyperparameters("fit")
hparams_spmd = IncrementalLinearRegression_SPMD.get_hyperparameters("fit")
hparams_spmd.cpu_macro_block = macro_block
hparams_spmd.gpu_macro_block = macro_block

Expand Down Expand Up @@ -225,11 +225,11 @@ def test_incremental_linear_regression_fit_spmd_random(
inclin = IncrementalLinearRegression(fit_intercept=fit_intercept)

if macro_block is not None:
hparams = inclin.get_hyperparameters("fit")
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block

hparams_spmd = inclin_spmd.get_hyperparameters("fit")
hparams_spmd = IncrementalLinearRegression_SPMD.get_hyperparameters("fit")
hparams_spmd.cpu_macro_block = macro_block
hparams_spmd.gpu_macro_block = macro_block

Expand Down Expand Up @@ -298,11 +298,11 @@ def test_incremental_linear_regression_partial_fit_spmd_random(
inclin = IncrementalLinearRegression(fit_intercept=fit_intercept)

if macro_block is not None:
hparams = inclin.get_hyperparameters("fit")
hparams = IncrementalLinearRegression.get_hyperparameters("fit")
hparams.cpu_macro_block = macro_block
hparams.gpu_macro_block = macro_block

hparams_spmd = inclin_spmd.get_hyperparameters("fit")
hparams_spmd = IncrementalLinearRegression_SPMD.get_hyperparameters("fit")
hparams_spmd.cpu_macro_block = macro_block
hparams_spmd.gpu_macro_block = macro_block

Expand Down
26 changes: 26 additions & 0 deletions sklearnex/tests/test_hyperparameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from sklearnex._utils import register_hyperparameters


def test_register_hyperparameters():
hyperparameters_map = {"op": "hyperparameters"}

@register_hyperparameters(hyperparameters_map)
class Test:
pass

# assert the correct value is returned
assert Test.get_hyperparameters("op") == "hyperparameters"


def test_register_hyperparameters_issues_warning():
hyperparameters_map = {"op": "hyperparameters"}

@register_hyperparameters(hyperparameters_map)
class Test:
pass

# assert a warning is issued when trying to modify the hyperparameters per instance
with pytest.warns(Warning):
Test().get_hyperparameters("op")
Loading