Skip to content

Support optimizing and storing tensor<i1> (boolean) types #3102

Closed

Description

@benvanik

Following up from #3066 and filing an issue to track the work.

@rsuderman if you can attach examples of real IR using bools to do more than just be a constant value that'd help nail some of this down. It's hard to come up with a good plan when doing things piecemeal - we need to see tensors of bools in variables, being computed, etc.

At a high level, anything changing tensor types that may influence the operations being performed on them (like 32xi1->1xi32) must happen within the flow transformation pipeline:
https://github.com/google/iree/blob/54771d42640fc4611c0671cc7be3feb445c71812/iree/compiler/Dialect/Flow/Transforms/Passes.cpp#L31-L202

Anything that is changing the representation of tensors in memory from its natural form can happen in the hal transformation pipeline, at greater complexity:
https://github.com/google/iree/blob/54771d42640fc4611c0671cc7be3feb445c71812/iree/compiler/Dialect/HAL/Transforms/Passes.cpp#L42-L97


For things like operating on boolean tensors as wider data types (32xi1->1xi32) it'd need to happen either in the frontend (prior to coming into IREE at all) or very early in flow at or around legalize input types: https://github.com/google/iree/blob/54771d42640fc4611c0671cc7be3feb445c71812/iree/compiler/Dialect/Flow/Transforms/Passes.cpp#L66

This is because the semantics of the data stored within the tensors matters when doing this and operations change based on this behavior; once you element-wise widen an i1 tensor to an i8 tensor you can no longer perform any operations on that i8 tensor that rely on i1 behavior (as the tensor could contain elements with any i8 value). The correct conversions/masking/etc would be inserted and the rest of the pipeline just sees mhlo.xor/mhlo.convert/mhlo.select ops no different than anything else and everything is consistent. Note that you still can use i1 values here after this process; you'd just be inserting the IR to extract the bit from the bitmap.

This is similar to how std::vector<bool> is a completely different implementation than std::vector<T> - bools have special properties that when you know the semantics you can do much better stuff.

It's also nice because this could be implemented as a pure mhlo transform and shared upstream in that repo.


For pure storage changes (just widening i1->i8 via a cast) it's trickier as once in the hal level there's a lot more complexity around how memory is represented and kept in sync across devices. Everything from tensor layout, buffer alignment, per-backend supported data types and memory access bit depths, etc needs to be taken into account the further you go in the transformation pipeline. This means it should be done as soon as possible - like materialize-interfaces (basically, the start): https://github.com/google/iree/blob/54771d42640fc4611c0671cc7be3feb445c71812/iree/compiler/Dialect/HAL/Transforms/Passes.cpp#L47

This is because materialize-interfaces is where we generate the shim functions that convert the ABI storage format (i8 here) to whatever native format is used within the executables (i1):
https://github.com/google/iree/blob/54771d42640fc4611c0671cc7be3feb445c71812/iree/compiler/Dialect/HAL/Transforms/MaterializeInterfaces.cpp#L178-L223

This is where we get the chance to say 'this tensor is stored as an i8 but really represents an i1'; for example, by having the hal.interface.load.tensor op of the i8 tensor be followed by an std.trunci op that truncates from i8 to i1. Then the rest of the code within the executable sees i1 and does whatever the backend wants. But what's critical here is that we've now explicitly inserted the cross-ABI type conversion in a consistent way with no ambiguity around the storage format.

On the host side, the i1->i8 transformation is much the same as we don't perform any math on them - just loads and stores. So inserting the same exact truncations/widenings is possible and changing types is enough to allow the rest of the transformation pipeline to make the right decisions as to layout/offset calculations/etc. A flow.tensor.load ... : i1 would turn into a flow.tensor.load ... : i8 + trunci ... : i1. This can happen automatically via the MLIR conversion framework:
https://github.com/llvm/llvm-project/blob/4589dd924dfc43c846652b85825e291af0d7428a/mlir/include/mlir/Transforms/DialectConversion.h#L114-L151

This is similar to how types are manipulated when lowering to machine ABIs - you may have a C function that takes an int8_t but that is going to be passed in a 32-bit register on x86 because that's all it can handle; the compiler inserts the appropriate widening/truncation (conceptually) to prevent you from reading back garbage from the upper 3 bytes of the register (like us inserting the trunci).

There's some hacky handling for this today but it's not extensible to this case and a big source of bugs. This would be a really good refactoring that would make handling i64 and other types consistently much easier.


So, if possible, it's always be better to semantically widen i1 at the flow level (memory, compute, etc). Effectively reinterpret-casting i1 to a wider type at the hal level should be considered a fallback for when the former is not possible, and there's a lot more details as to how such a casting needs to happen to keep things robust.

Activity

  1. added
    compiler/dialects
    on Sep 9, 2020
  2. GMNGeoffrey commented on Nov 8, 2021

    @GMNGeoffrey
    Member

    I've got another instance of compilation failure coming from poor i1 handling. https://gist.github.com/GMNGeoffrey/5c8e9f70798541ee72fe5c57a9b33bd7

    iree-translate lax_vmap_switch.canonicalized.input.mlir --iree-mlir-to-vm-bytecode-module --iree-hal-target-backends=dylib-llvm-aot
    error: 'hal.command_buffer.fill_buffer' op operand #4 must be 8-bit signless integer or 16-bit signless integer or 32-bit signless integer or 16-bit float or 32-bit float, but got 'i1'
        %14 = linalg.fill(%true, %13) : i1, tensor<i1> -> tensor<i1> 
    
  3. GMNGeoffrey commented on Nov 8, 2021

    @GMNGeoffrey
    Member

    This is coming up in the context of changing behavior to the jax/lax to fill with nans on OOB gathers.

    Here's the original MHLO: https://gist.github.com/875679e62d1163bdceb480019438e7bb

  4. hanhanW commented on Nov 8, 2021

    @hanhanW
    Contributor

    Could be fixed by this: #7078

    You don't have to patch the PR. You can add passManager.addNestedPass<FuncOp>(IREE::Flow::createPromoteI1ToI8Pass()); to https://github.com/google/iree/blob/11d6202b2562601348d08d14c2ff807fcfe70b6f/iree/compiler/InputConversion/MHLO/Passes.cpp#L63-L69

    (after converting all the ops to Linalg ops)

    I guess the main issue is that there are i1 constants, and it causes the issue.

  5. GMNGeoffrey commented on Nov 8, 2021

    @GMNGeoffrey
    Member

    Unfortunately it does not. That pass doesn't eliminate the i1 linalg.init_tensor ops

  6. GMNGeoffrey commented on Nov 8, 2021

    @GMNGeoffrey
    Member

    Which makes sense: it just has a single pattern that looks specifically for arith.constant

  7. benvanik commented on Jun 30, 2022

    @benvanik
    CollaboratorAuthor

    I think we've got many i1 tests and uses now up into python that works correctly. #9461 made boolean numeric types fully supported across the IREE HAL API.

  8. moved this to Done in IREEon Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

compiler/dialectsenhancement ➕

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions