Skip to content

Conversation

pbelevich
Copy link
Contributor

@pbelevich pbelevich commented Mar 9, 2020

This PR prepares at::Generator for pybind11's type_caster<at::Generator> which is required to implement custom RNG in python. The following changes are done:

  1. at::Generator was moved to c10::GeneratorImpl (similar to c10::TensorImpl)
  2. at::Generator was recreated as a holder of std::shared_ptr<c10::GeneratorImpl> (similar to at::Tensor that holds c10::intrusive_ptr<c10::TensorImpl>)
  3. Most of at::Generator* usages were replaced with at::Generator

TBD: replacing Generator generator = nullptr with {} requires JIT changes(adding Generator to IValue?)

Stack from ghstack:

Differential Revision: D20549420

[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Mar 9, 2020
ghstack-source-id: dca28dd
Pull Request resolved: #34468
@pbelevich pbelevich changed the title GeneratorHolder [WIP] GeneratorHolder Mar 9, 2020
@dr-ci
Copy link

dr-ci bot commented Mar 9, 2020

💊 CircleCI build failures summary and remediations

As of commit 52b450d (more details on the Dr. CI page):


  • 1/2 failures introduced in this PR

  • 1/2 broken upstream at merge base f515d87 since Mar 21

    Please rebase on the viable/strict branch (expand for instructions)

    If your commit is newer than viable/strict, you can try basing on an older, stable commit:

    git fetch https://github.com/pytorch/pytorch viable/strict
    git rebase --onto FETCH_HEAD $(git merge-base origin/master HEAD)
    

    If your commit is older than viable/strict:

    git fetch https://github.com/pytorch/pytorch viable/strict
    git rebase FETCH_HEAD
    

    Check out the recency history of this "viable master" tracking branch.


🕵️ 1 new failure recognized by patterns

The following build failures do not appear to be due to upstream breakages:

See CircleCI build pytorch_xla_linux_xenial_py3_6_clang7_build (1/1)

Step: "Build" (full log | pattern match details)

Mar 21 14:56:19 torch_xla/csrc/aten_xla_type_default.cpp:11460:8: error: no matching member function for call to 'impl_unboxedOnlyKernel'
Mar 21 14:56:19       ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
Mar 21 14:56:19 /opt/conda/lib/python3.6/site-packages/torch/include/ATen/core/op_registration/op_registration.h:294:74: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'kernel_func' 
Mar 21 14:56:19     std::enable_if_t<guts::is_function_type<FuncType>::value, Options&&> impl_unboxedOnlyKernel(DispatchKey dispatch_key) && { 
Mar 21 14:56:19                                                                          ^ 
Mar 21 14:56:19 torch_xla/csrc/aten_xla_type_default.cpp:11199:8: error: no matching member function for call to 'impl_unboxedOnlyKernel' 
Mar 21 14:56:19       .impl_unboxedOnlyKernel<at::Tensor &(at::Tensor &, double, double, at::Generator), &AtenXlaType::normal_>(at::DispatchKey::XLATensorId) 
Mar 21 14:56:19       ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
Mar 21 14:56:19 /opt/conda/lib/python3.6/site-packages/torch/include/ATen/core/op_registration/op_registration.h:294:74: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'kernel_func' 
Mar 21 14:56:19     std::enable_if_t<guts::is_function_type<FuncType>::value, Options&&> impl_unboxedOnlyKernel(DispatchKey dispatch_key) && { 
Mar 21 14:56:19                                                                          ^ 
Mar 21 14:56:19 torch_xla/csrc/aten_xla_type_default.cpp:11460:8: error: no matching member function for call to 'impl_unboxedOnlyKernel' 
Mar 21 14:56:19       .impl_unboxedOnlyKernel<at::Tensor(const at::Tensor &, const at::Tensor &, at::Scalar, at::Scalar, bool, at::Generator), &AtenXlaType::rrelu_with_noise>(at::DispatchKey::XLATensorId) 
Mar 21 14:56:19       ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
Mar 21 14:56:19 /opt/conda/lib/python3.6/site-packages/torch/include/ATen/core/op_registration/op_registration.h:294:74: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'kernel_func' 
Mar 21 14:56:19     std::enable_if_t<guts::is_function_type<FuncType>::value, Options&&> impl_unboxedOnlyKernel(DispatchKey dispatch_key) && { 
Mar 21 14:56:19                                                                          ^ 
ackages/torch/include/torch/csrc/api/include -I/opt/conda/lib/python3.6/site-packages/torch/include/TH -I/opt/conda/lib/python3.6/site-packages/torch/include/THC -I/opt/conda/include/python3.6m -c torch_xla/csrc/batch_norm.cpp -o build/temp.linux-x86_64-3.6/torch_xla/csrc/batch_norm.o -std=c++14 -Wno-sign-compare -Wno-deprecated-declarations -Wno-return-type -Wno-macro-redefined -Wno-return-std-move -DNDEBUG -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_XLAC -D_GLIBCXX_USE_CXX11_ABI=1 
torch/include/torch/csrc/api/include -I/opt/conda/lib/python3.6/site-packages/torch/include/TH -I/opt/conda/lib/python3.6/site-packages/torch/include/THC -I/opt/conda/include/python3.6m -c torch_xla/csrc/layout_manager.cpp -o build/temp.linux-x86_64-3.6/torch_xla/csrc/layout_manager.o -std=c++14 -Wno-sign-compare -Wno-deprecated-declarations -Wno-return-type -Wno-macro-redefined -Wno-return-std-move -DNDEBUG -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_XLAC -D_GLIBCXX_USE_CXX11_ABI=1 
Mar 21 14:56:26 11 errors generated. 
on3.6/site-packages/torch/include/torch/csrc/api/include -I/opt/conda/lib/python3.6/site-packages/torch/include/TH -I/opt/conda/lib/python3.6/site-packages/torch/include/THC -I/opt/conda/include/python3.6m -c torch_xla/csrc/view.cpp -o build/temp.linux-x86_64-3.6/torch_xla/csrc/view.o -std=c++14 -Wno-sign-compare -Wno-deprecated-declarations -Wno-return-type -Wno-macro-redefined -Wno-return-std-move -DNDEBUG -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_XLAC -D_GLIBCXX_USE_CXX11_ABI=1 
te-packages/torch/include/torch/csrc/api/include -I/opt/conda/lib/python3.6/site-packages/torch/include/TH -I/opt/conda/lib/python3.6/site-packages/torch/include/THC -I/opt/conda/include/python3.6m -c torch_xla/csrc/nll_loss.cpp -o build/temp.linux-x86_64-3.6/torch_xla/csrc/nll_loss.o -std=c++14 -Wno-sign-compare -Wno-deprecated-declarations -Wno-return-type -Wno-macro-redefined -Wno-return-std-move -DNDEBUG -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_XLAC -D_GLIBCXX_USE_CXX11_ABI=1 

🚧 1 upstream failure:

These were probably caused by upstream breakages:


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker.

This comment has been revised 142 times.

pbelevich added a commit that referenced this pull request Mar 9, 2020
ghstack-source-id: 1a8841c
Pull Request resolved: #34468
pbelevich added a commit that referenced this pull request Mar 10, 2020
ghstack-source-id: 5755613
Pull Request resolved: #34468
@pbelevich pbelevich requested a review from apaszke as a code owner March 10, 2020 21:42
pbelevich added a commit that referenced this pull request Mar 10, 2020
ghstack-source-id: 66906e6
Pull Request resolved: #34468
pbelevich added a commit that referenced this pull request Mar 11, 2020
ghstack-source-id: cbe8580
Pull Request resolved: #34468
pbelevich added a commit that referenced this pull request Mar 12, 2020
ghstack-source-id: 1108dee
Pull Request resolved: #34468
pbelevich added a commit that referenced this pull request Mar 12, 2020
ghstack-source-id: 6021401
Pull Request resolved: #34468
pbelevich added a commit that referenced this pull request Mar 13, 2020
ghstack-source-id: 5b5418a
Pull Request resolved: #34468
pbelevich added a commit that referenced this pull request Mar 18, 2020
This PR adds c-tor `Generator(Device device)` to C++ API to match Python API, to avoid code duplication the logic was moved from `THPGenerator_pynew` to corresponding `at::Generator` c-tor. Unfortunately it makes Generator.h depend on CPUGenerator.h and CUDAGenerator.h, but this is how our Python API works :(

* #34468 Replace Generator* with Generator that holds std::shared_ptr<GeneratorImpl>

[ghstack-poisoned]
…tr<GeneratorImpl>"

This PR prepares `at::Generator` for pybind11's `type_caster<at::Generator>` which is required to implement custom RNG in python. The following changes are done:
1. `at::Generator` was moved to `c10::GeneratorImpl` (similar to `c10::TensorImpl`)
2. `at::Generator` was recreated as a holder of `std::shared_ptr<c10::GeneratorImpl>` (similar to `at::Tensor` that holds `c10::intrusive_ptr<c10::TensorImpl>`)
3. Most of `at::Generator*` usages were replaced with `at::Generator`

TBD: replacing `Generator generator = nullptr` with `{}` requires JIT changes(adding Generator to IValue?)



[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Mar 19, 2020
This PR adds c-tor `Generator(Device device)` to C++ API to match Python API, to avoid code duplication the logic was moved from `THPGenerator_pynew` to corresponding `at::Generator` c-tor. Unfortunately it makes Generator.h depend on CPUGenerator.h and CUDAGenerator.h, but this is how our Python API works :(

* #34468 Replace Generator* with Generator that holds std::shared_ptr<GeneratorImpl>

[ghstack-poisoned]
…tr<GeneratorImpl>"

This PR prepares `at::Generator` for pybind11's `type_caster<at::Generator>` which is required to implement custom RNG in python. The following changes are done:
1. `at::Generator` was moved to `c10::GeneratorImpl` (similar to `c10::TensorImpl`)
2. `at::Generator` was recreated as a holder of `std::shared_ptr<c10::GeneratorImpl>` (similar to `at::Tensor` that holds `c10::intrusive_ptr<c10::TensorImpl>`)
3. Most of `at::Generator*` usages were replaced with `at::Generator`

TBD: replacing `Generator generator = nullptr` with `{}` requires JIT changes(adding Generator to IValue?)



[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Mar 19, 2020
This PR adds c-tor `Generator(Device device)` to C++ API to match Python API, to avoid code duplication the logic was moved from `THPGenerator_pynew` to corresponding `at::Generator` c-tor. Unfortunately it makes Generator.h depend on CPUGenerator.h and CUDAGenerator.h, but this is how our Python API works :(

* #34468 Replace Generator* with Generator that holds std::shared_ptr<GeneratorImpl>

[ghstack-poisoned]
…tr<GeneratorImpl>"

This PR prepares `at::Generator` for pybind11's `type_caster<at::Generator>` which is required to implement custom RNG in python. The following changes are done:
1. `at::Generator` was moved to `c10::GeneratorImpl` (similar to `c10::TensorImpl`)
2. `at::Generator` was recreated as a holder of `std::shared_ptr<c10::GeneratorImpl>` (similar to `at::Tensor` that holds `c10::intrusive_ptr<c10::TensorImpl>`)
3. Most of `at::Generator*` usages were replaced with `at::Generator`

TBD: replacing `Generator generator = nullptr` with `{}` requires JIT changes(adding Generator to IValue?)



[ghstack-poisoned]
pbelevich added a commit that referenced this pull request Mar 19, 2020
This PR adds c-tor `Generator(Device device)` to C++ API to match Python API, to avoid code duplication the logic was moved from `THPGenerator_pynew` to corresponding `at::Generator` c-tor. Unfortunately it makes Generator.h depend on CPUGenerator.h and CUDAGenerator.h, but this is how our Python API works :(

* #34468 Replace Generator* with Generator that holds std::shared_ptr<GeneratorImpl>

[ghstack-poisoned]
@pbelevich pbelevich requested a review from ezyang March 19, 2020 20:08
std::lock_guard<std::mutex> lock(cuda_gen.mutex_);
cuda_gen.set_current_seed(seed);
std::lock_guard<std::mutex> lock(cuda_gen->mutex_);
cuda_gen->set_current_seed(seed);
Copy link
Contributor

Choose a reason for hiding this comment

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

Also doesn't have to be this PR, but we should consider adding methods like set_current_seed to Generator directly, so dot-syntax works on them (making Generator consistent with Tensor)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do in #34987

return static_cast<T*>(expr);
static inline T* get_generator_or_default(const Generator& expr, const Generator& defaultValue) {
T* result = expr.defined() ? check_generator<T>(expr) : check_generator<T>(defaultValue);
if (result == nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

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

When can this be nullptr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rewrote this part, please take a look

virtual ~Generator() = default;
std::shared_ptr<Generator> clone() const;
// TODO(pbelevich): delete this after replace Generator generator = nullptr with c10::optional<at::Generator> = c10::nullopt
Generator(std::nullptr_t gen_impl) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

It's pretty important for us to get rid of this TODO soon, as it's pretty confusing that:

std::shared_ptr<c10::GeneratorImpl> impl = nullptr;
Generator g(impl); // errors

Generator g(nullptr); // this is OK?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree, #35067

virtual uint64_t seed() = 0;
Device device() const;
bool operator==(const Generator& rhs) const {
return (!(this->impl_) && !(rhs.impl_)) || (this->impl_ == rhs.impl_);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't this just be this->impl_ == rhs->impl_? Because if they're both nullptr they'll compare equal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed


DispatchKeySet key_set() const { return key_set_; }
bool defined() const {
return (bool)impl_;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: static_cast<bool>(impl_) for extra safety. (Or just let implicit conversion on pointer take care of it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

LGTM.

@ezyang
Copy link
Contributor

ezyang commented Mar 19, 2020

Please take care of replacing the = {} with explicit destructor call before landing

…tr<GeneratorImpl>"

This PR prepares `at::Generator` for pybind11's `type_caster<at::Generator>` which is required to implement custom RNG in python. The following changes are done:
1. `at::Generator` was moved to `c10::GeneratorImpl` (similar to `c10::TensorImpl`)
2. `at::Generator` was recreated as a holder of `std::shared_ptr<c10::GeneratorImpl>` (similar to `at::Tensor` that holds `c10::intrusive_ptr<c10::TensorImpl>`)
3. Most of `at::Generator*` usages were replaced with `at::Generator`

TBD: replacing `Generator generator = nullptr` with `{}` requires JIT changes(adding Generator to IValue?)



[ghstack-poisoned]
…tr<GeneratorImpl>"

This PR prepares `at::Generator` for pybind11's `type_caster<at::Generator>` which is required to implement custom RNG in python. The following changes are done:
1. `at::Generator` was moved to `c10::GeneratorImpl` (similar to `c10::TensorImpl`)
2. `at::Generator` was recreated as a holder of `std::shared_ptr<c10::GeneratorImpl>` (similar to `at::Tensor` that holds `c10::intrusive_ptr<c10::TensorImpl>`)
3. Most of `at::Generator*` usages were replaced with `at::Generator`

TBD: replacing `Generator generator = nullptr` with `{}` requires JIT changes(adding Generator to IValue?)


Differential Revision: [D20549420](https://our.internmc.facebook.com/intern/diff/D20549420)

[ghstack-poisoned]
…tr<GeneratorImpl>"

This PR prepares `at::Generator` for pybind11's `type_caster<at::Generator>` which is required to implement custom RNG in python. The following changes are done:
1. `at::Generator` was moved to `c10::GeneratorImpl` (similar to `c10::TensorImpl`)
2. `at::Generator` was recreated as a holder of `std::shared_ptr<c10::GeneratorImpl>` (similar to `at::Tensor` that holds `c10::intrusive_ptr<c10::TensorImpl>`)
3. Most of `at::Generator*` usages were replaced with `at::Generator`

TBD: replacing `Generator generator = nullptr` with `{}` requires JIT changes(adding Generator to IValue?)


Differential Revision: [D20549420](https://our.internmc.facebook.com/intern/diff/D20549420)

[ghstack-poisoned]
…tr<GeneratorImpl>"

This PR prepares `at::Generator` for pybind11's `type_caster<at::Generator>` which is required to implement custom RNG in python. The following changes are done:
1. `at::Generator` was moved to `c10::GeneratorImpl` (similar to `c10::TensorImpl`)
2. `at::Generator` was recreated as a holder of `std::shared_ptr<c10::GeneratorImpl>` (similar to `at::Tensor` that holds `c10::intrusive_ptr<c10::TensorImpl>`)
3. Most of `at::Generator*` usages were replaced with `at::Generator`

TBD: replacing `Generator generator = nullptr` with `{}` requires JIT changes(adding Generator to IValue?)


Differential Revision: [D20549420](https://our.internmc.facebook.com/intern/diff/D20549420)

[ghstack-poisoned]
…tr<GeneratorImpl>"

This PR prepares `at::Generator` for pybind11's `type_caster<at::Generator>` which is required to implement custom RNG in python. The following changes are done:
1. `at::Generator` was moved to `c10::GeneratorImpl` (similar to `c10::TensorImpl`)
2. `at::Generator` was recreated as a holder of `std::shared_ptr<c10::GeneratorImpl>` (similar to `at::Tensor` that holds `c10::intrusive_ptr<c10::TensorImpl>`)
3. Most of `at::Generator*` usages were replaced with `at::Generator`

TBD: replacing `Generator generator = nullptr` with `{}` requires JIT changes(adding Generator to IValue?)


Differential Revision: [D20549420](https://our.internmc.facebook.com/intern/diff/D20549420)

[ghstack-poisoned]
@facebook-github-bot
Copy link
Contributor

@pbelevich merged this pull request in 5306713.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants