Skip to content

Commit eed144b

Browse files
authored
[ONNX][MLIR] add Identity op support (#2754)
1 parent a8538e1 commit eed144b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/Conversion/TorchOnnxToTorch/DefaultDomainGtoP.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -560,4 +560,17 @@ void mlir::torch::onnx_c::populateDefaultDomainGtoP(
560560
binder.op, resultType, lhs, rhs);
561561
return success();
562562
});
563+
patterns.onOp(
564+
"Identity", 14, [](OpBinder binder, ConversionPatternRewriter &rewriter) {
565+
Torch::ValueTensorType resultType;
566+
Value tensor;
567+
if (binder.tensorOperand(tensor) ||
568+
binder.tensorResultType(resultType)) {
569+
return failure();
570+
}
571+
Value noneVal = rewriter.create<Torch::ConstantNoneOp>(binder.getLoc());
572+
rewriter.replaceOpWithNewOp<Torch::AtenCloneOp>(
573+
binder.op, resultType, tensor, /*memory_format=*/noneVal);
574+
return success();
575+
});
563576
}

test/Conversion/TorchOnnxToTorch/simple_ops_g_to_p.mlir

+8
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,11 @@ func.func @test_not_2d(%arg0: !torch.vtensor<[3,4],i1>) -> !torch.vtensor<[3,4],
456456
%0 = torch.operator "onnx.Or"(%arg0, %arg1) : (!torch.vtensor<[3,4],i1>, !torch.vtensor<[3,4],i1>) -> !torch.vtensor<[3,4],i1>
457457
return %0 : !torch.vtensor<[3,4],i1>
458458
}
459+
460+
// CHECK-LABEL: func.func @test_identity
461+
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 = ""} {
462+
// CHECK: %[[NONE:.*]] = torch.constant.none
463+
// CHECK: %0 = torch.aten.clone %arg0, %[[NONE]] : !torch.vtensor<[3,4],f32>, !torch.none -> !torch.vtensor<[3,4],f32>
464+
%0 = torch.operator "onnx.Identity"(%arg0) : (!torch.vtensor<[3,4], f32>) -> !torch.vtensor<[3,4], f32>
465+
return %0 : !torch.vtensor<[3,4], f32>
466+
}

0 commit comments

Comments
 (0)