-
Notifications
You must be signed in to change notification settings - Fork 534
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
Refactor to simplify input/output descriptors and decorators #6124
Draft
dantegd
wants to merge
1
commit into
rapidsai:branch-24.12
Choose a base branch
from
dantegd:fea-desdec-refactor
base: branch-24.12
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# | ||
# Copyright (c) 2024, 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.sample.estimator import Estimator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# | ||
# Copyright (c) 2024, 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. | ||
# | ||
|
||
import numpy as np | ||
|
||
from cuml.internals.array import CumlArray | ||
from cuml.internals.global_settings import GlobalSettings | ||
from cuml.internals.mixins import FMajorInputTagMixin | ||
from cuml.internals.base import UniversalBase, DynamicDescriptor | ||
|
||
|
||
def io_fit(func): | ||
def wrapper(self, *args, **kwargs): | ||
# increase global counter to detect we are internal | ||
GlobalSettings().increase_arc() | ||
|
||
# check input type of first arg and fit estimator | ||
self._set_output_type(args[0]) | ||
result = func(self, *args, **kwargs) | ||
self._is_fit = True | ||
|
||
# decrease counter after exiting function | ||
GlobalSettings().decrease_arc() | ||
|
||
return result | ||
|
||
return wrapper | ||
|
||
|
||
def io_predict_transform_array(func): | ||
def wrapper(self, *args, **kwargs): | ||
# increase global counter to detect we are internal | ||
GlobalSettings().increase_arc() | ||
|
||
result = func(self, *args, **kwargs) | ||
|
||
# decrease counter after exiting function | ||
GlobalSettings().decrease_arc() | ||
|
||
if GlobalSettings().is_internal: | ||
return result | ||
|
||
else: | ||
# need to add logic to check globalsettings and mirror output_type | ||
return result.to_output(self._input_type) | ||
|
||
return result | ||
|
||
return wrapper | ||
|
||
|
||
class Estimator(UniversalBase, | ||
FMajorInputTagMixin): | ||
coef_ = DynamicDescriptor("coef_") | ||
intercept_ = DynamicDescriptor("intercept_") | ||
|
||
def __init__(self, | ||
*, | ||
awesome=True, | ||
output_type=None, | ||
handle=None, | ||
verbose=None): | ||
|
||
super().__init__(handle=handle, | ||
verbose=verbose, | ||
output_type=output_type) | ||
|
||
self.awesome = awesome | ||
self._is_fit = False # this goes in base | ||
|
||
@io_fit | ||
def fit(self, | ||
X, | ||
y, | ||
convert_dtype=True): | ||
|
||
input_X = CumlArray.from_input( | ||
X, | ||
order="C", | ||
convert_dtype=convert_dtype, | ||
target_dtype=np.float32, | ||
check_dtype=[np.float32, np.float64], | ||
) | ||
self.n_features_in_ = input_X.n_cols | ||
self.dtype = input_X.dtype | ||
|
||
input_y = CumlArray.from_input( | ||
y, | ||
order="C", | ||
convert_dtype=convert_dtype, | ||
target_dtype=self.dtype, | ||
check_dtype=[np.float32, np.float64], | ||
) | ||
|
||
self.coef_ = CumlArray.zeros(self.n_features_in_, | ||
dtype=self.dtype) | ||
|
||
self.intercept_ = CumlArray.zeros(self.n_features_in_, | ||
dtype=self.dtype) | ||
|
||
# do awesome C++ fitting here :) | ||
|
||
return self | ||
|
||
@io_predict_transform_array | ||
def predict(self, | ||
X, | ||
convert_dtype=True): | ||
input_X = CumlArray.from_input( | ||
X, | ||
order="C", | ||
convert_dtype=convert_dtype, | ||
target_dtype=self.dtype, | ||
check_dtype=[np.float32, np.float64], | ||
) | ||
n_rows = input_X.shape[0] | ||
|
||
preds = CumlArray.zeros(n_rows, | ||
dtype=self.dtype, | ||
index=input_X.index) | ||
|
||
# more awesome C++ | ||
|
||
return preds |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One weird thing while playing with this a bit: when I add a
print(f"{self.coef_=}")
here I get the following:I have stared at this for quite a while but can't work out what is going on??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realised about 2min after leaving the office: it is because
_is_fitted
doesn't get set untilfit
returns. Maybe something to improve as it makes for a tedious to debug thing :D - I'll ponder a suggestionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is the same behavior a scikit-learn, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. I was trying to access the
coef_
attribute withinfit
.In scikit-learn these are normal attributes, so once they are set you can use them. Right now we define
__getattribute__
which uses_is_fit
. I think it is a bit weird to have code like this fail, mostly because it makes you question your sanity and because the exception doesn't contain a clue (we get to see theAttributeError
from__getattr__
not__getattribute__
:():Maybe we can get around the need to checking
_is_fit
and using__getattribute__
by recording inside theDynamicDescriptor
if it has been set or not:This might need a bit of tweaking to make the message in the exception look right (
"'Estimator' object has no attribute 'foo_'"
).