Repository navigation
[Inductor] Fix NaN output in LayerNorm CPU by guarding Welford variance. - #173989
karthik-0306 wants to merge 1 commit into
Conversation
|
|
|
@pytorchbot label "release notes: inductor" |
|
The label |
|
if it repros in cpu but not cuda it's probably not a lowering issue, just a codegen one. |
That makes sense, if CUDA isn't seeing this, it’s definitely a CPU codegen mismatch rather than a lowering bug. I'll dig into the generated C++ kernel for the Welford reduction and see where the inf handling is diverging from the Triton path. Will update once I've isolated the logic in the CPU backend. |
421f2ea to
0ae6ad7
Compare
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/173989
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ You can merge normally! (1 Unrelated Failure), 1 Unclassified FailureAs of commit 8b040c0 with merge base 868be1c ( UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:
UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
0ae6ad7 to
e6cfb7d
Compare
|
@claude explain those CI failures, check if they are related, and review the changes in this PR |
|
Claude finished @jansel's task in 2m 3s —— View job CI Failures Analysis & Code Review
CI FailuresAll 9 "New Failures" are unrelated to this PR. They all exit with code 127 ("command not found"), which indicates a missing binary or broken test environment — not a logic error from the code change. These are infrastructure failures (likely a test runner dependency issue on the None of these failures are related to the 1-line change in Code ReviewThe change ( @triton.jit
def welford_combine(mean_1, m2_1, weight_1, mean_2, m2_2, weight_2):
# Handle potential NaN if both means are infinite
delta = tl.where(mean_1 == mean_2, 0.0, mean_2 - mean_1)Issues
SummaryThe fix logic itself is sound (guarding |
e6cfb7d to
ae233e2
Compare
|
|
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows. |
|
Thanks for the review @jansel and @claude — the bot's analysis was spot on. cpp_prefix.h — added the isinf guard to the CPU C++ Welford combine (scalar path + vectorized blendv path), which is the actual root cause for the CPU bug The CI failures (exit code 127) are all pre-existing infrastructure issues unrelated to this change. |
|
@claude explain those CI failures |
|
Claude finished @jansel's task in 5m 12s —— View job CI Failure Analysis
FindingsThere are 2 failed workflow runs: 1. Lint failure (
|
ae233e2 to
8b040c0
Compare
|
Updated based on @claude bot's CI analysis: Lint fix — collapsed the tl.where(...) to a single line as flagged by PYFMT Both CI failures were caused by the same tl.math.isinf call. |
|
@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 |
Merge failedReason: 1 jobs have failed, first few of them are: inductor / unit-test / inductor-cpu-test / test (inductor_amx, 2, 2, linux.2xlarge.amx) Details for Dev Infra teamRaised by workflow job |
|
@jansel the merge failed due to the inductor_amx (2, 2) failure which is a pre-existing infra issue on the linux.2xlarge.amx runner — the annotation on the merge job itself shows it's a Node.js 20 deprecation issue on PyTorch's CI infrastructure. Note that inductor_amx (1, 2) passed on the same runner type, confirming this is not caused by this PR. The distributed job was also auto-cancelled after 245 minutes due to a runner timeout. Could you rerun the inductor_amx job or use @pytorchbot merge -f to merge past the infra failure? |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 2 checks: pull / linux-jammy-py3.10-gcc11 / test-osdc (distributed, 1, 3, mt-l-x86iamx-8-64), inductor / unit-test / inductor-cpu-test / test (inductor_amx, 2, 2, linux.2xlarge.amx) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
My oncall agent has flagged this PR to be responsible for compile time increase and speedup decreases. See the summary at internalfb.com/phabricator/paste/view/P2340863849 |
@anijain2305 would you prefer I revert the triton_helpers.py guard entirely since the original bug was CPU-only, or is there a preferred pattern in the codebase for guarding non-finite inputs in Triton reductions without hot path overhead? |
…ce. (pytorch#173989) Running torch.compile on CPU with float16 precision causes LayerNorm to produce NaN outputs when the input contains Inf values. This happens because the Welford variance calculation in lowering.py hits a numerical collapse ($\infty - \infty$) in the $M2$ accumulator, which then propagates as a NaN.This PR adds an ops.where guard in var_mean_welford_ to catch these non-finite results and reset them to 0.0. This ensures that even if the input overflows the precision range, the LayerNorm output remains numerically stable, matching the behavior of the CUDA backend and Eager mode. **Fixes**: pytorch#173885 Pull Request resolved: pytorch#173989 Approved by: https://github.com/jansel
…ce. (pytorch#173989) Running torch.compile on CPU with float16 precision causes LayerNorm to produce NaN outputs when the input contains Inf values. This happens because the Welford variance calculation in lowering.py hits a numerical collapse ($\infty - \infty$) in the $M2$ accumulator, which then propagates as a NaN.This PR adds an ops.where guard in var_mean_welford_ to catch these non-finite results and reset them to 0.0. This ensures that even if the input overflows the precision range, the LayerNorm output remains numerically stable, matching the behavior of the CUDA backend and Eager mode. **Fixes**: pytorch#173885 Pull Request resolved: pytorch#173989 Approved by: https://github.com/jansel
Running torch.compile on CPU with float16 precision causes LayerNorm to produce NaN outputs when the input contains Inf values. This happens because the Welford variance calculation in lowering.py hits a numerical collapse ($\infty - \infty$ ) in the $M2$ accumulator, which then propagates as a NaN.This PR adds an ops.where guard in var_mean_welford_ to catch these non-finite results and reset them to 0.0. This ensures that even if the input overflows the precision range, the LayerNorm output remains numerically stable, matching the behavior of the CUDA backend and Eager mode.
Fixes: #173885
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo @mlazos