Skip to content
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

[Intel GPU] xpu-ops codegen via backend whitelist #127865

Closed
wants to merge 15 commits into from

Conversation

ZhiweiYan-96
Copy link
Collaborator

@ZhiweiYan-96 ZhiweiYan-96 commented Jun 4, 2024

Motivation

This PR intends to enhance the gen.py to allow generate codes for a specific backend.

XPU operators need be registered in an hand-written way currently. Developers have no chance to take the advantage of shared code to handle tensor meta setting (like strides, proxy output, structured kernels). Manually porting code is erro-prone and may lead to high maintaining efforts.

We intend to enhance the bckend_whitelist argument in gen.py to generate independent backend specific files. Then backend developers could directly use such generated code

Usage

The specialty of XPU ops is that they lie in third_pary/torch-xpu-ops, so it codegen resides in a separate process.

We use the following commands to trigger XPU codegen

python -m torchgen.gen --source-path path/to/yaml/of/xpu --install-dir build/aten/src/ATen/xpu --per-operator-headers --static-dispatch-backend --backend-whitelist=XPU

The diff lies at backend-whitelist=XPU. The backend-whitelist key is an existent argument in torchgen. But we observe that it can be hardly used for generating source files like Registexxx.cpp. We enhance the backend-white list argument in gen.py, where we could use it to generate backend only code. Like build/aten/src/ATen/xpu/RegisterXPU.cpp, build/aten/src/ATen/xpu/ops/as_strided_native.h

The input of gen.py are code templates and operators yaml. We share the same templates in aten. A simplified yaml lies in third_party/torch-xpu-ops, which only includes the supported xpu operators. This yaml is a copy-and-modify of native_functions.yaml. No extra entry is added, the format is same as the one in aten

Result

All operators headers are generated in build/aten/src/ATen/xpu/ops independently, which would not affect operators declared/defined by CPU/CUDA or any other backend. XPU operators only include headers in this folder.

Verification

  • We add a ut for verifying xpu codgen in test_codegen.py and it could be triggered through
    python -m tools.test.test_codegen -k BackendWhitelist

  • In third-party/torch-xpu-ops, we migrate some operators to structured kernels style, where they are registered through REGISTER_XPU_DISPATCH or TORCH_IMPL_FUNC. And it is tested with test_xpu.py

Others

As for the longer term, we would unify code generation system with other backend also, like cuda, mps.

Stack from ghstack (oldest at bottom):

cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @fegin @XilunWu @wanchaol @fduwjj @wz337 @tianyu-l @wconstab @chauhang @d4l3k @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @mcarilli @ptrblck @leslie-fang-intel @EikanWang @voznesenskym @penguinwu @Guobing-Chen @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @LucasLLC @MeetVadakkanchery @mhorowitz

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Jun 4, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/127865

Note: Links to docs will display an error until the docs builds have been completed.

❌ 39 Cancelled Jobs, 13 Unrelated Failures

As of commit b31d00f with merge base 3a18577 (image):

CANCELLED JOBS - The following jobs were cancelled. Please retry:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

ZhiweiYan-96 added a commit that referenced this pull request Jun 4, 2024
ghstack-source-id: 47b77bf426021ce6aa3c64c13c7d24ca38b13703
Pull Request resolved: #127865
@ZhiweiYan-96 ZhiweiYan-96 marked this pull request as draft June 4, 2024 07:02
[ghstack-poisoned]
ZhiweiYan-96 added a commit that referenced this pull request Jun 6, 2024
ghstack-source-id: d2f0b7ca5b8f2d386a12b605e5ad1d19ba24041a
Pull Request resolved: #127865
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
ZhiweiYan-96 added a commit that referenced this pull request Jun 6, 2024
ghstack-source-id: d9b26a516a9ba776b7639a21a6df10cfc71f74a9
Pull Request resolved: #127865
torchgen/gen.py Outdated Show resolved Hide resolved
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
ZhiweiYan-96 added a commit that referenced this pull request Jun 11, 2024
ghstack-source-id: 8c71248152e86debe227fcf2f964b90c3ff35bae
Pull Request resolved: #127865
Copy link
Collaborator

@EikanWang EikanWang left a comment

Choose a reason for hiding this comment

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

Please add UT.

torchgen/gen.py Outdated Show resolved Hide resolved
torchgen/dest/register_dispatch_key.py Outdated Show resolved Hide resolved
torchgen/dest/register_dispatch_key.py Outdated Show resolved Hide resolved
torchgen/gen.py Outdated Show resolved Hide resolved
torchgen/gen.py Outdated Show resolved Hide resolved
torchgen/gen.py Outdated Show resolved Hide resolved
torchgen/gen.py Outdated Show resolved Hide resolved
torchgen/gen.py Outdated Show resolved Hide resolved
torchgen/gen.py Outdated Show resolved Hide resolved
torchgen/gen.py Outdated Show resolved Hide resolved
[ghstack-poisoned]
ZhiweiYan-96 added a commit that referenced this pull request Jun 12, 2024
ghstack-source-id: 4fe1274ff777b79e4a9e0c164eb2bb1e3b19b89f
Pull Request resolved: #127865
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suppose the test cases should be part of test_codegen.py but not a dedicated test file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I have moved the ut into test_codegen.py

[ghstack-poisoned]
ZhiweiYan-96 added a commit that referenced this pull request Jun 13, 2024
ghstack-source-id: 680b8b6b496a854d61f24327f7c5e35d86555433
Pull Request resolved: #127865
@ZhiweiYan-96 ZhiweiYan-96 added the ciflow/xpu Run XPU CI tasks label Jun 14, 2024
@ZhiweiYan-96 ZhiweiYan-96 added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 14, 2024
[ghstack-poisoned]
ZhiweiYan-96 added a commit that referenced this pull request Jun 14, 2024
ghstack-source-id: 36beb6c4158282d776ab3246a969028618810405
Pull Request resolved: #127865
@EikanWang
Copy link
Collaborator

@ZhiweiYan-96 , please fix UT failures and we need to enable it ASAP.

@@ -0,0 +1,55 @@
# This yaml is used only for testing the XPU codegen functionality.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to add this yaml to the source code? I think it should be the data file of test folder. Why is it not be a part of test/xpu?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed, have moved into test/xpu, thanks for the comment

[ghstack-poisoned]
[ghstack-poisoned]
ZhiweiYan-96 added a commit that referenced this pull request Jun 17, 2024
ghstack-source-id: 9b4b4ec286689733ab3da6d6b72e5f24b8203897
Pull Request resolved: #127865
[ghstack-poisoned]
ZhiweiYan-96 added a commit that referenced this pull request Jun 19, 2024
ghstack-source-id: 8f93fe94e2b0964208b29a7734f91756b48c0823
Pull Request resolved: #127865
[ghstack-poisoned]
@pytorch-bot pytorch-bot bot added ciflow/inductor ciflow/mps Run MPS tests (subset of trunk) module: amp (automated mixed precision) autocast module: cpu CPU specific problem (e.g., perf, algorithm) module: distributed_checkpoint module: dynamo module: inductor NNC oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: quantization release notes category labels Jul 3, 2024
17336310621 pushed a commit to 17336310621/-PyTorch that referenced this pull request Aug 5, 2024
ghstack-source-id: f3d8f75998366b2efc771df01483c97718377a8a
Pull Request resolved: pytorch/pytorch#127865
@github-actions github-actions bot deleted the gh/ZhiweiYan-96/12/head branch August 10, 2024 01:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/inductor ciflow/mps Run MPS tests (subset of trunk) ciflow/trunk Trigger trunk jobs on your pull request ciflow/xpu Run XPU CI tasks module: amp (automated mixed precision) autocast module: cpu CPU specific problem (e.g., perf, algorithm) module: distributed_checkpoint module: dynamo module: inductor NNC oncall: distributed Add this issue/PR to distributed oncall triage queue open source release notes: quantization release notes category topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants