Repository navigation
Keep unsigned dtypes in bitwise shifts - #3088
Open
Muhammad Usman Mateen (usmanmateen) wants to merge 1 commit into
Open
Muhammad Usman Mateen (usmanmateen) wants to merge 1 commit into
Muhammad Usman Mateen (usmanmateen) wants to merge 1 commit into
Conversation
aten_bitwise_left_shift and aten_bitwise_right_shift always cast the result to the signed integer type of the same width, so shifting a uint8 tensor gave int8: 0x0F << 4 came out as -16 instead of 240, and (hi << 4) | lo produced a BitwiseOr of int8 and uint8 that ONNX Runtime rejects. BitShift supports unsigned types directly, so use it as is for them.
This branch has not been deployed
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.
aten_bitwise_left_shiftandaten_bitwise_right_shiftcast the inputs to the unsigned type of the same width, shift, and then always cast the result to the signed type. For signed inputs that is the intent, but foruint8the result comes back asint8:Packing two 4-bit values into a byte,
(hi << 4) | lowithuint8tensors, exports a model that ONNX Runtime does not load:ONNX
BitShiftsupports the unsigned types directly, and a logical shift is what PyTorch does for them, so this returnsBitShifton the inputs as they are when the dtype is unsigned.Tests
test_bitwise_shift_uint8intests/function_libs/torch_lib/e2e_ops_tests.pyexportsx << 4,x >> 4,bitwise_left_shiftandbitwise_right_shiftwith a tensor shift amount, and(x << 4) | y, all onuint8, and checks them against eager. It fails onmainwith the error above and passes with this change.python -m pytest tests/function_libs/torch_lib/e2e_ops_tests.py -k bitwise: 2 passedpython -m pytest tests/function_libs/torch_lib/ops_test.py -k bitwise: 16 passed, 6 skipped (126 subtests passed)ruff checkandruff format --check(0.15.8) on the changed filesTested with torch 2.14.1+cpu and onnxruntime 1.31.0.