Skip to content

optimize sum kernel when input is Half on CPU - #96082

Closed
mingfeima wants to merge 12 commits into
gh/mingfeima/110/basefrom
gh/mingfeima/110/head
Closed

mingfeima wants to merge 12 commits into
gh/mingfeima/110/basefrom
gh/mingfeima/110/head

Conversation

@mingfeima

@mingfeima mingfeima commented Mar 6, 2023 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Originally sum has a specialized path for BFloat16, this patch expands that path to Half as well. Without this path, Half will also be accumulated in Float, but in a slower non-vectorized path in load_reduce_vec.

Performance result on Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz: previously Half is much slower than BFloat16, now they are of the same level.

Performance before

### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.879 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.363 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 13.875 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 43.573 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.743 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 208.055 ms

Performance after

### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.817 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.380 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 2.361 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 42.986 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.766 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 27.753 ms

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/96082

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

❌ 1 Failures

As of commit 459accf:

NEW FAILURES - The following jobs have failed:

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

…n CPU"

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
mingfeima added a commit that referenced this pull request Mar 7, 2023
…n CPU"

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
…n CPU"

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
mingfeima added a commit that referenced this pull request Mar 9, 2023
…n CPU"

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
…n CPU"

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
mingfeima added a commit that referenced this pull request Mar 10, 2023
…n CPU"

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
mingfeima added a commit that referenced this pull request Mar 14, 2023
@mingfeima
mingfeima marked this pull request as ready for review March 14, 2023 02:28
@mingfeima mingfeima added release notes: intel release notes category module: half Related to float16 half-precision floats labels Mar 14, 2023
@mingfeima mingfeima changed the title use float32 as acc type in sum kernel when input is Half on CPU optimize sum kernel when input is Half on CPU Mar 14, 2023
Originally `sum` has a specialized path for `BFloat16`, this patch expands that path to `Half` as well. Without this path, `Half` will also be accumulated in `Float`, but in a slower non-vectorized path in `load_reduce_vec`.

Performance result on Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz: previously `Half` is much slower than `BFloat16`, now they are of the same level.

### Performance before
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.879 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.363 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 13.875 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 43.573 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.743 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 208.055 ms
```

### Performance after
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.817 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.380 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 2.361 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 42.986 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.766 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 27.753 ms
```

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
Originally `sum` has a specialized path for `BFloat16`, this patch expands that path to `Half` as well. Without this path, `Half` will also be accumulated in `Float`, but in a slower non-vectorized path in `load_reduce_vec`.

Performance result on Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz: previously `Half` is much slower than `BFloat16`, now they are of the same level.

### Performance before
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.879 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.363 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 13.875 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 43.573 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.743 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 208.055 ms
```

### Performance after
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.817 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.380 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 2.361 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 42.986 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.766 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 27.753 ms
```

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
mingfeima added a commit that referenced this pull request Mar 28, 2023
Originally `sum` has a specialized path for `BFloat16`, this patch expands that path to `Half` as well. Without this path, `Half` will also be accumulated in `Float`, but in a slower non-vectorized path in `load_reduce_vec`.

Performance result on Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz: previously `Half` is much slower than `BFloat16`, now they are of the same level.

### Performance before
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.879 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.363 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 13.875 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 43.573 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.743 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 208.055 ms
```

### Performance after
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.817 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.380 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 2.361 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 42.986 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.766 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 27.753 ms
```

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
mingfeima added a commit that referenced this pull request Mar 30, 2023
Originally `sum` has a specialized path for `BFloat16`, this patch expands that path to `Half` as well. Without this path, `Half` will also be accumulated in `Float`, but in a slower non-vectorized path in `load_reduce_vec`.

Performance result on Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz: previously `Half` is much slower than `BFloat16`, now they are of the same level.

### Performance before
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.879 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.363 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 13.875 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 43.573 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.743 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 208.055 ms
```

### Performance after
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.817 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.380 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 2.361 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 42.986 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.766 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 27.753 ms
```

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
mingfeima added a commit that referenced this pull request Apr 2, 2023
Originally `sum` has a specialized path for `BFloat16`, this patch expands that path to `Half` as well. Without this path, `Half` will also be accumulated in `Float`, but in a slower non-vectorized path in `load_reduce_vec`.

Performance result on Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz: previously `Half` is much slower than `BFloat16`, now they are of the same level.

### Performance before
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.879 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.363 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 13.875 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 43.573 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.743 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 208.055 ms
```

### Performance after
```
### using single socket of 20 cores
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 4.817 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 2.380 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 2.361 ms

### using single core
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float32 ; time per iter: 42.986 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.bfloat16 ; time per iter: 27.766 ms
Input shape:  torch.Size([128, 1024, 1024]) ; dtype:  torch.float16 ; time per iter: 27.753 ms
```

cc jgong5 XiaobingSuper sanchitintel ashokei jingxu10

[ghstack-poisoned]
mingfeima added a commit that referenced this pull request Apr 8, 2023
@mingfeima mingfeima added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 10, 2023
@github-actions

github-actions Bot commented Jun 9, 2023

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Jun 9, 2023
@github-actions github-actions Bot closed this Jul 9, 2023
@facebook-github-bot
facebook-github-bot deleted the gh/mingfeima/110/head branch August 8, 2023 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request 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 Stale

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants