Repository navigation
Register aten::__lshift__.Tensor and aten::__rshift__.Tensor - #3109
Merged
Justin Chu (justinchuby) merged 2 commits intoOct 20, 2026
Merged
Justin Chu (justinchuby) merged 2 commits into
Justin Chu (justinchuby) merged 2 commits into
Conversation
x << s and x >> s with a tensor s export as aten::__lshift__.Tensor and aten::__rshift__.Tensor. torchlib doesn't register those overloads, so the export fails with a DispatchError. The second test unpacks int4 values the way packed low-bit weight code does.
They are the same operation as bitwise_left_shift.Tensor and bitwise_right_shift.Tensor, which are already registered, as are the .Scalar overloads of both operators.
Justin Chu (justinchuby)
approved these changes
Oct 20, 2026
Justin Chu (justinchuby)
enabled auto-merge (squash)
October 20, 2026 19:04
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3109 +/- ##
=======================================
Coverage 73.07% 73.07%
=======================================
Files 267 267
Lines 32722 32722
Branches 3114 3114
=======================================
Hits 23913 23913
Misses 7767 7767
Partials 1042 1042 ☔ View full report in Codecov by Harness. |
Justin Chu (justinchuby)
merged commit Oct 20, 2026
fef8486
into
microsoft:main
25 of 32 checks passed
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.
x << sandx >> swith a tensorsfail to export:torch.bitwise_left_shift(x, s)andtorch.bitwise_right_shift(x, s)export fine, as dox << 2andx >> 2(the.Scalaroverloads, #1645). Only the operators with a tensor shift amount are missing. A shift tensor is typical when unpacking packed low-bit weights, e.g.(packed[:, :, None] >> shifts) & 0xF.This registers
aten::__lshift__.Tensorandaten::__rshift__.Tensoron the existingaten_bitwise_left_shiftandaten_bitwise_right_shift.Tests
test_shift_operators_with_tensor_shift(int8, int32 and int64, with negative values) andtest_unpack_int4_with_shift_operatorine2e_ops_tests.py. Both fail on main with the error above and pass with the change.e2e_ops_tests.py -k "shift or bitwise": 10 passed.ops_test.py -k "shift or bitwise": 16 passed, 6 skipped.ruff checkandruff format --check(0.15.8) pass.I also exported the operators and the
torch.bitwise_*_shiftfunctions with the same inputs: int8, int16, int32, int64 and uint8, shift amounts at and above the bit width, 0-d and broadcast shift tensors, and empty inputs. In every case the operators now give the same result as the functions, or the same error. Two things fail the same way for both forms, before and after this change:BitShiftkernel for them.Not covered here
Operands of different dtypes still fail for the operators, e.g.
packed_int32 >> torch.arange(0, 32, 4)(int64 shifts). PyTorch's exporter type-promotion table (torch/onnx/_internal/fx/passes/type_promotion.py) has rules forbitwise_left_shiftandbitwise_right_shiftbut not for__lshift__and__rshift__, so torchlib gets the operands unpromoted and the dtype assertion fails. With this change, adding those two rules withELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT(the same as forbitwise_left_shift) made the mixed cases I tried match eager, including 0-d shifts and thearangeunpacking above.