Skip to content

Add outer-product-decomo - #176552

Closed
drisspg wants to merge 3 commits into
gh/drisspg/293/basefrom
gh/drisspg/293/head
Closed

drisspg wants to merge 3 commits into
gh/drisspg/293/basefrom
gh/drisspg/293/head

Conversation

@drisspg

@drisspg drisspg commented Mar 5, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary

cublas sucks at fp32 outer reduction,

One commit before this PR:
Eager Perf:
image

Compiled Perf:
image

You can see grad_routed is no bueno. I wrote a small triton kernel that does a pretty naive tiled product and got near roofline.

If we add this decomp:

image

Reported: pytorch/torchtitan#2225

Eager:
image

Compile
image

cc @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo @mlazos

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Mar 5, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ 2 Pending, 5 Unrelated Failures

As of commit e375e41 with merge base 2bf9338 (image):

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

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 is marked as unstable, possibly due to flakiness on trunk:

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

drisspg added a commit that referenced this pull request Mar 5, 2026
ghstack-source-id: 62038e6
Pull-Request: #176552
@pytorch-bot

pytorch-bot Bot commented Mar 5, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@drisspg drisspg changed the title Add outer-product-decom Add outer-product-decomo Mar 5, 2026
@drisspg
drisspg requested a review from eellison March 5, 2026 02:06
@drisspg drisspg added the module: cuda Related to torch.cuda, and CUDA support in general label Mar 5, 2026
[ghstack-poisoned]
drisspg added a commit that referenced this pull request Mar 5, 2026
ghstack-source-id: 567bf30
Pull-Request: #176552
@drisspg

drisspg commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 5, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@pytorch-bot

pytorch-bot Bot commented Mar 5, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@hirschsn

hirschsn commented Mar 5, 2026

Copy link
Copy Markdown

@drisspg That was quick, thanks for this! 👍

Comment thread torch/_inductor/decomposition.py Resolved
Comment thread torch/_inductor/decomposition.py Resolved
## Summary

cublas sucks at fp32 outer reduction, 

One commit before this PR: 
Eager Perf: 
<img width="524" height="386" alt="image" src="https://github.com/user-attachments/assets/ad349a62-5fce-4bb9-8d87-3b088077d1e5" />

Compiled Perf:
<img width="497" height="392" alt="image" src="https://github.com/user-attachments/assets/1f7f436d-7750-4b65-ad0d-c2c627edef0c" />

You can see grad_routed is no bueno. I wrote a small triton kernel that does a pretty naive tiled product and got near roofline. 

If we add this decomp:

<img width="538" height="405" alt="image" src="https://github.com/user-attachments/assets/cd54591c-706d-480e-b951-df89bbb388b3" />


Reported: pytorch/torchtitan#2225



Eager: 
<img width="995" height="231" alt="image" src="https://github.com/user-attachments/assets/f1d33908-3372-46ad-a854-ca7af736be2e" />


Compile
<img width="780" height="250" alt="image" src="https://github.com/user-attachments/assets/8c8881f9-b19a-40f3-91f2-d353e3d80071" />



cc ptrblck msaroufim eqy jerryzh168 tinglvv nWEIdia voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo

[ghstack-poisoned]
drisspg added a commit that referenced this pull request Mar 5, 2026
ghstack-source-id: a57a742
Pull-Request: #176552
@drisspg

drisspg commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@pytorch-bot

pytorch-bot Bot commented Mar 5, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@drisspg

drisspg commented Mar 5, 2026

Copy link
Copy Markdown
Contributor 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

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: HTTP Error 502: Bad Gateway

Details for Dev Infra team Raised by workflow job

@drisspg

drisspg commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
## Summary

cublas sucks at fp32 outer reduction,

One commit before this PR:
Eager Perf:
<img width="524" height="386" alt="image" src="https://github.com/user-attachments/assets/ad349a62-5fce-4bb9-8d87-3b088077d1e5" />

Compiled Perf:
<img width="497" height="392" alt="image" src="https://github.com/user-attachments/assets/1f7f436d-7750-4b65-ad0d-c2c627edef0c" />

You can see grad_routed is no bueno. I wrote a small triton kernel that does a pretty naive tiled product and got near roofline.

If we add this decomp:

<img width="538" height="405" alt="image" src="https://github.com/user-attachments/assets/cd54591c-706d-480e-b951-df89bbb388b3" />

Reported: pytorch/torchtitan#2225

Eager:
<img width="995" height="231" alt="image" src="https://github.com/user-attachments/assets/f1d33908-3372-46ad-a854-ca7af736be2e" />

Compile
<img width="780" height="250" alt="image" src="https://github.com/user-attachments/assets/8c8881f9-b19a-40f3-91f2-d353e3d80071" />

Pull Request resolved: pytorch#176552
Approved by: https://github.com/ngimel, https://github.com/eellison
@github-actions
github-actions Bot deleted the gh/drisspg/293/head branch April 5, 2026 02:27
acisseJZhong added a commit to pytorch/torchtitan that referenced this pull request Apr 6, 2026
### Change
scatter_add was replaced by torch.bmm in
#1974 due to its non
determinism. However, bmm backward kernel was reported to be slow in
#2225. Although @drisspg has
pytorch/pytorch#176552 which greatly optimized
the bmm backward time under compile, eager mode backward is still slow.

Therefore we try to switch back to scatter_add with
`torch.use_deterministic_algorithms(True)`, see more in
https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms.
We tested out the determinism part using [this test script.
](https://gist.github.com/acisseJZhong/c56dbc331d3248e488edb24894dc4565),
updated
[test](https://gist.github.com/acisseJZhong/562b635e6ef96d4f2ca68769dbdbad82)

### Test with topk=2
llama4 debug model(topk=2), qwen3 debug model(topk=8)
```
NCCL_NVLS_ENABLE=0 NGPU=8 ./run_train.sh     --module llama4     --config llama4_debugmodel     --parallelism.pipeline_parallel_degree 1     --parallelism.data_parallel_shard_degree 4     --parallelism.tensor_parallel_degree 2     --parallelism.expert_parallel_degree 2     --parallelism.expert_tensor_parallel_degree 2      --metrics.enable_wandb --training.steps 100
```
<img width="1075" height="288" alt="image"
src="https://github.com/user-attachments/assets/81e7523b-e833-4351-ad57-f5e1276807f3"
/>

### Loss compare

```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --test-ngpus=4 --assert-equal --no-seed-checkpoint
```
Testing with llama4 debug model, scatter_add with torch deterministic
mode on seems still slightly faster than torch.bmm.

cc @garrett361 @chelsea0x3b @rakkit @drisspg
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
### Change
scatter_add was replaced by torch.bmm in
pytorch#1974 due to its non
determinism. However, bmm backward kernel was reported to be slow in
pytorch#2225. Although @drisspg has
pytorch/pytorch#176552 which greatly optimized
the bmm backward time under compile, eager mode backward is still slow.

Therefore we try to switch back to scatter_add with
`torch.use_deterministic_algorithms(True)`, see more in
https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms.
We tested out the determinism part using [this test script.
](https://gist.github.com/acisseJZhong/c56dbc331d3248e488edb24894dc4565),
updated
[test](https://gist.github.com/acisseJZhong/562b635e6ef96d4f2ca68769dbdbad82)

### Test with topk=2
llama4 debug model(topk=2), qwen3 debug model(topk=8)
```
NCCL_NVLS_ENABLE=0 NGPU=8 ./run_train.sh     --module llama4     --config llama4_debugmodel     --parallelism.pipeline_parallel_degree 1     --parallelism.data_parallel_shard_degree 4     --parallelism.tensor_parallel_degree 2     --parallelism.expert_parallel_degree 2     --parallelism.expert_tensor_parallel_degree 2      --metrics.enable_wandb --training.steps 100
```
<img width="1075" height="288" alt="image"
src="https://github.com/user-attachments/assets/81e7523b-e833-4351-ad57-f5e1276807f3"
/>

### Loss compare

```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --test-ngpus=4 --assert-equal --no-seed-checkpoint
```
Testing with llama4 debug model, scatter_add with torch deterministic
mode on seems still slightly faster than torch.bmm.

cc @garrett361 @chelsea0x3b @rakkit @drisspg
jemitche1 pushed a commit to jemitche1/torchtitan that referenced this pull request Apr 14, 2026
### Change
scatter_add was replaced by torch.bmm in
pytorch#1974 due to its non
determinism. However, bmm backward kernel was reported to be slow in
pytorch#2225. Although @drisspg has
pytorch/pytorch#176552 which greatly optimized
the bmm backward time under compile, eager mode backward is still slow.

Therefore we try to switch back to scatter_add with
`torch.use_deterministic_algorithms(True)`, see more in
https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms.
We tested out the determinism part using [this test script.
](https://gist.github.com/acisseJZhong/c56dbc331d3248e488edb24894dc4565),
updated
[test](https://gist.github.com/acisseJZhong/562b635e6ef96d4f2ca68769dbdbad82)

### Test with topk=2
llama4 debug model(topk=2), qwen3 debug model(topk=8)
```
NCCL_NVLS_ENABLE=0 NGPU=8 ./run_train.sh     --module llama4     --config llama4_debugmodel     --parallelism.pipeline_parallel_degree 1     --parallelism.data_parallel_shard_degree 4     --parallelism.tensor_parallel_degree 2     --parallelism.expert_parallel_degree 2     --parallelism.expert_tensor_parallel_degree 2      --metrics.enable_wandb --training.steps 100
```
<img width="1075" height="288" alt="image"
src="https://github.com/user-attachments/assets/81e7523b-e833-4351-ad57-f5e1276807f3"
/>

### Loss compare

```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --test-ngpus=4 --assert-equal --no-seed-checkpoint
```
Testing with llama4 debug model, scatter_add with torch deterministic
mode on seems still slightly faster than torch.bmm.

cc @garrett361 @chelsea0x3b @rakkit @drisspg
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
### Change
scatter_add was replaced by torch.bmm in
pytorch#1974 due to its non
determinism. However, bmm backward kernel was reported to be slow in
pytorch#2225. Although @drisspg has
pytorch/pytorch#176552 which greatly optimized
the bmm backward time under compile, eager mode backward is still slow.

Therefore we try to switch back to scatter_add with
`torch.use_deterministic_algorithms(True)`, see more in
https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms.
We tested out the determinism part using [this test script.
](https://gist.github.com/acisseJZhong/c56dbc331d3248e488edb24894dc4565),
updated
[test](https://gist.github.com/acisseJZhong/562b635e6ef96d4f2ca68769dbdbad82)

### Test with topk=2
llama4 debug model(topk=2), qwen3 debug model(topk=8)
```
NCCL_NVLS_ENABLE=0 NGPU=8 ./run_train.sh     --module llama4     --config llama4_debugmodel     --parallelism.pipeline_parallel_degree 1     --parallelism.data_parallel_shard_degree 4     --parallelism.tensor_parallel_degree 2     --parallelism.expert_parallel_degree 2     --parallelism.expert_tensor_parallel_degree 2      --metrics.enable_wandb --training.steps 100
```
<img width="1075" height="288" alt="image"
src="https://github.com/user-attachments/assets/81e7523b-e833-4351-ad57-f5e1276807f3"
/>

### Loss compare

```
NCCL_NVLS_ENABLE=0 python scripts/loss_compare.py . main --baseline-module='llama4' --baseline-config='llama4_debugmodel' --baseline-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --baseline-ngpus=4 --test-module='llama4' --test-config='llama4_debugmodel' --test-options="--parallelism.pipeline_parallel_degree 1 --parallelism.data_parallel_shard_degree 2 --parallelism.tensor_parallel_degree 2 --parallelism.expert_parallel_degree 4 --parallelism.expert_tensor_parallel_degree 1" --test-ngpus=4 --assert-equal --no-seed-checkpoint
```
Testing with llama4 debug model, scatter_add with torch deterministic
mode on seems still slightly faster than torch.bmm.

cc @garrett361 @chelsea0x3b @rakkit @drisspg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged module: cuda Related to torch.cuda, and CUDA support in general module: inductor release notes: inductor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants