Skip to content

Commit 07ae0cd

Browse files
committed
Remove verbose descriptor magic
This is now just a regular attribute, we convert to a `level_enum` when needed to forward to the `libcuml` API. In the future we'll remove this forwarding entirely in favor of configuring the logger contextually outside of the libcuml calls.
1 parent 4bb8048 commit 07ae0cd

16 files changed

Lines changed: 29 additions & 113 deletions

File tree

python/cuml/cuml/cluster/dbscan.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ class DBSCAN(Base,
386386

387387
cdef float eps = self.eps
388388
cdef int min_samples = self.min_samples
389-
cdef level_enum verbose = self.verbose
389+
cdef level_enum verbose = self._verbose_level
390390
cdef bool multi_gpu = self._multi_gpu
391391
cdef size_t max_mbytes_per_batch = self.max_mbytes_per_batch or 0
392392

python/cuml/cuml/cluster/kmeans.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ from libcpp cimport bool
2525
from pylibraft.common.handle cimport handle_t
2626

2727
cimport cuml.cluster.cpp.kmeans as lib
28-
from cuml.internals.logger cimport level_enum
2928
from cuml.metrics.distance_type cimport DistanceType
3029

3130

@@ -36,7 +35,7 @@ cdef _kmeans_init_params(kmeans, lib.KMeansParams& params):
3635
params.n_clusters = kmeans.n_clusters
3736
params.max_iter = kmeans.max_iter
3837
params.tol = kmeans.tol
39-
params.verbosity = <level_enum>(<int>kmeans.verbose)
38+
params.verbosity = kmeans._verbose_level
4039
params.metric = DistanceType.L2Expanded
4140
params.batch_samples = int(kmeans.max_samples_per_batch)
4241
params.oversampling_factor = kmeans.oversampling_factor

python/cuml/cuml/ensemble/randomforest_common.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ class BaseRandomForestModel(Base, InteropMixin):
421421
cdef uintptr_t y_ptr = y.ptr
422422
cdef int n_rows = X.shape[0]
423423
cdef int n_cols = X.shape[1]
424-
cdef level_enum verbose = <level_enum> self.verbose
424+
cdef level_enum verbose = <level_enum> self._verbose_level
425425
cdef int n_classes = self.n_classes_ if is_classifier else 0
426426

427427
if self.max_depth <= 0:

python/cuml/cuml/experimental/linear_model/lars.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,13 @@ class Lars(Base, RegressorMixin):
262262
larsFit(handle_[0], <float*> X_ptr, n_rows, <int> self.n_cols,
263263
<float*> y_ptr, <float*> beta_ptr, <int*> active_idx_ptr,
264264
<float*> alphas_ptr, &n_active, <float*> Gram_ptr,
265-
max_iter, <float*> coef_path_ptr, self.verbose, ld_X,
265+
max_iter, <float*> coef_path_ptr, self._verbose_level, ld_X,
266266
ld_G, <float> self.eps)
267267
else:
268268
larsFit(handle_[0], <double*> X_ptr, n_rows, <int> self.n_cols,
269269
<double*> y_ptr, <double*> beta_ptr, <int*> active_idx_ptr,
270270
<double*> alphas_ptr, &n_active, <double*> Gram_ptr,
271-
max_iter, <double*> coef_path_ptr, self.verbose,
271+
max_iter, <double*> coef_path_ptr, self._verbose_level,
272272
ld_X, ld_G, <double> self.eps)
273273
self.n_active = n_active
274274
self.n_iter_ = n_active

python/cuml/cuml/internals/base.py

Lines changed: 13 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import cuml.internals.input_utils
1515
import cuml.internals.logger as logger
1616
import cuml.internals.nvtx as nvtx
17-
from cuml.internals import api_context_managers
18-
from cuml.internals.global_settings import GlobalSettings
1917
from cuml.internals.input_utils import determine_array_type
2018
from cuml.internals.mixins import TagsMixin
2119
from cuml.internals.output_type import (
@@ -24,40 +22,6 @@
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-
6125
class 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):

python/cuml/cuml/linear_model/elastic_net.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def fit(
265265
max_iter=self.max_iter,
266266
tol=self.tol,
267267
penalty_normalized=False,
268-
verbose=self.verbose,
268+
verbose=self._verbose_level,
269269
handle=self.handle,
270270
)
271271
coef = CumlArray(data=coef.to_output("cupy").flatten())

python/cuml/cuml/linear_model/logistic_regression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def fit(
312312
max_iter=self.max_iter,
313313
tol=self.tol,
314314
linesearch_max_iter=self.linesearch_max_iter,
315-
verbose=self.verbose,
315+
verbose=self._verbose_level,
316316
handle=self.handle,
317317
lbfgs_memory=self.lbfgs_memory,
318318
penalty_normalized=self.penalty_normalized,

python/cuml/cuml/linear_model/logistic_regression_mg.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class LogisticRegressionMG(MGFitMixin, LogisticRegression):
186186
linesearch_max_iter=self.linesearch_max_iter,
187187
lbfgs_memory=self.lbfgs_memory,
188188
penalty_normalized=self.penalty_normalized,
189-
verbose=self.verbose,
189+
verbose=self._verbose_level,
190190
)
191191

192192
# Allocate outputs

python/cuml/cuml/manifold/t_sne.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ cdef _init_params(self, int n_samples, TSNEParams &params):
253253
params.pre_momentum = pre_momentum
254254
params.post_momentum = post_momentum
255255
params.random_state = seed
256-
params.verbosity = self.verbose
256+
params.verbosity = self._verbose_level
257257
params.square_distances = self.square_distances
258258
params.algorithm = algo
259259
params.init = init

python/cuml/cuml/manifold/umap/umap.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ cdef init_params(self, lib.UMAPParams &params, n_rows, is_sparse=False, is_fit=T
337337
params.repulsion_strength = self.repulsion_strength
338338
params.negative_sample_rate = self.negative_sample_rate
339339
params.transform_queue_size = self.transform_queue_size
340-
params.verbosity = self.verbose
340+
params.verbosity = self._verbose_level
341341
params.a = self._a
342342
params.b = self._b
343343
params.target_n_neighbors = self.target_n_neighbors

0 commit comments

Comments
 (0)