Skip to content

Desugar missing dispatch field into singleton Math entry#46970

Closed
ezyang wants to merge 2 commits intogh/ezyang/857/basefrom
gh/ezyang/857/head
Closed

Desugar missing dispatch field into singleton Math entry#46970
ezyang wants to merge 2 commits intogh/ezyang/857/basefrom
gh/ezyang/857/head

Conversation

@ezyang
Copy link
Copy Markdown
Contributor

@ezyang ezyang commented Oct 28, 2020

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:

  • 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

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]
ezyang added a commit that referenced this pull request Oct 28, 2020
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
@ezyang ezyang requested review from ailzhang and bdhirsh October 28, 2020 05:23
@ezyang
Copy link
Copy Markdown
Contributor Author

ezyang commented Oct 28, 2020

@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Oct 28, 2020

💊 CI failures summary and remediations

As 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 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.


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.

See how this bot performed.

This comment has been revised 5 times.

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]
Comment thread tools/codegen/model.py
# them. In native_functions.yaml, the dispatch entry is optional; in that
# case, that is equivalent to having written:
#
# dispatch:
Copy link
Copy Markdown
Collaborator

@bdhirsh bdhirsh Oct 28, 2020

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah yeah, I didn't notice that some ops require it (since they have other more specific dispatch keys). Probably not worth it then

@ezyang
Copy link
Copy Markdown
Contributor Author

ezyang commented Oct 28, 2020

@ailzhang I would still love it if you can take a look, since this changes what meatdata we feed to xla.

Copy link
Copy Markdown
Contributor

@ailzhang ailzhang left a comment

Choose a reason for hiding this comment

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

Thanks!

Comment thread tools/codegen/gen.py
('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),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This field is only used for the old RegistrationDeclarations.h. Should be good to remove now. ;)

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@ezyang merged this pull request in 54d8329.

@facebook-github-bot facebook-github-bot deleted the gh/ezyang/857/head branch November 2, 2020 15:17
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
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
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