Skip to content

add explicit vectorization for Half dtype on CPU - #96076

Closed
mingfeima wants to merge 8 commits into
gh/mingfeima/104/basefrom
gh/mingfeima/104/head
Closed

mingfeima wants to merge 8 commits into
gh/mingfeima/104/basefrom
gh/mingfeima/104/head

Conversation

@mingfeima

@mingfeima mingfeima commented Mar 6, 2023 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

This patch is part of half float performance optimization on CPU:

  • add specification for dtype Half in Vectorized<> under both avx256 and avx512.
  • add specification for dtype Half in functional utils, e.g. vec::map_reduce<>(), which uses float32 as accumulate type.

Also add a helper struct vec_hold_type<scalar_t>, since Vectorized::value_type is pointing to its underlying storage type which is uint16_t, leading to error if the kernel uses Vec::value_type.

Half uses the same logic as BFloat16 in the Vectorized<>, each half vector is mapped to 2x float vectors for computation.

Notice that this patch modified the cmake files by adding -mf16c on AVX2 build, from https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html, we can see that all the hardware platforms that support avx2 already have f16c

cc @jgong5 @XiaobingSuper @sanchitintel @ashokei @jingxu10

@pytorch-bot

pytorch-bot Bot commented Mar 6, 2023 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 6f8ed09:
💚 Looks good so far! There are no failures yet. 💚

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

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@mingfeima mingfeima added the release notes: intel release notes category label Mar 9, 2023
cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@mingfeima
mingfeima marked this pull request as ready for review March 9, 2023 08:11
This patch is part of half float performance optimization on CPU:
* add specification for dtype `Half` in `Vectorized<>` under both avx256 and avx512.
* add specification for dtype `Half` in functional utils, e.g. `vec::map_reduce<>()`, which uses float32 as accumulate type.

Also add a helper struct `vec_hold_type<scalar_t>`, since Vectorized<Half>::value_type is pointing to its underlying storage type which is `uint16_t`, leading to error if the kernel uses `Vec::value_type`.

Half uses the same logic as BFloat16 in the Vectorized<>, each half vector is mapped to 2x float vectors for computation.

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]

@malfet malfet left a comment

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.

Overall looks fine, but please reply on how code duplication can be avoided. If this can't be templatized, perhaps using preprocessor will do.
Also, use C++14/C++17 language features when possible

Comment thread aten.bzl Resolved
Comment thread aten/src/ATen/cpu/vec/functional_base.h Resolved
This patch is part of half float performance optimization on CPU:
* add specification for dtype `Half` in `Vectorized<>` under both avx256 and avx512.
* add specification for dtype `Half` in functional utils, e.g. `vec::map_reduce<>()`, which uses float32 as accumulate type.

Also add a helper struct `vec_hold_type<scalar_t>`, since Vectorized<Half>::value_type is pointing to its underlying storage type which is `uint16_t`, leading to error if the kernel uses `Vec::value_type`.

Half uses the same logic as BFloat16 in the Vectorized<>, each half vector is mapped to 2x float vectors for computation.

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@mingfeima

mingfeima commented Mar 28, 2023 •

Copy link
Copy Markdown
Collaborator Author

@malfet @jgong5 since this patch modifies the cmake file (add f16c to AVX2 build), add some comments here: all the hardware platforms that have avx2 already have f16c support.

source info: https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html

platform vendor AVX FP16C AVX2 AVX512
ivybridge intel Yes Yes No No
haswell intel Yes Yes Yes No
broadwell intel Yes Yes Yes No
skylake intel Yes Yes Yes Yes
cannonlake intel Yes Yes Yes Yes
icelake intel Yes Yes Yes Yes
cascadelake intel Yes Yes Yes Yes
cooperlake intel Yes Yes Yes Yes
tigerlake intel Yes Yes Yes Yes
sapphirerapids intel Yes Yes Yes Yes
alderlake intel Yes Yes Yes No
rocketlake intel Yes Yes Yes Yes
graniterapids intel Yes Yes Yes Yes
bdver2,3,4 amd Yes Yes No No
znver1,2,3 amd Yes Yes Yes No
znver4 amd Yes Yes Yes Yes
btver2 amd Yes Yes No No

This patch is part of half float performance optimization on CPU:
* add specification for dtype `Half` in `Vectorized<>` under both avx256 and avx512.
* add specification for dtype `Half` in functional utils, e.g. `vec::map_reduce<>()`, which uses float32 as accumulate type.

Also add a helper struct `vec_hold_type<scalar_t>`, since Vectorized<Half>::value_type is pointing to its underlying storage type which is `uint16_t`, leading to error if the kernel uses `Vec::value_type`.

Half uses the same logic as BFloat16 in the Vectorized<>, each half vector is mapped to 2x float vectors for computation.

Notice that this patch modified the cmake files by adding **-mf16c** on AVX2 build, from https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html, we can see that all the hardware platforms that support **avx2** already have **f16c**

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
This patch is part of half float performance optimization on CPU:
* add specification for dtype `Half` in `Vectorized<>` under both avx256 and avx512.
* add specification for dtype `Half` in functional utils, e.g. `vec::map_reduce<>()`, which uses float32 as accumulate type.

Also add a helper struct `vec_hold_type<scalar_t>`, since Vectorized<Half>::value_type is pointing to its underlying storage type which is `uint16_t`, leading to error if the kernel uses `Vec::value_type`.

Half uses the same logic as BFloat16 in the Vectorized<>, each half vector is mapped to 2x float vectors for computation.

Notice that this patch modified the cmake files by adding **-mf16c** on AVX2 build, from https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html, we can see that all the hardware platforms that support **avx2** already have **f16c**

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@mingfeima mingfeima added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 30, 2023
@mingfeima mingfeima added the ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR label Mar 30, 2023
This patch is part of half float performance optimization on CPU:
* add specification for dtype `Half` in `Vectorized<>` under both avx256 and avx512.
* add specification for dtype `Half` in functional utils, e.g. `vec::map_reduce<>()`, which uses float32 as accumulate type.

Also add a helper struct `vec_hold_type<scalar_t>`, since Vectorized<Half>::value_type is pointing to its underlying storage type which is `uint16_t`, leading to error if the kernel uses `Vec::value_type`.

Half uses the same logic as BFloat16 in the Vectorized<>, each half vector is mapped to 2x float vectors for computation.

Notice that this patch modified the cmake files by adding **-mf16c** on AVX2 build, from https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html, we can see that all the hardware platforms that support **avx2** already have **f16c**

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
@mingfeima

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

pytorchmergebot pushed a commit that referenced this pull request Apr 10, 2023
…96079)

Use float32 as acc type for `min`, `max` and `minmax`, in the function ` vec::reduce_all`, float16 inputs will be accumulated in float32.

The performance benefit basically comes from the vectorization of `Half` #96076

Tested on Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz

**single socket**
```
(before)
### using OMP_NUM_THREADS=20
### using numactl --physcpubind=0-19 --membind=0
max: size: torch.Size([64, 128, 1024])  2.071 ms

(after)
### using OMP_NUM_THREADS=20
### using numactl --physcpubind=0-19 --membind=0
max: size: torch.Size([64, 128, 1024])  0.071 ms
```

**single core**
```
(before)
### using OMP_NUM_THREADS=1
### using numactl --physcpubind=0 --membind=0
max: size: torch.Size([64, 128, 1024])  33.488 ms

(after)
### using OMP_NUM_THREADS=1
### using numactl --physcpubind=0 --membind=0
max: size: torch.Size([64, 128, 1024])  0.953 ms
```

Pull Request resolved: #96079
Approved by: https://github.com/jgong5, https://github.com/kit1980
skotapati pushed a commit to kulinseth/pytorch that referenced this pull request Apr 10, 2023
…ytorch#96079)

Use float32 as acc type for `min`, `max` and `minmax`, in the function ` vec::reduce_all`, float16 inputs will be accumulated in float32.

The performance benefit basically comes from the vectorization of `Half` pytorch#96076

Tested on Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz

**single socket**
```
(before)
### using OMP_NUM_THREADS=20
### using numactl --physcpubind=0-19 --membind=0
max: size: torch.Size([64, 128, 1024])  2.071 ms

(after)
### using OMP_NUM_THREADS=20
### using numactl --physcpubind=0-19 --membind=0
max: size: torch.Size([64, 128, 1024])  0.071 ms
```

**single core**
```
(before)
### using OMP_NUM_THREADS=1
### using numactl --physcpubind=0 --membind=0
max: size: torch.Size([64, 128, 1024])  33.488 ms

(after)
### using OMP_NUM_THREADS=1
### using numactl --physcpubind=0 --membind=0
max: size: torch.Size([64, 128, 1024])  0.953 ms
```

Pull Request resolved: pytorch#96079
Approved by: https://github.com/jgong5, https://github.com/kit1980
@tbennun

tbennun commented Apr 25, 2023

Copy link
Copy Markdown
Contributor

@mingfeima Merging this PR breaks IBM POWER builds.

@facebook-github-bot
facebook-github-bot deleted the gh/mingfeima/104/head branch June 8, 2023 17:59
JohT added a commit to JohT/convolution-benchmarks that referenced this pull request Jan 6, 2024
This patch is part of half float performance optimization on CPU:
* add specification for dtype `Half` in `Vectorized<>` under both avx256 and avx512.
* add specification for dtype `Half` in functional utils, e.g. `vec::map_reduce<>()`, which uses float32 as accumulate type.

Also add a helper struct `vec_hold_type<scalar_t>`, since Vectorized<Half>::value_type is pointing to its underlying storage type which is `uint16_t`, leading to error if the kernel uses `Vec::value_type`.

Half uses the same logic as BFloat16 in the Vectorized<>, each half vector is mapped to 2x float vectors for computation.

Notice that this patch modified the cmake files by adding **-mf16c** on AVX2 build, from https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html, we can see that all the hardware platforms that support **avx2** already have **f16c**

Pull Request resolved: pytorch/pytorch#96076
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR 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 release notes: intel release notes category

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants