You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<stdin>:2:8: error: 'tensor.cast' op operand type '!hal.buffer_view' and result type 'tensor<?x?xf32>' are cast incompatible
%5 = tensor.cast %arg0 : tensor<4x4xf32> to tensor<?x?xf32>
^
<stdin>:1:1: note: called from
func @forward(%arg0: tensor<4x4xf32>, %arg1: tensor<4x4xf32>) -> tensor<?x?xf32> {
^
<stdin>:0:0: error: conversion from source -> vm failed
Thanks! Looks like we are missing a conversion from tensor.cast to hal.tensor.cast. I am not sure why we even have hal.tensor.cast in the inputs in one place and tensor.cast in the other...
hal.tensor.cast is what we want on external ABI boundaries - it lets us explicitly specify the type moved across the program boundary (an iree_hal_buffer_view_t) and how to get its shape information. Internally within a module tensor.cast should be fine (though it doesn't carry shapes and is not a great op - it'd be better to have a variant that was like hal.tensor.cast as otherwise we'd need to insert things like the shapex.tie_shape stuff again to attach that information).
If this is a public function then we can do something like what we do with the SavedModelToIreeABI pass. That is what does this type casting on the tensorflow side, saying that tensorflow I/O comes into the module in as bufferviews or nested lists and other things that are frontend-specific and difficult/impossible to infer later on in the lowering. There's a generic version of this in WrapEntryPoints that is used for generic inputs so func(tensor) -> tensor becomes func(buffer_view) -> buffer_view (iree-translate adds that with the -iree-native-bindings-support option).
If this is internal to the model then maybe a lowering to a flow.tensor.reshape could handle this.
The tensor.cast op itself. As mentioned, it is really an identity, and e.g after doing per-dimension shape inference becomes a no-op, EXCEPT
At ABI boundaries, where we need to formally add/erase static type information to satisfy the function return type/function argument types
In npcomp, we run shape inference which eliminates almost all of these sorts of ops (this is actually still at the Torch level, this op comes in from TorchToLinalg, but the same idea applies), and then a pass which propagates the information to the ABI boundaries to eliminate the need for these formal ops. Doing that relies on the assumption that tensor<4x4xf32> and tensor<?x?xf32> are passed the same way from user API's, which is true for IREE (but is not true e.g. for some of the upstream weird calling conventions where memrefs get split into a C ABI as a raw pointer + a number of 'index' values equal to the number of ?'s -- changing the number of ?'s is an ABI-breaking change in that case)
Yeah - the ABI boundary is where we could handle these. We could, for example, extend WrapEntryPoints to look for these ops and replace them with the appropriate forms.
What we previously had was func(tensor) lowering deep into the HAL dialect before it decided to turn the tensors into hal.buffer_view, but that was a mess as then if targeting a different runtime API like you mention it isn't guaranteed to become that (tflite passes just unshaped buffers and sends shape information in a side-channel, for example). It can also hide information from the linalg/flow levels that want to better carry shape dimensions around as SSA values rooted up to input/down to output.
The new approach is to specify what types you expect at runtime on the ABI boundary (like an __attribute__((fastcall)) or __attribute__((stdcall)) in C) and insert the placeholder conversion for them - in the case of iree_hal_buffer_view_t being passed adding a hal.tensor.cast to move between that type/shape-erased !hal.buffer_view into the tensor<...>. This avoids the ambiguity lower in the stack as the HAL knows precisely when it needs to materialize the buffer view types vs. when it's dealing with an internal tensor that it can treat as effectively just an offset into a ringbuffer with no runtime materialization at all. The bonus is that this also enables all the other kind of interop that may be desired - for example, passing in dicts for kwargs that are fetched and unpacked into primitives, custom types, and buffer_views without anything lower in the stack needing to somehow infer that - especially tricky for custom types/other dialect interaction.
So, now that we know there are two things - which are you hitting? Both I'm guessing?
I just looked, and I think it is mainly the ABI boundary one. I tried a 2 layer MLP but the tensor.cast ops end up disappearing through canonicalization currently. Probably more complex examples could result in tensor.cast ops that survive in the middle of the program, but that's not the immediate issue.
These boundary conditions drive me nuts and need some more rigor.
While not how hal.tensor.cast is defined now (i.e. it lacks builders for generically constructing this form), I'm not sure that the following is an incorrect use of the op (i.e. if we converted tensor.cast -> hal.tensor.cast). Like Ben said, it isn't great because as non-shape-carrying, it has to use local shape resolution (i.e. memref.dim and looking up the chain), but for these kind of widening cases, I'm not sure it is wrong (although, likely missing canonicalizations to be good).
I tried a 2 layer MLP but the tensor.cast ops end up disappearing through canonicalization currently.
It is useful to distinguish these "loopback" cases that just return an operand and functions that do real work. I've had to find/fix a number of the loopback cases that don't show up in real programs.
It is useful to distinguish these "loopback" cases that just return an operand and functions that do real work. I've had to find/fix a number of the loopback cases that don't show up in real programs.
Sorry, my statement was not clear. Canonicalization removes all the tensor.cast ops in the program except the "ABI" tensor.cast at the end before the return. It's not specific to this loopback test case (you can see in my reduced test case it is called %5 :) ).
I should have said
I tried a 2 layer MLP but the [internal] tensor.cast ops end up disappearing through canonicalization currently.
Since landing the above requires an LLVM bump to avoid a mid-air collision, someone else will need to pick it up next week as I'll be out. Maybe Yi can finish/land it? I pushed it to a branch in the main repo so that others can push commits to it and merge.
Activity
MaheshRavishankar commentedon Jun 30, 2021
(would be useful to also attach a dump of
-print-ir-after-allfor such errors to see where the issue really is). Will do so in a bit and check.silvasean commentedon Jun 30, 2021
print-ir-after-all: https://gist.github.com/silvasean/d067d1c6100d1f8d3b7e05e776a24d7a
MaheshRavishankar commentedon Jun 30, 2021
Thanks! Looks like we are missing a conversion from
tensor.casttohal.tensor.cast. I am not sure why we even havehal.tensor.castin the inputs in one place andtensor.castin the other...benvanik commentedon Jun 30, 2021
hal.tensor.cast is what we want on external ABI boundaries - it lets us explicitly specify the type moved across the program boundary (an
iree_hal_buffer_view_t) and how to get its shape information. Internally within a module tensor.cast should be fine (though it doesn't carry shapes and is not a great op - it'd be better to have a variant that was like hal.tensor.cast as otherwise we'd need to insert things like the shapex.tie_shape stuff again to attach that information).MaheshRavishankar commentedon Jun 30, 2021
Ok, actually I feel like this op should not even make it into flow. Its actually losing shape information and seems like an intermediate IR state.
benvanik commentedon Jun 30, 2021
If this is a public function then we can do something like what we do with the SavedModelToIreeABI pass. That is what does this type casting on the tensorflow side, saying that tensorflow I/O comes into the module in as bufferviews or nested lists and other things that are frontend-specific and difficult/impossible to infer later on in the lowering. There's a generic version of this in WrapEntryPoints that is used for generic inputs so
func(tensor) -> tensorbecomesfunc(buffer_view) -> buffer_view(iree-translate adds that with the-iree-native-bindings-supportoption).If this is internal to the model then maybe a lowering to a flow.tensor.reshape could handle this.
silvasean commentedon Jun 30, 2021
There's two things going on here:
In npcomp, we run shape inference which eliminates almost all of these sorts of ops (this is actually still at the Torch level, this op comes in from TorchToLinalg, but the same idea applies), and then a pass which propagates the information to the ABI boundaries to eliminate the need for these formal ops. Doing that relies on the assumption that
tensor<4x4xf32>andtensor<?x?xf32>are passed the same way from user API's, which is true for IREE (but is not true e.g. for some of the upstream weird calling conventions where memrefs get split into a C ABI as a raw pointer + a number of 'index' values equal to the number of?'s -- changing the number of?'s is an ABI-breaking change in that case)benvanik commentedon Jun 30, 2021
Yeah - the ABI boundary is where we could handle these. We could, for example, extend WrapEntryPoints to look for these ops and replace them with the appropriate forms.
What we previously had was
func(tensor)lowering deep into the HAL dialect before it decided to turn the tensors intohal.buffer_view, but that was a mess as then if targeting a different runtime API like you mention it isn't guaranteed to become that (tflite passes just unshaped buffers and sends shape information in a side-channel, for example). It can also hide information from the linalg/flow levels that want to better carry shape dimensions around as SSA values rooted up to input/down to output.The new approach is to specify what types you expect at runtime on the ABI boundary (like an
__attribute__((fastcall))or__attribute__((stdcall))in C) and insert the placeholder conversion for them - in the case ofiree_hal_buffer_view_tbeing passed adding ahal.tensor.castto move between that type/shape-erased!hal.buffer_viewinto thetensor<...>. This avoids the ambiguity lower in the stack as the HAL knows precisely when it needs to materialize the buffer view types vs. when it's dealing with an internal tensor that it can treat as effectively just an offset into a ringbuffer with no runtime materialization at all. The bonus is that this also enables all the other kind of interop that may be desired - for example, passing in dicts for kwargs that are fetched and unpacked into primitives, custom types, and buffer_views without anything lower in the stack needing to somehow infer that - especially tricky for custom types/other dialect interaction.So, now that we know there are two things - which are you hitting? Both I'm guessing?
silvasean commentedon Jun 30, 2021
I just looked, and I think it is mainly the ABI boundary one. I tried a 2 layer MLP but the tensor.cast ops end up disappearing through canonicalization currently. Probably more complex examples could result in tensor.cast ops that survive in the middle of the program, but that's not the immediate issue.
stellaraccident commentedon Jul 1, 2021
These boundary conditions drive me nuts and need some more rigor.
While not how
hal.tensor.castis defined now (i.e. it lacks builders for generically constructing this form), I'm not sure that the following is an incorrect use of the op (i.e. if we convertedtensor.cast->hal.tensor.cast). Like Ben said, it isn't great because as non-shape-carrying, it has to use local shape resolution (i.e. memref.dim and looking up the chain), but for these kind of widening cases, I'm not sure it is wrong (although, likely missing canonicalizations to be good).To:
I'm not an expert on this, though.
stellaraccident commentedon Jul 1, 2021
It is useful to distinguish these "loopback" cases that just return an operand and functions that do real work. I've had to find/fix a number of the loopback cases that don't show up in real programs.
benvanik commentedon Jul 1, 2021
I think you are right about doing tensor_cast to HAL.tensor.cast early on for the public functions - we could make this part of WrapEntryPoints.
1 remaining item
silvasean commentedon Jul 1, 2021
Sorry, my statement was not clear. Canonicalization removes all the tensor.cast ops in the program except the "ABI" tensor.cast at the end before the return. It's not specific to this loopback test case (you can see in my reduced test case it is called
%5:) ).I should have said
MaheshRavishankar commentedon Jul 1, 2021
@silvasean moving this back to you for now, till exact way forward is decided.
stellaraccident commentedon Jul 1, 2021
I assigned to me but have hours left before going offline for an extended period. Will see if a solution emerges.
Add a pattern to convert tensor<->tensor static information casts to …
stellaraccident commentedon Jul 1, 2021
Since landing the above requires an LLVM bump to avoid a mid-air collision, someone else will need to pick it up next week as I'll be out. Maybe Yi can finish/land it? I pushed it to a branch in the main repo so that others can push commits to it and merge.
Add a pattern to convert tensor<->tensor static information casts to …
Add a pattern to convert tensor<->tensor static information casts to …
Add a pattern to convert tensor<->tensor static information casts to …
Add a pattern to convert tensor<->tensor static information casts to …
Add a pattern to convert tensor<->tensor static information casts to …
Add a pattern to convert tensor<->tensor static information casts to …