Repository navigation
Conversation
PR Status: in progressTo become ready for review, please address automated review on your PR either with a code/PR description change. If a review comment is irrelevant, react with 👎 on that specific comment to reject it, and leave a short reply explaining why. The change must pass any running CI and existing failures must be classified as unrelated (see status below in this comment). Passing automated review does not guarantee that this PR will be merged. It still requires maintainer review and may be closed if its design or direction does not align with the project. To minimize iteration time, feel free to run the pr-review skill from the repo locally. To trigger a fresh automated review, make a new push or comment @pytorchbot review. To bypass automated review, please add the "no automated review" label. ✅ AUTOMATED REVIEW - Ready for human review:
Findings (5): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/203408
Note: Links to docs will display an error until the docs builds have been completed.
|
|
Maybe related issue, but for (bool, int32) pair: |
|
Thanks for the pointer! #55366 is the integer side of the same problem. I have a local fix for it and posted the details and numbers there: #55366 (comment) Drafted with Claude (AI) and edited by me. |
`sum(dtype=torch.float32)` and `mean` on a bfloat16 or float16 CPU tensor were 5x to 25x slower than a plain `sum`. `sum_out` builds its iterator with `make_reduction_from_out_ty`, which keeps a reduced floating point input as is only on CUDA, XPU and MPS; on CPU it reduced `self.to(kFloat)`, a full float32 copy of the input. `mean_out` always takes that path for bfloat16/float16, for accuracy. The CPU sum kernel already loads bfloat16/float16 and accumulates in float, so `cascade_sum` now also writes a float result for a reduced floating point input, and `sum_out` uses it on CPU. `make_reduction_from_out_ty` is left alone, since prod, mean on other devices and all/any share it. `should_use_acc_buffer` is now keyed on the output dtype, because the input and output dtypes can differ. Fixes pytorch#202882 Test Plan: ``` python -m pytest -q test/test_reductions.py python -m pytest -q test/test_ops.py -k "TestCommonCPU and (sum or mean) and (bfloat16 or float16)" python -m pytest -q test/test_linalg.py -k "norm and cpu" lintrunner --take CLANGFORMAT,RUFF,FLAKE8 aten/src/ATen/native/ReduceOps.cpp aten/src/ATen/native/cpu/SumKernel.cpp test/test_reductions.py ``` Run in WSL on a CPU build of main at 87f51fb (pytorch#202533 landed) with and without this change. test_reductions.py: 4517 passed with both, including the new `test_sum_lowp_float_out`, which covers full, dim=0, dim=1 and transposed/strided/unsqueezed inputs above GRAIN_SIZE for both dtypes. TestCommonCPU bfloat16/float16 sum/mean: 44 passed. test_linalg.py -k "norm and cpu": 43 passed. Medians of 3 interleaved runs on an i7-11800H (AVX512): `mean` and `sum(dtype=torch.float32)` on 4096x4096 and 1e7-element bfloat16/float16 inputs are 4.6x to 18.9x faster at 1 thread and 6.8x to 27.9x at 4 threads (dim=0 gains least); a same-dtype `sum` stays within noise (0.89x to 1.11x). Authored with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 <[email protected]>
2c2728c to
35d10e8
Compare
There was a problem hiding this comment.
Automated review of 35d10e8: nothing blocking; ready for human review.
On CPU, the PR makes bfloat16/float16 sum(dtype=torch.float32) and mean read the low-precision input directly and accumulate in float, instead of first making a float32 copy, and the review found no correctness, security or backward-compatibility bugs. Recommendation: Approve, with every point raised labelled non-blocking.
⚪ 5 non-blocking findings
(non-blocking) The new branch checks only iter.input_dtype() != iter.dtype(), then hardcodes out_t = float. The PR removes the static_assert that guarded the identity map's same-dtype assumption, so nothing checks this precondition any more. No caller can hit this today, but a future iterator with bf16 input and, say, double output would write the wrong element width: float stores for partial reductions, and a bf16 store through the else at line 701 for full reductions. cascade_powsum_kernel in the same file (lines 756760) checks both dtypes. Either tighten the condition to isReducedFloatingType(iter.input_dtype()) && iter.dtype() == kFloat, or add TORCH_INTERNAL_ASSERT(iter.dtype() == kFloat) in the branch.
pytorch/aten/src/ATen/native/ReduceOps.cpp
Line 1486 in 35d10e8
(non-blocking) The PR deletes the comment about materializing the FP32 copy, but the comment kept above it (lines 1474-1476) still describes the CPU flow as cast_fp32 -> sum -> div -> cast_bf16_or_fp16. The input is no longer cast; it is accumulated in fp32. Suggested wording: "accumulate in fp32 -> div -> cast".
pytorch/test/test_reductions.py
Line 1422 in 35d10e8
(non-blocking) test_sum_lowp_float_out calls t.double() but has no @ skipIfMPS. TestReductionsDevice is instantiated with allow_mps=True (line 4310), and MPS has no float64. The neighbouring test_sum_noncontig_lowp (line 1374) and other float64 tests in this class (e.g. lines 551 and 2928) skip MPS for this reason. CI's MPS job doesn't run this file, so only local and out-of-tree MPS runs will hit the failure.
(non-blocking) Bitwise results change: CPU sum(bf16/fp16, dtype=float) and mean(bf16/fp16) won't be bit-identical to earlier releases. Accuracy is not worse; both paths accumulate in fp32. Two things change the summation order: InnerSumCastLoadPolicy (SumKernel.cpp:116-130) now loads a full bf16 vector, converts it to two float vectors and adds them before accumulating; and multithreaded full reductions now combine per-thread results with std::plus in at::parallel_reduce instead of the cascade loop in two_pass_reduction. The description says "BC-breaking? No" and doesn't mention this. Please note the last-bit change in the release notes, since downstream tests comparing exactly against old outputs may change. The release notes: linalg_frontend label also looks wrong for a CPU sum/mean change.
pytorch/aten/src/ATen/native/ReduceOps.cpp
Line 1313 in 35d10e8
(non-blocking follow-up) The accumulation-buffer path for permuted same-dtype bf16/fp16 sums (issue # 83149) at line 1328 still runs at::sum_outf(self.to(ScalarType::Float), ...), which makes the full float copy this PR removes elsewhere. Fix: at::sum_outf(self, opt_dim, keepdim, /*dtype=*/kFloat, tmp_output). The meta function accepts this call, and mean_out already makes it at line 1486. On re-entry cpu_lowp_to_f32 is true and should_use_acc_buffer returns false, so the recursion ends at sum_stub. The kernel accumulates straight into the float buffer, so the fix for # 83149 still holds.
This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review
|
For CUDA, it would still copy? |
|
@vadimkantorov I was wondering about that too, so I looked into the GPU path. It turns out there's no copy happening there. I've included the details below.
|
|
@frost-intel Would you mind taking a look at this PR when you get a chance?
|
Linked issue or supporting maintainer
Fixes #202882
Supported by @frost-intel
BC-breaking?
No
Summary (human written only)
See #202882. On CPU, bfloat16/float16
meanandsum(dtype=torch.float32)now accumulate in float directly instead of reducing a float32 copy of the input.Agent details (if applicable)
🤖 Generated with Claude Code
cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @aditew01