/
cd.py
82 lines (66 loc) · 2.65 KB
/
cd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from cuml.dask.common.base import BaseEstimator
from cuml.dask.common.base import DelayedPredictionMixin
from cuml.dask.common.base import mnmg_import
from cuml.dask.common.base import SyncFitMixinLinearModel
from raft_dask.common.comms import get_raft_comm_state
from dask.distributed import get_worker
class CD(BaseEstimator, SyncFitMixinLinearModel, DelayedPredictionMixin):
"""
Model-Parallel Multi-GPU Linear Regression Model.
"""
def __init__(self, *, client=None, **kwargs):
"""
Initializes the linear regression class.
"""
super().__init__(client=client, **kwargs)
self._model_fit = False
self._consec_call = 0
def fit(self, X, y):
"""
Fit the model with X and y.
Parameters
----------
X : Dask cuDF dataframe or CuPy backed Dask Array (n_rows, n_features)
Features for regression
y : Dask cuDF dataframe or CuPy backed Dask Array (n_rows, 1)
Labels (outcome values)
"""
models = self._fit(model_func=CD._create_model, data=(X, y))
self._set_internal_model(list(models.values())[0])
return self
def predict(self, X, delayed=True):
"""
Make predictions for X and returns a dask collection.
Parameters
----------
X : Dask cuDF dataframe or CuPy backed Dask Array (n_rows, n_features)
Distributed dense matrix (floats or doubles) of shape
(n_samples, n_features).
delayed : bool (default = True)
Whether to do a lazy prediction (and return Delayed objects) or an
eagerly executed one.
Returns
-------
y : Dask cuDF dataframe or CuPy backed Dask Array (n_rows, 1)
"""
return self._predict(X, delayed=delayed)
@staticmethod
@mnmg_import
def _create_model(sessionId, datatype, **kwargs):
from cuml.solvers.cd_mg import CDMG
handle = get_raft_comm_state(sessionId, get_worker())["handle"]
return CDMG(handle=handle, output_type=datatype, **kwargs)