-
Notifications
You must be signed in to change notification settings - Fork 670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using rank-reducing subtensor/subviews #5385
Comments
I did some study on this, it looks like subtensor op has a canonicalization pattern to do it when the types are not match. It failed (in my case) because the source type and result type do not have the same rank. Dumping IRs from the pattern could see %2449 = subtensor %2377[%2447, %c0] [1, 512] [1, 1] : tensor<1x512xf32> to tensor<512xf32>
%2448 = subtensor %2377[%2447, 0] [1, 512] [1, 1] : tensor<1x512xf32> to tensor<1x512xf32>
%2449 = tensor.cast %2448 : tensor<1x512xf32> to tensor<512xf32> @MaheshRavishankar suggested that it should be tensor_reshape, but I don't think that we should replace it with
side question: module {
func @dynamic_slice(%arg0: tensor<2x513xi16>, %arg1: tensor<i64>, %arg2: tensor<i64>) -> tensor<513xi16> {
%c1_i64 = constant 1 : i64
%c0_i64 = constant 0 : i64
%0 = tensor.extract %arg1[] : tensor<i64>
%1 = cmpi slt, %0, %c1_i64 : i64
%2 = select %1, %0, %c1_i64 : i64
%3 = cmpi sgt, %2, %c0_i64 : i64
%4 = select %3, %2, %c0_i64 : i64
%5 = index_cast %4 : i64 to index
%6 = tensor.extract %arg2[] : tensor<i64>
%7 = cmpi slt, %6, %c0_i64 : i64
%8 = select %7, %6, %c0_i64 : i64
%9 = cmpi sgt, %8, %c0_i64 : i64
%10 = select %9, %8, %c0_i64 : i64
%11 = index_cast %10 : i64 to index
%12 = subtensor %arg0[%5, %11] [1, 513] [1, 1] : tensor<2x513xi16> to tensor<513xi16>
return %12 : tensor<513xi16>
}
} |
Definitely doing this as a cast is invalid. (you hit the same error message, but the issue is different). From my experimentation this is all coming from |
I skimmed through that discourse thread but given the discussion I
pattern-matched it as a "mhlo semantics problem".
The CastOp is meant for the ? <-> static casting case, tha canonicalization
pattern is missing the rank-reduction case.
For the rank-reducing case, there is linalg.tensor_reshape but it lives in
Linalg.
One solution would be to move linalg.reshape_tensor to the tensor dialect,
another one would be to move the canonicalization pattern in linalg.
It seems like the first solution is better because, despite
subtensor/subtensor_insert being linalg ops that have moved to other
dialects, they can be technically separated from linalg.
` but I don't think that we should replace it with linalg.reshape_tensor and
there is no tensor.reshape. `
Could you elaborate on why not ?
I see the obvious layering issue but I don't see something else offhand.
…On Mon, Apr 12, 2021 at 2:31 PM Han-Chung Wang ***@***.***> wrote:
I did some study on this, it looks like subtensor op has a canonicalization
pattern
<https://github.com/llvm/llvm-project/blob/0a92aff721f43406691e38a0965fd0c917121d09/mlir/lib/Dialect/StandardOps/IR/Ops.cpp#L1919-L1984>
to do it when the types are not match.
It failed (in my case) because the source type and result type do not have
the same rank. Dumping IRs from the pattern could see
%2449 = subtensor %2377[%2447, %c0] [1, 512] [1, 1] : tensor<1x512xf32> to tensor<512xf32>
%2448 = subtensor %2377[%2447, 0] [1, 512] [1, 1] : tensor<1x512xf32> to tensor<1x512xf32>
%2449 = tensor.cast %2448 : tensor<1x512xf32> to tensor<512xf32>
@MaheshRavishankar <https://github.com/MaheshRavishankar> suggested that
it should be tensor_reshape, but I don't think that we should replace it
with linalg.reshape_tensor and there is no tensor.reshape. This is
definitely a bug in the pattern because it would create an invalid cast op.
I actually hit a similar issue before:
https://llvm.discourse.group/t/lack-of-support-when-lowering-mhlo-reduce-to-linalg/2674
The issue in step one is that we are lacking a tensor_reshape like
operation. I tried tensor::CastOp and it complains about 'tensor<1xf32>'
and result type 'tensor' are cast incompatible. One workaround is to cast
it twice, but I don’t think this is the way to go.
cc @nicolasvasilache <https://github.com/nicolasvasilache>
side question:
I can not trigger the pattern with simpler example. I ran mlir-opt
-canonicalize a.mlir, but it seems that it never enter the pattern. It
did not dump anything. Is there a way to trigger it?
module {
func @dynamic_slice(%arg0: tensor<2x513xi16>, %arg1: tensor<i64>, %arg2: tensor<i64>) -> tensor<513xi16> {
%c1_i64 = constant 1 : i64
%c0_i64 = constant 0 : i64
%0 = tensor.extract %arg1[] : tensor<i64>
%1 = cmpi slt, %0, %c1_i64 : i64
%2 = select %1, %0, %c1_i64 : i64
%3 = cmpi sgt, %2, %c0_i64 : i64
%4 = select %3, %2, %c0_i64 : i64
%5 = index_cast %4 : i64 to index
%6 = tensor.extract %arg2[] : tensor<i64>
%7 = cmpi slt, %6, %c0_i64 : i64
%8 = select %7, %6, %c0_i64 : i64
%9 = cmpi sgt, %8, %c0_i64 : i64
%10 = select %9, %8, %c0_i64 : i64
%11 = index_cast %10 : i64 to index
%12 = subtensor %arg0[%5, %11] [1, 513] [1, 1] : tensor<2x513xi16> to tensor<513xi16>
return %12 : tensor<513xi16>
}
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5385 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACNNU5B2Z2BBYBQ3IKLI2ATTIM4B3ANCNFSM42VNTYPA>
.
--
N
|
From my experimentation this is all coming from concatenate and slice operations
that are inserting/taking unit-dim slices. The way to go is to have
rank-reducing versions of these generated to begin with. Then you could
insert a linalg.tensor_reshape for those.
Is it mhlo slice or something else (IIRC I killed linalg.slice a while
back).
Makes sense that the problem did not appear until now in core if
IREE-specific rewrites expose the problematic behavior.
Still it seems like a core canonicalization issue that does not look at
whether the cast will be valid?
…On Mon, Apr 12, 2021 at 3:02 PM MaheshRavishankar ***@***.***> wrote:
Definitely doing this as a cast is invalid. (you hit the same error
message, but the issue is different).
From my experimentation this is all coming from concatenate and slice
operations that are inserting/taking unit-dim slices. The way to go is to
have rank-reducing versions of these generated to begin with. Then you
could insert a linalg.tensor_reshape for those.
The rank-reducing versions are not handled properly in IREE, and there are
some canonicalization patterns missing in different places to make thse
work correctly. Here is the WIP changes to core I plan to work on this week
***@***.***
<MaheshRavishankar/llvm-project@1eae450>
. I can assign this bug to myself, it will take a few steps to plumb this
through.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5385 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACNNU5DHVGNWUFCNTXLTYODTIM7SZANCNFSM42VNTYPA>
.
--
N
|
Thanks @MaheshRavishankar , my case is actually coming from mhlo.dynamic-slice. The pattern was landed early today: tensorflow/mlir-hlo@a3fc99e Re @nicolasvasilache I was trying to say that
I think it is
Yes, you are right. it unconditionally creates a cast op when the operand type and result type mismatch. |
+ @pifon2a
Ok so it is prob. time that we think of unifying linalg.tensor_reshape +
linalg.reshape with the memref variant that is called memref.reshape.
Let's chat tomorrow CET ?
…On Mon, Apr 12, 2021 at 3:09 PM Han-Chung Wang ***@***.***> wrote:
Thanks @MaheshRavishankar <https://github.com/MaheshRavishankar> , my
case is actually coming from DynamicSlice which is added today.
Re @nicolasvasilache <https://github.com/nicolasvasilache> I was trying
to say that linalg.tensor_reshape lives in Linalg, so I don't think that
we should replace tensor.cast directly. Having a reshape op in tensor
dialect looks really good to me.
Is it mhlo slice or something else (IIRC I killed linalg.slice a while
back).
I think it is mhlo.slice and it will be lowered to subtensor op.
Still it seems like a core canonicalization issue that does not look at
whether the cast will be valid?
Yes, you are right. it unconditionally creates a cast op when the operand
type and result type mismatch.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5385 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACNNU5E77MXFLDKPERD33E3TINAPHANCNFSM42VNTYPA>
.
--
N
|
Sure, I sent the invitation.
On Mon, 12 Apr 2021, 21:36 Nicolas Vasilache, ***@***.***>
wrote:
… + @pifon2a
Ok so it is prob. time that we think of unifying linalg.tensor_reshape +
linalg.reshape with the memref variant that is called memref.reshape.
Let's chat tomorrow CET ?
On Mon, Apr 12, 2021 at 3:09 PM Han-Chung Wang ***@***.***>
wrote:
> Thanks @MaheshRavishankar <https://github.com/MaheshRavishankar> , my
> case is actually coming from DynamicSlice which is added today.
>
> Re @nicolasvasilache <https://github.com/nicolasvasilache> I was trying
> to say that linalg.tensor_reshape lives in Linalg, so I don't think that
> we should replace tensor.cast directly. Having a reshape op in tensor
> dialect looks really good to me.
>
> Is it mhlo slice or something else (IIRC I killed linalg.slice a while
> back).
>
> I think it is mhlo.slice and it will be lowered to subtensor op.
>
> Still it seems like a core canonicalization issue that does not look at
> whether the cast will be valid?
>
> Yes, you are right. it unconditionally creates a cast op when the operand
> type and result type mismatch.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#5385 (comment)>, or
> unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/ACNNU5E77MXFLDKPERD33E3TINAPHANCNFSM42VNTYPA
>
> .
>
--
N
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5385 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHYD636YU2I6D44W44B27CDTINDSDANCNFSM42VNTYPA>
.
|
Do you mind involving some folks from IREE too, or we could discuss in the codegen meeting tomorrow (9 AM PDT). |
Currently, the mhlo.pad will be lowered to linalg.pad_tensor and then lowered to `linalg.init_tensor + linalg.fill + subtensor_insert`. The init_tensor op will produce a dynamic shape even if the shape is static. This leads a `tensor.cast` op added and relies on further patterns to fix them. This is not needed to static shape and will hit some issues related to iree-org#5385.
…tic (#5516) Currently, the mhlo.pad will be lowered to linalg.pad_tensor and then lowered to `linalg.init_tensor + linalg.fill + subtensor_insert`. The init_tensor op will produce a dynamic shape even if the shape is static. This leads a `tensor.cast` op added and relies on further patterns to fix them. This is not needed to static shape and will hit some issues related to #5385.
Fixed by dbd43a7 |
Reopening (closed wrong bug) |
Should be fixed by this commit llvm/llvm-project@41849a9. @hanhanW please verify and close after the change comes in through the integrate process. |
Closing this. rank-reducing subtensors/subtensor_insert support is implemented now. |
subtensor
andsubview
ops support rank-reducing behavior. E.g.,This can fold some tensor_reshape away.
I also found that there are some issues in canonicalization pass. Some
tensor.cast
op will be created, and raising errors likeerror: 'tensor.cast' op operand type 'tensor<1x513xi16>' and result type 'tensor<513xi16>' are cast incompatible
.The text was updated successfully, but these errors were encountered: