Skip to content

native_group_norm_backward: Fix precision errors - #190245

Closed
benjaminglass1 wants to merge 12 commits into
gh/benjaminglass1/163/basefrom
gh/benjaminglass1/163/head
Closed

benjaminglass1 wants to merge 12 commits into
gh/benjaminglass1/163/basefrom
gh/benjaminglass1/163/head

Conversation

@benjaminglass1

@benjaminglass1 benjaminglass1 commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Fixes missed upcasts in the CUDA kernel and decomposition for native_group_norm_backward. Re-works infinitely_differentiable_native_group_norm_backward to use native_group_norm_backward where possible (ensures we actually use the decomposition in cases where Inductor can't prove the other two gradients are unused). Fixes a broken Inductor benchmark in the pipeline, as well as three XFAILed tests.

Closes #180740

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 473c5f7 with merge base ae5ff14 (image):
💚 Looks good so far! There are no failures yet. 💚

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

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 21, 2026
Fixes missed upcasts in the CUDA kernel and decomposition for native_group_norm_backward.  Re-works infinitely_differentiable_native_group_norm_backward to use native_group_norm_backward where possible (ensures we actually use the decomposition in cases where Inductor can't prove the other two gradients are unused).  Fixes a broken Inductor benchmark in the pipeline, as well as three XFAILed tests.

Closes #180740

ghstack-source-id: 9f64c44
Pull-Request: #190245
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@jansel

jansel commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

@claude review these changes

@claude

claude Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

Claude finished @jansel's task in 4m 20s —— View job


PR Review: #190245

One sentence summary: The refactor is correct and the precision fixes are well-targeted; I only found minor wording nits, so this looks good to merge.

Full review details

Summary

Fixes missing accumulator upcasts in the 1D group-norm backward CUDA kernel and the addcmul decomposition, and restructures infinitely_differentiable_native_group_norm_backward into a native_group_norm_backward_dispatcher that routes through the fast native_group_norm_backward kernel when possible. I verified the split, the derivatives.yaml wiring, the eps drop, and the var_mean_backward contribution; the change is correct. Only two minor comment/wording issues below.

Code Quality

  • torch/csrc/autograd/FunctionsManual.cpp:5507 - The comment "dX is guaranteed to be defined below if we're here" is misleading. dX is only assigned when grad_input_mask[0] is true; when it's false (e.g. input.requires_grad=False but weight.requires_grad=True with dmean/drstd flowing), dX is correctly returned undefined. The behavior is right, but the comment overstates the guarantee. Suggest rewording to something like "dX is filled in below only when input needs a gradient; otherwise it is intentionally left undefined." The odd formatting of the helper signature at 5433 (std::\n tuple<...> static inline ...) is clang-format output and worth collapsing manually per the repo style guidance on readability, if it doesn't fight the linter.

Testing

  • test/test_decomp.py:227-230 - The added tolerance comment says the difference is "on float16", but the block adds entries for both bfloat16 and float16. The comment also references "an FMA for the eager operator" — reword to cover both dtypes (e.g. "...causing a difference on float16/bfloat16"). No functional issue.

Notes (verified, no action needed)

  • The dropped eps parameter only fed the dead var local in the old code (((rstd*rstd).reciprocal_() - eps).clamp_min(0) was never used), so removing it is safe.
  • The var_mean_backward reduction over X.reshape({N, group, C/group*HxW}) dim {2} is equivalent to the old {N,G,D,HxW} dims {2,3} (same D*HxW elements per group), and both undefined gvar/gmean are guarded inside var_mean_backward (FunctionsManual.cpp:2176,2179).
  • torch/_decomp/decompositions.py:1769 - torch.addcmul(-ds_val, db_val, mean) == db_val*mean - ds_val, and intentionally emits an FMA to match eager; algebraically correct.
  • The general (non-1D) ComputeBackwardFusedParamsCUDAKernel path already upcast (group_norm_kernel.cu:309-310); only the 1D path (:139) was missing it, so the targeted fix is complete.
  • No dangling references to the old symbol remain; derivatives.yaml:1291 passes the correct argument set to the new dispatcher.

Recommendation

Approve — the two items above are comment-only clarity fixes and don't block. The removed XFAIL/skip entries are appropriate since the fixes make those OpInfo/decomp tests pass, giving adequate regression coverage.

(branch: `gh/benjaminglass1/163/head`)

@slayton58

Copy link
Copy Markdown
Contributor

This looks good to me, I think the suggested comment change from janselbot's review is valid, and would be worth quickly doing, but otherwise nothing else I can see to mention.

@slayton58 slayton58 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These need to be tracked down before merge

I need more coffee, nothing to be changed here

[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 24, 2026
Fixes missed upcasts in the CUDA kernel and decomposition for native_group_norm_backward.  Re-works infinitely_differentiable_native_group_norm_backward to use native_group_norm_backward where possible (ensures we actually use the decomposition in cases where Inductor can't prove the other two gradients are unused).  Fixes a broken Inductor benchmark in the pipeline, as well as three XFAILed tests.

Closes #180740

ghstack-source-id: 25bd6d3
Pull-Request: #190245
benjaminglass1 added a commit that referenced this pull request Jul 24, 2026
Fixes missed upcasts in the CUDA kernel and decomposition for native_group_norm_backward.  Re-works infinitely_differentiable_native_group_norm_backward to use native_group_norm_backward where possible (ensures we actually use the decomposition in cases where Inductor can't prove the other two gradients are unused).  Fixes a broken Inductor benchmark in the pipeline, as well as three XFAILed tests.

Closes #180740

ghstack-source-id: 25bd6d3
Pull-Request: #190245
@albanD
albanD removed their request for review July 24, 2026 22:18
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 25, 2026
Fixes missed upcasts in the CUDA kernel and decomposition for native_group_norm_backward.  Re-works infinitely_differentiable_native_group_norm_backward to use native_group_norm_backward where possible (ensures we actually use the decomposition in cases where Inductor can't prove the other two gradients are unused).  Fixes a broken Inductor benchmark in the pipeline, as well as three XFAILed tests.

Closes #180740

ghstack-source-id: 776457a
Pull-Request: #190245
@benjaminglass1

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

aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Fixes missed upcasts in the CUDA kernel and decomposition for native_group_norm_backward.  Re-works infinitely_differentiable_native_group_norm_backward to use native_group_norm_backward where possible (ensures we actually use the decomposition in cases where Inductor can't prove the other two gradients are unused).  Fixes a broken Inductor benchmark in the pipeline, as well as three XFAILed tests.

Closes pytorch#180740

Pull Request resolved: pytorch#190245
Approved by: https://github.com/slayton58
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
Fixes missed upcasts in the CUDA kernel and decomposition for native_group_norm_backward.  Re-works infinitely_differentiable_native_group_norm_backward to use native_group_norm_backward where possible (ensures we actually use the decomposition in cases where Inductor can't prove the other two gradients are unused).  Fixes a broken Inductor benchmark in the pipeline, as well as three XFAILed tests.

Closes pytorch#180740

Pull Request resolved: pytorch#190245
Approved by: https://github.com/slayton58
pytorchmergebot pushed a commit that referenced this pull request Aug 1, 2026
…1601)

## Motivation

#190245 fixed missed upcasts in the CUDA `native_group_norm_backward` kernel/decomposition and replaced the previous `check_gradient: False` overrides with looser tolerances (`grad_atol=2e-3, grad_rtol=1e-3`). That PR left the CUDA override in place but removed the corresponding XPU override without adding anything back, leaving XPU on the default fp16 tolerance (`atol=1e-5, rtol=1e-3`) which is too tight for the residual 1-ULP rounding differences between the XPU aten backward kernel and the Inductor decomposition. Result: #185367 (`test_comprehensive_native_group_norm_xpu_float16`) has been disabled since May 2026.

## Root cause on the failing sample

Sample: `input=(1,6,3), groups=2, eps=0.5, fp16`

```
d_input:
  eager  vs ref-fp32: max_abs = 9.77e-04
  comp   vs ref-fp32: max_abs = 9.77e-04
  comp   vs eager   : max_abs = 9.77e-04    (>> default atol=1e-5)
```

Both eager and compiled backward stay within fp16 quantization of an fp32 reference, but they round to different fp16 values at some indices. The XPU aten kernel already accumulates in fp32 (`T_ACC`); the residual difference comes from the aten kernel executing as several TensorIterator launches vs Inductor materializing one fused fp32 graph. This is intrinsic fp16 rounding, not a correctness bug on either side, and matches the situation upstream addressed on CUDA in #190245.

## Fix

Align XPU with the CUDA precedent from the same PR by adding the same `(native_group_norm, f16)` override to `inductor_override_kwargs["xpu"]`.

## Test plan

Against nightly wheel `torch==2.14.0.dev20260728+xpu` on Intel Data Center GPU Max 1100:

```
python -m pytest test/inductor/test_torchinductor_opinfo.py -v \
    -k test_comprehensive_native_group_norm_xpu --timeout=600
# native_group_norm_xpu_float16  PASSED
# native_group_norm_xpu_float32  PASSED
# native_group_norm_xpu_float64  PASSED
# 3 passed, 7450 deselected
```

Fixes #185367

_Note: authored with AI assistance._
Pull Request resolved: #191601
Approved by: https://github.com/etaf, https://github.com/guangyey, https://github.com/jansel
@github-actions
github-actions Bot deleted the gh/benjaminglass1/163/head branch August 27, 2026 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Inductor] functorch_dp_cifar10 accuracy regression in dynamic_inductor_torchbench

5 participants