-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TorchToStablehlo constant integer type fixes #1
base: main
Are you sure you want to change the base?
Conversation
…t types to singless integer element type
@@ -883,13 +884,29 @@ LogicalResult ConvertAtenOp<ValueTensorLiteralOp>::matchAndRewrite( | |||
|
|||
DenseElementsAttr valueAttr = | |||
elements.mapValues(builtinTensorElemTy, [&](const APInt &v) { | |||
return APInt(bitWidth, v.getSExtValue()); | |||
return APInt(bitWidth, | |||
bitWidth == 1 ? v.getZExtValue() : v.getSExtValue()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how the fix is so targeted 👍
@@ -883,13 +884,29 @@ LogicalResult ConvertAtenOp<ValueTensorLiteralOp>::matchAndRewrite( | |||
|
|||
DenseElementsAttr valueAttr = | |||
elements.mapValues(builtinTensorElemTy, [&](const APInt &v) { | |||
return APInt(bitWidth, v.getSExtValue()); | |||
return APInt(bitWidth, | |||
bitWidth == 1 ? v.getZExtValue() : v.getSExtValue()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The more I think about this, the weirder it seems
signed and unsigned +1 share the same bit representation 0b'000....01, so the SignExtend64 function in LLVM actually does convert a +1 into a -1 if the bit width = 1 when using getSExtValue. I think that can be considered a bug in LLVM that we're working around through this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if you have a single bit and you want to consider it a signed integer, then that bit must be the sign. And so you can basically just represent a magnitude-less "positive" or "negative". The issue is that for booleans 1
is true
and 0
is false
. But for a signed integer 1
as the sign bit means the integer is "negative"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, so this 'issue' stems from the fact that, for bit width 1 it is impossible to store a boolean true = +1; and the only possible values are 0b'0 = 0 and 0b'1 = -1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
v.getZExtValue()
when copyingAPInt
with bit-width of 1.DenseResourceElementsAttr
with integer element type to signless.