From eed144bfbc4cf9eafebba1949aa81f615a865eea Mon Sep 17 00:00:00 2001 From: Phaneesh Barwaria Date: Tue, 16 Jan 2024 19:06:54 +0530 Subject: [PATCH] [ONNX][MLIR] add Identity op support (#2754) --- .../TorchOnnxToTorch/DefaultDomainGtoP.cpp | 13 +++++++++++++ .../TorchOnnxToTorch/simple_ops_g_to_p.mlir | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/Conversion/TorchOnnxToTorch/DefaultDomainGtoP.cpp b/lib/Conversion/TorchOnnxToTorch/DefaultDomainGtoP.cpp index c0a7473e4..d6b4e5bf1 100644 --- a/lib/Conversion/TorchOnnxToTorch/DefaultDomainGtoP.cpp +++ b/lib/Conversion/TorchOnnxToTorch/DefaultDomainGtoP.cpp @@ -560,4 +560,17 @@ void mlir::torch::onnx_c::populateDefaultDomainGtoP( binder.op, resultType, lhs, rhs); return success(); }); + patterns.onOp( + "Identity", 14, [](OpBinder binder, ConversionPatternRewriter &rewriter) { + Torch::ValueTensorType resultType; + Value tensor; + if (binder.tensorOperand(tensor) || + binder.tensorResultType(resultType)) { + return failure(); + } + Value noneVal = rewriter.create(binder.getLoc()); + rewriter.replaceOpWithNewOp( + binder.op, resultType, tensor, /*memory_format=*/noneVal); + return success(); + }); } diff --git a/test/Conversion/TorchOnnxToTorch/simple_ops_g_to_p.mlir b/test/Conversion/TorchOnnxToTorch/simple_ops_g_to_p.mlir index c85659c25..5d6b86172 100644 --- a/test/Conversion/TorchOnnxToTorch/simple_ops_g_to_p.mlir +++ b/test/Conversion/TorchOnnxToTorch/simple_ops_g_to_p.mlir @@ -456,3 +456,11 @@ func.func @test_not_2d(%arg0: !torch.vtensor<[3,4],i1>) -> !torch.vtensor<[3,4], %0 = torch.operator "onnx.Or"(%arg0, %arg1) : (!torch.vtensor<[3,4],i1>, !torch.vtensor<[3,4],i1>) -> !torch.vtensor<[3,4],i1> return %0 : !torch.vtensor<[3,4],i1> } + +// CHECK-LABEL: func.func @test_identity + func.func @test_identity(%arg0: !torch.vtensor<[3,4], f32>) -> !torch.vtensor<[3,4], f32> attributes {torch.onnx_meta.ir_version = 14 : si64, torch.onnx_meta.opset_version = 14 : si64, torch.onnx_meta.producer_name = "backend-test", torch.onnx_meta.producer_version = ""} { + // CHECK: %[[NONE:.*]] = torch.constant.none + // CHECK: %0 = torch.aten.clone %arg0, %[[NONE]] : !torch.vtensor<[3,4],f32>, !torch.none -> !torch.vtensor<[3,4],f32> + %0 = torch.operator "onnx.Identity"(%arg0) : (!torch.vtensor<[3,4], f32>) -> !torch.vtensor<[3,4], f32> + return %0 : !torch.vtensor<[3,4], f32> + }