Skip to content

[TorchToLinalg] Fix unsigned integer lowerings - #4918

Open
sahas3 wants to merge 6 commits into
llvm:mainfrom
sahas3:linalg-unsigned-convert-scalar
Open

sahas3 wants to merge 6 commits into
llvm:mainfrom
sahas3:linalg-unsigned-convert-scalar

Conversation

@sahas3

@sahas3 sahas3 commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

TorchToLinalg converts torch tensors to signless builtin tensors, so every integer conversion has to take its signedness from the torch dtype. Several lowerings converted values without the torch dtype. As a result, uint8 values of 128 and above were sign-extended, and some patterns crashed when the result had an 8-bit integer type. This PR passes the source and result torch dtypes to convertScalarToDtype in these lowerings, and returns a match failure when a conversion fails, so a null value no longer crashes the pattern.

Each commit fixes one group of ops, so it maybe easier to review each commit individually:

  • sum, sum.dim_IntList, prod: the input element is extended according to its torch dtype. These ops used to crash when the result was an 8-bit integer.
  • Full max/min reductions: the init value reads the signedness from the torch dtype for every integer width. Before, it skipped 8-bit integers, which left a null init. Unsigned 16/32/64-bit reductions started from the signed limit.
  • Compares:
    • Mixed-width tensor compares extend each side with its own signedness and compare in the dtype of the wider operand.
    • Integer-to-float conversions pass the source dtype.
    • Comparing a bool tensor with an 8-bit tensor no longer crashes.
  • cat: convertTensorToElementType takes the original torch dtypes, so a uint8 operand concatenated with a float tensor uses uitofp instead of sitofp.
  • remainder.Scalar, remainder.Tensor: unsigned results use remui without the signed sign fix-up.
  • threshold, threshold_backward: unsigned inputs compare with ule.

Behavior change for compares. An int scalar is now cast to the dtype of the integer tensor it is compared with, as PyTorch does. The same goes for a 0-d tensor compared with a dimensioned one. Out-of-range values now wrap, and this also changes results for signed tensors. For example, an int8 tensor compared with 200 now compares against -56.

Assisted By: Claude

The function body uses signless integers, so the sum, sum.dim_IntList
and prod payloads must take the signedness from the torch dtypes. They
converted the input element without the original dtypes, which
sign-extended uint8 values of 128 and above and crashed when the
result is an 8-bit integer.

Pass the input and result torch dtypes to convertScalarToDtype, and
return a match failure when the conversion fails.
The init value of the full max/min reductions read the signedness from
the builtin element type, which is signless, and skipped 8-bit
integers, leaving a null init that crashed the pattern. Unsigned
16/32/64-bit reductions started from the signed limit.

Read the signedness from the torch dtype for every integer width, and
return a match failure when no init value can be created.
Integer compares widened operands without their torch dtypes, so
uint8 values of 128 and above were sign-extended, and bool vs 8-bit
compares crashed.

- An int scalar, or a 0-d tensor compared with a dimensioned one, is
  cast to the dtype of the other integer operand, as PyTorch does.
  Out-of-range values now wrap, which also changes the result for
  signed tensors.
- Mixed-width tensor compares extend each side with its own
  signedness and compare in the wider operand's dtype.
- Integer to float conversions pass the source torch dtype.

The TOSA and StableHLO backends promote the 0-d operand instead of
casting it, so the new 0-d e2e tests are added to their xfail sets.
aten.cat converted operands whose dtype differs from the result with
convertScalarToDtype and no original dtypes, so a uint8 operand
concatenated with a float tensor went through sitofp.

Let convertTensorToElementType take the original torch dtypes and pass
them from the cat lowering.
remainder.Scalar and remainder.Tensor emitted remsi with the signed
sign fix-up for uint8 and crashed on 8-bit results, because the
operands were converted without the result torch dtype.

Pass the torch dtypes to the conversions and let createRemainder emit
remui without a sign fix-up for unsigned results.
threshold and threshold_backward compared with sle and crashed on
8-bit types, because the threshold and value scalars were converted
without the torch dtype.

Pass the torch dtypes to the conversions and compare with ule for
unsigned inputs.
@sahas3
sahas3 marked this pull request as ready for review November 23, 2026 14:13
@sahas3

sahas3 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

cc: @nmalimban @hariprasadravi

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant