-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Move generator state APIs to ATen #49589
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
Conversation
💊 CI failures summary and remediationsAs of commit 93decdd (more details on the Dr. CI page):
🕵️ 1 new failure recognized by patternsThe following CI failures do not appear to be due to upstream breakages:
|
34b5294 to
f7df038
Compare
|
I guess this is ready for review now. Since I'm not familiar with the C++ side of the project, maybe @ezyang or someone can take a look at this? |
Codecov Report
@@ Coverage Diff @@
## master #49589 +/- ##
===========================================
+ Coverage 32.88% 80.69% +47.81%
===========================================
Files 511 1891 +1380
Lines 68918 204977 +136059
===========================================
+ Hits 22665 165416 +142751
+ Misses 46253 39561 -6692 |
c0a9f7b to
7293324
Compare
|
@pbelevich let me know if you need extra eyeballs on this PR |
Will review it and other prs today, sorry for late review |
facebook-github-bot
left a comment
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.
@pbelevich has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@pbelevich I revisited the PR today and figured out although the implementation is correct, there's still a few places where the code quality can be improved:
What's your opinions on these? Since the PR is already imported to FB's internal systems, should I update this PR or open a new one? |
pbelevich
left a comment
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.
@lqf96 thanks a lot for this work!
- Should we name the new APIs as
state/set_stateorrng_state/set_rng_state? The latter sounds clearer and matches the legacy APIs names (THTensor_(get|set)RNGState/THCRandom_(get|set)RNGState) better.
I think we should try to match Python API names instead of legacy names to make C++ API and Python API look similar. So, let's rename state to set_state and keep get_state as is.
2.
c10::GeneratorImpl::state(orrng_state) should acceptconst c10::TensorImpl&instead ofc10::TensorImpl&. Similarly,at::Generator::state(orrng_state) should acceptconst Tensor&.
Agree, let's make them const
Since the PR is already imported to FB's internal systems, should I update this PR or open a new one?
Amend this PR, it's easy to reimport it again to FB internal code
|
TODO(for pbelevich): amend this PR with meta-pytorch/csprng#106 after reimport to fbcode |
7293324 to
8df4d29
Compare
0520138 to
93decdd
Compare
|
@pbelevich I applied your suggestions and this PR should be ready for review again. |
facebook-github-bot
left a comment
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.
@pbelevich has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
This pull request has been merged in 876dfbd. |
Summary: ## Rationale While most of the `torch.Generator` properties and methods are implemented as a thin wrapper of the corresponding `at::Generator` methods, `torch.Generator.get_state()` and `torch.Generator.set_state()` are implemented in legacy Torch code and are not dispatched through the `c10::GeneratorImpl` interface. This is not structured well and makes implementing generators for new backends (e.g. `XLAGeneratorImpl` for the XLA backend) inconvenient. As such, this pull request seeks to move these generator state APIs to c10 and ATen. ## What is being refactored? * Interfaces - Added `c10::GeneratorImpl::set_state` and `c10::GeneratorImpl::state` for getting and setting the internal state of a random number generator. - `at::Generator::set_state` and `at::Generator::state` wraps the above-mentioned APIs, as it's basically a PIMPL. - Added helper function `at::detail::check_rng_state` for checking the validity of new RNG state tensor. * CPU Generator - Renamed and moved `THTensor_(setRNGState)` and `THTensor_(getRNGState)` to `CPUGeneratorImpl::set_state` and `CPUGenerator::state`. - Renamed and moved `THGeneratorState` and `THGeneratorStateNew` to `CPUGeneratorStateLegacy` and `CPUGeneratorState`. * CUDA Generator - Renamed and moved `THCRandom_setRNGState` and `THCRandom_getRNGState` to `CUDAGeneratorImpl::set_state` and `CUDAGeneratorImpl::state`. * PyTorch Bindings - `THPGenerator_setState` and `THPGenerator_getState` now simply forward to `at::Generator::set_state` and `at::Generator::state`. Pull Request resolved: #49589 Reviewed By: H-Huang Differential Revision: D25785774 Pulled By: pbelevich fbshipit-source-id: 8ed79209c4ffb1a0ae8b19952ac8871ac9e0255f
Summary: ## Rationale While most of the `torch.Generator` properties and methods are implemented as a thin wrapper of the corresponding `at::Generator` methods, `torch.Generator.get_state()` and `torch.Generator.set_state()` are implemented in legacy Torch code and are not dispatched through the `c10::GeneratorImpl` interface. This is not structured well and makes implementing generators for new backends (e.g. `XLAGeneratorImpl` for the XLA backend) inconvenient. As such, this pull request seeks to move these generator state APIs to c10 and ATen. ## What is being refactored? * Interfaces - Added `c10::GeneratorImpl::set_state` and `c10::GeneratorImpl::state` for getting and setting the internal state of a random number generator. - `at::Generator::set_state` and `at::Generator::state` wraps the above-mentioned APIs, as it's basically a PIMPL. - Added helper function `at::detail::check_rng_state` for checking the validity of new RNG state tensor. * CPU Generator - Renamed and moved `THTensor_(setRNGState)` and `THTensor_(getRNGState)` to `CPUGeneratorImpl::set_state` and `CPUGenerator::state`. - Renamed and moved `THGeneratorState` and `THGeneratorStateNew` to `CPUGeneratorStateLegacy` and `CPUGeneratorState`. * CUDA Generator - Renamed and moved `THCRandom_setRNGState` and `THCRandom_getRNGState` to `CUDAGeneratorImpl::set_state` and `CUDAGeneratorImpl::state`. * PyTorch Bindings - `THPGenerator_setState` and `THPGenerator_getState` now simply forward to `at::Generator::set_state` and `at::Generator::state`. Pull Request resolved: pytorch#49589 Reviewed By: H-Huang Differential Revision: D25785774 Pulled By: pbelevich fbshipit-source-id: 8ed79209c4ffb1a0ae8b19952ac8871ac9e0255f
Summary: ## Rationale While most of the `torch.Generator` properties and methods are implemented as a thin wrapper of the corresponding `at::Generator` methods, `torch.Generator.get_state()` and `torch.Generator.set_state()` are implemented in legacy Torch code and are not dispatched through the `c10::GeneratorImpl` interface. This is not structured well and makes implementing generators for new backends (e.g. `XLAGeneratorImpl` for the XLA backend) inconvenient. As such, this pull request seeks to move these generator state APIs to c10 and ATen. ## What is being refactored? * Interfaces - Added `c10::GeneratorImpl::set_state` and `c10::GeneratorImpl::state` for getting and setting the internal state of a random number generator. - `at::Generator::set_state` and `at::Generator::state` wraps the above-mentioned APIs, as it's basically a PIMPL. - Added helper function `at::detail::check_rng_state` for checking the validity of new RNG state tensor. * CPU Generator - Renamed and moved `THTensor_(setRNGState)` and `THTensor_(getRNGState)` to `CPUGeneratorImpl::set_state` and `CPUGenerator::state`. - Renamed and moved `THGeneratorState` and `THGeneratorStateNew` to `CPUGeneratorStateLegacy` and `CPUGeneratorState`. * CUDA Generator - Renamed and moved `THCRandom_setRNGState` and `THCRandom_getRNGState` to `CUDAGeneratorImpl::set_state` and `CUDAGeneratorImpl::state`. * PyTorch Bindings - `THPGenerator_setState` and `THPGenerator_getState` now simply forward to `at::Generator::set_state` and `at::Generator::state`. Pull Request resolved: pytorch/pytorch#49589 Reviewed By: H-Huang Differential Revision: D25785774 Pulled By: pbelevich fbshipit-source-id: 8ed79209c4ffb1a0ae8b19952ac8871ac9e0255f
Rationale
While most of the
torch.Generatorproperties and methods are implemented as a thin wrapper of the correspondingat::Generatormethods,torch.Generator.get_state()andtorch.Generator.set_state()are implemented in legacy Torch code and are not dispatched through thec10::GeneratorImplinterface. This is not structured well and makes implementing generators for new backends (e.g.XLAGeneratorImplfor the XLA backend) inconvenient. As such, this pull request seeks to move these generator state APIs to c10 and ATen.What is being refactored?
c10::GeneratorImpl::set_stateandc10::GeneratorImpl::statefor getting and setting the internal state of a random number generator.at::Generator::set_stateandat::Generator::statewraps the above-mentioned APIs, as it's basically a PIMPL.at::detail::check_rng_statefor checking the validity of new RNG state tensor.THTensor_(setRNGState)andTHTensor_(getRNGState)toCPUGeneratorImpl::set_stateandCPUGenerator::state.THGeneratorStateandTHGeneratorStateNewtoCPUGeneratorStateLegacyandCPUGeneratorState.THCRandom_setRNGStateandTHCRandom_getRNGStatetoCUDAGeneratorImpl::set_stateandCUDAGeneratorImpl::state.THPGenerator_setStateandTHPGenerator_getStatenow simply forward toat::Generator::set_stateandat::Generator::state.