-
Notifications
You must be signed in to change notification settings - Fork 48
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
Missing support for handling negative step values for Onnx.Slice op #824
Comments
Created an issue on IREE before iree-org/iree#18387, it fails on iree-compile. |
It seems that the issue is with the IR itself, since all the op lowerings are working as expected. Below is the line by line detailed analysis of the IR along with the inputs/outputs:
Link to the gist containing above analysis: https://gist.github.com/vivekkhandelwal1/c581d7c2a09b14f19519d3d6c10f7004 |
Oh man, this must have been exported from pytorch. All of the garbage leading into %250 is literally to re-arrange a constant list of ints, which we have to re-arrange back to the original when lowering torch-onnx to torch. |
Here is a repro of the real issue: module {
func.func @torch_jit(%arg0: !torch.vtensor<[1,3,240,240],f32>) -> !torch.vtensor<[?,?,?,?],f32> attributes {torch.onnx_meta.ir_version = 7 : si64, torch.onnx_meta.opset_version = 21 : si64, torch.onnx_meta.producer_name = "pytorch", torch.onnx_meta.producer_version = "1.12.1"} {
%0 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<_onnx__ConstantOfShape_1460> : tensor<1xsi64>} : () -> !torch.vtensor<[1],si64>
%1 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<_onnx__Concat_1461> : tensor<4xsi64>} : () -> !torch.vtensor<[4],si64>
%none = torch.constant.none
%2 = torch.operator "onnx.ConstantOfShape"(%0) {torch.onnx.value = dense_resource<_> : tensor<1xsi64>} : (!torch.vtensor<[1],si64>) -> !torch.vtensor<[4],si64>
%3 = torch.operator "onnx.Concat"(%1, %2) {torch.onnx.axis = 0 : si64} : (!torch.vtensor<[4],si64>, !torch.vtensor<[4],si64>) -> !torch.vtensor<[8],si64>
%4 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<__1> : tensor<2xsi64>} : () -> !torch.vtensor<[2],si64>
%5 = torch.operator "onnx.Reshape"(%3, %4) : (!torch.vtensor<[8],si64>, !torch.vtensor<[2],si64>) -> !torch.vtensor<[4,2],si64>
%6 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<__2> : tensor<1xsi64>} : () -> !torch.vtensor<[1],si64>
%7 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<__3> : tensor<1xsi64>} : () -> !torch.vtensor<[1],si64>
%8 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<__4> : tensor<1xsi64>} : () -> !torch.vtensor<[1],si64>
%9 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<__5> : tensor<1xsi64>} : () -> !torch.vtensor<[1],si64>
%10 = torch.operator "onnx.Slice"(%5, %7, %8, %6, %9) : (!torch.vtensor<[4,2],si64>, !torch.vtensor<[1],si64>, !torch.vtensor<[1],si64>, !torch.vtensor<[1],si64>, !torch.vtensor<[1],si64>) -> !torch.vtensor<[4,2],si64>
%11 = torch.operator "onnx.Transpose"(%10) {torch.onnx.perm = [1 : si64, 0 : si64]} : (!torch.vtensor<[4,2],si64>) -> !torch.vtensor<[2,4],si64>
%12 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<__6> : tensor<1xsi64>} : () -> !torch.vtensor<[1],si64>
%13 = torch.operator "onnx.Reshape"(%11, %12) : (!torch.vtensor<[2,4],si64>, !torch.vtensor<[1],si64>) -> !torch.vtensor<[8],si64>
%14 = torch.operator "onnx.Cast"(%13) {torch.onnx.to = 7 : si64} : (!torch.vtensor<[8],si64>) -> !torch.vtensor<[8],si64>
%15 = torch.operator "onnx.Constant"() {torch.onnx.value = dense_resource<__7> : tensor<f32>} : () -> !torch.vtensor<[],f32>
%16 = torch.operator "onnx.Pad"(%arg0, %14, %15) {torch.onnx.mode = "constant"} : (!torch.vtensor<[1,3,240,240],f32>, !torch.vtensor<[8],si64>, !torch.vtensor<[],f32>) -> !torch.vtensor<[?,?,?,?],f32>
return %16 : !torch.vtensor<[?,?,?,?],f32>
}
}
{-#
dialect_resources: {
builtin: {
_onnx__ConstantOfShape_1460: "0x080000000400000000000000",
_onnx__Concat_1461: "0x080000000000000000000000010000000000000000000000000000000100000000000000",
_: "0x080000000000000000000000",
__1: "0x08000000FFFFFFFFFFFFFFFF0200000000000000",
__2: "0x080000000000000000000000",
__3: "0x08000000FFFFFFFFFFFFFFFF",
__4: "0x080000000100000000000080",
__5: "0x08000000FFFFFFFFFFFFFFFF",
__6: "0x08000000FFFFFFFFFFFFFFFF",
__7: "0x0800000000000000"
}
}
#-} I ran this in the test suite by truncating the model "efficientnet_b1_pruned.in1k" at Pad node 0, then printed out the output and gold output shapes:
We need to figure out a better way to handle the insanity generated by pytorch exports of the pad op. |
Hi @zjgarvey, can you please let me know how did you do this? I would like to see the result of |
@vivekkhandelwal1 This test was generated by the following python code (added to azure_models.py, for example). from onnx_tests.helper_classes import TruncatedModel, get_trucated_constructor
class UpdateInit(TruncatedModel):
def __init__(self, n: int, op_type: str, *args, **kwargs):
super().__init__(n, op_type, *args, **kwargs)
self.opset_version = 21
self.update_opset_version_and_overwrite()
const = get_trucated_constructor(UpdateInit, AzureDownloadableModel, "efficientnet_b1_pruned.in1k")
register_test(const(0, "Pad"), f"efficientnet_pad_repro_0") You could add tests for the first few reshape ops by for i in range(0,3):
register_test(const(i,"Reshape"),f'efficientnet_Reshape_{i}') |
I followed these steps but I'm getting the following error:
|
So the issue here is that because of the negative step value in the Onnx.Slice op and that negative step value going down in the pipeline generates the following IR:
Now the issue is that the MLIR doesn't support negative strides(AFAIK), and hence it's giving wrong result(which is also wrong, ideally it should have errored out). Ideally the result of this IR should be:
but the result generated through iree-compilation and execution is:
and because of this the Solution: The Onnx.Slice and torch.aten.slice lowering has to be extended to handle the cases of negative steps. P.S. The reason why |
@MaheshRavishankar to check that maybe we should support negative strides and that is also a bug in the lowering? I think negative strides (and offsets) should actually be allowed by the semantics of tensor.extract_slice op. But fixing this in the The other broken thing seems, with an offset of [3,0] the output should just go out of bounds and be garbage if its not understanding the negative stride, so not sure how we got the non flipped output. |
For now, I'm adding the support in Onnx->Torch lowering. For the rest, we may have broader discussions. |
Fixed by llvm/torch-mlir#3763. |
Closing this since the fix llvm/torch-mlir#3763 is merged. |
Getting error as ''linalg.conv_2d_nchw_fchw' op inferred input/output operand #1 has shape's dimension #1 to be 4, but found 3' for following IR
command:
error:
The text was updated successfully, but these errors were encountered: