Skip to content

Boolean constant support - #3066

Merged
benvanik merged 10 commits into
iree-org:mainfrom
rsuderman:boolConstant
Sep 11, 2020
Merged

benvanik merged 10 commits into
iree-org:mainfrom
rsuderman:boolConstant

Conversation

@rsuderman

@rsuderman rsuderman commented Sep 3, 2020 •

Copy link
Copy Markdown
Collaborator

Support boolean constants at the HAL -> VM lowering

@benvanik

benvanik commented Sep 3, 2020

Copy link
Copy Markdown
Collaborator

not llvm related?

@benvanik benvanik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't valid - you shouldn't have any i1 types by the time you are serializing to bytecode - it indicates you are missing an i1->i8 expansion earlier on

@rsuderman

Copy link
Copy Markdown
Collaborator Author

not llvm related?

I wanted to highlight that both VMLA and the LLVMJit were now working given its a HAL/VM change (waiting on vulkan changes). Updated comment.

@rsuderman

Copy link
Copy Markdown
Collaborator Author

This isn't valid - you shouldn't have any i1 types by the time you are serializing to bytecode - it indicates you are missing an i1->i8 expansion earlier on

I updated to occur at the HAL to VM lowering. I can also place it at the std.const to HAL lowering but it felt a little strange to change the type at that point. Either works but went with the HAL to VM lowering.

@rsuderman
rsuderman requested a review from benvanik September 3, 2020 02:38

@rsuderman rsuderman left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in follow up


// VM supports a minimum of 8-bit elements. Round 1-bit bools to 8-bits.
auto value = op.value().cast<ElementsAttr>();
if (value.getType().getElementType().isInteger(1)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not that familiar with the complete list of steps in IREE's conversion pipelines. Is there an earlier place where a cast from bool to i8 would be sufficient? By the time we're here in the HAL dialect (HALToVM), I'd expect us to be missing some higher level context that would help us understand where such a conversion makes the most sense. This could benefit from an expanded comment here, or a section within a more holistic design doc (similar to https://google.github.io/iree/design-docs/codegen-passes).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is still too low-level - the HAL cannot support 1 bit data types either.

@benvanik benvanik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You made it one level higher, but it needs to be one level higher still :)
The HAL and the VM have machine-level semantics. Just as you can't malloc 1-bit or memset 1-bit you cannot create a HAL buffer of 1-bit. The conversion you're doing needs to happen between flow (where things are tensors) and hal (where things are buffers).

This could probably happen in iree\compiler\Dialect\HAL\Conversion\TypeConverter.cpp. As Scott mentions though it is probably worth more exhaustively testing that such a conversion is valid (tests that perform operations on the bool tensors, etc) or what alternatives are. For example, the intent is that an 8/16/32-bit aligned 1-bit tensor could be widened to work on the byte-aligned types if the operations performed on it are compatible (bitwise op, usually). So tensor<8x32xi1> -> tensor<8xi32>. This wastes no memory (vs always expanding) and the HAL doesn't care about what the bits represent so it should pass right through (as 32 i1's and 1 i32 are the same buffer).

@rsuderman

Copy link
Copy Markdown
Collaborator Author

You made it one level higher, but it needs to be one level higher still :)
The HAL and the VM have machine-level semantics. Just as you can't malloc 1-bit or memset 1-bit you cannot create a HAL buffer of 1-bit. The conversion you're doing needs to happen between flow (where things are tensors) and hal (where things are buffers).

Yeah this was the clarification I was hoping for. We should probably define the expected types at
the HAL level. Overall how we would expand types should have a firm spec in the future.

This could probably happen in iree\compiler\Dialect\HAL\Conversion\TypeConverter.cpp. As Scott mentions though it is probably worth more exhaustively testing that such a conversion is valid (tests that perform operations on the bool tensors, etc) or what alternatives are. For example, the intent is that an 8/16/32-bit aligned 1-bit tensor could be widened to work on the byte-aligned types if the operations performed on it are compatible (bitwise op, usually). So tensor<8x32xi1> -> tensor<8xi32>. This wastes no memory (vs always expanding) and the HAL doesn't care about what the bits represent so it should pass right through (as 32 i1's and 1 i32 are the same buffer).

For now its probably best to stick to i8 expansion as this is what we assume for VMLA. I don't think
we can perform the conversion in TypeConverter though; it handles the conversion of value types and
does not recursively handle attributes.

Overall its updated to handle the conversion at the lowering to HAL. Important note - notice
that there are two const lowerings. This is to handle the situation where further lowerings to
VMLA create new constants that need to be serialized with VMLA's constant implementation.

@rsuderman
rsuderman requested a review from ScottTodd September 9, 2020 17:21
@rsuderman
rsuderman dismissed stale reviews from ScottTodd and benvanik September 9, 2020 17:51

Completed

@benvanik

benvanik commented Sep 9, 2020

Copy link
Copy Markdown
Collaborator

I'm still a bit sketched out as this is not consistently applying things. What happens to ops that are acting on these constants? Or values extracted from the constants and used for other purposes? Are there ops that would stop working with the type getting changed? What about constants that are already inlined into dispatch regions and going to other backends like LLVM or SPIR-V?

The issue with doing it in the flow->hal conversion is that you are now making a global assertion (that all i1->i8) but only then handling it in one local usage (VMLA constant ops). This indicates that it's too low level: the whole point of having the flow dialect (and whatever frontend dialects that live above it) is that these kind of global transformations can be applied consistently and that subsequent lowerings don't need to know about these implicit semantics.

I understand just wanting to land some constant bools, but these kind of changes are really hard to debug later on and can greatly complicate backend implementations (that can no longer rely on consistency across the ABI boundary). It's a lot of work to unwind the existing i64->i32 implicit conversions and I'd really like to avoid adding new things like that.

iree\compiler\Dialect\Flow\Transforms\LegalizeInputTypes.cpp is an example of a possible place where this could live. It can perform type conversion and already handles attribute conversion for constant ops. It'll clearly highlight the issues above (what happens to ops that have requirements on certain types like conditional operations, etc) and even if such issues aren't handled right away will at least put them all in a logical place that's easy to find (verification would fail after the pass) and update (just one global location, not per-backend). We don't want people hitting random type issues in linalg because of spooky action at a distance.

@rsuderman

Copy link
Copy Markdown
Collaborator Author

I'm still a bit sketched out as this is not consistently applying things. What happens to ops that are acting on these constants? Or values extracted from the constants and used for other purposes? Are there ops that would stop working with the type getting changed? What about constants that are already inlined into dispatch regions and going to other backends like LLVM or SPIR-V?

I think this stems from not having a clearly define boolean representation. Operations in
both LLVM and VMLA assume i1 <-> i8 equivalency and any lowering for constants will need
to match/change those assumptions.

The issue with doing it in the flow->hal conversion is that you are now making a global assertion (that all i1->i8) but only then handling it in one local usage (VMLA constant ops). This indicates that it's too low level: the whole point of having the flow dialect (and whatever frontend dialects that live above it) is that these kind of global transformations can be applied consistently and that subsequent lowerings don't need to know about these implicit semantics.

Similar problem to mentioned above - at this point we dont clearly specify the specify
IREE's boolean buffer semantics. Because we convert straight from the Standard dialect to IREE's internal representation we lack a layer to handle any changing abstraction (though
a solution could be introducing a Flow constant operation).

I understand just wanting to land some constant bools, but these kind of changes are really hard to debug later on and can greatly complicate backend implementations (that can no longer rely on consistency across the ABI boundary). It's a lot of work to unwind the existing i64->i32 implicit conversions and I'd really like to avoid adding new things like that.

This is understandable, but for boolean operations we need to establish how to handle sub
8-bit operations. We can limit the breadth of our first implementations as it is extremely unlikely we will start examining 2-bit or or 4-bit quantized models where the sub-bit benefits become more significant.

iree\compiler\Dialect\Flow\Transforms\LegalizeInputTypes.cpp is an example of a possible place where this could live. It can perform type conversion and already handles attribute conversion for constant ops. It'll clearly highlight the issues above (what happens to ops that have requirements on certain types like conditional operations, etc) and even if such issues aren't handled right away will at least put them all in a logical place that's easy to find (verification would fail after the pass) and update (just one global location, not per-backend). We don't want people hitting random type issues in linalg because of spooky action at a distance.

So the problem with LegalizeInputTypes is that it occurs separately from the lowering to HALBuffers. This means when the i1->i8 conversion occurs any operations that require I1s
are invalidated (e.g. mhlo.compare expects to produce a predicate). While its possible to
change the underlying attribute without changing operation types, this results in validation
error between Constant operation types and attributes. (This could be fixed by introducing
constants into the Flow dialect).

At this point I think we need to define a clear plan for boolean types going forward. While
low-bit representations have some potential for significant performance improvement, we
likely won't be investigating those implementations until examining 2-bit / 4-bit
quantization where the gains will be more significant.

@benvanik

benvanik commented Sep 9, 2020

Copy link
Copy Markdown
Collaborator

To be clear, boolean and low-bit depth values are not at all the same; you don't perform transcendental math on bools.

@benvanik

benvanik commented Sep 9, 2020

Copy link
Copy Markdown
Collaborator

I filed some longer term notes here: #3102

For this PR, I really just want to see that this is something that is more than just hack 1/N - if you're going to be next week needing another hack as you start to do more with bools that's going to continue compounding the tech debt here in a part of the code that really can't hold any more. This means doing some legwork to get what you actually want to run together in IR form so it can be analyzed - which it sounds like you're close to. If this change with some issues/TODOs tracking it works well for your extended use cases (again, not just defining a const value) then it's ok to land and clean up later, but this limited usage doesn't instill much confidence (spoken from experience having written the other hacks that are now causing much pain :).

@benvanik
benvanik merged commit 2a8f957 into iree-org:main Sep 11, 2020
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Sep 14, 2020
@ScottTodd ScottTodd mentioned this pull request Sep 14, 2020
@rsuderman
rsuderman deleted the boolConstant branch June 2, 2022 21:29
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.

3 participants