Keep the integer dtype in torch.clamp when only one bound is given - #2839
Open
LeSingh1 wants to merge 1 commit into
Open
Keep the integer dtype in torch.clamp when only one bound is given#2839LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
Collaborator
|
This change looks good. CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2797455245 |
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.
torch.clamp(x, min=...)andtorch.clamp(x, max=...)on an int tensor come out of the converter as fp32, and the values are rounded to what fp32 can hold.When only one bound is given, the converter substitutes
np.finfo(np.float32).min/.maxfor the missing one. That sentinel is a float const, sopromote_input_dtypespromotesxalong with it and the whole op becomes fp32. PyTorch keeps the integer dtype unless one of the given bounds is a float.Repro on
main:Before:
Four of the eight values are wrong, not just the dtype: 16777217 -> 16777216, 16777219 -> 16777220, 2000000001 -> 2000000000, 123456789 -> 123456792. Anything downstream that expects an integer (indices, for instance) gets a float instead.
After:
The fix takes the sentinel from
x's own dtype range instead of always from fp32. A float bound that is actually passed still promotes as before, sotorch.clamp(int_x, min=2.5)remains fp32, matching PyTorch.Testing
New
TestElementWiseUnary::test_clamp_int_input_single_bound(min-only and max-only). 4 of its 8 cases fail onmainand all 8 pass here. The rest ofTestElementWiseUnary, including the existingtest_clamp,test_clamp_int_input,test_clamp_non_const_rangeandtest_clamp_min_max, still passes.