Skip to content

[native_group_norm] Do some basic code cleanups - #190246

Closed
benjaminglass1 wants to merge 10 commits into
gh/benjaminglass1/164/basefrom
gh/benjaminglass1/164/head
Closed

benjaminglass1 wants to merge 10 commits into
gh/benjaminglass1/164/basefrom
gh/benjaminglass1/164/head

Conversation

@benjaminglass1

@benjaminglass1 benjaminglass1 commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

[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/190246

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

✅ No Failures

As of commit 873024b with merge base 526b9ce (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added the module: cpu CPU specific problem (e.g., perf, algorithm) label Jul 16, 2026
benjaminglass1 added a commit that referenced this pull request Jul 16, 2026
1. Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr).
2. Remove a parameter to infinitely_differentiable_native_group_norm_backward that was unused.
3. Ensure gradient outputs from infinitely_differentiable_native_group_norm_backward don't read from undefined tensors.


ghstack-source-id: 13cd28a
Pull-Request: #190246
@benjaminglass1 benjaminglass1 self-assigned this Jul 16, 2026
@benjaminglass1 benjaminglass1 added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category labels Jul 16, 2026
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 18, 2026
1. Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr).
2. Remove a parameter to infinitely_differentiable_native_group_norm_backward that was unused.
3. Ensure gradient outputs from infinitely_differentiable_native_group_norm_backward don't read from undefined tensors.


ghstack-source-id: 7896e85
Pull-Request: #190246
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 20, 2026
1. Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr).
2. Remove a parameter to infinitely_differentiable_native_group_norm_backward that was unused.
3. Ensure gradient outputs from infinitely_differentiable_native_group_norm_backward don't read from undefined tensors.


ghstack-source-id: 1f067cd
Pull-Request: #190246
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 20, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

ghstack-source-id: bec4051
Pull-Request: #190246
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 20, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

ghstack-source-id: 33ae8a4
Pull-Request: #190246
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 21, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

ghstack-source-id: 4e4aca5
Pull-Request: #190246
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 21, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

ghstack-source-id: ec50c86
Pull-Request: #190246
benjaminglass1 added a commit that referenced this pull request Jul 21, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

ghstack-source-id: ec50c86
Pull-Request: #190246
[ghstack-poisoned]
benjaminglass1 added a commit that referenced this pull request Jul 22, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

ghstack-source-id: 2f5ff87
Pull-Request: #190246
@benjaminglass1
benjaminglass1 requested a review from slayton58 July 22, 2026 17:36
@benjaminglass1
benjaminglass1 marked this pull request as ready for review July 22, 2026 17:36
[ghstack-poisoned]
@slayton58

Copy link
Copy Markdown
Contributor

@claude do a review in a collapsed comment, focusing on whether there are any unintended side-effects of the code-cleanup, notably any behaviour changes.

@claude

claude Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

Claude finished @slayton58's task in 1m 22s —— View job


Reviewed for behavior changes: the nullptr-to-implicit-bool rewrites and reformatting are all semantics-preserving, but two changes go beyond cosmetic cleanup — the CUDA HxW == 1 early-return was moved earlier, and the CPU backward arrays are now value-initialized — both are correct/beneficial and worth calling out in the description.

Full review: unintended side-effects / behavior changes

Summary

This PR converts (ptr == nullptr)-style checks to implicit bool(ptr), flips the associated ternary branches, and does assorted reformatting across the CPU/CUDA group_norm kernels and the autograd dispatcher. I traced every hunk for behavior changes; the ternary flips are all logically identical, but a few hunks are more than pure cleanup and deserve mention.

Behavior-preserving (verified)

  • Ternary branch flips (both .cpp and .cu): every (x == nullptr) ? A : B → x ? B : A swaps the operands along with the condition, so the result is unchanged. This includes the pointer-select cases like gamma_null ? gamma_data : (gamma_data + g*D) → gamma_data ? (gamma_data + g*D) : nullptr (when the pointer is null both forms yield nullptr).
  • at::empty({...}, ...).zero_() → at::zeros({...}, ...) (cpp lines ~380, ~1424): semantically equivalent zero-filled allocation.
  • enable_if<...>::type → enable_if_t<...> and std::tuple<A,B>(x,y) → std::make_tuple(x,y): no runtime change.
  • FunctionsManual.cpp get_var_mean lambda: the old code was var_mean_backward(...) + (dX.defined() ? dX : _efficientzerotensor(...)); the new code is dX.defined() ? (dX + get_var_mean()) : get_var_mean(). When dX is undefined this drops a mathematically-no-op add of a zero tensor; when defined it's dX + var_mean vs. var_mean + dX (addition is commutative). Same result, and it avoids materializing the efficient-zero tensor.
  • Hoisted gamma_vec in CalcInternalGradientsChannelsLast (cpp ~1245, ~1263): the load previously appeared twice inside two ternaries; folding it into one Vec gamma_vec{...} computes the identical value once instead of twice. Minor perf win, no numeric change.

More than cleanup — please confirm these are intended

  1. CUDA HxW == 1 early-return relocation (group_norm_kernel.cu, GroupNormBackwardKernelImplInternal): the if (HxW == 1) { GroupNorm1dBackward(...); return; } block was moved from after the kAccType/ds/db allocations to before them. This is correct — GroupNorm1dBackward doesn't touch ds/db — and is actually a small improvement (avoids two allocations on the HxW == 1 path). But it is a logic reordering, not a nullptr rewrite, so it's worth noting explicitly in the PR description.

  2. std::array value-initialization (GroupNormInputBackward, cpp ~666): std::array<...> ds_arr; /* NOLINT uninitialized */ → ds_arr{}; (and db_arr). This adds zero-initialization that the old code deliberately skipped. It's safe — CalcDsDb stores a full vector into each array before it's read by std::accumulate — so output is unchanged; the cost is a negligible one-time zero-fill. No behavior change, but note it's a real semantic change from the suppressed-uninitialized pattern rather than reformatting.

Signature change

  • GroupNorm1dBackward params changed from const Tensor (by value) to const Tensor& (.cu ~696). This is a file-local static/anonymous-namespace helper, so no ABI/BC concern; behavior identical, just avoids refcount bumps.

Recommendation

Approve — no unintended behavior changes found. The only items beyond pure cleanup (the CUDA early-return move and the array value-init) are correct and, if anything, small improvements; consider a one-line mention in the PR description since they exceed the stated "make nullptr checks implicit bool" scope.

· branch `gh/benjaminglass1/164/head`

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

I'm comfortable with the changes and claude's assertions of unchanged functionality (modulo the flagged minor changes that I'm fine with)

[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #190794

@albanD
albanD removed their request for review July 24, 2026 22:18
Harsha0Sree pushed a commit to Harsha0Sree/pytorch that referenced this pull request Jul 25, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

Pull Request resolved: pytorch#190246
Approved by: https://github.com/slayton58
ghstack dependencies: pytorch#186414
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

Pull Request resolved: pytorch#190246
Approved by: https://github.com/slayton58
ghstack dependencies: pytorch#186414
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

Pull Request resolved: pytorch#190246
Approved by: https://github.com/slayton58
ghstack dependencies: pytorch#186414
@github-actions
github-actions Bot deleted the gh/benjaminglass1/164/head branch August 24, 2026 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: cpu CPU specific problem (e.g., perf, algorithm) open source topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants