Move TensorOptions ops to c10#39492
Conversation
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) ghstack-source-id: 105217041 Pull Request resolved: #39492
💊 CI failures summary and remediationsAs of commit 58bd7ff (more details on the Dr. CI page):
XLA failureJob pytorch_xla_linux_bionic_py3_6_clang9_build is failing. Please create an issue with title prefixed by 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 or post in the (internal) Dr. CI Users group. This comment has been revised 215 times. |
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Pull Request resolved: #39492 ghstack-source-id: 105363132 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/)
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Pull Request resolved: #39492 ghstack-source-id: 105374944 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/)
|
This PR is pretty long, can we get a longer PR description? |
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
Pull Request resolved: #39492 ghstack-source-id: 105637896 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/)
aten/src/ATen/core/op_registration/hacky_wrapper_for_legacy_signatures.h
Outdated
Show resolved
Hide resolved
aten/src/ATen/core/op_registration/hacky_wrapper_for_legacy_signatures.h
Show resolved
Hide resolved
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
ailzhang
left a comment
There was a problem hiding this comment.
Stamping as we have a fix for the XLA failure. Thanks!
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
bhosmer
left a comment
There was a problem hiding this comment.
Hey, made it down to native_functions.yaml, will resume in a bit - meanwhile a few actionable suggestions in line that I'd pitch pretty enthusiastically. :)
| DEFAULT_FUNCTION_REGISTRATION = CodeTemplate("""\ | ||
| m.impl("${unqual_operator_name_with_overload}", TORCH_FN(TypeDefault::${type_wrapper_name})); | ||
| m.impl("${unqual_operator_name_with_overload}", | ||
| c10::impl::hacky_wrapper_for_legacy_signatures(TORCH_FN(TypeDefault::${type_wrapper_name}))); |
There was a problem hiding this comment.
It's a bit of a bummer that our nice clean API has so quickly gotten encrusted with this shim, in particular to the extent these calls will serve as example API usage. Ergonomically it's kind of rough (especially since it might do nothing). Two ideas:
- teach the codegen to only include the wrapper when necessary. I realize this intrudes on the complete decoupling you were going for here, but I think it would be worth it - the API usage will be better exemplified both for legacy and non-legacy cases. I.e., I think the value of complete decoupling is outweighed by the extent to which it obscures the (evolving) relationship between registered and implemented signatures, and by the readability hit.
To pitch the change another way: we want registrations for kernels that are already in our desired final state to look like it.
- hide the wrapper behind TORCH_FN, or a macro that wraps TORCH_FN. I think this is less desirable since it simply hides everything, rather than clarifying when the wrapper is needed, but it would fix the readability issue.
We can teach the codegen when to emit the wrapper just by looking for tensor options in the signature, correct? If so, I'd suggest having a function needs_legacy_wrapper where any future triggering conditions would also go (per the comment header in above the metaprogramming for this)
| m.impl_UNBOXED("aten::${op_name_with_overload_name}", ${function_name}); | ||
| """) | ||
|
|
||
| FUNCTION_REGISTRATION = CodeTemplate("""\ |
There was a problem hiding this comment.
Same suggestion re making hacky_wrapper conditional. Could use the same central discriminant function to decide; this would actually reinforce the difference as a canonical part of codegen logic and clarify where/why it was necessary.
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
bhosmer
left a comment
There was a problem hiding this comment.
LG, a few things inline, mostly copy pasting, but the only major question is whether to conditionalize the hacky wrapper use in this PR or a followup - up to you 😁
aten/src/ATen/core/op_registration/hacky_wrapper_for_legacy_signatures.h
Show resolved
Hide resolved
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
This PR adds `use_c10_dispatcher: full` to ops taking `TensorOptions`. To allow this, since the c10 operator library doesn't know about `TensorOptions`, we need to register the operator kernels as `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead, and also call them this way. Changes: - Add `use_c10_dispatcher: full` to those ops - Write hacky_wrapper_for_legacy_signatures which takes an old-style kernel (i.e. one written to take `TensorOptions`) an creates a wrapper kernel for it that takes the scattered `optional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>` instead. - Change codegen so that all op registrations are wrapped into `hacky_wrapper_for_legacy_signatures`. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature from `TensorOptions` to the scattered version and have it work without having to touch codegen. - Change codegen so that the frontend calls those operators with expanded arguments instead of with a `TensorOptions` object. This is required because now the kernels are written in this way. This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels. Codegen diff: P133121032 Differential Revision: [D21581908](https://our.internmc.facebook.com/intern/diff/D21581908/) [ghstack-poisoned]
|
This pull request has been merged in b623bde. |
| using parameters_before_tensoroptions = | ||
| guts::typelist::take_t<gathered_parameter_types, tensoroptions_arg_index>; | ||
| using parameters_after_tensoroptions = | ||
| guts::typelist::drop_t<gathered_parameter_types, tensoroptions_arg_index + 1>; |
There was a problem hiding this comment.
Not gonna lie: I'm impressed this all works at all! Do you remember if there were any hidden bombshells, e.g., compiler bugs, you had to work around to get this working? (What are the buried skeletons?)
Stack from ghstack:
This PR adds
use_c10_dispatcher: fullto ops takingTensorOptions. To allow this, since the c10 operator library doesn't know aboutTensorOptions, we need to register the operator kernels asoptional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>instead, and also call them this way.Changes:
use_c10_dispatcher: fullto those opsTensorOptions) an creates a wrapper kernel for it that takes the scatteredoptional<ScalarType>, optional<Device>, optional<Layout>, optional<bool>instead.hacky_wrapper_for_legacy_signatures. This is added to all ops but is a no-op if the op doesn't take TensorOptions. This allows us in the future to just change a kernel signature fromTensorOptionsto the scattered version and have it work without having to touch codegen.TensorOptionsobject. This is required because now the kernels are written in this way.This PR does not remove TensorOptions special cases from codegen, but instead it separates kernels from the codegen/frontend issues. After this, kernels can be worked on separately without having to touch codegen and codegen can be worked on without having to touch kernels.
Codegen diff: P133121032
Differential Revision: D21581908