1414import cuml .internals .input_utils
1515import cuml .internals .logger as logger
1616import cuml .internals .nvtx as nvtx
17- from cuml .internals import api_context_managers
18- from cuml .internals .global_settings import GlobalSettings
1917from cuml .internals .input_utils import determine_array_type
2018from cuml .internals .mixins import TagsMixin
2119from cuml .internals .output_type import (
2422)
2523
2624
27- class VerbosityDescriptor :
28- """Descriptor for ensuring correct type is used for verbosity
29-
30- This descriptor ensures that when the 'verbose' attribute of a cuML
31- estimator is accessed external to the cuML API, an integer is returned
32- (consistent with Scikit-Learn's API for verbosity). Internal to the API, an
33- enum is used. Scikit-Learn's numerical values for verbosity are the inverse
34- of those used by spdlog, so the numerical value is also inverted internal
35- to the cuML API. This ensures that cuML code treats verbosity values as
36- expected for an spdlog-based codebase.
37- """
38-
39- def __get__ (self , obj , cls = None ):
40- if api_context_managers .in_internal_api ():
41- return logger ._verbose_to_level (obj ._verbose )
42- else :
43- return obj ._verbose
44-
45- def __set__ (self , obj , value ):
46- if api_context_managers .in_internal_api ():
47- assert isinstance (value , logger .level_enum ), (
48- "The log level should always be provided as a level_enum, "
49- "not an integer"
50- )
51- obj ._verbose = logger ._verbose_from_level (value )
52- else :
53- if isinstance (value , logger .level_enum ):
54- raise ValueError (
55- "The log level should always be provided as an integer, "
56- "not using the enum"
57- )
58- obj ._verbose = value
59-
60-
6125class Base (TagsMixin , metaclass = cuml .internals .BaseMetaClass ):
6226 """
6327 Base class for all the ML algos. It handles some of the common operations
@@ -191,28 +155,10 @@ def __init__(
191155 verbose = False ,
192156 output_type = None ,
193157 ):
194- """
195- Constructor. All children must call init method of this base class.
196-
197- """
198158 self .handle = (
199159 pylibraft .common .handle .Handle () if handle is None else handle
200160 )
201-
202- # The following manipulation of the root_cm ensures that the verbose
203- # descriptor sees any set or get of the verbose attribute as happening
204- # internal to the cuML API. Currently, __init__ calls do not take place
205- # within an api context manager, so setting "verbose" here would
206- # otherwise appear to be external to the cuML API. This behavior will
207- # be corrected with the update of cuML's API context manager
208- # infrastructure in https://github.com/rapidsai/cuml/pull/6189.
209- GlobalSettings ().prev_root_cm = GlobalSettings ().root_cm
210- GlobalSettings ().root_cm = True
211- self .verbose = logger ._verbose_to_level (verbose )
212- # Please see above note on manipulation of the root_cm. This should be
213- # rendered unnecessary with https://github.com/rapidsai/cuml/pull/6189.
214- GlobalSettings ().root_cm = GlobalSettings ().prev_root_cm
215-
161+ self .verbose = verbose
216162 self .output_type = _check_output_type_str (
217163 cuml .global_settings .output_type
218164 if output_type is None
@@ -224,8 +170,6 @@ def __init__(
224170 if nvtx_benchmark and nvtx_benchmark .lower () == "true" :
225171 self .set_nvtx_annotations ()
226172
227- verbose = VerbosityDescriptor ()
228-
229173 def __repr__ (self ):
230174 """
231175 Pretty prints the arguments of a class using Scikit-learn standard :)
@@ -250,6 +194,11 @@ def __repr__(self):
250194 output += " <sk_model_ attribute used>"
251195 return output
252196
197+ @property
198+ def _verbose_level (self ):
199+ """The current `verbose` setting as a `logger.level_enum`"""
200+ return logger ._verbose_to_level (self .verbose )
201+
253202 @classmethod
254203 def _get_param_names (cls ):
255204 """
@@ -267,20 +216,7 @@ def get_params(self, deep=True):
267216 need anything other than what is there in this method, then it doesn't
268217 have to override this method
269218 """
270- params = dict ()
271- variables = self ._get_param_names ()
272- for key in variables :
273- var_value = getattr (self , key , None )
274- # We are currently internal to the cuML API, but the value we
275- # return will immediately be returned external to the API, so we
276- # must perform the translation from enum to integer before
277- # returning the value. Ordinarily, this is handled by
278- # VerbosityDescriptor for direct access to the verbose
279- # attribute.
280- if key == "verbose" :
281- var_value = logger ._verbose_from_level (var_value )
282- params [key ] = var_value
283- return params
219+ return {name : getattr (self , name ) for name in self ._get_param_names ()}
284220
285221 def set_params (self , ** params ):
286222 """
@@ -291,15 +227,13 @@ def set_params(self, **params):
291227 """
292228 if not params :
293229 return self
294- variables = self ._get_param_names ()
230+ valid_params = self ._get_param_names ()
295231 for key , value in params .items ():
296- if key not in variables :
297- raise ValueError ("Bad param '%s' passed to set_params" % key )
298- else :
299- # Switch verbose to enum since we are now internal to cuML API
300- if key == "verbose" :
301- value = logger ._verbose_to_level (value )
302- setattr (self , key , value )
232+ if key not in valid_params :
233+ raise ValueError (
234+ f"Invalid parameter { key !r} for `{ type (self ).__name__ } `"
235+ )
236+ setattr (self , key , value )
303237 return self
304238
305239 def _set_output_type (self , inp ):
0 commit comments