fix(transforms): handle float16/bfloat16 in convert_image_dtype#9485
Open
jashshah999 wants to merge 1 commit into
Open
fix(transforms): handle float16/bfloat16 in convert_image_dtype#9485jashshah999 wants to merge 1 commit into
jashshah999 wants to merge 1 commit into
Conversation
Two bugs when using half-precision floating point dtypes: 1. float16/bfloat16 to integer: 255.999 rounds to 256.0 in float16 due to limited precision, then .to(uint8) wraps to 0. Fix: upcast to float32 before the multiply. 2. int32/int64 to float16: max values exceed float16 range (65504), producing NaN/inf silently. Fix: raise RuntimeError (same pattern as the existing float32->int32 safety check). Fixes pytorch#6799.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9485
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
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.
Summary
Fixes two bugs in
convert_image_dtypefor half-precision floating point types (#6799, open since 2022):float16/bfloat16 to integer overflow:
1.0 * (255 + 1.0 - 0.001) = 255.999rounds to256.0in float16 (insufficient mantissa bits), then.to(uint8)wraps to0. Fix: upcast to float32 before the multiply, preserving the existingeps = 1e-3distribution logic.int32/int64 to float16 produces NaN/inf:
int32.max = 2^31-1exceedsfloat16.max = 65504, so the.to(float16)cast silently producesinf. Fix: raiseRuntimeError(same pattern as the existing float32->int32 safety check).Both fixes are compatible with
torch.jit.script(noisinstancecalls, only direct dtype comparisons).Test plan
test_convert_image_dtype_half_precision— verifies 0.0->0, 0.5->mid, 1.0->max for float16/bfloat16 to each integer dtypetest_convert_image_dtype_int_to_float16_raises— verifies int32/int64 to float16 raises RuntimeErrorFixes #6799.