Skip to content

[ROCm] Optimize AMD normalization backward kernel by using tiled and two pass implementations where they are most efficient. - #189405

Closed
anatoliylitv wants to merge 11 commits into
pytorch:mainfrom
anatoliylitv:anatoliylitv/tune-up-layer-norm
Closed

anatoliylitv wants to merge 11 commits into
pytorch:mainfrom
anatoliylitv:anatoliylitv/tune-up-layer-norm

Conversation

@anatoliylitv

@anatoliylitv anatoliylitv commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Hugging Face model dropped 5-10% after switching to tiled kernel (10+ model and tests).

Implemented mixed approach, using combination of Tiled and Two Pass:

Hugging Face (huggingface_bart) performance is back:
Legacy Two pass performance=1652, 1644
Tiled performance =1574, 1568
Mixed performance =1649, 1652

Synthetic reproducer where performance from chess board like became uniform, keeping benefits from both implementation:

Layer Norm Backward Benchmark: Tiled only vs Tiled+Two pass

Device: AMD Instinct MI350X | Warmup: 20 | Iters: 100 | Runs averaged: 3

Summary (avg µs over 3 runs)

Benchmark Op Shape dtype Branch Tiled only Tiled+Two pass Δ (µs) Speedup Winner
tile8_small layer_norm (32, 512) float16 Tile-8 7.94 8.16 -0.22 0.97× ~tie
tile64_medium layer_norm (96, 768) float16 Tile-64 20.71 20.78 -0.07 1.00× ~tie
tile128_medium layer_norm (192, 1024) bfloat16 Tile-128 8.20 8.51 -0.31 0.96× ~tie
tile256_large layer_norm (4096, 1024) float16 Tile-256 31.07 13.12 +17.95 2.37× Two pass
tile256_bert layer_norm (1024, 768) float16 Tile-256 BERT 11.15 11.05 +0.10 1.01× ~tie
tile256_large_bf16 layer_norm (4096, 1024) bfloat16 Tile-256 31.28 13.10 +18.18 2.39× Two pass
tile256_large_fp32 layer_norm (4096, 1024) float32 Tile-256 30.87 15.90 +14.97 1.94× Two pass
two_pass_huge_M layer_norm (131072, 64) float16 Two-pass M-parallel 26.48 27.70 -1.22 0.96× ~tie
llm_hidden_4096 layer_norm (32, 4096, 4096) bfloat16 Tile-256 LLM 509.65 513.38 -3.73 0.99× ~tie
gpt2_style layer_norm (8, 1024, 768) float16 Tile-256 GPT2 58.22 16.56 +41.66 3.52× Two pass
rms_tile256 rms_norm (4096, 1024) float16 Tile-256 rms 22.34 11.18 +11.16 2.00× Two pass
rms_llm rms_norm (32, 4096, 4096) bfloat16 Tile-256 rms LLM 476.18 476.03 +0.15 1.00× ~tie
rms_two_pass rms_norm (131072, 64) float16 Two-pass rms 16.73 17.29 -0.56 0.97× ~tie

Δ = Tiled only − Tiled+Two pass (negative = Two pass faster). Speedup = Tiled only / Tiled+Two pass.

Also result of generated test for correcteness:
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile128_medium] PASSED [ 4%]
tests/test_correctness.py::test_rms_norm_autograd_matches_cpu_reference PASSED [ 9%]
tests/test_correctness.py::test_edge_case_shape_M_eq_1 PASSED [ 13%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_two_pass] PASSED [ 18%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_and_dbeta] PASSED [ 22%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_tile256] PASSED [ 27%]
tests/test_correctness.py::test_noncontiguous_input_matches_reference PASSED [ 31%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[two_pass_huge_M] PASSED [ 36%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dbeta_only] PASSED [ 40%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_fp32] PASSED [ 45%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_llm] PASSED [ 50%]
tests/test_correctness.py::test_configs_cover_all_tile_branches PASSED [ 54%]
tests/test_correctness.py::test_layer_norm_autograd_matches_cpu_reference PASSED [ 59%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_only] PASSED [ 63%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[gpt2_style] PASSED [ 68%]
tests/test_correctness.py::test_edge_case_shape_N_eq_1 PASSED [ 72%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_bert] PASSED [ 77%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large] PASSED [ 81%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_bf16] PASSED [ 86%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[llm_hidden_4096] PASSED [ 90%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile8_small] PASSED [ 95%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile64_medium] PASSED [100%]

========== 22 passed in 9.26s ===================================

Used AI assistance from Cursor.

Short performance reproducer:
layer_norm_gamma_beta_backward_reproducer.py
run.sh

cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang

@pytorch-bot

pytorch-bot Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (19 Unrelated Failures)

As of commit 5e4cae2 with merge base e90d868 (image):

CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:

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 Jul 9, 2026
@eqy

eqy commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

@claude are these changes specific to the ROCm backend or do they affect the CUDA implementation as the PR title suggests?

@claude

claude Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Claude finished @eqy's task in 40s —— View job


Answer

These changes are specific to the ROCm backend and do not affect the CUDA implementation, despite the "CUDA" wording in the PR title (the file lives under aten/src/ATen/native/cuda/, but that directory is shared by both CUDA and ROCm/HIP builds).

The single edited hunk is entirely inside an #ifdef USE_ROCM block, and further inside the if constexpr (block_dim_x == 64) branch (GCN/CDNA warp-size-64 devices):

} else {
#ifdef USE_ROCM
  if constexpr (block_dim_x == 64) {
    // GCN/CDNA devices use warp size 64 in ROCm.
    ConfigureAndLaunchGammaBetaBackwardKernel<T, T_ACC, block_dim_x, 16, 128, rms_norm>(...);  // <-- changed (was 16, 256)
  } else {
    static_assert(block_dim_x == 32);
    // RDNA devices (gfx10/11/12) use warp size 32.
    ConfigureAndLaunchGammaBetaBackwardKernel<T, T_ACC, block_dim_x, 32, 256, rms_norm>(...);
  }
#else
  ConfigureAndLaunchGammaBetaBackwardKernel<T, T_ACC, block_dim_x, 32, 256, rms_norm>(...);  // <-- CUDA path, UNCHANGED
#endif
}

(aten/src/ATen/native/cuda/layer_norm_kernel.cu:1058-1074)

Concretely:

  • The NVIDIA/CUDA path is the #else branch (ConfigureAndLaunchGammaBetaBackwardKernel<..., 32, 256, ...>), which is untouched.
  • Even the RDNA (warp-size-32) ROCm path is untouched.
  • Only the warp-size-64 GCN/CDNA path changed: rows_per_block_y 256 → 128, halving idle rows per block. This is consistent with the benchmark device (MI350X) in the PR description.

A couple of follow-up suggestions:

  1. PR title / description: since the effect is ROCm-only, it may be worth clarifying the title to say "ROCm" rather than "CUDA" to avoid confusion for reviewers and in the changelog.
  2. Stale comment: the comment on line 1063 was updated to rows_per_thread_y = 256/16 = 8, but the template now uses rows_per_block_y = 128. 128/16 = 8 works out, but the 256/16 arithmetic in the comment no longer matches the code — consider updating it to 128/16 = 8 for clarity. Fix this →
     •  branch: anatoliylitv/tune-up-layer-norm

@anatoliylitv anatoliylitv changed the title Optimize CUDA layer normalization backward kernel configuration by ad… [ROCM] Optimize AMD 64 warp size normalization backward kernel configuration. Jul 9, 2026
@pytorch-bot pytorch-bot Bot added the module: rocm AMD GPU support for Pytorch label Jul 9, 2026
@jithunnair-amd jithunnair-amd added ciflow/trunk Trigger trunk jobs on your pull request ciflow/rocm-mi300 Trigger "default" config CI on ROCm MI300 ciflow/rocm-mi355 Trigger "default" config CI on ROCm MI355 runners labels Jul 9, 2026
@pytorch-bot

pytorch-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

Warning: Unknown label ciflow/rocm-mi355.
Currently recognized labels are

  • ciflow/b200
  • ciflow/b200-distributed
  • ciflow/b200-symm-mem
  • ciflow/binaries
  • ciflow/binaries_libtorch
  • ciflow/binaries_wheel
  • ciflow/docker
  • ciflow/dtensor
  • ciflow/dynamo
  • ciflow/h100
  • ciflow/h100-cutlass-backend
  • ciflow/h100-distributed
  • ciflow/h100-symm-mem
  • ciflow/inductor
  • ciflow/inductor-cu126
  • ciflow/inductor-micro-benchmark
  • ciflow/inductor-micro-benchmark-cpu-x86
  • ciflow/inductor-pallas
  • ciflow/inductor-perf-compare
  • ciflow/inductor-perf-test-nightly-rocm-mi300
  • ciflow/inductor-perf-test-nightly-rocm-mi350
  • ciflow/inductor-perf-test-nightly-x86-zen
  • ciflow/inductor-perf-test-nightly-xpu
  • ciflow/inductor-periodic
  • ciflow/inductor-rocm-mi200
  • ciflow/inductor-rocm-mi300
  • ciflow/mps
  • ciflow/nightly
  • ciflow/op-benchmark
  • ciflow/periodic
  • ciflow/periodic-rocm-mi200
  • ciflow/periodic-rocm-mi300
  • ciflow/pull
  • ciflow/riscv64
  • ciflow/rocm-mi200
  • ciflow/rocm-mi300
  • ciflow/rocm-navi31
  • ciflow/rocm-nightly
  • ciflow/s390
  • ciflow/slow
  • ciflow/slow-rocm-mi200
  • ciflow/torchtitan
  • ciflow/triton_binaries
  • ciflow/trunk
  • ciflow/tsan
  • ciflow/unstable
  • ciflow/vllm
  • ciflow/win-arm64
  • ciflow/xpu

Please add the new label to .github/pytorch-probot.yml

@pytorch-bot

pytorch-bot Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@anatoliylitv anatoliylitv changed the title [ROCM] Optimize AMD 64 warp size normalization backward kernel configuration. [ROCm] Optimize AMD 64 warp size normalization backward kernel configuration. Jul 9, 2026
@anatoliylitv
anatoliylitv marked this pull request as draft July 9, 2026 18:18
@anatoliylitv anatoliylitv changed the title [ROCm] Optimize AMD 64 warp size normalization backward kernel configuration. [ROCm] Optimize AMD normalization backward kernel by using tiled and two pass implementations where they are most efficient. Jul 11, 2026
@anatoliylitv

Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased anatoliylitv/tune-up-layer-norm onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout anatoliylitv/tune-up-layer-norm && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the anatoliylitv/tune-up-layer-norm branch from e6b590f to ae23619 Compare July 11, 2026 22:25
@anatoliylitv
anatoliylitv marked this pull request as ready for review July 13, 2026 20:23
@albanD albanD added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jul 14, 2026
@albanD
albanD requested a review from jeffdaily July 14, 2026 14:24

@jeffdaily jeffdaily 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.

I ran this PR through an AI-assisted review pass and then verified the load-bearing claims against the source myself (in particular the undefined-gamma path and the dead-code consequence of the duplicated predicate). The findings below are the assistant's wording; I agree with them and the first one is a merge blocker from my side.

Summary

Reintroduces the legacy two-pass cuComputePartGradGammaBeta + cuComputeGradGammaBeta gamma/beta backward on ROCm and dispatches to the tiled kernel only in the huge-M / small-N regime. One hard crash, a dead-code/duplication problem in the dispatch, no new tests, and an unexplained inconsistency in the benchmark table.

Code Quality

aten/src/ATen/native/cuda/layer_norm_kernel.cu:1487 - gamma.options() throws when gamma is undefined. layer_norm_backward_cuda passes *gamma through unconditionally, and F.layer_norm(x, shape, weight=None, bias=b) with bias.requires_grad=True reaches LayerNormBackwardKernelImplInternal with an undefined gamma and a defined dbeta (grad_input_mask == {*, false, true}). TensorBase::options() calls device(), which for UndefinedTensorImpl hits TORCH_CHECK(device_opt_.has_value(), "tensor does not have a device") (c10/core/TensorImpl.h:1302; UndefinedTensorImpl is constructed with std::nullopt for device at c10/core/UndefinedTensorImpl.cpp:8). This is a regression against main, where that shape range goes through the tiled kernel and never touches gamma.

Use X.options().dtype(part_grad_dtype) instead. That also lets you drop the gamma parameter from LaunchTwoPassGammaBetaBackwardCUDAKernel entirely.

layer_norm_kernel.cu:1744 - typo hich -> which, and the comment is a run-on fragment ("...for that regime. but 3x slower than the two-pass kernel for other cases."). The claim in the last clause also contradicts the PR's own table, where tiled beats legacy at 1024x768 and 192x1024.

layer_norm_kernel.cu:1764 and 1770-1771 - misindented closing brace and under-indented call.

layer_norm_kernel.cu:1767-1771 - the two branches use different calling conventions: the tiled launcher takes Tensor* dgamma, Tensor* dbeta, while the new two-pass launcher takes raw T* and forces the caller to re-extract the pointers. Have LaunchTwoPassGammaBetaBackwardCUDAKernel take Tensor* and do the defined() ? data_ptr<T>() : nullptr extraction internally, matching ConfigureAndLaunchGammaBetaBackwardKernel (layer_norm_kernel.cu:967-969).

API Design

layer_norm_kernel.cu:1746-1747 - M > 64 * 1024 && N / warp_size < sm_count / 2 is a verbatim copy of the predicate inside LaunchGammaBetaBackwardCUDAKernel (layer_norm_kernel.cu:1004). Two problems:

  1. The two copies can silently diverge. If they ever disagree, ROCm either re-enters the tiled kernel's non-huge-M path (which this PR is trying to avoid) or skips the huge-M path it wants. Factor the predicate into one named helper called from both sites, or pass a flag.
  2. As written, ROCm now only ever enters the if side of that predicate, so the entire else branch of LaunchGammaBetaBackwardCUDAKernel (layer_norm_kernel.cu:1047-1075) is dead code under USE_ROCM. That includes both #ifdef USE_ROCM tuning blocks: the block_dim_y=16, 256 CDNA cap added by #179019 and the block_dim_y=32 RDNA specialization added by #183864 (Strix Halo). If the intent is to stop using the tiled kernel there, delete those ROCm blocks so the next reader does not tune dead code. If it is not the intent, the dispatch condition is wrong.

layer_norm_kernel.cu:1758-1765 - the TORCH_INTERNAL_ASSERT on an unexpected ROCm warp size now only fires on the huge-M path. For M <= 65536 an unexpected warp size falls into the two-pass launcher, where cuComputeGradGammaBeta computes num_warp_reductions = part_size / blockDim.y with part_size == warp_size and blockDim.y == 8 (layer_norm_kernel.cu:1496); a warp size that is not a multiple of 8 silently truncates and drops partial sums rather than asserting. Hoist the warp-size check above the branch.

Testing

No test is added, and the PR body has no test plan (only benchmarks).

The only test that exercises gamma/beta gradients across a range of M on CUDA/ROCm is TestNN.test_layer_norm_backwards_eps (test/test_nn.py:6995, fp32 only). Its shape list covers the new two-pass band with just (1024,32), (1024,1024), (1025,33), (1025,1025), and the tiled band with a single (131072, 32). Missing:

  • The M == 65536 switch boundary (and M == 128).
  • Any case with M > 65536 where N / warp_size >= sm_count / 2, i.e. the predicate is false and the two-pass kernel runs at huge M. That combination is newly reachable on ROCm and is currently unreachable by any test. Pick N from torch.cuda.get_device_properties().multi_processor_count rather than hardcoding, since the threshold is arch-dependent (N > 9728 on gfx942, N > 3328 on gfx90a).
  • The weight=None, bias.requires_grad=True case that triggers the crash above. nn.LayerNorm cannot produce it (elementwise_affine=False also drops bias), so it needs an explicit F.layer_norm call.

There is no rms_norm backward numerics test at any meaningful size either (sample_inputs_rms_norm in common_methods_invocations.py:4694 only reaches M<=2), yet this PR changes the rms_norm backward dispatch too.

Performance

The tile128_medium row (192x1024, bf16) in the PR description is self-inconsistent: mixed is annotated as selecting legacy, yet reports 12.53us against legacy's own 7.86us. Identical code path, 59% apart. Either the annotation is wrong (mixed is not actually picking legacy at M=192) or the measurement is. Please re-measure or explain; if the dispatch really is falling somewhere unexpected at M=192, the heuristic has a bug.

Separately, the table shows mixed losing to pure tiled at 1024x768 (12.54 vs 11.04, -14%). That is a very common transformer shape. Worth saying explicitly whether that regression is accepted and why a second cut (e.g. keeping tiled below some M) was rejected.

@anatoliylitv

Copy link
Copy Markdown
Contributor Author

I ran this PR through an AI-assisted review pass and then verified the load-bearing claims against the source myself (in particular the undefined-gamma path and the dead-code consequence of the duplicated predicate). The findings below are the assistant's wording; I agree with them and the first one is a merge blocker from my side.

Summary

Reintroduces the legacy two-pass cuComputePartGradGammaBeta + cuComputeGradGammaBeta gamma/beta backward on ROCm and dispatches to the tiled kernel only in the huge-M / small-N regime. One hard crash, a dead-code/duplication problem in the dispatch, no new tests, and an unexplained inconsistency in the benchmark table.

Code Quality

aten/src/ATen/native/cuda/layer_norm_kernel.cu:1487 - gamma.options() throws when gamma is undefined. layer_norm_backward_cuda passes *gamma through unconditionally, and F.layer_norm(x, shape, weight=None, bias=b) with bias.requires_grad=True reaches LayerNormBackwardKernelImplInternal with an undefined gamma and a defined dbeta (grad_input_mask == {*, false, true}). TensorBase::options() calls device(), which for UndefinedTensorImpl hits TORCH_CHECK(device_opt_.has_value(), "tensor does not have a device") (c10/core/TensorImpl.h:1302; UndefinedTensorImpl is constructed with std::nullopt for device at c10/core/UndefinedTensorImpl.cpp:8). This is a regression against main, where that shape range goes through the tiled kernel and never touches gamma.
Use X.options().dtype(part_grad_dtype) instead. That also lets you drop the gamma parameter from LaunchTwoPassGammaBetaBackwardCUDAKernel entirely.
layer_norm_kernel.cu:1744 - typo hich -> which, and the comment is a run-on fragment ("...for that regime. but 3x slower than the two-pass kernel for other cases."). The claim in the last clause also contradicts the PR's own table, where tiled beats legacy at 1024x768 and 192x1024.
layer_norm_kernel.cu:1764 and 1770-1771 - misindented closing brace and under-indented call.
layer_norm_kernel.cu:1767-1771 - the two branches use different calling conventions: the tiled launcher takes Tensor* dgamma, Tensor* dbeta, while the new two-pass launcher takes raw T* and forces the caller to re-extract the pointers. Have LaunchTwoPassGammaBetaBackwardCUDAKernel take Tensor* and do the defined() ? data_ptr<T>() : nullptr extraction internally, matching ConfigureAndLaunchGammaBetaBackwardKernel (layer_norm_kernel.cu:967-969).

API Design

layer_norm_kernel.cu:1746-1747 - M > 64 * 1024 && N / warp_size < sm_count / 2 is a verbatim copy of the predicate inside LaunchGammaBetaBackwardCUDAKernel (layer_norm_kernel.cu:1004). Two problems:

  1. The two copies can silently diverge. If they ever disagree, ROCm either re-enters the tiled kernel's non-huge-M path (which this PR is trying to avoid) or skips the huge-M path it wants. Factor the predicate into one named helper called from both sites, or pass a flag.
  2. As written, ROCm now only ever enters the if side of that predicate, so the entire else branch of LaunchGammaBetaBackwardCUDAKernel (layer_norm_kernel.cu:1047-1075) is dead code under USE_ROCM. That includes both #ifdef USE_ROCM tuning blocks: the block_dim_y=16, 256 CDNA cap added by [AMD] Use optimized tiled kernel for LayerNorm gamma beta backward #179019 and the block_dim_y=32 RDNA specialization added by [ROCm] Fix LayerNorm backward kernel for AMD Strix Halo GPUs #183864 (Strix Halo). If the intent is to stop using the tiled kernel there, delete those ROCm blocks so the next reader does not tune dead code. If it is not the intent, the dispatch condition is wrong.

layer_norm_kernel.cu:1758-1765 - the TORCH_INTERNAL_ASSERT on an unexpected ROCm warp size now only fires on the huge-M path. For M <= 65536 an unexpected warp size falls into the two-pass launcher, where cuComputeGradGammaBeta computes num_warp_reductions = part_size / blockDim.y with part_size == warp_size and blockDim.y == 8 (layer_norm_kernel.cu:1496); a warp size that is not a multiple of 8 silently truncates and drops partial sums rather than asserting. Hoist the warp-size check above the branch.

Testing

No test is added, and the PR body has no test plan (only benchmarks).
The only test that exercises gamma/beta gradients across a range of M on CUDA/ROCm is TestNN.test_layer_norm_backwards_eps (test/test_nn.py:6995, fp32 only). Its shape list covers the new two-pass band with just (1024,32), (1024,1024), (1025,33), (1025,1025), and the tiled band with a single (131072, 32). Missing:

  • The M == 65536 switch boundary (and M == 128).
  • Any case with M > 65536 where N / warp_size >= sm_count / 2, i.e. the predicate is false and the two-pass kernel runs at huge M. That combination is newly reachable on ROCm and is currently unreachable by any test. Pick N from torch.cuda.get_device_properties().multi_processor_count rather than hardcoding, since the threshold is arch-dependent (N > 9728 on gfx942, N > 3328 on gfx90a).
  • The weight=None, bias.requires_grad=True case that triggers the crash above. nn.LayerNorm cannot produce it (elementwise_affine=False also drops bias), so it needs an explicit F.layer_norm call.

There is no rms_norm backward numerics test at any meaningful size either (sample_inputs_rms_norm in common_methods_invocations.py:4694 only reaches M<=2), yet this PR changes the rms_norm backward dispatch too.

Performance

The tile128_medium row (192x1024, bf16) in the PR description is self-inconsistent: mixed is annotated as selecting legacy, yet reports 12.53us against legacy's own 7.86us. Identical code path, 59% apart. Either the annotation is wrong (mixed is not actually picking legacy at M=192) or the measurement is. Please re-measure or explain; if the dispatch really is falling somewhere unexpected at M=192, the heuristic has a bug.
Separately, the table shows mixed losing to pure tiled at 1024x768 (12.54 vs 11.04, -14%). That is a very common transformer shape. Worth saying explicitly whether that regression is accepted and why a second cut (e.g. keeping tiled below some M) was rejected.

Thank a lot @jeffdaily. Agree that once resurrected code and new way is working together, they have to be consistent now. Will work on it.

@anatoliylitv

Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased anatoliylitv/tune-up-layer-norm onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout anatoliylitv/tune-up-layer-norm && git pull --rebase)

anatoliylitv and others added 10 commits August 14, 2026 02:48
…ecting rows_per_thread_y from 256 to 128, ensuring compliance with GPU limits while maintaining performance optimization.
…on, enhancing performance for specific tensor sizes. Adjusted kernel launch conditions to optimize for large M and small N scenarios, while maintaining compatibility with existing configurations.
…nsor's own shape instead of reusing dgamma->size(-1), optimized two-pass launcher to remove rgression
The comment in the tiled branch still said the tiled kernel "replaces the legacy two-pass approach", which is no longer true now that the else branch right below dispatches to that approach; reworded to describe only what the branch does.

test_layer_norm_gamma_beta_backward_dispatch_bands kept fp32 at atol=1e-4 while its own comment predicted ~4e-4 reduction-order noise at M=4096. That noise does not shrink with the reference magnitude, so near-zero dgamma/dbeta entries were protected by atol alone and the test was a flake risk on both the ROCm two-pass band and the NVIDIA tiled reduction. Bump fp32 to atol=rtol=1e-3 for M >= 2048 only, matching what test_layer_norm_gamma_beta_backward_dispatch_boundaries already uses for the same band.

Test Plan:

Ran the modified test on gfx90a against a current build (main kernel; the kernel edit here is comment-only, so behavior is identical to what MI350 CI already validated on this branch):

```
cd test && HIP_VISIBLE_DEVICES=3 python test_nn.py -k test_layer_norm_gamma_beta_backward_dispatch_bands -v
```

Passed in 5.3s. lintrunner clean on both changed files.

Maintainer fixup commit, authored with Claude (AI assistant) and reviewed by Jeff Daily.
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased anatoliylitv/tune-up-layer-norm onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout anatoliylitv/tune-up-layer-norm && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the anatoliylitv/tune-up-layer-norm branch from 7a984dd to 5e4cae2 Compare August 14, 2026 02:49
@anatoliylitv

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

jerrymannil pushed a commit to ROCm/pytorch that referenced this pull request Aug 14, 2026
…two pass implementations where they are most efficient. (pytorch#189405)

Hugging Face model dropped **5-10%** after switching to tiled kernel (10+ model and tests).

Implemented mixed approach, using combination of Tiled and Two Pass:

Hugging Face (huggingface_bart) performance is back:
Legacy Two pass performance=1652, 1644
Tiled performance                   =**1574, 1568**
Mixed performance                 =1649, 1652

Synthetic reproducer where performance from chess board like became  uniform, keeping benefits from both implementation:

# Layer Norm Backward Benchmark: Tiled only vs Tiled+Two pass

**Device:** AMD Instinct MI350X | **Warmup:** 20 | **Iters:** 100 | **Runs averaged:** 3

## Summary (avg µs over 3 runs)

| Benchmark | Op | Shape | dtype | Branch | Tiled only | Tiled+Two pass | Δ (µs) | Speedup | Winner |
|-----------|-----|-------|-------|--------|------------|----------------|--------|---------|--------|
| tile8_small | layer_norm | (32, 512) | float16 | Tile-8 | 7.94 | 8.16 | -0.22 | 0.97× | ~tie |
| tile64_medium | layer_norm | (96, 768) | float16 | Tile-64 | 20.71 | 20.78 | -0.07 | 1.00× | ~tie |
| tile128_medium | layer_norm | (192, 1024) | bfloat16 | Tile-128 | 8.20 | 8.51 | -0.31 | 0.96× | ~tie |
| tile256_large | layer_norm | (4096, 1024) | float16 | Tile-256 | 31.07 | 13.12 | +17.95 | 2.37× | Two pass |
| tile256_bert | layer_norm | (1024, 768) | float16 | Tile-256 BERT | 11.15 | 11.05 | +0.10 | 1.01× | ~tie |
| tile256_large_bf16 | layer_norm | (4096, 1024) | bfloat16 | Tile-256 | 31.28 | 13.10 | +18.18 | 2.39× | Two pass |
| tile256_large_fp32 | layer_norm | (4096, 1024) | float32 | Tile-256 | 30.87 | 15.90 | +14.97 | 1.94× | Two pass |
| two_pass_huge_M | layer_norm | (131072, 64) | float16 | Two-pass M-parallel | 26.48 | 27.70 | -1.22 | 0.96× | ~tie |
| llm_hidden_4096 | layer_norm | (32, 4096, 4096) | bfloat16 | Tile-256 LLM | 509.65 | 513.38 | -3.73 | 0.99× | ~tie |
| gpt2_style | layer_norm | (8, 1024, 768) | float16 | Tile-256 GPT2 | 58.22 | 16.56 | +41.66 | 3.52× | Two pass |
| rms_tile256 | rms_norm | (4096, 1024) | float16 | Tile-256 rms | 22.34 | 11.18 | +11.16 | 2.00× | Two pass |
| rms_llm | rms_norm | (32, 4096, 4096) | bfloat16 | Tile-256 rms LLM | 476.18 | 476.03 | +0.15 | 1.00× | ~tie |
| rms_two_pass | rms_norm | (131072, 64) | float16 | Two-pass rms | 16.73 | 17.29 | -0.56 | 0.97× | ~tie |

Δ = Tiled only − Tiled+Two pass (negative = Two pass faster). Speedup = Tiled only / Tiled+Two pass.

Also result of generated test for correcteness:
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile128_medium] PASSED                                             [  4%]
tests/test_correctness.py::test_rms_norm_autograd_matches_cpu_reference PASSED                                                                          [  9%]
tests/test_correctness.py::test_edge_case_shape_M_eq_1 PASSED                                                                                           [ 13%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_two_pass] PASSED                                                      [ 18%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_and_dbeta] PASSED                                                             [ 22%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_tile256] PASSED                                                       [ 27%]
tests/test_correctness.py::test_noncontiguous_input_matches_reference PASSED                                                                            [ 31%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[two_pass_huge_M] PASSED                                            [ 36%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dbeta_only] PASSED                                                                   [ 40%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_fp32] PASSED                                         [ 45%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_llm] PASSED                                                           [ 50%]
tests/test_correctness.py::test_configs_cover_all_tile_branches PASSED                                                                                  [ 54%]
tests/test_correctness.py::test_layer_norm_autograd_matches_cpu_reference PASSED                                                                        [ 59%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_only] PASSED                                                                  [ 63%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[gpt2_style] PASSED                                                 [ 68%]
tests/test_correctness.py::test_edge_case_shape_N_eq_1 PASSED                                                                                           [ 72%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_bert] PASSED                                               [ 77%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large] PASSED                                              [ 81%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_bf16] PASSED                                         [ 86%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[llm_hidden_4096] PASSED                                            [ 90%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile8_small] PASSED                                                [ 95%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile64_medium] PASSED                                              [100%]

========== 22 passed in 9.26s ===================================

Used AI assistance from Cursor.

Short performance reproducer:
[layer_norm_gamma_beta_backward_reproducer.py](https://github.com/user-attachments/files/31031711/layer_norm_gamma_beta_backward_reproducer.py)
[run.sh](https://github.com/user-attachments/files/31031712/run.sh)

Pull Request resolved: pytorch#189405
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
(cherry picked from commit e61531f)
jerrymannil pushed a commit to ROCm/pytorch that referenced this pull request Aug 18, 2026
…two pass implementations where they are most efficient. (pytorch#189405)

Hugging Face model dropped **5-10%** after switching to tiled kernel (10+ model and tests).

Implemented mixed approach, using combination of Tiled and Two Pass:

Hugging Face (huggingface_bart) performance is back:
Legacy Two pass performance=1652, 1644
Tiled performance                   =**1574, 1568**
Mixed performance                 =1649, 1652

Synthetic reproducer where performance from chess board like became  uniform, keeping benefits from both implementation:

# Layer Norm Backward Benchmark: Tiled only vs Tiled+Two pass

**Device:** AMD Instinct MI350X | **Warmup:** 20 | **Iters:** 100 | **Runs averaged:** 3

## Summary (avg µs over 3 runs)

| Benchmark | Op | Shape | dtype | Branch | Tiled only | Tiled+Two pass | Δ (µs) | Speedup | Winner |
|-----------|-----|-------|-------|--------|------------|----------------|--------|---------|--------|
| tile8_small | layer_norm | (32, 512) | float16 | Tile-8 | 7.94 | 8.16 | -0.22 | 0.97× | ~tie |
| tile64_medium | layer_norm | (96, 768) | float16 | Tile-64 | 20.71 | 20.78 | -0.07 | 1.00× | ~tie |
| tile128_medium | layer_norm | (192, 1024) | bfloat16 | Tile-128 | 8.20 | 8.51 | -0.31 | 0.96× | ~tie |
| tile256_large | layer_norm | (4096, 1024) | float16 | Tile-256 | 31.07 | 13.12 | +17.95 | 2.37× | Two pass |
| tile256_bert | layer_norm | (1024, 768) | float16 | Tile-256 BERT | 11.15 | 11.05 | +0.10 | 1.01× | ~tie |
| tile256_large_bf16 | layer_norm | (4096, 1024) | bfloat16 | Tile-256 | 31.28 | 13.10 | +18.18 | 2.39× | Two pass |
| tile256_large_fp32 | layer_norm | (4096, 1024) | float32 | Tile-256 | 30.87 | 15.90 | +14.97 | 1.94× | Two pass |
| two_pass_huge_M | layer_norm | (131072, 64) | float16 | Two-pass M-parallel | 26.48 | 27.70 | -1.22 | 0.96× | ~tie |
| llm_hidden_4096 | layer_norm | (32, 4096, 4096) | bfloat16 | Tile-256 LLM | 509.65 | 513.38 | -3.73 | 0.99× | ~tie |
| gpt2_style | layer_norm | (8, 1024, 768) | float16 | Tile-256 GPT2 | 58.22 | 16.56 | +41.66 | 3.52× | Two pass |
| rms_tile256 | rms_norm | (4096, 1024) | float16 | Tile-256 rms | 22.34 | 11.18 | +11.16 | 2.00× | Two pass |
| rms_llm | rms_norm | (32, 4096, 4096) | bfloat16 | Tile-256 rms LLM | 476.18 | 476.03 | +0.15 | 1.00× | ~tie |
| rms_two_pass | rms_norm | (131072, 64) | float16 | Two-pass rms | 16.73 | 17.29 | -0.56 | 0.97× | ~tie |

Δ = Tiled only − Tiled+Two pass (negative = Two pass faster). Speedup = Tiled only / Tiled+Two pass.

Also result of generated test for correcteness:
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile128_medium] PASSED                                             [  4%]
tests/test_correctness.py::test_rms_norm_autograd_matches_cpu_reference PASSED                                                                          [  9%]
tests/test_correctness.py::test_edge_case_shape_M_eq_1 PASSED                                                                                           [ 13%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_two_pass] PASSED                                                      [ 18%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_and_dbeta] PASSED                                                             [ 22%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_tile256] PASSED                                                       [ 27%]
tests/test_correctness.py::test_noncontiguous_input_matches_reference PASSED                                                                            [ 31%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[two_pass_huge_M] PASSED                                            [ 36%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dbeta_only] PASSED                                                                   [ 40%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_fp32] PASSED                                         [ 45%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_llm] PASSED                                                           [ 50%]
tests/test_correctness.py::test_configs_cover_all_tile_branches PASSED                                                                                  [ 54%]
tests/test_correctness.py::test_layer_norm_autograd_matches_cpu_reference PASSED                                                                        [ 59%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_only] PASSED                                                                  [ 63%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[gpt2_style] PASSED                                                 [ 68%]
tests/test_correctness.py::test_edge_case_shape_N_eq_1 PASSED                                                                                           [ 72%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_bert] PASSED                                               [ 77%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large] PASSED                                              [ 81%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_bf16] PASSED                                         [ 86%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[llm_hidden_4096] PASSED                                            [ 90%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile8_small] PASSED                                                [ 95%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile64_medium] PASSED                                              [100%]

========== 22 passed in 9.26s ===================================

Used AI assistance from Cursor.

Short performance reproducer:
[layer_norm_gamma_beta_backward_reproducer.py](https://github.com/user-attachments/files/31031711/layer_norm_gamma_beta_backward_reproducer.py)
[run.sh](https://github.com/user-attachments/files/31031712/run.sh)

Pull Request resolved: pytorch#189405
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
jerrymannil pushed a commit to ROCm/pytorch that referenced this pull request Aug 18, 2026
…two pass implementations where they are most efficient. (pytorch#189405)

Hugging Face model dropped **5-10%** after switching to tiled kernel (10+ model and tests).

Implemented mixed approach, using combination of Tiled and Two Pass:

Hugging Face (huggingface_bart) performance is back:
Legacy Two pass performance=1652, 1644
Tiled performance                   =**1574, 1568**
Mixed performance                 =1649, 1652

Synthetic reproducer where performance from chess board like became  uniform, keeping benefits from both implementation:

# Layer Norm Backward Benchmark: Tiled only vs Tiled+Two pass

**Device:** AMD Instinct MI350X | **Warmup:** 20 | **Iters:** 100 | **Runs averaged:** 3

## Summary (avg µs over 3 runs)

| Benchmark | Op | Shape | dtype | Branch | Tiled only | Tiled+Two pass | Δ (µs) | Speedup | Winner |
|-----------|-----|-------|-------|--------|------------|----------------|--------|---------|--------|
| tile8_small | layer_norm | (32, 512) | float16 | Tile-8 | 7.94 | 8.16 | -0.22 | 0.97× | ~tie |
| tile64_medium | layer_norm | (96, 768) | float16 | Tile-64 | 20.71 | 20.78 | -0.07 | 1.00× | ~tie |
| tile128_medium | layer_norm | (192, 1024) | bfloat16 | Tile-128 | 8.20 | 8.51 | -0.31 | 0.96× | ~tie |
| tile256_large | layer_norm | (4096, 1024) | float16 | Tile-256 | 31.07 | 13.12 | +17.95 | 2.37× | Two pass |
| tile256_bert | layer_norm | (1024, 768) | float16 | Tile-256 BERT | 11.15 | 11.05 | +0.10 | 1.01× | ~tie |
| tile256_large_bf16 | layer_norm | (4096, 1024) | bfloat16 | Tile-256 | 31.28 | 13.10 | +18.18 | 2.39× | Two pass |
| tile256_large_fp32 | layer_norm | (4096, 1024) | float32 | Tile-256 | 30.87 | 15.90 | +14.97 | 1.94× | Two pass |
| two_pass_huge_M | layer_norm | (131072, 64) | float16 | Two-pass M-parallel | 26.48 | 27.70 | -1.22 | 0.96× | ~tie |
| llm_hidden_4096 | layer_norm | (32, 4096, 4096) | bfloat16 | Tile-256 LLM | 509.65 | 513.38 | -3.73 | 0.99× | ~tie |
| gpt2_style | layer_norm | (8, 1024, 768) | float16 | Tile-256 GPT2 | 58.22 | 16.56 | +41.66 | 3.52× | Two pass |
| rms_tile256 | rms_norm | (4096, 1024) | float16 | Tile-256 rms | 22.34 | 11.18 | +11.16 | 2.00× | Two pass |
| rms_llm | rms_norm | (32, 4096, 4096) | bfloat16 | Tile-256 rms LLM | 476.18 | 476.03 | +0.15 | 1.00× | ~tie |
| rms_two_pass | rms_norm | (131072, 64) | float16 | Two-pass rms | 16.73 | 17.29 | -0.56 | 0.97× | ~tie |

Δ = Tiled only − Tiled+Two pass (negative = Two pass faster). Speedup = Tiled only / Tiled+Two pass.

Also result of generated test for correcteness:
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile128_medium] PASSED                                             [  4%]
tests/test_correctness.py::test_rms_norm_autograd_matches_cpu_reference PASSED                                                                          [  9%]
tests/test_correctness.py::test_edge_case_shape_M_eq_1 PASSED                                                                                           [ 13%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_two_pass] PASSED                                                      [ 18%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_and_dbeta] PASSED                                                             [ 22%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_tile256] PASSED                                                       [ 27%]
tests/test_correctness.py::test_noncontiguous_input_matches_reference PASSED                                                                            [ 31%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[two_pass_huge_M] PASSED                                            [ 36%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dbeta_only] PASSED                                                                   [ 40%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_fp32] PASSED                                         [ 45%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_llm] PASSED                                                           [ 50%]
tests/test_correctness.py::test_configs_cover_all_tile_branches PASSED                                                                                  [ 54%]
tests/test_correctness.py::test_layer_norm_autograd_matches_cpu_reference PASSED                                                                        [ 59%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_only] PASSED                                                                  [ 63%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[gpt2_style] PASSED                                                 [ 68%]
tests/test_correctness.py::test_edge_case_shape_N_eq_1 PASSED                                                                                           [ 72%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_bert] PASSED                                               [ 77%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large] PASSED                                              [ 81%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_bf16] PASSED                                         [ 86%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[llm_hidden_4096] PASSED                                            [ 90%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile8_small] PASSED                                                [ 95%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile64_medium] PASSED                                              [100%]

========== 22 passed in 9.26s ===================================

Used AI assistance from Cursor.

Short performance reproducer:
[layer_norm_gamma_beta_backward_reproducer.py](https://github.com/user-attachments/files/31031711/layer_norm_gamma_beta_backward_reproducer.py)
[run.sh](https://github.com/user-attachments/files/31031712/run.sh)

Pull Request resolved: pytorch#189405
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
(cherry picked from commit e61531f)
pablo-garay pushed a commit to ROCm/pytorch that referenced this pull request Aug 24, 2026
…two pass implementations where they are most efficient. (pytorch#189405)

Hugging Face model dropped **5-10%** after switching to tiled kernel (10+ model and tests).

Implemented mixed approach, using combination of Tiled and Two Pass:

Hugging Face (huggingface_bart) performance is back:
Legacy Two pass performance=1652, 1644
Tiled performance                   =**1574, 1568**
Mixed performance                 =1649, 1652

Synthetic reproducer where performance from chess board like became  uniform, keeping benefits from both implementation:

# Layer Norm Backward Benchmark: Tiled only vs Tiled+Two pass

**Device:** AMD Instinct MI350X | **Warmup:** 20 | **Iters:** 100 | **Runs averaged:** 3

## Summary (avg µs over 3 runs)

| Benchmark | Op | Shape | dtype | Branch | Tiled only | Tiled+Two pass | Δ (µs) | Speedup | Winner |
|-----------|-----|-------|-------|--------|------------|----------------|--------|---------|--------|
| tile8_small | layer_norm | (32, 512) | float16 | Tile-8 | 7.94 | 8.16 | -0.22 | 0.97× | ~tie |
| tile64_medium | layer_norm | (96, 768) | float16 | Tile-64 | 20.71 | 20.78 | -0.07 | 1.00× | ~tie |
| tile128_medium | layer_norm | (192, 1024) | bfloat16 | Tile-128 | 8.20 | 8.51 | -0.31 | 0.96× | ~tie |
| tile256_large | layer_norm | (4096, 1024) | float16 | Tile-256 | 31.07 | 13.12 | +17.95 | 2.37× | Two pass |
| tile256_bert | layer_norm | (1024, 768) | float16 | Tile-256 BERT | 11.15 | 11.05 | +0.10 | 1.01× | ~tie |
| tile256_large_bf16 | layer_norm | (4096, 1024) | bfloat16 | Tile-256 | 31.28 | 13.10 | +18.18 | 2.39× | Two pass |
| tile256_large_fp32 | layer_norm | (4096, 1024) | float32 | Tile-256 | 30.87 | 15.90 | +14.97 | 1.94× | Two pass |
| two_pass_huge_M | layer_norm | (131072, 64) | float16 | Two-pass M-parallel | 26.48 | 27.70 | -1.22 | 0.96× | ~tie |
| llm_hidden_4096 | layer_norm | (32, 4096, 4096) | bfloat16 | Tile-256 LLM | 509.65 | 513.38 | -3.73 | 0.99× | ~tie |
| gpt2_style | layer_norm | (8, 1024, 768) | float16 | Tile-256 GPT2 | 58.22 | 16.56 | +41.66 | 3.52× | Two pass |
| rms_tile256 | rms_norm | (4096, 1024) | float16 | Tile-256 rms | 22.34 | 11.18 | +11.16 | 2.00× | Two pass |
| rms_llm | rms_norm | (32, 4096, 4096) | bfloat16 | Tile-256 rms LLM | 476.18 | 476.03 | +0.15 | 1.00× | ~tie |
| rms_two_pass | rms_norm | (131072, 64) | float16 | Two-pass rms | 16.73 | 17.29 | -0.56 | 0.97× | ~tie |

Δ = Tiled only − Tiled+Two pass (negative = Two pass faster). Speedup = Tiled only / Tiled+Two pass.

Also result of generated test for correcteness:
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile128_medium] PASSED                                             [  4%]
tests/test_correctness.py::test_rms_norm_autograd_matches_cpu_reference PASSED                                                                          [  9%]
tests/test_correctness.py::test_edge_case_shape_M_eq_1 PASSED                                                                                           [ 13%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_two_pass] PASSED                                                      [ 18%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_and_dbeta] PASSED                                                             [ 22%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_tile256] PASSED                                                       [ 27%]
tests/test_correctness.py::test_noncontiguous_input_matches_reference PASSED                                                                            [ 31%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[two_pass_huge_M] PASSED                                            [ 36%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dbeta_only] PASSED                                                                   [ 40%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_fp32] PASSED                                         [ 45%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_llm] PASSED                                                           [ 50%]
tests/test_correctness.py::test_configs_cover_all_tile_branches PASSED                                                                                  [ 54%]
tests/test_correctness.py::test_layer_norm_autograd_matches_cpu_reference PASSED                                                                        [ 59%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_only] PASSED                                                                  [ 63%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[gpt2_style] PASSED                                                 [ 68%]
tests/test_correctness.py::test_edge_case_shape_N_eq_1 PASSED                                                                                           [ 72%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_bert] PASSED                                               [ 77%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large] PASSED                                              [ 81%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_bf16] PASSED                                         [ 86%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[llm_hidden_4096] PASSED                                            [ 90%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile8_small] PASSED                                                [ 95%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile64_medium] PASSED                                              [100%]

========== 22 passed in 9.26s ===================================

Used AI assistance from Cursor.

Short performance reproducer:
[layer_norm_gamma_beta_backward_reproducer.py](https://github.com/user-attachments/files/31031711/layer_norm_gamma_beta_backward_reproducer.py)
[run.sh](https://github.com/user-attachments/files/31031712/run.sh)

Pull Request resolved: pytorch#189405
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
(cherry picked from commit 29e524b)
jithunnair-amd pushed a commit to ROCm/pytorch that referenced this pull request Aug 25, 2026
…l) (#3594)

## Summary

Carry-forward of ROCm-specific commits from `release/2.13` onto
`release/2.14` ? the 2.14 analog of the 2.13 "IFU dedup" ([PR
#3503](#3503)).

Analyzes the full IFU delta from [PR
#3591](#3591) at `release/2.13`'s
tip (`6596a0d`): **81 commits** in `release/2.13` not in `release/2.14`,
each either dropped (already in 2.14, or 2.13-only) or cherry-picked
here.

## Method

Same rules as the [release/2.13 cherry-pick
process](https://amd.atlassian.net/wiki/spaces/MLSE/pages/1819593560):

- **Drop ? already in 2.14**: patch-id equivalence (`git cherry`),
upstream PR already present, or content already relocated/absorbed into
2.14 (verified during cherry-pick).
- **Drop ? 2.13-only**: version bump, `related_commits`,
dependency/requirements pins, release-line CD/build changes.
- **Carry-forward**: ROCm-specific fixes not present in 2.14 in any
form.

## Commits carried forward (13)

| Source | Notes |
|--------|-------|
| Fix reentrant deadlock in `torch.cuda._lazy_call` (#3496) | clean |
| Skip ROCm-failing tests in test_cuda/test_nn (#3504) | **conflict
resolved** ? kept 2.14's `test_graph_capture_error...` cleanup + took
`@skipIfRocm`; kept 2.14's `test_allocator_backend` (already fixes
LD_LIBRARY_PATH via `subprocess_env()`) |
| skip test_hip_device_count (rocprofiler-sdk) | clean |
| skip flaky test_Embedding_discontiguous_cuda | clean |
| Skip profiler check for foreach tests on ROCm (#3262) | clean |
| Skip test_is_pinned_no_context on python 3.14+ | clean |
| Skip flaky test_reentrant_parent_error_on_cpu | clean |
| Filter out CK gemms for gfx1250-only arch (#3545) | clean |
| Fix hipify skipping sources under Windows subst drives (ROCM-29365)
(#3561) | clean |
| Fixed gfx1250_preferred_backend test (#3546) | clean |
| [ROCm] Optimize AMD normalization backward kernel (pytorch#189405) | clean |
| [release/2.13] Build rocSHMEM for the requested gfx target (#3582) |
clean |
| fix PicklingError handling for Python 3.14 | clean |

## Removed from this PR (per team)

- [RELEASE_ONLY] skip test_autoheuristic in-code (pytorch#180927) ? not needed
on 2.14. Upstream
[pytorch#180927](pytorch#180927)
is `RELEASE_ONLY`, merged to upstream `release/2.12` only (never on
`main` or upstream `release/2.14`).

## Reviewed and excluded (team decision: not needed for 2.14)

| Commit | Reason |
|--------|--------|
| Update composable_kernel submodule with gfx1033 support (#3499) |
2.14's CK pin already includes gfx1033; carrying would move the
submodule pointer **backward**. |
| Revert "Skip cudagraphs for kernel-free inductor graphs" (pytorch#187347) |
2.14 already has the fixed upstream **re-land** on top of the revert;
carrying = regressing 2.14. |
| Revert "[Reland] Port index_add fast path" (pytorch#188196) | 2.14 already
has the newer **re-land (pytorch#189506)** on top of the revert; carrying =
regressing 2.14. |

## Dropped as already in 2.14 (verified during cherry-pick)

- rocm-smi deprecation (#3434) ? 2.14 already has zero `rocm_smi` refs.
- Enable linalg via hipSOLVER, remove Magma (#3455) ? all 6 upstream PRs
already in 2.14.
- Stabilize ROCm 7.14 testing (#3525) ? 2.14 already carries the guards
+ skip messages.
- Add gfx1250 support in GroupedBlas (#3532) ? already in 2.14 via
upstream pytorch#190703 (`ScaledBlasDeviceUtils`).
- Fix uninitialized embedding indices `test_embedding_meta_indices`
(pytorch#188002) ? upstream already in 2.14.
- Unskip ROCSHMEM triton UTs on rocm 7.14 (#3578) ? targets a 2.13-only
7.14 module skip that 2.14 never had; tests already run on 2.14.

Remaining commits drop as 2.13-only (version/pins/related_commits/CD) or
as already-in-2.14 by patch-id/upstream.

## Test plan

- [x] TheRock full-stack dev build (torch + torchaudio + torchvision +
triton) on gfx94X-dcgpu for Python 3.10-3.14, all **Build** jobs green
(ROCm `10.1.0a20260822`, on the `aws-linux-scale-rocm-prod` pool), after
bringing the branch up to its base so `related_commits` resolves
torchaudio/torchvision:
  - py3.10  https://github.com/ROCm/TheRock/actions/runs/32863299013
  - py3.11  https://github.com/ROCm/TheRock/actions/runs/32863305363
  - py3.12  https://github.com/ROCm/TheRock/actions/runs/32863311391
  - py3.13  https://github.com/ROCm/TheRock/actions/runs/32863318109
  - py3.14  https://github.com/ROCm/TheRock/actions/runs/32863325370
- [x] TheRock CI wheel build on gfx94X-dcgpu for Python 3.10-3.14 all
green (ROCm `10.1.0a20260822`, torch-only, on the
`aws-linux-scale-rocm-prod` pool, ~1h40m each):
  - py3.10  https://github.com/ROCm/TheRock/actions/runs/32818564344
  - py3.11  https://github.com/ROCm/TheRock/actions/runs/32818568751
  - py3.12  https://github.com/ROCm/TheRock/actions/runs/32818572249
  - py3.13  https://github.com/ROCm/TheRock/actions/runs/32818576288
  - py3.14  https://github.com/ROCm/TheRock/actions/runs/32818580229



## Processed Commits List
Spreasheet with each commit evaluated for cherry-pick & resolution
(drop/include) with reasoning:


[pr3594-commit-outcomes.xlsx](https://github.com/user-attachments/files/31390971/pr3594-commit-outcomes.xlsx)

---------

Signed-off-by: pablo-garay <[email protected]>
Co-authored-by: Iurii Paikov <[email protected]>
Co-authored-by: Chinmay Dattanand Kuchinad <[email protected]>
Co-authored-by: rocm-repo-management-api-6[bot] <212817015+rocm-repo-management-api-6[bot]@users.noreply.github.com>
Co-authored-by: Dmitry Nikolaev <[email protected]>
Co-authored-by: srinivamd <[email protected]>
Co-authored-by: Subramanian <[email protected]>
Co-authored-by: zjliu-amd <[email protected]>
Co-authored-by: Andres Lugo <[email protected]>
Co-authored-by: pablo-garay <[email protected]>
Co-authored-by: Bo Li <[email protected]>
Co-authored-by: Anatoliy Litvinenko <[email protected]>
Co-authored-by: Jeff Daily <[email protected]>
Co-authored-by: Luca Bruni <[email protected]>
Co-authored-by: albmalamd <[email protected]>
jithunnair-amd pushed a commit to ROCm/pytorch that referenced this pull request Aug 27, 2026
…two pass implementations where they are most efficient. (pytorch#189405)

Hugging Face model dropped **5-10%** after switching to tiled kernel (10+ model and tests).

Implemented mixed approach, using combination of Tiled and Two Pass:

Hugging Face (huggingface_bart) performance is back:
Legacy Two pass performance=1652, 1644
Tiled performance                   =**1574, 1568**
Mixed performance                 =1649, 1652

Synthetic reproducer where performance from chess board like became  uniform, keeping benefits from both implementation:

# Layer Norm Backward Benchmark: Tiled only vs Tiled+Two pass

**Device:** AMD Instinct MI350X | **Warmup:** 20 | **Iters:** 100 | **Runs averaged:** 3

## Summary (avg µs over 3 runs)

| Benchmark | Op | Shape | dtype | Branch | Tiled only | Tiled+Two pass | Δ (µs) | Speedup | Winner |
|-----------|-----|-------|-------|--------|------------|----------------|--------|---------|--------|
| tile8_small | layer_norm | (32, 512) | float16 | Tile-8 | 7.94 | 8.16 | -0.22 | 0.97× | ~tie |
| tile64_medium | layer_norm | (96, 768) | float16 | Tile-64 | 20.71 | 20.78 | -0.07 | 1.00× | ~tie |
| tile128_medium | layer_norm | (192, 1024) | bfloat16 | Tile-128 | 8.20 | 8.51 | -0.31 | 0.96× | ~tie |
| tile256_large | layer_norm | (4096, 1024) | float16 | Tile-256 | 31.07 | 13.12 | +17.95 | 2.37× | Two pass |
| tile256_bert | layer_norm | (1024, 768) | float16 | Tile-256 BERT | 11.15 | 11.05 | +0.10 | 1.01× | ~tie |
| tile256_large_bf16 | layer_norm | (4096, 1024) | bfloat16 | Tile-256 | 31.28 | 13.10 | +18.18 | 2.39× | Two pass |
| tile256_large_fp32 | layer_norm | (4096, 1024) | float32 | Tile-256 | 30.87 | 15.90 | +14.97 | 1.94× | Two pass |
| two_pass_huge_M | layer_norm | (131072, 64) | float16 | Two-pass M-parallel | 26.48 | 27.70 | -1.22 | 0.96× | ~tie |
| llm_hidden_4096 | layer_norm | (32, 4096, 4096) | bfloat16 | Tile-256 LLM | 509.65 | 513.38 | -3.73 | 0.99× | ~tie |
| gpt2_style | layer_norm | (8, 1024, 768) | float16 | Tile-256 GPT2 | 58.22 | 16.56 | +41.66 | 3.52× | Two pass |
| rms_tile256 | rms_norm | (4096, 1024) | float16 | Tile-256 rms | 22.34 | 11.18 | +11.16 | 2.00× | Two pass |
| rms_llm | rms_norm | (32, 4096, 4096) | bfloat16 | Tile-256 rms LLM | 476.18 | 476.03 | +0.15 | 1.00× | ~tie |
| rms_two_pass | rms_norm | (131072, 64) | float16 | Two-pass rms | 16.73 | 17.29 | -0.56 | 0.97× | ~tie |

Δ = Tiled only − Tiled+Two pass (negative = Two pass faster). Speedup = Tiled only / Tiled+Two pass.

Also result of generated test for correcteness:
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile128_medium] PASSED                                             [  4%]
tests/test_correctness.py::test_rms_norm_autograd_matches_cpu_reference PASSED                                                                          [  9%]
tests/test_correctness.py::test_edge_case_shape_M_eq_1 PASSED                                                                                           [ 13%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_two_pass] PASSED                                                      [ 18%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_and_dbeta] PASSED                                                             [ 22%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_tile256] PASSED                                                       [ 27%]
tests/test_correctness.py::test_noncontiguous_input_matches_reference PASSED                                                                            [ 31%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[two_pass_huge_M] PASSED                                            [ 36%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dbeta_only] PASSED                                                                   [ 40%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_fp32] PASSED                                         [ 45%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_llm] PASSED                                                           [ 50%]
tests/test_correctness.py::test_configs_cover_all_tile_branches PASSED                                                                                  [ 54%]
tests/test_correctness.py::test_layer_norm_autograd_matches_cpu_reference PASSED                                                                        [ 59%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_only] PASSED                                                                  [ 63%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[gpt2_style] PASSED                                                 [ 68%]
tests/test_correctness.py::test_edge_case_shape_N_eq_1 PASSED                                                                                           [ 72%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_bert] PASSED                                               [ 77%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large] PASSED                                              [ 81%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_bf16] PASSED                                         [ 86%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[llm_hidden_4096] PASSED                                            [ 90%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile8_small] PASSED                                                [ 95%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile64_medium] PASSED                                              [100%]

========== 22 passed in 9.26s ===================================

Used AI assistance from Cursor.

Short performance reproducer:
[layer_norm_gamma_beta_backward_reproducer.py](https://github.com/user-attachments/files/31031711/layer_norm_gamma_beta_backward_reproducer.py)
[run.sh](https://github.com/user-attachments/files/31031712/run.sh)

Pull Request resolved: pytorch#189405
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
(cherry picked from commit e61531f)
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…two pass implementations where they are most efficient. (pytorch#189405)

Hugging Face model dropped **5-10%** after switching to tiled kernel (10+ model and tests).

Implemented mixed approach, using combination of Tiled and Two Pass:

Hugging Face (huggingface_bart) performance is back:
Legacy Two pass performance=1652, 1644
Tiled performance                   =**1574, 1568**
Mixed performance                 =1649, 1652

Synthetic reproducer where performance from chess board like became  uniform, keeping benefits from both implementation:

# Layer Norm Backward Benchmark: Tiled only vs Tiled+Two pass

**Device:** AMD Instinct MI350X | **Warmup:** 20 | **Iters:** 100 | **Runs averaged:** 3

## Summary (avg µs over 3 runs)

| Benchmark | Op | Shape | dtype | Branch | Tiled only | Tiled+Two pass | Δ (µs) | Speedup | Winner |
|-----------|-----|-------|-------|--------|------------|----------------|--------|---------|--------|
| tile8_small | layer_norm | (32, 512) | float16 | Tile-8 | 7.94 | 8.16 | -0.22 | 0.97× | ~tie |
| tile64_medium | layer_norm | (96, 768) | float16 | Tile-64 | 20.71 | 20.78 | -0.07 | 1.00× | ~tie |
| tile128_medium | layer_norm | (192, 1024) | bfloat16 | Tile-128 | 8.20 | 8.51 | -0.31 | 0.96× | ~tie |
| tile256_large | layer_norm | (4096, 1024) | float16 | Tile-256 | 31.07 | 13.12 | +17.95 | 2.37× | Two pass |
| tile256_bert | layer_norm | (1024, 768) | float16 | Tile-256 BERT | 11.15 | 11.05 | +0.10 | 1.01× | ~tie |
| tile256_large_bf16 | layer_norm | (4096, 1024) | bfloat16 | Tile-256 | 31.28 | 13.10 | +18.18 | 2.39× | Two pass |
| tile256_large_fp32 | layer_norm | (4096, 1024) | float32 | Tile-256 | 30.87 | 15.90 | +14.97 | 1.94× | Two pass |
| two_pass_huge_M | layer_norm | (131072, 64) | float16 | Two-pass M-parallel | 26.48 | 27.70 | -1.22 | 0.96× | ~tie |
| llm_hidden_4096 | layer_norm | (32, 4096, 4096) | bfloat16 | Tile-256 LLM | 509.65 | 513.38 | -3.73 | 0.99× | ~tie |
| gpt2_style | layer_norm | (8, 1024, 768) | float16 | Tile-256 GPT2 | 58.22 | 16.56 | +41.66 | 3.52× | Two pass |
| rms_tile256 | rms_norm | (4096, 1024) | float16 | Tile-256 rms | 22.34 | 11.18 | +11.16 | 2.00× | Two pass |
| rms_llm | rms_norm | (32, 4096, 4096) | bfloat16 | Tile-256 rms LLM | 476.18 | 476.03 | +0.15 | 1.00× | ~tie |
| rms_two_pass | rms_norm | (131072, 64) | float16 | Two-pass rms | 16.73 | 17.29 | -0.56 | 0.97× | ~tie |

Δ = Tiled only − Tiled+Two pass (negative = Two pass faster). Speedup = Tiled only / Tiled+Two pass.

Also result of generated test for correcteness:
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile128_medium] PASSED                                             [  4%]
tests/test_correctness.py::test_rms_norm_autograd_matches_cpu_reference PASSED                                                                          [  9%]
tests/test_correctness.py::test_edge_case_shape_M_eq_1 PASSED                                                                                           [ 13%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_two_pass] PASSED                                                      [ 18%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_and_dbeta] PASSED                                                             [ 22%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_tile256] PASSED                                                       [ 27%]
tests/test_correctness.py::test_noncontiguous_input_matches_reference PASSED                                                                            [ 31%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[two_pass_huge_M] PASSED                                            [ 36%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dbeta_only] PASSED                                                                   [ 40%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_fp32] PASSED                                         [ 45%]
tests/test_correctness.py::test_rms_norm_gamma_backward_matches_cpu_reference[rms_llm] PASSED                                                           [ 50%]
tests/test_correctness.py::test_configs_cover_all_tile_branches PASSED                                                                                  [ 54%]
tests/test_correctness.py::test_layer_norm_autograd_matches_cpu_reference PASSED                                                                        [ 59%]
tests/test_correctness.py::test_output_mask_selects_expected_grads[dgamma_only] PASSED                                                                  [ 63%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[gpt2_style] PASSED                                                 [ 68%]
tests/test_correctness.py::test_edge_case_shape_N_eq_1 PASSED                                                                                           [ 72%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_bert] PASSED                                               [ 77%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large] PASSED                                              [ 81%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile256_large_bf16] PASSED                                         [ 86%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[llm_hidden_4096] PASSED                                            [ 90%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile8_small] PASSED                                                [ 95%]
tests/test_correctness.py::test_layer_norm_gamma_beta_backward_matches_cpu_reference[tile64_medium] PASSED                                              [100%]

========== 22 passed in 9.26s ===================================

Used AI assistance from Cursor.

Short performance reproducer:
[layer_norm_gamma_beta_backward_reproducer.py](https://github.com/user-attachments/files/31031711/layer_norm_gamma_beta_backward_reproducer.py)
[run.sh](https://github.com/user-attachments/files/31031712/run.sh)

Pull Request resolved: pytorch#189405
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rocm-mi300 Trigger "default" config CI on ROCm MI300 ciflow/rocm-mi355 Trigger "default" config CI on ROCm MI355 runners ciflow/trunk Trigger trunk jobs on your pull request keep-going Don't stop on first failure, keep running tests until the end Merged module: rocm AMD GPU support for Pytorch open source release notes: cuda release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants