Skip to content

Keep selective builds working with the CPU cascade norm kernel - #205834

Closed
ngimel wants to merge 1 commit into
mainfrom
ngimel/powsum-selective-build
Closed

ngimel wants to merge 1 commit into
mainfrom
ngimel/powsum-selective-build

Conversation

@ngimel

@ngimel ngimel commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

#202533 moved CPU vector_norm and _powsum with ord 0, 1 and 2 to a new cascade_powsum_kernel dispatched under a new kernel tag, cascade_powsum_cpu. Selective (mobile) builds keep only the dtypes their traced model lists name for each tag, and lists traced before #202533 have norm_cpu and powsum_cpu but not the new tag, so those builds now fail with "dtype 'Float' not selected for kernel tag cascade_powsum_cpu".

Dispatch the cascade kernel under the tags of the kernels it replaces, and fall back to the old kernel when the build did not select the input dtype for that tag (e.g. a half input with a float output, which the old code reduced without a tagged dispatch). Builds traced before #202533 then behave as before or use the new kernel; full builds are unchanged. Regenerating every traced dtype list instead would leave any app not re-traced broken at runtime.

Test Plan:
Compiled SumKernel.cpp before and after this change, normally and with -DTEMPLATE_SELECTIVE_BUILD and a selected_mobile_ops.h selecting only Float for norm_cpu, powsum_cpu and sum_cpu. With the selection, the old object has no float cascade_powsum kernels (the dispatch throws), the new one has them:

nm -C sel.o | grep -o 'cascade_sum<[^(]*'
# before: IdentityMap, float, float
# after:  AbsMap, IdentityMap, NonZeroMap, SquareMap (float, float)
lintrunner -a aten/src/ATen/native/cpu/SumKernel.cpp

Authored with an AI assistant.

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @aditew01

#202533 moved CPU vector_norm and _powsum with ord 0, 1 and 2 to a new
cascade_powsum_kernel dispatched under a new kernel tag, cascade_powsum_cpu.
Selective (mobile) builds keep only the dtypes their traced model lists name
for each tag, and lists traced before #202533 have norm_cpu and powsum_cpu but
not the new tag, so those builds now fail with "dtype 'Float' not selected for
kernel tag cascade_powsum_cpu".

Dispatch the cascade kernel under the tags of the kernels it replaces, and fall
back to the old kernel when the build did not select the input dtype for that
tag (e.g. a half input with a float output, which the old code reduced without
a tagged dispatch). Builds traced before #202533 then behave as before or use
the new kernel; full builds are unchanged. Regenerating every traced dtype list
instead would leave any app not re-traced broken at runtime.

Test Plan:
Compiled SumKernel.cpp before and after this change, normally and with
-DTEMPLATE_SELECTIVE_BUILD and a selected_mobile_ops.h selecting only Float for
norm_cpu, powsum_cpu and sum_cpu. With the selection, the old object has no
float cascade_powsum kernels (the dispatch throws), the new one has them:

```
nm -C sel.o | grep -o 'cascade_sum<[^(]*'
# before: IdentityMap, float, float
# after:  AbsMap, IdentityMap, NonZeroMap, SquareMap (float, float)
lintrunner -a aten/src/ATen/native/cpu/SumKernel.cpp
```

Authored with an AI assistant.
@pytorch-bot pytorch-bot Bot added the module: cpu CPU specific problem (e.g., perf, algorithm) label Nov 20, 2026
@pytorch-bot

pytorch-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - Ready for human review:

The PR runs the CPU cascade vector_norm/_powsum kernel under the old
norm_cpu/powsum_cpu tags, falling back to the old kernel when a selective build
did not select the dtype. This fixes builds traced before # 202533 but not
builds traced while # 202533 was in. Recommendation: Needs Discussion. Whether
those builds break depends on an open question for maintainers (was any
production list traced while # 202533 was in?), so no defect was named that must
be fixed unconditionally.

Findings (4):

[minor] aten/src/ATen/native/cpu/SumKernel.cpp:771
Builds traced after # 202533 and before this PR now fail at runtime. With #
202533 in, a tracer build running float/double vector_norm or _powsum with ord
0, 1 or 2 records `cascade_powsum_cpu$Float` and never reaches the
norm_cpu/powsum_cpu dispatches, so `norm_cpu$Float` is missing unless another
ord or a complex input was used. With this PR,
`should_include_kernel_dtype("norm_cpu", Float)` returns false, and the fallback
at ReduceOpsKernel.cpp:221 throws `dtype 'Float' not selected for kernel tag
norm_cpu`. The same happens for Double, and for Half/BFloat16 reduced to a
one-element result of the same dtype. Those apps work today and would break
unless re-traced, which is the outcome the PR gives as its reason not to
regenerate lists. Fix: also take the cascade path when
`should_include_kernel_dtype("cascade_powsum_cpu", in)` is true. That needs a
third AT_DISPATCH under that literal, which only adds a switch because the
instantiations are shared. Or state in the PR that no list was traced or shipped
while # 202533 was in. Must land before merge if such lists exist.

[minor] aten/src/ATen/native/cpu/SumKernel.cpp:773
Low confidence, non-blocking: pre-# 202533 lists can still fail inside the
cascade kernel on `fill_`. For outputs with more than one element, cascade_sum
(SumKernel.cpp:707) calls `iter.output_base().fill_(out_t(0))`, which goes
through the dispatcher and the "fill_cpu" dtype dispatch. The old
binary_kernel_reduce path never calls fill_. So a list with `norm_cpu$Float` but
not `aten::fill_.Scalar` or `fill_cpu$Float` now passes this check and then
fails. Most real lists probably have fill_, but the claim that such builds
"behave as before or use the new kernel" is not guaranteed, and a test plan that
only compiles and inspects symbols would not catch it. Related, not new here:
cpu_powsum_in_float (LinearAlgebra.cpp:2894-2910) still sends Half/BFloat16
results with more than one element through `result.copy_` whatever the build
selected.

[minor] aten/src/ATen/native/cpu/SumKernel.cpp:768
Non-blocking: two comments now misstate when the kernel returns false or falls
back. SumKernel.h:15-22 still says cascade_powsum_kernel returns false only for
unsupported p or dtypes. It now also returns false when the build did not select
`in` for the tag, so callers must keep the fallback even for supported p and
dtypes. The comment here says an unselected dtype "falls back to the old kernel
instead of throwing". That only holds for Half/BFloat16 input with Float output,
which uses the untagged branch at ReduceOpsKernel.cpp:213-218 / :448-451. Every
other case dispatches under the same tag on the same dtype and still throws.

[minor] aten/src/ATen/native/cpu/SumKernel.cpp:777
Non-blocking: the tag strings are duplicated and must be kept in sync by hand
(line 771 vs 777 and 781), and also with the literals at ReduceOpsKernel.cpp:221
and :454. If they drift, the check passes for a dtype the dispatch did not
select, and the call throws instead of falling back. Fix: move the
should_include_kernel_dtype check into each `if (is_norm)` branch next to its
AT_DISPATCH, with one named constexpr string per branch. The restructure for the
cascade_powsum_cpu tag finding is a natural place to do this.

Reviewed commit: 3b4a687

Review run

🐛 Report a bad review

🔗 Helpful Links

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

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

✅ No Failures

As of commit 3b4a687 with merge base 944193e (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - PR requires human review:

  • Regression for dtype lists traced after #​202533
    • The diff renames the cascade dispatch tag from cascade_powsum_cpu to norm_cpu/powsum_cpu in SumKernel.cpp
    • AT_DISPATCH_SWITCH in Dispatch.h records NAME via RECORD_KERNEL_FUNCTION_DTYPE, so recent traces list cascade_powsum_cpu$Float and no norm_cpu$Float
    • Such builds now fail should_include_kernel_dtype and take the old norm_cpu dispatch in ReduceOpsKernel.cpp, which throws for Float
  • Fix is otherwise sound
    • Full builds unchanged: the default should_include_kernel_dtype in Dispatch.h always returns true
    • Half/BFloat16 to Float fallback uses the untagged norm_kernel_cpu_impl path in norm_kernel_tensor_iterator_impl
  • Human call needed
    • The description covers lists traced before #​202533 but not ones traced since; checking both tags would cover both

reason: possible_regression
Reviewed commit: 3b4a687

Inference job

🐛 Report a wrong verdict

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

@pytorchgreenlight pytorchgreenlight Bot added the GL/human GreenLight marked as needing human review label Nov 20, 2026
@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Nov 20, 2026

@github-actions github-actions Bot 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.

Automated review of 3b4a687: nothing blocking; ready for human review.

The PR runs the CPU cascade vector_norm/_powsum kernel under the old norm_cpu/powsum_cpu tags, falling back to the old kernel when a selective build did not select the dtype. This fixes builds traced before # 202533 but not builds traced while # 202533 was in. Recommendation: Needs Discussion. Whether those builds break depends on an open question for maintainers (was any production list traced while # 202533 was in?), so no defect was named that must be fixed unconditionally.

⚪ 4 non-blocking findings

const char* tag = is_norm ? "norm_cpu" : "powsum_cpu";

Builds traced after # 202533 and before this PR now fail at runtime. With # 202533 in, a tracer build running float/double vector_norm or _powsum with ord 0, 1 or 2 records cascade_powsum_cpu$Float and never reaches the norm_cpu/powsum_cpu dispatches, so norm_cpu$Float is missing unless another ord or a complex input was used. With this PR, should_include_kernel_dtype("norm_cpu", Float) returns false, and the fallback at ReduceOpsKernel.cpp:221 throws dtype 'Float' not selected for kernel tag norm_cpu. The same happens for Double, and for Half/BFloat16 reduced to a one-element result of the same dtype. Those apps work today and would break unless re-traced, which is the outcome the PR gives as its reason not to regenerate lists. Fix: also take the cascade path when should_include_kernel_dtype("cascade_powsum_cpu", in) is true. That needs a third AT_DISPATCH under that literal, which only adds a switch because the instantiations are shared. Or state in the PR that no list was traced or shipped while # 202533 was in. Must land before merge if such lists exist.


!at::should_include_kernel_dtype(tag, in)) {

Low confidence, non-blocking: pre-# 202533 lists can still fail inside the cascade kernel on fill_. For outputs with more than one element, cascade_sum (SumKernel.cpp:707) calls iter.output_base().fill_(out_t(0)), which goes through the dispatcher and the "fill_cpu" dtype dispatch. The old binary_kernel_reduce path never calls fill_. So a list with norm_cpu$Float but not aten::fill_.Scalar or fill_cpu$Float now passes this check and then fails. Most real lists probably have fill_, but the claim that such builds "behave as before or use the new kernel" is not guaranteed, and a test plan that only compiles and inspects symbols would not catch it. Related, not new here: cpu_powsum_in_float (LinearAlgebra.cpp:2894-2910) still sends Half/BFloat16 results with more than one element through result.copy_ whatever the build selected.


// Dispatch under the tags of the kernels this replaces, so that selective

Non-blocking: two comments now misstate when the kernel returns false or falls back. SumKernel.h:15-22 still says cascade_powsum_kernel returns false only for unsupported p or dtypes. It now also returns false when the build did not select in for the tag, so callers must keep the fallback even for supported p and dtypes. The comment here says an unselected dtype "falls back to the old kernel instead of throwing". That only holds for Half/BFloat16 input with Float output, which uses the untagged branch at ReduceOpsKernel.cpp:213-218 / :448-451. Every other case dispatches under the same tag on the same dtype and still throws.


AT_DISPATCH_FLOATING_TYPES_AND2(ScalarType::BFloat16, ScalarType::Half, in, "norm_cpu", [&] {

Non-blocking: the tag strings are duplicated and must be kept in sync by hand (line 771 vs 777 and 781), and also with the literals at ReduceOpsKernel.cpp:221 and :454. If they drift, the check passes for a dtype the dispatch did not select, and the call throws instead of falling back. Fix: move the should_include_kernel_dtype check into each if (is_norm) branch next to its AT_DISPATCH, with one named constexpr string per branch. The restructure for the cascade_powsum_cpu tag finding is a natural place to do this.

This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Nov 20, 2026
@izaitsevfb

Copy link
Copy Markdown
Contributor

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 20, 2026
@ngimel

ngimel commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team

Raised by workflow job

@izaitsevfb

Copy link
Copy Markdown
Contributor

@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

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 GL/human GreenLight marked as needing human review Merged module: cpu CPU specific problem (e.g., perf, algorithm) ready for review Automated review passed, ready for a final review by a maintainer. topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants