Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/196646
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (2 Unrelated Failures)As of commit 13caca3 with merge base 253d0a2 ( UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@Nicoshev has exported this pull request. If you are a Meta employee, you can view the originating Diff in D119352973. |
|
Warning: Unknown label
Please add the new label to .github/pytorch-probot.yml |
f2cb963 to
2e44abb
Compare
…ch#196646) Summary: Fix NEON vectorized f32->i8/u8 conversion routines have a different behaviour than c10::convert/AVX The inputs differing are out-of-range, but f32->uint8_t has well defined behaviour in TypeCast.h to get consistent results across CPU/GPU f32->int8_t has well defined behaviour in AVX headers, clamping to support inputs fitting int32_ts Test Plan: CI Differential Revision: D119352973
|
@claude review this PR |
|
Claude finished @Skylion007's task in 42m 7s —— View job The Full reviewReview checklist
Blocking1.
@xfailIf(IS_ARM64) # see https://github.com/pytorch/pytorch/issues/168972
def test_float32_to_uint8(self):Correctness concerns2. The
Concretely,
The pre-existing ARM/x86 divergence for f32→i8 is much narrower than the PR implies: both platforms truncate the low byte for anything inside The PR description justifies this with "f32->int8_t has well defined behaviour in AVX headers, clamping to support inputs fitting int32_ts" — but that clamping lives in Which target is intended here — x86 eager ( 3. Same-arch behavior now depends on the host compiler The 4. Both helpers are templated on unconstrained } else if constexpr (src_n >= 2) {
return vcombine_s8(vqmovn_s16(to_i16(src[0], src[1])), zero);
}For Performance5. The The comment at 398–403 says converting via A So the elaborate double-widening path buys exactly "0 instead of 0xff", which a mask gets far more cheaply. Roughly: const float32x4_t bound = vdupq_n_f32(2147483648.0f); // 2^31
int32x4_t i = vcvtq_s32_f32(f);
// |f| >= 2^31 saturates; its true low byte is 0 (any such float is a
// multiple of 256), so clear those lanes instead of keeping 0x7fffffff.
i = vbicq_s32(i, vreinterpretq_s32_u32(vcageq_f32(f, bound)));That's ~2 extra instructions per 4 lanes versus the current 2x This matters most for the scalar 6.
Testing7. No test exercises any of the fixed behavior The whole PR is about out-of-range inputs, but the C++ vec suite can't see them: Given how subtle the saturation boundaries are ( Minor / style8. Name collides conceptually with the AVX helpers. 9. Missing 10. 11. Little-endian assumption. Remaining divergence (follow-up, not this PR)
What I verifiedI traced the intrinsic sequences by hand and they are internally self-consistent, which is the good news:
I did not build or run anything — no ARM64 hardware in this environment, and a PyTorch build plus the aarch64 vec test suite is out of reach here. All findings above are from reading the intrinsic semantics and the surrounding dispatch, so the behavior tables should be confirmed on real hardware before acting on them. |
2e44abb to
35ad278
Compare
…196646) Summary: Fix NEON vectorized f32->u8 quant conversion routines have a different behaviour than c10::convert/AVX The inputs differing are out-of-range f32->uint8_t has well defined behaviour in TypeCast.h, supporting inputs fitting up to an int64_t, to get consistent results across CPU/GPU Addresses: github.com/pytorch/issues/168972 Test Plan: CI Differential Revision: D119352973
|
@Skylion007 @aditew01 I just changed the PR to only change f32->u8 behaviour, which has consensus and has been reported as broken in github.com/pytorch/pytorch/issues/168972 I'll leave f32->i8 for another PR, as it has greater divergencies across platforms |
…196646) Summary: Fix NEON vectorized f32->u8 quant conversion routines have a different behaviour than c10::convert/AVX The inputs differing are out-of-range f32->uint8_t has well defined behaviour in TypeCast.h, supporting inputs fitting up to an int64_t, to get consistent results across CPU/GPU Addresses: github.com/pytorch/issues/168972 Test Plan: CI Differential Revision: D119352973
35ad278 to
fa2960f
Compare
…196646) Summary: Fix NEON vectorized f32->u8 quant conversion routines have a different behaviour than c10::convert/AVX The inputs differing are out-of-range f32->uint8_t has well defined behaviour in TypeCast.h, supporting inputs fitting up to an int64_t, to get consistent results across CPU/GPU Addresses: github.com/pytorch/issues/168972 Test Plan: CI Differential Revision: D119352973
fa2960f to
c02af83
Compare
…196646) Summary: Fix NEON vectorized f32->u8 quant conversion routines have a different behaviour than c10::convert/AVX The inputs differing are out-of-range f32->uint8_t has well defined behaviour in TypeCast.h, supporting inputs fitting up to an int64_t, to get consistent results across CPU/GPU Addresses: github.com/pytorch/issues/168972 Test Plan: CI Differential Revision: D119352973
c02af83 to
c232b94
Compare
…196646) Summary: Pull Request resolved: pytorch#196646 Fix NEON vectorized f32->u8 quant conversion routines have a different behaviour than c10::convert/AVX The inputs differing are out-of-range f32->uint8_t has well defined behaviour in TypeCast.h, supporting inputs fitting up to an int64_t, to get consistent results across CPU/GPU Addresses: github.com/pytorch/issues/168972 Test Plan: CI Reviewed By: Rajeev975 Differential Revision: D119352973
|
@pytorchbot merge -i (Initiating merge automatically since Phabricator Diff has merged, merging with -i because oss signals were bypassed internally) |
Merge failedReason: Approvers from one of the following sets are needed:
|
|
@pytorchbot merge -f "CI failures are unrelated" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: Approvers from one of the following sets are needed:
|
|
@pytorchbot merge -f "CI failures are unrelated" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
Hmm, it did not fix anything, as it tries to force certain behavior out of compiler, which is absolutely not guaranteed. Root cause of the failure is that casting negative to unsigned type results in undefined behavior, from C99/C11 6.3.1.4
|
…onvert
On aarch64, contiguous `x.byte()` for float16/bfloat16 still does a direct `static_cast<uint8_t>`, which clang lowers to a saturating `fcvtzu` (-1 -> 0), while strided copies go through `c10::convert`, which narrows via int64 (-1 -> 255). This breaks `test_non_contig_byte_cpu_{float16,bfloat16}`, `test_contig_vs_every_other_byte_cpu_{float16,bfloat16}` and their `_refs` variants on Apple Silicon (e.g. https://github.com/pytorch/pytorch/actions/runs/36282025606/job/108516516926). pytorch#196646 fixed the same mismatch for float32, but by widening to int64, which on NEON means going through float64 and halves the lane count.
This adds float16/bfloat16 specializations and switches float32 to narrow via int32 instead, using the fact that a type with p significant bits has only multiples of 256 above 2^(p+7): every finite fp16 fits in int32; bf16 is clamped to +-2^16; float32 maps |x| >= 2^31 to 0. All three match `c10::convert` bit for bit wherever it is defined (|x| < 2^63), verified exhaustively for every float16, bfloat16 and float32 bit pattern.
Performance of `x.byte()`, 4M elements, single thread, M5 Pro (us):
| dtype | main | this PR |
|----------|------|---------|
| float32 | 353 | 294 |
| float16 | 101 | 172 |
| bfloat16 | 170 | 281 |
For reference, a direct (UB) float32 -> uint8 cast takes ~180us, so the defined float32 path is now ~1.6x the plain cast instead of ~2x.
Question for reviewers: does defining this behavior make sense at all? `torch.Tensor.to` documents that when the truncated value cannot fit into the target type "the behavior is undefined and the result may vary across platforms", so the int64 wraparound is a convention that dates back to pytorch#36807 (CPU/CUDA consistency) rather than a documented contract. Keeping it costs ~1.7x for float16/bfloat16 relative to the plain cast. The alternative is to drop the int64 convention on the vectorized paths (including pytorch#196646's) and restrict the `byte` OpInfo's test inputs to the uint8 range (`domain=(0, None)`), accepting that contiguous and strided copies may disagree on out-of-range values.
The register-level `VecConvert<uint8_t, 1, float, N>` from pytorch#196646 (used by inductor) is left unchanged; it still narrows via int64 and gives identical results.
Test Plan:
```
python -m pytest test/test_unary_ufuncs.py test/test_ops.py -k "cpu and (byte or char or uint8 or _refs__conversions or float16 and to)" # 5560 passed
python -m pytest test/test_torch.py test/test_type_promotion.py -k "cpu and (uint8 or byte or cast or copy or to_)" # 219 passed
```
plus exhaustive bit-pattern checks of contiguous vs strided vs `x.double().to(torch.int64).to(torch.uint8)`.
This PR was authored with the help of an AI assistant (Claude Code).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
It fixes the UB you quoted in the vectorized part. The eager part does not have this issue because it does not use real->unsigned conversion: it does real->signed->unsigned So either PyTorch contains superflous code in the eager part or this does fix an actual issue. It basically depends on how PyTorch defines the "cast operator", not how C++ defines it. |
…198744) On aarch64, contiguous x.byte() for float16/bfloat16 still does a direct static_cast<uint8_t>, which clang lowers to a saturating fcvtzu (-1 -> 0), while strided copies go through c10::convert, which narrows via int64 (-1 -> 255). This breaks test_non_contig_byte_cpu_{float16,bfloat16}, test_contig_vs_every_other_byte_cpu_{float16,bfloat16} and their _refs variants on Apple Silicon (e.g. https://github.com/pytorch/pytorch/actions/runs/36282025606/job/108516516926). #196646 fixed the same mismatch for float32, but by widening to int64, which on NEON means going through float64 and halves the lane count. This makes operations ridiculously slow, but match the wrapped-around cast behavior. Probably follow up PR is to kill the test, as uniform behavior is not something one should worry about for UB-values 🤖 Generated with [Claude Code](https://claude.com/claude-code) Pull Request resolved: #198744 Approved by: https://github.com/huydhn Co-authored-by: Claude Opus 5.5 <[email protected]>
Make it similar to aarch64 fix in PR pytorch#196646 This change fixes test test_float32_to_uint8 in test/inductor/test_cpu_repro.py.
Make it similar to aarch64 fix in PR pytorch#196646 This change fixes test test_float32_to_uint8 in test/inductor/test_cpu_repro.py.
Summary:
Fix NEON/SVE vectorized f32->u8 quant conversion routines having a different behaviour than c10::convert/AVX
The inputs differing are out-of-range
f32->uint8_t has well defined behaviour in TypeCast.h, supporting inputs fitting up to an int64_t, to get consistent results across CPU/GPU
Addresses: github.com//issues/168972
Test Plan: CI
Reviewed By: Rajeev975
Differential Revision: D119352973
cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @aditew01 @voznesenskym @penguinwu @EikanWang @Guobing-Chen @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo