-
Notifications
You must be signed in to change notification settings - Fork 796
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
add oneflow.cuda.get_rng_state and oneflow.cuda.get_rng_state_all api #9760
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f1183b3
add oneflow.cuda.get_rng_state and oneflow.cuda.get_rng_state_all api
BBuf d6af897
Merge branch 'master' into add_cuda_random
BBuf 2a56c10
auto format by CI
oneflow-ci-bot 5036fdd
Merge branch 'master' into add_cuda_random
mergify[bot] b661646
auto format by CI
oneflow-ci-bot 38f1173
Merge branch 'master' into add_cuda_random
mergify[bot] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,8 @@ Random Number Generator | |
|
||
manual_seed_all | ||
manual_seed | ||
get_rng_state | ||
get_rng_state_all | ||
|
||
|
||
GPU tensor | ||
|
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 |
---|---|---|
|
@@ -191,3 +191,5 @@ def empty_cache() -> None: | |
|
||
""" | ||
return flow._oneflow_internal.EmptyCache() | ||
|
||
from .random import * # noqa: F403 |
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,32 @@ | ||
import oneflow as flow | ||
from oneflow import Tensor | ||
from typing import Union, List | ||
from . import current_device, device_count | ||
|
||
def get_rng_state(device: Union[int, str, flow.device] = 'cuda') -> Tensor: | ||
r"""Returns the random number generator state of the specified GPU as a ByteTensor. | ||
|
||
Args: | ||
device (flow.device or int, optional): The device to return the RNG state of. | ||
Default: ``'cuda'`` (i.e., ``flow.device('cuda')``, the current CUDA device). | ||
""" | ||
# TODO (add lazy initialization mechanism in OneFlow) | ||
# _lazy_init() | ||
if isinstance(device, str): | ||
device = flow.device(device) | ||
elif isinstance(device, int): | ||
device = flow.device('cuda', device) | ||
idx = device.index | ||
if idx is None: | ||
idx = current_device() | ||
default_generator = flow.cuda.default_generators[idx] | ||
return default_generator.get_state() | ||
|
||
|
||
def get_rng_state_all() -> List[Tensor]: | ||
r"""Returns a list of ByteTensor representing the random number states of all devices.""" | ||
|
||
results = [] | ||
for i in range(device_count()): | ||
results.append(get_rng_state(i)) | ||
return results |
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.
lightning 适配中发现代码运行到这里会报错,因为 flow.cuda 下并没有 default_generators 😂