diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td --- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td +++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td @@ -174,8 +174,8 @@ buildUnaryOpWithQuantInfo($_builder, $_state, outputType, input); }]>; -// This builder is called on the TOSA pad operator that needs to create its own -// OptionalAttr quantization_attr parameter to scale the padding values +// These builders are called on the TOSA pad operator that needs to create its +// own OptionalAttr quantization_attr parameter to scale the padding values // correctly. def Tosa_PadOpQuantInfoBuilder : OpBuilder< (ins "Type":$outputType, "Value":$input, "Value":$paddings), @@ -184,6 +184,14 @@ input, paddings); }]>; +def Tosa_ExplicitValuePadOpQuantInfoBuilder : OpBuilder< + (ins "Type":$outputType, "Value":$input, "Value":$paddings, + "Value":$pad_value), + [{ + buildExplicitValuePadOpWithQuantInfo($_builder, $_state, outputType, + input, paddings, pad_value); + }]>; + //===----------------------------------------------------------------------===// // TOSA Operator. //===----------------------------------------------------------------------===// diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td --- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td +++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td @@ -321,9 +321,11 @@ let summary = "Computes clamp(features, min, max)."; let description = [{ - Clamp to an arbitrary minimum and maximum value. Note that the maximum and - minimum values are specified as signed quantized values, no scaling happens - before or after this operation. + Clamp to an arbitrary minimum and maximum value. + Maximum and minimum values are specified as values in the range of the + input type. + No zero point subtraction is done to the values, thus to clamp to the zero + point value, the zero point itself should be supplied as the minimum value. }]; let arguments = (ins @@ -1394,15 +1396,16 @@ DeclareOpInterfaceMethods, NoSideEffect]> { - let summary = "Pads a tensor with zeros."; + let summary = "Pads a tensor with value specified."; let description = [{ - Zero-pads a tensor along borders of each dimension. + Pads a tensor along borders of each dimension with pad_value. }]; let arguments = (ins Tosa_RankedTensor:$input1, Tosa_Int32Or64Tensor:$padding, + Optional:$pad_const, OptionalAttr:$quantization_info ); @@ -1410,7 +1413,8 @@ Tosa_RankedTensor:$output ); - let builders = [Tosa_PadOpQuantInfoBuilder]; + let builders = [Tosa_PadOpQuantInfoBuilder, + Tosa_ExplicitValuePadOpQuantInfoBuilder]; } //===----------------------------------------------------------------------===// diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td --- a/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td +++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td @@ -117,6 +117,9 @@ // Tensor types with constrained ranks. //===----------------------------------------------------------------------===// +// Rank-0 (scalar) tensor +def Tosa_ScalarTensor : TensorRankOf<[Tosa_AnyNumber], [0]>; + // We include unranked tensors as a supported type for all possible tosa // Tensors as unranked does not guarantee invalid. If unranked tensors exist // they should be shape propagate used Tosa's shape inference pass and verified diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp --- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp +++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp @@ -609,7 +609,7 @@ /// This builder is called on TOSA pad operator that needs to create its own /// OptionalAttr quantization_attr parameter to scale the padding values -/// correctly. +/// correctly. No pad_const is interpreted as zero-padding. static void buildPadOpWithQuantInfo(OpBuilder &builder, OperationState &result, Type outputType, Value input, Value paddings) { @@ -620,6 +620,20 @@ result.types.push_back(outputType); } +/// This builder is called on TOSA pad operator when an explicit pad_const +/// value is passed in. It also optionally constructs quantization_attr. +static void buildExplicitValuePadOpWithQuantInfo(OpBuilder &builder, + OperationState &result, + Type outputType, Value input, + Value paddings, + Value pad_const) { + result.addOperands({input, paddings, pad_const}); + auto quantAttr = buildPadOpQuantizationAttr(builder, input); + if (quantAttr) + result.addAttribute("quantization_info", quantAttr); + result.types.push_back(outputType); +} + //===----------------------------------------------------------------------===// // TOSA Operator Return Type Inference. //===----------------------------------------------------------------------===// diff --git a/mlir/test/Dialect/Tosa/ops.mlir b/mlir/test/Dialect/Tosa/ops.mlir --- a/mlir/test/Dialect/Tosa/ops.mlir +++ b/mlir/test/Dialect/Tosa/ops.mlir @@ -395,6 +395,14 @@ return %0 : tensor<13x21x3xf32> } +// ----- +// CHECK-LABEL: pad_explicit_value +func @test_pad_explicit_value(%arg0: tensor<13x21x3xf32>, %arg1: tensor<3x2xi32>) -> tensor<13x21x3xf32> { + %0 = "tosa.const"() {value = dense<3.14> : tensor} : () -> tensor + %1 = "tosa.pad"(%arg0, %arg1, %0) : (tensor<13x21x3xf32>, tensor<3x2xi32>, tensor) -> tensor<13x21x3xf32> + return %1 : tensor<13x21x3xf32> +} + // ----- // CHECK-LABEL: reshape func @test_reshape(%arg0: tensor<13x21x3xf32>) -> tensor<1x819xf32> {