-
Notifications
You must be signed in to change notification settings - Fork 540
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
FEA Add support for accepting a Numpy RandomState #6150
Open
betatim
wants to merge
10
commits into
rapidsai:branch-25.02
Choose a base branch
from
betatim:random-state-everywhere
base: branch-25.02
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
61768cb
Add support for accepting a Numpy RandomState
betatim 77aa6e3
Tweak multi-gpu case
betatim ea1dfe5
Change check_random_seed to return a random int for None
betatim 8b973ce
Use a default value for seeds
betatim 814c62b
Use a fixed random state
betatim c411530
Merge branch 'branch-24.12' into random-state-everywhere
betatim 7c4aae3
Always derive a new seed on `fit`
betatim 1daab08
Merge remote-tracking branch 'origin/random-state-everywhere' into ra…
betatim b0f7841
Ping
betatim 7092d59
Merge branch 'branch-25.02' into random-state-everywhere
betatim 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
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,39 @@ | ||
# | ||
# 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 numbers | ||
import numpy as np | ||
|
||
|
||
def check_random_seed(seed): | ||
"""Turn a np.random.RandomState instance into a seed. | ||
Parameters | ||
---------- | ||
seed : None | int | instance of RandomState | ||
If seed is None, return a random int as seed. | ||
If seed is an int, return it. | ||
If seed is a RandomState instance, derive a seed from it. | ||
Otherwise raise ValueError. | ||
""" | ||
if seed is None: | ||
seed = np.random.RandomState(None) | ||
|
||
if isinstance(seed, numbers.Integral): | ||
return seed | ||
if isinstance(seed, np.random.RandomState): | ||
return seed.randint( | ||
low=0, high=np.iinfo(np.uint32).max, dtype=np.uint32 | ||
) | ||
raise ValueError("%r cannot be used to create a seed." % seed) |
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,42 @@ | ||
# | ||
# 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 pytest | ||
|
||
import numpy as np | ||
import cuml | ||
from cuml.datasets import make_blobs | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"Estimator", | ||
[ | ||
cuml.KMeans, | ||
cuml.RandomForestRegressor, | ||
cuml.RandomForestClassifier, | ||
cuml.TSNE, | ||
cuml.UMAP, | ||
], | ||
) | ||
def test_random_state_argument(Estimator): | ||
X, y = make_blobs(random_state=0) | ||
# Check that both integer and np.random.RandomState are accepted | ||
for seed in (42, np.random.RandomState(42)): | ||
est = Estimator(random_state=seed) | ||
|
||
if est.__class__.__name__ != "TSNE": | ||
est.fit(X, y) | ||
else: | ||
est.fit(X) |
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
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.
Should we add a quick test here that the results are the same with the seed, or is that tested in the individual algo tests?
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 the results will be the same because
RandomState(42)
will not lead to42
being passed as the seed to the internal functions that cuml calls.We can't pass any form of "RNG state" to the internal functions, we can just pass an integer. So I think the best we can do when a
RandomState
is passed in is to use it to generate auint64
and use that as seed for the internal functions. I think this is better than trying to extract the (original) seed from theRandomState
because that way you get a different value if the random state has been used previously.For example in this (contrived) example I think the two RFs should not both use
42
as the seed internally as they are two separate instances.