From 84377fe862a0bf36bb3c27166b766bc1097954ae Mon Sep 17 00:00:00 2001 From: Richard Uhler Date: Thu, 11 Feb 2021 11:17:07 -0800 Subject: [PATCH] Verify that the paddings argument to Pad ops has rank 2. Otherwise legalization of tf.Pad to mhlo.pad was giving the mysterious error 'mhlo.pad' op requires attribute 'edge_padding_low'. PiperOrigin-RevId: 357017629 Change-Id: I4271f559d8ebc0c43fa35cbdd80a344770b252ea --- .../mlir/tensorflow/ir/tf_generated_ops.td | 8 +++--- .../mlir/tensorflow/tests/tf-ops.mlir | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/tensorflow/compiler/mlir/tensorflow/ir/tf_generated_ops.td b/tensorflow/compiler/mlir/tensorflow/ir/tf_generated_ops.td index 9b3aab597ef..a5d16d7eeb2 100644 --- a/tensorflow/compiler/mlir/tensorflow/ir/tf_generated_ops.td +++ b/tensorflow/compiler/mlir/tensorflow/ir/tf_generated_ops.td @@ -8389,7 +8389,7 @@ def TF_MinimumOp : TF_Op<"Minimum", [NoSideEffect, ResultsBroadcastableShape, TF TF_DerivedOperandTypeAttr T = TF_DerivedOperandTypeAttr<0>; } -def TF_MirrorPadOp : TF_Op<"MirrorPad", [NoSideEffect]> { +def TF_MirrorPadOp : TF_Op<"MirrorPad", [NoSideEffect, TF_OperandHasRank<1, 2>]> { let summary = "Pads a tensor with mirrored values."; let description = [{ @@ -8436,7 +8436,7 @@ rows must be the same as the rank of `input`.}]>:$paddings, TF_DerivedOperandTypeAttr Tpaddings = TF_DerivedOperandTypeAttr<1>; } -def TF_MirrorPadGradOp : TF_Op<"MirrorPadGrad", [NoSideEffect]> { +def TF_MirrorPadGradOp : TF_Op<"MirrorPadGrad", [NoSideEffect, TF_OperandHasRank<1, 2>]> { let summary = [{ Gradient op for `MirrorPad` op. This op folds a mirror-padded tensor. }]; @@ -9315,7 +9315,7 @@ This is the opposite of `unpack`. let hasFolder = 1; } -def TF_PadOp : TF_Op<"Pad", [NoSideEffect, TF_FoldOperandsTransposeInterface]> { +def TF_PadOp : TF_Op<"Pad", [NoSideEffect, TF_FoldOperandsTransposeInterface, TF_OperandHasRank<1, 2>]> { let summary = "Pads a tensor with zeros."; let description = [{ @@ -9363,7 +9363,7 @@ pad(t, paddings) ==> [[0, 0, 0, 0, 0, 0] }]; } -def TF_PadV2Op : TF_Op<"PadV2", [NoSideEffect]> { +def TF_PadV2Op : TF_Op<"PadV2", [NoSideEffect, TF_OperandHasRank<1, 2>]> { let summary = "Pads a tensor."; let description = [{ diff --git a/tensorflow/compiler/mlir/tensorflow/tests/tf-ops.mlir b/tensorflow/compiler/mlir/tensorflow/tests/tf-ops.mlir index e226da59703..f0dcfd3b9f0 100644 --- a/tensorflow/compiler/mlir/tensorflow/tests/tf-ops.mlir +++ b/tensorflow/compiler/mlir/tensorflow/tests/tf-ops.mlir @@ -308,6 +308,34 @@ func @testIncompatibleElementTypes(%arg0: tensor<3x2xf32>, %arg1: tensor<3x2xf32 // ----- +func @testPadRank1Paddings(%input: tensor<2xi64>) -> tensor<3xi64> { + %paddings = "tf.Const"() {value = dense<[0, 1]> : tensor<2xi64>} : () -> tensor<2xi64> + // expected-error @+1 {{failed to verify that operand 1 is 2-D}} + %0 = "tf.Pad"(%input, %paddings) : (tensor<2xi64>, tensor<2xi64>) -> tensor<3xi64> + return %0 : tensor<3xi64> +} + +// ----- + +func @testPadV2Rank1Paddings(%input: tensor<2xi64>) -> tensor<3xi64> { + %constant = "tf.Const"() {value = dense<1> : tensor} : () -> tensor + %paddings = "tf.Const"() {value = dense<[0, 1]> : tensor<2xi64>} : () -> tensor<2xi64> + // expected-error @+1 {{failed to verify that operand 1 is 2-D}} + %0 = "tf.PadV2"(%input, %paddings, %constant) : (tensor<2xi64>, tensor<2xi64>, tensor) -> tensor<3xi64> + return %0 : tensor<3xi64> +} + +// ----- + +func @testMirrorPadRank1Paddings(%input: tensor<2xi64>) -> tensor<3xi64> { + %paddings = "tf.Const"() {value = dense<[0, 1]> : tensor<2xi64>} : () -> tensor<2xi64> + // expected-error @+1 {{failed to verify that operand 1 is 2-D}} + %0 = "tf.MirrorPad"(%input, %paddings) { mode = "SYMMETRIC" }: (tensor<2xi64>, tensor<2xi64>) -> tensor<3xi64> + return %0 : tensor<3xi64> +} + +// ----- + // CHECK-LABEL: func @testReshape(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>, %arg2: tensor<10000xf32>, %arg3: tensor<*xi32>) func @testReshape(%arg0: tensor<*xf32>, %arg1: tensor<*xf32>, %arg2: tensor<10000xf32>, %arg3: tensor<*xi32>) -> (tensor<100x100xf32>, tensor<*xf32>, tensor<100x100xf32>, tensor<100x100xf32>, tensor<*xf32>, tensor<*xf32>) { %shape1 = constant dense<100> : tensor<2xi32>