argmin-negation-overflow: fix aten.min.dim indices at dtype-extremal minima - #43
Merged
gokulkrishna98 merged 1 commit intoJul 20, 2026
Conversation
arames
commented
Jul 16, 2026
arames
marked this pull request as ready for review
July 16, 2026 23:54
gokulkrishna98
approved these changes
Jul 17, 2026
Contributor
|
Please install pre-commit, to resolve linting issues: https://github.com/apple/coreai-torch/blob/main/.pre-commit-config.yaml |
arames
force-pushed
the
user/arames/fuzz/argmin-negation-overflow
branch
from
July 17, 2026 21:19
e9eb406 to
b9e8cec
Compare
Author
|
Installed pre-commit and re-ran it over the changed files ( |
arames
force-pushed
the
user/arames/fuzz/argmin-negation-overflow
branch
from
July 17, 2026 22:32
b9e8cec to
556fdd8
Compare
Contributor
|
Hi Alex, for merging, your commits must have verified signature. I think you have to setup signing-key ssh for this GitHub account. |
arames
force-pushed
the
user/arames/fuzz/argmin-negation-overflow
branch
from
July 17, 2026 22:59
556fdd8 to
048c67d
Compare
Core AI has argmax but not argmin, so replace_min_dim derives argmin by negating x and taking argmax. Plain negation (x * -1) overflows at integer dtype extremes (e.g. int8 min -128, or uint8 min 0), producing wrong argmin indices. Reverse integer inputs with bitwise complement (~x) instead, which is exact and overflow-free across the full integer range; float/bool inputs keep the exact negation path.
arames
force-pushed
the
user/arames/fuzz/argmin-negation-overflow
branch
from
July 17, 2026 23:07
048c67d to
ae069d7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
aten.min.dimreturns the wrongindiceswhen the true minimum is adtype-extremal integer value (e.g. uint8 min
0, int8 min-128).valuesis correct; only
indicesis wrong. Silent — no crash.Root cause
replace_min_dimincoreai_torch/_aten_to_core.pycomputed argmin asargmax(x * -1). Negation via multiply is not order-reversing at integerextremes:
0 * -1 == 0(not the range top) for unsigned, and-128 * -1overflows int8. So
argmaxscores the true minimum incorrectly.Fix
Reverse order for integer dtypes with the bitwise complement
~x = x ^ -1(
broadcasting_bitwise_xorwith an all-ones constant) instead of negation.~x = -x - 1is a strictly decreasing bijection over the full range of everyinteger dtype (signed and unsigned), never overflows, and introduces no new
ties, so
argmax(~x) == argmin(x)with first-index-on-tie semantics preserved.The float path keeps
x * -1(exact, no overflow).Tests
Adds dtype-extremal coverage (uint8 min 0; int8/int16/int32 dtype-min),
duplicate-minima tie-break, and a float case proving the float path is
unchanged. Reverting the fix fails all integer cases.