Skip to content

Revisit signed/unsigned integer in IREE #5383

Closed

Description

@hanhanW

MLIR introduced signedness for integer types in the past year. Some passes and checks were added before it, so we actually did not handle it correctly.

E.g.,

In flow type converter, it does not handle ui64 types.

https://github.com/google/iree/blob/5b952e42adcd2c7c076d4ce1814225d1590aaf60/iree/compiler/Dialect/Flow/Conversion/TypeConverter.cpp#L41-L51

In MHLO -> Linalg patterns, it does not handle unsigned types.

https://github.com/tensorflow/mlir-hlo/blob/6d2209e30192f71d8164cd0fc9dbbe1f8f60680f/lib/Dialect/mhlo/transforms/legalize_to_linalg.cc#L239-L243

IIRC, the isSignlessIntOrFloat stuff were added by integration fixes. Now it causes issues to compile ASR models, and it's a good time to clean these up.

Activity

  1. added
    compiler/dialects
    codegen
    on Apr 9, 2021
  2. GMNGeoffrey commented on Apr 9, 2021

    @GMNGeoffrey
    Member

    I think we're trying to lower some very similar models here :-P I just hit the same issue of unhandled unsigned types in linalg. See some discussion in https://discord.com/channels/689900678990135345/689932666505920596/830177574096207892

  3. self-assigned this
    on Apr 10, 2021
  4. hanhanW commented on Apr 12, 2021

    @hanhanW
    ContributorAuthor

    After talking to @antiagainst , we found that there are unsigned integers and signless integers in the model. I don't see si[0-9] in the MLIR files.

  5. GMNGeoffrey commented on Apr 12, 2021

    @GMNGeoffrey
    Member

    Yes that's because MHLO incorrectly uses signless to mean signed because integer signedness was not originally present in MLIR. So for legacy reasons signless integers in MHLO should be interpreted as signed, but that's something that really needs to be fixed.

  6. removed their assignment
    on Apr 27, 2021
  7. GMNGeoffrey commented on May 4, 2021

    @GMNGeoffrey
    Member

    MHLO -> linalg now handles unsigned types at a basic level. IREE itself uses signless ints, but this new conversion of ui64->i32 is happening before dispatch region formation (which is why we're still seeing unsigned). Once we switch entirely to linalg-on-tensors this should be easier to cleanup. For now though, this is doing more than just silently truncating (which is already not great). It's also silently converting unsigned to signed, effectively, because this is still happening on MHLO where signless means signed. This means that the op created would be an signed instead of an unsigned variant. Stuff gets weird and this is likely going to result in some confusing bug.

  8. self-assigned this
    on May 4, 2021
  9. GMNGeoffrey commented on May 4, 2021

    @GMNGeoffrey
    Member

    The rationale for converting unsigned to signless is that std.constant doesn't support unsigned.

    https://github.com/google/iree/blob/0b3289aea9fcea8d0e00117045a1256fdddb93f7/iree/compiler/Dialect/Flow/Conversion/TypeConverter.cpp#L41-L51

    I don't think that's right though. At least, it's not enforced by the verifier. My understanding is that std.constant is supposed to support any type, which I assume would include [un]signed integers. You hit an assert printing such a constant though.

    Sent https://reviews.llvm.org/D101810 to clean that up

  10. GMNGeoffrey commented on May 4, 2021

    @GMNGeoffrey
    Member

    Ok my take on std.constant was wrong. It does indeed require signless integers. This is just a bug in the lowering of mhlo.dynamic-slice to std.subtensor though, I think. It's assuming signed (aka signless)

    /tmp/repro.mlir:428:12: error: expected result element type to be 'i64'
    %388 = "mhlo.dynamic-slice"(%364, %387) {slice_sizes = dense<1> : tensor<1xi64>} : (tensor<3xui64>, tensor) -> tensor
    ^
    /tmp/repro.mlir:428:12: note: see current operation: %639 = "std.subtensor"(%632, %638) {operand_segment_sizes = dense<[1, 1, 0, 0]> : vector<4xi32>, static_offsets = [-9223372036854775808], static_sizes = [1], static_strides = [1]} : (tensor<3xi64>, index) -> tensor

  11. GMNGeoffrey commented on May 4, 2021

    @GMNGeoffrey
    Member

    Sent a patch to fix the lowering. Now hitting the same issue with mhlo.concatenate. I'm just going to fix these and we can hopefully remove this hack

  12. GMNGeoffrey commented on May 4, 2021

    @GMNGeoffrey
    Member

    Fixing in upstream MHLO is not hard. For some reason IREE has its own pattern for concat as well though

  13. GMNGeoffrey commented on May 4, 2021

    @GMNGeoffrey
    Member
  14. GMNGeoffrey commented on May 4, 2021

    @GMNGeoffrey
    Member

    tensorflow/tensorflow@6adc0d0 for concatenate (except apparently we actually use our own concatenate lowering)

  15. hanhanW commented on May 11, 2021

    @hanhanW
    ContributorAuthor

    Thanks for working on this!

    I think we still use our own lowering for concatenate op. I did not know that there is a version in MHLO repo. I suspect that MHLO's one has some issues that I hit before. I will take a look and raise the issue to MHLO folks if that is the case. Everything looks good in IREE now. :)

  16. GMNGeoffrey commented on May 11, 2021

    @GMNGeoffrey
    Member

    I think we still use our own lowering for concatenate op. I did not know that there is a version in MHLO repo. I suspect that MHLO's one has some issues that I hit before. I will take a look and raise the issue to MHLO folks if that is the case. Everything looks good in IREE now. :)

    Yeah I think I looked at using it briefly and there was some issue I hit (should've written it down). Should be easy to repro though by just removing our pattern

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

Metadata

Metadata

Assignees

Labels

codegencompiler/dialects

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions