Skip to content
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

Let set_rng_state and get_rng_state accept string parameter #23448

Closed
wants to merge 1 commit into from

Conversation

xuhdev
Copy link
Collaborator

@xuhdev xuhdev commented Jul 26, 2019

Currently set_rng_state and get_rng_state do not accept string as their parameters. This commit let them accept strings.

@pytorchbot pytorchbot added the module: cuda Related to torch.cuda, and CUDA support in general label Jul 26, 2019
@xuhdev xuhdev changed the title Incorrect default value of set_rng_state and get_rng_state Incorrect default parameter value of set_rng_state and get_rng_state Jul 26, 2019
@xuhdev xuhdev added the module: random Related to random number generation in PyTorch (rng generator) label Jul 26, 2019
Copy link
Contributor

@syed-ahmed syed-ahmed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I learnt something new about python :) LGTM.

@ezyang
Copy link
Contributor

ezyang commented Jul 26, 2019

Waiting on CI

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soumith is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@ssnl
Copy link
Collaborator

ssnl commented Jul 26, 2019

Nice. Another (possibly better) way is to make the default argument the “cuda” string. :)

@xuhdev
Copy link
Collaborator Author

xuhdev commented Jul 26, 2019

Nice. Another (possibly better) way is to make the default argument the “cuda” string. :)

Good point! I've updated the PR so string is now also accommodated.

Currently the default device of set_rng_state and get_rng_state is
torch.device('cuda'). But this is only evaluated once when the function
definition is executed. In other words, the default value would not be
correct if the user calls torch.set_device() to a different device than
the one when the function definitions of set_rng_state and get_rng_state
are executed. See

https://docs.python.org/3.7/reference/compound_stmts.html#function-definitions

> Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same **pre-computed** value is used for each cal
@ssnl
Copy link
Collaborator

ssnl commented Jul 27, 2019

Wait I’m actually a little bit confused. torch.device(“cuda”) returns a device object with device index None, I.e., it always refers to the current device. Are you sure that the old default values have problems?

@xuhdev
Copy link
Collaborator Author

xuhdev commented Jul 27, 2019

Wait I’m actually a little bit confused. torch.device(“cuda”) returns a device object with device index None, I.e., it always refers to the current device. Are you sure that the old default values have problems?

I'm not sure whether it always refers to the current device, which is not clearly written in torch.device doc. If it is so, I don't think there will be any issue with the default param and I can edit the message of this PR to "set_rng_state and get_rng_state now accept string param". In either case, I'll also send a PR to clarify this in the doc.

@ssnl
Copy link
Collaborator

ssnl commented Jul 27, 2019

Okay, yeah that sounds good to me. :) You can use this function to get the device index:

def _get_device_index(device, optional=False):

@xuhdev xuhdev changed the title Incorrect default parameter value of set_rng_state and get_rng_state Let set_rng_state and get_rng_state accept string parameter Jul 27, 2019
@xuhdev
Copy link
Collaborator Author

xuhdev commented Jul 27, 2019

@ssnl Updated!

facebook-github-bot pushed a commit that referenced this pull request Jul 27, 2019
…current device (#23468)

Summary:
Per discussion in #23448
Pull Request resolved: #23468

Differential Revision: D16532950

Pulled By: soumith

fbshipit-source-id: 48c97060aaf55f1d7589afab42c6cd623d71a9a7
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soumith has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@ssnl
Copy link
Collaborator

ssnl commented Jul 29, 2019

Can we switch to _get_device_index please? :) It will be consistent with all other things and avoid code duplication.

@facebook-github-bot
Copy link
Contributor

@soumith merged this pull request in 97f129b.

@xuhdev xuhdev deleted the rng_state-device branch July 29, 2019 17:39
@xuhdev
Copy link
Collaborator Author

xuhdev commented Jul 29, 2019

@ssnl Are you talking about this line idx = device.index?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged module: cuda Related to torch.cuda, and CUDA support in general module: random Related to random number generation in PyTorch (rng generator) open source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants