Repository navigation
[ROCm] Optimize AMD normalization backward kernel by using tiled and two pass implementations where they are most efficient. - #189405
anatoliylitv wants to merge 11 commits into
Conversation
|
@claude are these changes specific to the ROCm backend or do they affect the CUDA implementation as the PR title suggests? |
|
Claude finished @eqy's task in 40s —— View job AnswerThese 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 The single edited hunk is entirely inside an } 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
}( Concretely:
A couple of follow-up suggestions:
|
|
Warning: Unknown label
Please add the new label to .github/pytorch-probot.yml |
|
|
|
@pytorchbot rebase |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
|
Successfully rebased |
e6b590f to
ae23619
Compare
jeffdaily
left a comment
There was a problem hiding this comment.
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+cuComputeGradGammaBetagamma/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 whengammais undefined.layer_norm_backward_cudapasses*gammathrough unconditionally, andF.layer_norm(x, shape, weight=None, bias=b)withbias.requires_grad=TruereachesLayerNormBackwardKernelImplInternalwith an undefinedgammaand a defineddbeta(grad_input_mask == {*, false, true}).TensorBase::options()callsdevice(), which forUndefinedTensorImplhitsTORCH_CHECK(device_opt_.has_value(), "tensor does not have a device")(c10/core/TensorImpl.h:1302;UndefinedTensorImplis constructed withstd::nulloptfor device atc10/core/UndefinedTensorImpl.cpp:8). This is a regression againstmain, where that shape range goes through the tiled kernel and never touchesgamma.Use
X.options().dtype(part_grad_dtype)instead. That also lets you drop thegammaparameter fromLaunchTwoPassGammaBetaBackwardCUDAKernelentirely.
layer_norm_kernel.cu:1744- typohich->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:1764and1770-1771- misindented closing brace and under-indented call.
layer_norm_kernel.cu:1767-1771- the two branches use different calling conventions: the tiled launcher takesTensor* dgamma, Tensor* dbeta, while the new two-pass launcher takes rawT*and forces the caller to re-extract the pointers. HaveLaunchTwoPassGammaBetaBackwardCUDAKerneltakeTensor*and do thedefined() ? data_ptr<T>() : nullptrextraction internally, matchingConfigureAndLaunchGammaBetaBackwardKernel(layer_norm_kernel.cu:967-969).API Design
layer_norm_kernel.cu:1746-1747-M > 64 * 1024 && N / warp_size < sm_count / 2is a verbatim copy of the predicate insideLaunchGammaBetaBackwardCUDAKernel(layer_norm_kernel.cu:1004). Two problems:
- 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.
- As written, ROCm now only ever enters the
ifside of that predicate, so the entireelsebranch ofLaunchGammaBetaBackwardCUDAKernel(layer_norm_kernel.cu:1047-1075) is dead code underUSE_ROCM. That includes both#ifdef USE_ROCMtuning blocks: theblock_dim_y=16, 256CDNA cap added by #179019 and theblock_dim_y=32RDNA 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- theTORCH_INTERNAL_ASSERTon an unexpected ROCm warp size now only fires on the huge-M path. ForM <= 65536an unexpected warp size falls into the two-pass launcher, wherecuComputeGradGammaBetacomputesnum_warp_reductions = part_size / blockDim.ywithpart_size == warp_sizeandblockDim.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 == 65536switch boundary (andM == 128).- Any case with
M > 65536whereN / 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 fromtorch.cuda.get_device_properties().multi_processor_countrather than hardcoding, since the threshold is arch-dependent (N > 9728 on gfx942, N > 3328 on gfx90a).- The
weight=None, bias.requires_grad=Truecase that triggers the crash above.nn.LayerNormcannot produce it (elementwise_affine=Falsealso drops bias), so it needs an explicitF.layer_normcall.There is no rms_norm backward numerics test at any meaningful size either (
sample_inputs_rms_normincommon_methods_invocations.py:4694only reaches M<=2), yet this PR changes the rms_norm backward dispatch too.Performance
The
tile128_mediumrow (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. |
|
@pytorchbot rebase |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
|
Successfully rebased |
…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.
…dKernelImplInternal to use arrow operator.
…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.
|
Successfully rebased |
7a984dd to
5e4cae2
Compare
|
@pytorchbot merge |
Merge startedYour 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 |
…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)
…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]>
…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)
…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)
…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]>
…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)
…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]>
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)
Δ = 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