argmin-negation-overflow: fix aten.min.dim indices at dtype-extremal minima#43
Open
arames wants to merge 1 commit into
Open
argmin-negation-overflow: fix aten.min.dim indices at dtype-extremal minima#43arames wants to merge 1 commit into
arames wants to merge 1 commit into
Conversation
replace_min_dim computed argmin as argmax(x * -1), which is wrong at integer dtype extremes: uint8 min 0 negates to 0 (not the range top), and int8 min -128 overflows on negation. Reverse order with the bitwise complement ~x = x ^ -1 (broadcasting_bitwise_xor) for integer dtypes -- a strictly decreasing, overflow-free bijection that preserves first-index tie-breaking; keep x * -1 for float. Adds dtype-extremal and tie-break coverage.
arames
commented
Jul 16, 2026
| await validate_numerical_output(model=model, x=x, dynamic_shapes=dynamic_shapes) | ||
|
|
||
|
|
||
| class TestMinDimArgminDtypeExtremes: |
Author
There was a problem hiding this comment.
@gokulkrishna98 : Should we split this test file per op? Maybe in a separate commit?
arames
marked this pull request as ready for review
July 16, 2026 23:54
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.