Desugar missing dispatch field into singleton Math entry#46970
Desugar missing dispatch field into singleton Math entry#46970ezyang wants to merge 2 commits intogh/ezyang/857/basefrom
Conversation
Now that catchall declarations are reinterpreted as registrations to dispatch key Math, we can now simplify code generation logic by directly generating to Math, and bypasing logic for catchall. This also helps avoid bugs where we incorrectly classify some kernels as Math and others as not, even though they get registered in the same way. Bill of changes: - Give Math its own unique TORCH_LIBRARY_IMPL - Make it so NativeFunction.dispatch is always non-None. Simplify downstream conditionals accordingly - When parsing NativeFunction, fill in missing dispatch with a singleton Math entry (pointing to the cpp.name!) One thing that is a little big about this change is a lot of kernels which previously didn't report as "math" now report as math. I picked a setting for these booleans that made sense to me, but I'm not sure if e.g. XLA will handle it 100% correctly. Signed-off-by: Edward Z. Yang <ezyang@fb.com> [ghstack-poisoned]
Now that catchall declarations are reinterpreted as registrations to dispatch key Math, we can now simplify code generation logic by directly generating to Math, and bypasing logic for catchall. This also helps avoid bugs where we incorrectly classify some kernels as Math and others as not, even though they get registered in the same way. Bill of changes: - Give Math its own unique TORCH_LIBRARY_IMPL - Make it so NativeFunction.dispatch is always non-None. Simplify downstream conditionals accordingly - When parsing NativeFunction, fill in missing dispatch with a singleton Math entry (pointing to the cpp.name!) One thing that is a little big about this change is a lot of kernels which previously didn't report as "math" now report as math. I picked a setting for these booleans that made sense to me, but I'm not sure if e.g. XLA will handle it 100% correctly. Signed-off-by: Edward Z. Yang <ezyang@fb.com> ghstack-source-id: 2c9a1d0 Pull Request resolved: #46970
💊 CI failures summary and remediationsAs of commit 1191e5b (more details on the Dr. CI page): ✅ None of the CI failures appear to be your fault 💚
🚧 1 fixed upstream failure:These were probably caused by upstream breakages that were already fixed.
Please rebase on the
|
Now that catchall declarations are reinterpreted as registrations to dispatch key Math, we can now simplify code generation logic by directly generating to Math, and bypasing logic for catchall. This also helps avoid bugs where we incorrectly classify some kernels as Math and others as not, even though they get registered in the same way. Bill of changes: - Give Math its own unique TORCH_LIBRARY_IMPL - Make it so NativeFunction.dispatch is always non-None. Simplify downstream conditionals accordingly - When parsing NativeFunction, fill in missing dispatch with a singleton Math entry (pointing to the cpp.name!) One thing that is a little big about this change is a lot of kernels which previously didn't report as "math" now report as math. I picked a setting for these booleans that made sense to me, but I'm not sure if e.g. XLA will handle it 100% correctly. Signed-off-by: Edward Z. Yang <ezyang@fb.com> Differential Revision: [D24592391](https://our.internmc.facebook.com/intern/diff/D24592391) [ghstack-poisoned]
| # them. In native_functions.yaml, the dispatch entry is optional; in that | ||
| # case, that is equivalent to having written: | ||
| # | ||
| # dispatch: |
There was a problem hiding this comment.
Is it worth deleting Math from all of the native_functions.yaml entries for consistency? Tentatively, I think making it an implementation detail that Math is the default dispatch entry makes more sense than explicitly offering two options for how people should register ops in native_functions.yaml.
There was a problem hiding this comment.
I see only one Math entry, in a situation where it is mandatory:
- func: native_group_norm(Tensor input, Tensor? weight, Tensor? bias, int N, int C, int HxW, int group, float eps) -> (Tensor, Tensor, Tensor)
use_c10_dispatcher: hacky_wrapper_for_legacy_signatures
dispatch:
CPU, CUDA: native_group_norm
Math: math_group_norm
So we could enforce a canonical form where, if you only have a single Math entry, and its name matches the function schema name, then we can error saying that the Math entry is unnecessary. IDK if it's worth or not.
There was a problem hiding this comment.
Ah yeah, I didn't notice that some ops require it (since they have other more specific dispatch keys). Probably not worth it then
|
@ailzhang I would still love it if you can take a look, since this changes what meatdata we feed to xla. |
| ('with_gil', False), | ||
| ('deprecated', False), | ||
| ('has_math_kernel', f.dispatch is not None and 'Math' in f.dispatch), | ||
| ('has_math_kernel', 'Math' in f.dispatch), |
There was a problem hiding this comment.
This field is only used for the old RegistrationDeclarations.h. Should be good to remove now. ;)
Summary: Pull Request resolved: pytorch#46970 Now that catchall declarations are reinterpreted as registrations to dispatch key Math, we can now simplify code generation logic by directly generating to Math, and bypasing logic for catchall. This also helps avoid bugs where we incorrectly classify some kernels as Math and others as not, even though they get registered in the same way. Bill of changes: - Give Math its own unique TORCH_LIBRARY_IMPL - Make it so NativeFunction.dispatch is always non-None. Simplify downstream conditionals accordingly - When parsing NativeFunction, fill in missing dispatch with a singleton Math entry (pointing to the cpp.name!) One thing that is a little big about this change is a lot of kernels which previously didn't report as "math" now report as math. I picked a setting for these booleans that made sense to me, but I'm not sure if e.g. XLA will handle it 100% correctly. Signed-off-by: Edward Z. Yang <ezyang@fb.com> Test Plan: Imported from OSS Reviewed By: albanD Differential Revision: D24592391 Pulled By: ezyang fbshipit-source-id: 2e3355f19f9525698864312418df08411f30a85d
Stack from ghstack:
Now that catchall declarations are reinterpreted as registrations to
dispatch key Math, we can now simplify code generation logic by directly
generating to Math, and bypasing logic for catchall. This also helps
avoid bugs where we incorrectly classify some kernels as Math and others
as not, even though they get registered in the same way.
Bill of changes:
downstream conditionals accordingly
singleton Math entry (pointing to the cpp.name!)
One thing that is a little big about this change is a lot of kernels
which previously didn't report as "math" now report as math. I picked
a setting for these booleans that made sense to me, but I'm not sure
if e.g. XLA will handle it 100% correctly.
Signed-off-by: Edward Z. Yang ezyang@fb.com
Differential Revision: D24592391