Skip to content

add fp16 support for mkldnn conv and deconv on CPU - #99496

Closed
CaoE wants to merge 33 commits into
gh/CaoE/17/basefrom
gh/CaoE/17/head
Closed

CaoE wants to merge 33 commits into
gh/CaoE/17/basefrom
gh/CaoE/17/head

Conversation

@CaoE

@CaoE CaoE commented Apr 19, 2023 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

The PR is part of #97068, which is to add fp16 support for mkldnn conv and mkldnn deconv to leverage avx_ne_convert, avx512-fp16, and amx-fp16 via the oneDNN library.

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10

@pytorch-bot

pytorch-bot Bot commented Apr 19, 2023 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (9 Unrelated Failures)

As of commit 3680182 with merge base 2c1554a (image):

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

UNSTABLE - The following job failed but was likely due to flakiness present on trunk and has been marked as unstable:

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

@github-actions github-actions Bot added the module: cpu CPU specific problem (e.g., perf, algorithm) label Apr 19, 2023
@CaoE
CaoE marked this pull request as draft April 19, 2023 01:36
Comment thread aten/src/ATen/native/mkldnn/Utils.h Outdated
"mkldnn_convolution_transpose: bf16 path needs the cpu support avx512bw, avx512vl and avx512dq");
} else if (input_t == ScalarType::Half) {
TORCH_CHECK(mkldnn_fp16_device_check(),
"mkldnn_convolution_transpose: fp16 path needs the cpu support avx512fp16");

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.

avx512fp16 doesn't have dot-product support. Are we sure that onednn has efficient conv/gemm support based on that?

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.

we need some benchmark number here.

@CaoE CaoE Apr 24, 2023 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

avx512fp16 doesn't have dot-product support. Are we sure that onednn has efficient conv/gemm support based on that?

Sorry for slow reply. I'm occupied by a urgent task. The performance should be worse than fp32 for computation bound ops on avx512fp16 platforms since there is internal fp16<->fp32 overhead. Avx512_fp16 has compute instructions (fma, etc) , but those accumulate to f16, not f32. So unfortunately oneDNN cant use avx512_f16 compute instructions, only convert instructions are used. For the performance between naïve fp16 impl of PT and onednn fp16 impl (avx512fp16), hard to say, it just depends on who optimized it well.

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.

So, if this is only for the reference implementation, not necessarily we would rely on avx512fp16, right? Also, with avx512fp16, there might be accuracy concern since the accumulation is with f16. We shouldn't turn it on by default, right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think we would rely on avx512_fp16 since OneDNN requires the cpu has avx512_fp16 at least. Onednn does not use avx512_f16 to compute. Only convert instructions are used to prevent accuracy issue.

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.

But why has it to depend on avx512_fp16 if only conversion instructions are used for ref implementation? Can f16c be used instead?

@CaoE CaoE May 22, 2023 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Currently oneDNN has no interest in supporting fp16 for anything earlier platforms than SPR so it requires the cpu has avx512_fp16 at least. oneDNN fp16 implements have much better performance compared to naive fp16 implements of PyTorch (shapes collect from RN50, not adequate testing) #99497 (comment) . It benefits to call oneDNN with avx512_fp16. We can add some conditions to avoid some slow cases of oneDNN in the future, and ask onednn to improve the performance for such cases.

@mingfeima mingfeima added the module: half Related to float16 half-precision floats label Apr 21, 2023
@CaoE CaoE added the topic: not user facing topic category label Apr 24, 2023
CaoE added 2 commits April 24, 2023 02:55
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
CaoE added a commit to CaoE/pytorch that referenced this pull request May 4, 2023
CaoE added 2 commits May 4, 2023 20:47
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
CaoE added a commit to CaoE/pytorch that referenced this pull request May 16, 2023
@CaoE CaoE added ciflow/mps Run MPS tests (subset of trunk) ciflow/trunk Trigger trunk jobs on your pull request ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR labels May 18, 2023
CaoE added 5 commits May 17, 2023 23:15
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@CaoE CaoE changed the title add fp16 support for mkldnn conv on CPU add fp16 support for mkldnn conv and deconv on CPU May 22, 2023
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
CaoE added a commit to CaoE/pytorch that referenced this pull request May 24, 2023
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
CaoE added a commit to CaoE/pytorch that referenced this pull request Aug 31, 2023
CaoE added 2 commits August 31, 2023 01:59
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@CaoE
CaoE marked this pull request as ready for review September 6, 2023 06:12
@CaoE
CaoE marked this pull request as draft September 6, 2023 06:29
@CaoE
CaoE marked this pull request as ready for review September 7, 2023 02:11
The PR is part of #97068, which is to add fp16 support for mkldnn conv and mkldnn deconv to leverage avx512-fp16 or amx-fp16 via the oneDNN library.

cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@CaoE

CaoE commented Sep 15, 2023

Copy link
Copy Markdown
Collaborator Author

@cpuhrsch Could you please review this PR ? Thanks.

Comment thread test/test_mkldnn.py
self._test_conv_lower_precision_base(dim=2, dtype=torch.bfloat16)
self._test_conv_lower_precision_base(dim=2, dtype=torch.half)

def test_conv3d_lower_precision(self):

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.

nit: If you make these dtypes arguments to the tests and use a dtype decorator it'll be easier to see if one of them fails. Otherwise you'll get one failure for either one of the dtypes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the comments. I will improve such test cases in #99497 since it also involves these tests.

@cpuhrsch cpuhrsch left a comment

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.

Generally looks good, just see one nit around using dtype + decorator to create separate, individual tests per dtype.

The PR is part of #97068, which is to add fp16 support for mkldnn conv and mkldnn deconv to leverage  avx_ne_convert, avx512-fp16, and amx-fp16 via the oneDNN library.

cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@CaoE

CaoE commented Sep 18, 2023

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

The PR is part of #97068, which is to add fp16 support for mkldnn conv and mkldnn deconv to leverage  avx_ne_convert, avx512-fp16, and amx-fp16 via the oneDNN library.

cc jgong5 mingfeima XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@CaoE

CaoE commented Sep 19, 2023

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@facebook-github-bot
facebook-github-bot deleted the gh/CaoE/17/head branch September 22, 2023 14:24
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/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR ciflow/slow ciflow/trunk Trigger trunk jobs on your pull request Merged module: cpu CPU specific problem (e.g., perf, algorithm) module: half Related to float16 half-precision floats open source topic: not user facing topic category

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants