Skip to content

[AMD] Use optimized tiled kernel for LayerNorm gamma beta backward - #179019

Closed
echen4096 wants to merge 1 commit into
pytorch:mainfrom
echen4096:export-D98176771
Closed

echen4096 wants to merge 1 commit into
pytorch:mainfrom
echen4096:export-D98176771

Conversation

@echen4096

Copy link
Copy Markdown
Contributor

Summary: Adopt the GammaBetaBackwardCUDAKernelTemplate tiled kernel introduced in D72575858 for ROCm, replacing the legacy two-pass cuComputePartGradGammaBeta and cuComputeGradGammaBeta approach. This brings AMD towards Nvidia parity.

Test Plan:
Results:

  • Local Benchmark:
    • Higher performance across 39 shapes: 1.02x speedup on mi350 (1 pass vs 2 pass implementation)
    • Higher numerical accuracy on mi350 vs h100
      • Avg rel error vs CPU fp32 (mi350): dgamma=3.040359e-06 dbeta=1.750184e-06
      • Avg rel error vs CPU fp32 (h100): dgamma=3.506308e-06 dbeta=1.792510e-06
  • E2E Test
    • 1.02x speedup in qps
      • baseline: 765k
      • with diff: 782k
Benchmarks:
buck2 run //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label mi350 --compare h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True  \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_2pass
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_1pass --compare mi350x_2pass

E2E tests:
Baseline: 765k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_131047-d9f3209936?job_attempt=0&version=0&tab=tb_legacy
With diff: 782k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_141250-d9120ce4bb?job_attempt=1&version=0&tab=tb_legacy

Reviewed By: jhadidjojo

Differential Revision: D98176771

Summary: Adopt the GammaBetaBackwardCUDAKernelTemplate tiled kernel introduced in D72575858 for ROCm, replacing the legacy two-pass cuComputePartGradGammaBeta and cuComputeGradGammaBeta approach. This brings AMD towards Nvidia parity.

Test Plan:
Results:
- Local Benchmark:
  - Higher performance across 39 shapes: 1.02x speedup on mi350 (1 pass vs 2 pass implementation)
  - Higher numerical accuracy on mi350 vs h100
    - Avg rel error vs CPU fp32 (mi350): dgamma=3.040359e-06  dbeta=1.750184e-06
     - Avg rel error vs CPU fp32 (h100): dgamma=3.506308e-06  dbeta=1.792510e-06
- E2E Test
  - 1.02x speedup in qps
    - baseline: 765k
    - with diff: 782k


```
Benchmarks:
buck2 run //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label mi350 --compare h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True  \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_2pass
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_1pass --compare mi350x_2pass
```

E2E tests:
Baseline: 765k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_131047-d9f3209936?job_attempt=0&version=0&tab=tb_legacy
With diff: 782k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_141250-d9120ce4bb?job_attempt=1&version=0&tab=tb_legacy

Reviewed By: jhadidjojo

Differential Revision: D98176771
@pytorch-bot

pytorch-bot Bot commented Apr 1, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 1a6fa9c with merge base a8e6433 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added the release notes: cuda release notes category label Apr 1, 2026
@meta-codesync

meta-codesync Bot commented Apr 1, 2026

Copy link
Copy Markdown

@echen4096 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D98176771.

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 1, 2026
@@ -1010,7 +1014,13 @@ void LaunchGammaBetaBackwardCUDAKernel(
} else if (M < 256) {
ConfigureAndLaunchGammaBetaBackwardKernel<T, T_ACC, block_dim_x, 16, 128, rms_norm>(dY_data, X_data, mean_data, rstd_data, M, N, dgamma, dbeta, cuda_stream);

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.

Should we apply specific changes for ROCm here as well or is this universally perfect?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The intermediate branches are safe for ROCm as-is since all thread counts stay within GPU limits. That said, there may be tuning opportunities for ROCm - may be worth revisiting in a future PR

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.

Sounds great!

@anatoliylitv anatoliylitv Jul 6, 2026 •

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.

Hello, @echen4096. @benediktjohannes. Could you please help me with Local Benchmark and E2E tests since they behind wall. I think this is time for tuning opportunities for ROCm since this pr causing some perf drops in huggingface models:
image

@facebook-github-tools

Copy link
Copy Markdown

@pytorchbot merge -i

(Initiating merge automatically since Phabricator Diff has merged, merging with -i because oss signals were bypassed internally)

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 0 checks:

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

weifengpy pushed a commit that referenced this pull request Apr 7, 2026
…179019)

Summary: Adopt the GammaBetaBackwardCUDAKernelTemplate tiled kernel introduced in D72575858 for ROCm, replacing the legacy two-pass cuComputePartGradGammaBeta and cuComputeGradGammaBeta approach. This brings AMD towards Nvidia parity.

Test Plan:
Results:
- Local Benchmark:
  - Higher performance across 39 shapes: 1.02x speedup on mi350 (1 pass vs 2 pass implementation)
  - Higher numerical accuracy on mi350 vs h100
    - Avg rel error vs CPU fp32 (mi350): dgamma=3.040359e-06  dbeta=1.750184e-06
     - Avg rel error vs CPU fp32 (h100): dgamma=3.506308e-06  dbeta=1.792510e-06
- E2E Test
  - 1.02x speedup in qps
    - baseline: 765k
    - with diff: 782k

```
Benchmarks:
buck2 run //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label mi350 --compare h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True  \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_2pass
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_1pass --compare mi350x_2pass
```

E2E tests:
Baseline: 765k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_131047-d9f3209936?job_attempt=0&version=0&tab=tb_legacy
With diff: 782k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_141250-d9120ce4bb?job_attempt=1&version=0&tab=tb_legacy

Reviewed By: jhadidjojo

Differential Revision: D98176771

Pull Request resolved: #179019
Approved by: https://github.com/haoyuz
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
…ytorch#179019)

Summary: Adopt the GammaBetaBackwardCUDAKernelTemplate tiled kernel introduced in D72575858 for ROCm, replacing the legacy two-pass cuComputePartGradGammaBeta and cuComputeGradGammaBeta approach. This brings AMD towards Nvidia parity.

Test Plan:
Results:
- Local Benchmark:
  - Higher performance across 39 shapes: 1.02x speedup on mi350 (1 pass vs 2 pass implementation)
  - Higher numerical accuracy on mi350 vs h100
    - Avg rel error vs CPU fp32 (mi350): dgamma=3.040359e-06  dbeta=1.750184e-06
     - Avg rel error vs CPU fp32 (h100): dgamma=3.506308e-06  dbeta=1.792510e-06
- E2E Test
  - 1.02x speedup in qps
    - baseline: 765k
    - with diff: 782k

```
Benchmarks:
buck2 run //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label mi350 --compare h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True  \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_2pass
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_1pass --compare mi350x_2pass
```

E2E tests:
Baseline: 765k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_131047-d9f3209936?job_attempt=0&version=0&tab=tb_legacy
With diff: 782k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_141250-d9120ce4bb?job_attempt=1&version=0&tab=tb_legacy

Reviewed By: jhadidjojo

Differential Revision: D98176771

Pull Request resolved: pytorch#179019
Approved by: https://github.com/haoyuz
pytorch-bot Bot pushed a commit that referenced this pull request Apr 10, 2026
…179019)

Summary: Adopt the GammaBetaBackwardCUDAKernelTemplate tiled kernel introduced in D72575858 for ROCm, replacing the legacy two-pass cuComputePartGradGammaBeta and cuComputeGradGammaBeta approach. This brings AMD towards Nvidia parity.

Test Plan:
Results:
- Local Benchmark:
  - Higher performance across 39 shapes: 1.02x speedup on mi350 (1 pass vs 2 pass implementation)
  - Higher numerical accuracy on mi350 vs h100
    - Avg rel error vs CPU fp32 (mi350): dgamma=3.040359e-06  dbeta=1.750184e-06
     - Avg rel error vs CPU fp32 (h100): dgamma=3.506308e-06  dbeta=1.792510e-06
- E2E Test
  - 1.02x speedup in qps
    - baseline: 765k
    - with diff: 782k

```
Benchmarks:
buck2 run //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label mi350 --compare h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True  \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_2pass
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_1pass --compare mi350x_2pass
```

E2E tests:
Baseline: 765k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_131047-d9f3209936?job_attempt=0&version=0&tab=tb_legacy
With diff: 782k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_141250-d9120ce4bb?job_attempt=1&version=0&tab=tb_legacy

Reviewed By: jhadidjojo

Differential Revision: D98176771

Pull Request resolved: #179019
Approved by: https://github.com/haoyuz
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…ytorch#179019)

Summary: Adopt the GammaBetaBackwardCUDAKernelTemplate tiled kernel introduced in D72575858 for ROCm, replacing the legacy two-pass cuComputePartGradGammaBeta and cuComputeGradGammaBeta approach. This brings AMD towards Nvidia parity.

Test Plan:
Results:
- Local Benchmark:
  - Higher performance across 39 shapes: 1.02x speedup on mi350 (1 pass vs 2 pass implementation)
  - Higher numerical accuracy on mi350 vs h100
    - Avg rel error vs CPU fp32 (mi350): dgamma=3.040359e-06  dbeta=1.750184e-06
     - Avg rel error vs CPU fp32 (h100): dgamma=3.506308e-06  dbeta=1.792510e-06
- E2E Test
  - 1.02x speedup in qps
    - baseline: 765k
    - with diff: 782k

```
Benchmarks:
buck2 run //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label mi350 --compare h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True  \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_2pass
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_1pass --compare mi350x_2pass
```

E2E tests:
Baseline: 765k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_131047-d9f3209936?job_attempt=0&version=0&tab=tb_legacy
With diff: 782k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_141250-d9120ce4bb?job_attempt=1&version=0&tab=tb_legacy

Reviewed By: jhadidjojo

Differential Revision: D98176771

Pull Request resolved: pytorch#179019
Approved by: https://github.com/haoyuz
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ytorch#179019)

Summary: Adopt the GammaBetaBackwardCUDAKernelTemplate tiled kernel introduced in D72575858 for ROCm, replacing the legacy two-pass cuComputePartGradGammaBeta and cuComputeGradGammaBeta approach. This brings AMD towards Nvidia parity.

Test Plan:
Results:
- Local Benchmark:
  - Higher performance across 39 shapes: 1.02x speedup on mi350 (1 pass vs 2 pass implementation)
  - Higher numerical accuracy on mi350 vs h100
    - Avg rel error vs CPU fp32 (mi350): dgamma=3.040359e-06  dbeta=1.750184e-06
     - Avg rel error vs CPU fp32 (h100): dgamma=3.506308e-06  dbeta=1.792510e-06
- E2E Test
  - 1.02x speedup in qps
    - baseline: 765k
    - with diff: 782k

```
Benchmarks:
buck2 run //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --correctness --label mi350 --compare h100
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True  \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_2pass
HIP_VISIBLE_DEVICES=2 buck2 run @//mode/opt-amd-gpu -m rocm70 -m ovr_config//triton:beta -m rccl_dev -m fbcode//triton/cc:force_noop -c fbcode.split-dwarf=True \
    //scripts/echen40/amd/benchmark/layer_norm:layer_norm_backward_benchmark -- \
    --performance --label mi350x_1pass --compare mi350x_2pass
```

E2E tests:
Baseline: 765k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_131047-d9f3209936?job_attempt=0&version=0&tab=tb_legacy
With diff: 782k qps
https://www.internalfb.com/mlhub/pipelines/runs/mast/aps-cmf_amd_echen40_20260320_141250-d9120ce4bb?job_attempt=1&version=0&tab=tb_legacy

Reviewed By: jhadidjojo

Differential Revision: D98176771

Pull Request resolved: pytorch#179019
Approved by: https://github.com/haoyuz
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 fb-exported Merged meta-exported release notes: cuda release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants