Skip to content

Commit cc5c33d

Browse files
committed
[mlir][tosa] Change the shift of mul to be required
Set the shift to be a mandatory operand. Change-Id: Ic8f6e2f653c6f5875f8b789a2da27d2834aa442a Signed-off-by: Tai Ly <[email protected]>
1 parent bddc815 commit cc5c33d

File tree

12 files changed

+135
-78
lines changed

12 files changed

+135
-78
lines changed

mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def Tosa_Conv2DOp : Tosa_ConvOp<"conv2d"> {
105105
Tosa_Tensor4D:$input,
106106
TosaTensorRankOf<[Tosa_Weight], [4]>:$weight,
107107
Tosa_Tensor1D:$bias,
108-
Optional<Tosa_ZeroPointTensor>:$input_zp,
109-
Optional<Tosa_ZeroPointTensor>:$weight_zp,
108+
Optional<Tosa_ScalarTensor>:$input_zp,
109+
Optional<Tosa_ScalarTensor>:$weight_zp,
110110
Tosa_IntArrayAttr4:$pad,
111111
Tosa_IntArrayAttr2:$stride,
112112
Tosa_IntArrayAttr2:$dilation,
@@ -136,8 +136,8 @@ def Tosa_Conv3DOp : Tosa_ConvOp<"conv3d"> {
136136
Tosa_Tensor5D:$input,
137137
TosaTensorRankOf<[Tosa_Weight], [5]>:$weight,
138138
Tosa_Tensor1D:$bias,
139-
Optional<Tosa_ZeroPointTensor>:$input_zp,
140-
Optional<Tosa_ZeroPointTensor>:$weight_zp,
139+
Optional<Tosa_ScalarTensor>:$input_zp,
140+
Optional<Tosa_ScalarTensor>:$weight_zp,
141141
Tosa_IntArrayAttr6:$pad,
142142
Tosa_IntArrayAttr3:$stride,
143143
Tosa_IntArrayAttr3:$dilation,
@@ -168,8 +168,8 @@ def Tosa_DepthwiseConv2DOp : Tosa_ConvOp<"depthwise_conv2d"> {
168168
Tosa_Tensor4D:$input,
169169
TosaTensorRankOf<[Tosa_Weight], [4]>:$weight,
170170
Tosa_Tensor1D:$bias,
171-
Optional<Tosa_ZeroPointTensor>:$input_zp,
172-
Optional<Tosa_ZeroPointTensor>:$weight_zp,
171+
Optional<Tosa_ScalarTensor>:$input_zp,
172+
Optional<Tosa_ScalarTensor>:$weight_zp,
173173
Tosa_IntArrayAttr4:$pad,
174174
Tosa_IntArrayAttr2:$stride,
175175
Tosa_IntArrayAttr2:$dilation,
@@ -356,8 +356,8 @@ def Tosa_TransposeConv2DOp : Tosa_ConvOp<"transpose_conv2d"> {
356356
Tosa_Tensor4D:$input,
357357
TosaTensorRankOf<[Tosa_Weight], [4]>:$weight,
358358
Tosa_Tensor1D:$bias,
359-
Optional<Tosa_ZeroPointTensor>:$input_zp,
360-
Optional<Tosa_ZeroPointTensor>:$weight_zp,
359+
Optional<Tosa_ScalarTensor>:$input_zp,
360+
Optional<Tosa_ScalarTensor>:$weight_zp,
361361
Tosa_IntArrayAttr4:$out_pad,
362362
Tosa_IntArrayAttr2:$stride,
363363
Tosa_IntArrayAttr4:$out_shape,
@@ -819,7 +819,8 @@ def Tosa_MulOp : Tosa_Op<"mul", [
819819
let arguments = (ins
820820
Tosa_Tensor:$input1,
821821
Tosa_Tensor:$input2,
822-
Optional<TosaTensorRankOf<[Tosa_Int8], [1]>>:$shift
822+
// Apply right shift on i32_t input data only
823+
Tosa_ScalarInt8Tensor:$shift
823824
);
824825

825826
let results = (outs
@@ -1592,7 +1593,7 @@ def Tosa_PadOp : Tosa_InferShapedTypeOp<"pad"> {
15921593
let arguments = (ins
15931594
Tosa_RankedTensor:$input1,
15941595
Tosa_Shape:$padding,
1595-
Optional<Tosa_ScalarTensor>:$pad_const,
1596+
Optional<Tosa_Rank0Tensor>:$pad_const,
15961597
OptionalAttr<I32Attr>:$input_zp
15971598
);
15981599

mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def HasNo0Dimensions : And<[
9393
IsRankedTensorTypePred,
9494
CPred<"::llvm::all_of(::llvm::cast<::mlir::RankedTensorType>($_self).getShape(), [](auto v) { return v != 0; })">]>;
9595

96+
def AllDimensionsAreSizeOne : And<[
97+
IsRankedTensorTypePred,
98+
CPred<"::llvm::all_of(::llvm::cast<::mlir::RankedTensorType>($_self).getShape(), [](auto v) { return v == 1; })">]>;
99+
96100
class TosaTensorOf<
97101
list<Type> allowedTypes, string summary = "tosa-conformant tensor">
98102
: TensorOf<allowedTypes, [Or<[HasNo0Dimensions, IsUnrankedTensorTypePred]>], summary>;
@@ -109,6 +113,11 @@ class TosaTensorRankOf<list<Type> allowedTypes, list<int> ranks>
109113
[HasAnyRankOfPred<ranks>],
110114
!interleave(!foreach(rank, ranks, rank # "D"), "/") # " tensor">;
111115

116+
class TosaScalarTensorOf<list<Type> allowedTypes, list<int> ranks>
117+
: TosaRankedTensorOf<allowedTypes,
118+
[HasAnyRankOfPred<ranks>, AllDimensionsAreSizeOne],
119+
"tosa-conformant scalar tensor">;
120+
112121
//===----------------------------------------------------------------------===//
113122
// Tensor types
114123
//===----------------------------------------------------------------------===//
@@ -136,8 +145,10 @@ class Tosa_TensorOfOrNone<list<Type> allowedTypes, string description = ""> :
136145
// Tensor types with constrained ranks.
137146
//===----------------------------------------------------------------------===//
138147

139-
// Rank-0 (scalar) tensor
140-
def Tosa_ScalarTensor : TosaTensorRankOf<[Tosa_AnyNumber], [0]>;
148+
def Tosa_Rank0Tensor : TosaTensorRankOf<[Tosa_AnyNumber], [0]>;
149+
150+
def Tosa_ScalarTensor : TosaScalarTensorOf<[Tosa_AnyNumber], [1]>;
151+
def Tosa_ScalarInt8Tensor : TosaScalarTensorOf<[Tosa_Int8], [1]>;
141152

142153
// We include unranked tensors as a supported type for all possible tosa
143154
// Tensors as unranked does not guarantee invalid. If unranked tensors exist
@@ -288,9 +299,4 @@ def Rank1TosaShape : TosaShapeOfRank<1>;
288299
def Rank2TosaShape : TosaShapeOfRank<2>;
289300
def Rank4TosaShape : TosaShapeOfRank<4>;
290301

291-
// NOTE: Tosa_ScalarTensor is currently defined as rank-0. If and when this
292-
// becomes rank-1 it can be used in place of Tosa_ZeroPointTensor and the
293-
// following def can be removed.
294-
def Tosa_ZeroPointTensor : TosaTensorRankOf<[Tosa_AnyNumber], [1]>;
295-
296302
#endif // TOSA_TYPES_BASE

mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,27 @@ static Value createLinalgBodyCalculationForElementwiseOp(
9292
// tosa::MulOp
9393
if (isa<tosa::MulOp>(op)) {
9494
auto shift_val = cast<tosa::MulOp>(op).getShift();
95+
ElementsAttr shift_elem;
96+
if (!shift_val.getImpl() ||
97+
!matchPattern(shift_val, m_Constant(&shift_elem))) {
98+
(void)rewriter.notifyMatchFailure(op, "shift value of mul not found");
99+
}
100+
101+
int32_t shift = shift_elem.getValues<IntegerAttr>()[0].getInt();
95102

96103
if (isa<FloatType>(elementTy)) {
104+
if (shift != 0) {
105+
(void)rewriter.notifyMatchFailure(op,
106+
"Cannot have shift value for float");
107+
return nullptr;
108+
}
97109
return rewriter.create<arith::MulFOp>(loc, resultTypes, args[0], args[1]);
98110
}
99111

100112
if (isa<IntegerType>(elementTy)) {
101-
int32_t shift = 0;
102-
ElementsAttr shift_elem;
103-
if (shift_val.getImpl() &&
104-
matchPattern(shift_val, m_Constant(&shift_elem))) {
105-
// Explicit shift is set.
106-
shift = shift_elem.getValues<IntegerAttr>()[0].getInt();
107-
}
108-
109113
Value a = args[0];
110114
Value b = args[1];
115+
111116
if (shift > 0) {
112117
auto shiftConst =
113118
rewriter.create<arith::ConstantIntOp>(loc, shift, /*bitwidth=*/8);

mlir/lib/Dialect/Tosa/IR/TosaOps.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,16 +1104,10 @@ LogicalResult tosa::MulOp::inferReturnTypeComponents(
11041104
ValueShapeRange operands, DictionaryAttr attributes,
11051105
OpaqueProperties properties, RegionRange regions,
11061106
SmallVectorImpl<ShapedTypeComponents> &inferredReturnShapes) {
1107-
LogicalResult status = success();
1107+
// mul op's output shape only depend on input1 and input2, not on shift
1108+
ValueShapeRange twoInputs = operands.drop_back();
11081109
llvm::SmallVector<int64_t> outShape;
1109-
if (operands.size() == 2) {
1110-
status = resolveBroadcastShape(operands, outShape);
1111-
} else {
1112-
// mul op's output shape only depend on input1 and input2, not on shift
1113-
ValueShapeRange two_inputs = operands.drop_back();
1114-
status = resolveBroadcastShape(two_inputs, outShape);
1115-
}
1116-
if (status.failed()) {
1110+
if (resolveBroadcastShape(twoInputs, outShape).failed()) {
11171111
inferredReturnShapes.push_back(ShapedTypeComponents());
11181112
} else {
11191113
inferredReturnShapes.push_back(ShapedTypeComponents(outShape));
@@ -1148,6 +1142,15 @@ LogicalResult tosa::MulOp::verify() {
11481142
return emitOpError(
11491143
"requires the same element type for all operands and results");
11501144
}
1145+
1146+
// verify shift has value 0 for non-integer types
1147+
ElementsAttr shift_elem;
1148+
if (matchPattern(getShift(), m_Constant(&shift_elem))) {
1149+
int32_t shift = shift_elem.getValues<IntegerAttr>()[0].getInt();
1150+
if (shift != 0) {
1151+
return emitOpError() << "require shift to be 0 for float type";
1152+
}
1153+
}
11511154
}
11521155

11531156
// Verify the op has same ranks for all main operands (excludes extra operands

mlir/lib/Dialect/Tosa/Transforms/TosaReduceTransposes.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ bool TosaReduceTransposes::collectFanIn(Operation *op,
287287

288288
for (Value operand : op->getOperands()) {
289289
// If this is a problem in future, think about alternatives to recursion.
290-
if (llvm::isa<tosa::MulOp>(op) && op->getNumOperands() == 3 &&
291-
operand == op->getOperand(2)) {
290+
if (llvm::isa<tosa::MulOp>(op) && operand == op->getOperand(2)) {
292291
// do not recurse into MulOp's shift operand
293292
continue;
294293
}
@@ -332,8 +331,7 @@ std::optional<Value> TosaReduceTransposes::buildMappedToValue(
332331
for (Value v : op->getOperands()) {
333332
if (valuesMap.contains(v)) {
334333
operands.push_back(valuesMap.at(v));
335-
} else if (llvm::isa<tosa::MulOp>(op) && op->getNumOperands() == 3 &&
336-
v == op->getOperand(2)) {
334+
} else if (llvm::isa<tosa::MulOp>(op) && v == op->getOperand(2)) {
337335
// special case for MulOp's shift operand
338336
operands.push_back(v);
339337
} else {

mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ func.func @test_simple_f32(%arg0: tensor<1xf32>) -> () {
472472

473473
// CHECK: linalg.generic
474474
// CHECK: arith.mulf
475-
%4 = tosa.mul %0, %1 : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32>
475+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
476+
%4 = tosa.mul %0, %1, %shift : (tensor<1xf32>, tensor<1xf32>, tensor<1xi8>) -> tensor<1xf32>
476477

477478
// CHECK: linalg.generic
478479
// CHECK: arith.negf
@@ -618,7 +619,8 @@ func.func @test_simple_i16(%arg0: tensor<1xi16>) -> () {
618619
// CHECK: arith.extsi
619620
// CHECK: arith.extsi
620621
// CHECK: arith.muli
621-
%0 = tosa.mul %arg0, %arg0 : (tensor<1xi16>, tensor<1xi16>) -> tensor<1xi32>
622+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
623+
%0 = tosa.mul %arg0, %arg0, %shift : (tensor<1xi16>, tensor<1xi16>, tensor<1xi8>) -> tensor<1xi32>
622624

623625
return
624626
}

mlir/test/Dialect/Tosa/canonicalize.mlir

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ func.func @pad_determine_val_quant(%arg0: tensor<?x?xi32>, %arg1 : tensor<2x2xi3
331331
func.func @mul_one_float(%arg0: tensor<2x3xf32>) -> tensor<2x3xf32> {
332332
// CHECK: return %arg0
333333
// CHECK-NOT: tosa.mul
334+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
334335
%ones = "tosa.const"() {value = dense<1.0> : tensor<2x3xf32>} : () -> tensor<2x3xf32>
335-
%1 = tosa.mul %arg0, %ones : (tensor<2x3xf32>, tensor<2x3xf32>) -> tensor<2x3xf32>
336+
%1 = tosa.mul %arg0, %ones, %shift : (tensor<2x3xf32>, tensor<2x3xf32>, tensor<1xi8>) -> tensor<2x3xf32>
336337
return %1 : tensor<2x3xf32>
337338
}
338339

@@ -343,7 +344,8 @@ func.func @mul_bcast_one_float(%arg0: tensor<2x3xf32>) -> tensor<2x3xf32> {
343344
// CHECK: return %arg0
344345
// CHECK-NOT: tosa.mul
345346
%ones = "tosa.const"() {value = dense<1.0> : tensor<1x1xf32>} : () -> tensor<1x1xf32>
346-
%1 = tosa.mul %ones, %arg0 : (tensor<1x1xf32>, tensor<2x3xf32>) -> tensor<2x3xf32>
347+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
348+
%1 = tosa.mul %ones, %arg0, %shift : (tensor<1x1xf32>, tensor<2x3xf32>, tensor<1xi8>) -> tensor<2x3xf32>
347349
return %1 : tensor<2x3xf32>
348350
}
349351

@@ -379,11 +381,12 @@ func.func @mul_zero_broadcast(%arg0: tensor<2x3xf32>) -> (tensor<2x3xf32>, tenso
379381
// CHECK: %[[ZERO:.*]] = "tosa.const"() <{value = dense<0.000000e+00> : tensor<2x3xf32>}
380382
// CHECK-NOT: tosa.mul
381383
%zeros = "tosa.const"() {value = dense<0.0> : tensor<1x1xf32>} : () -> tensor<1x1xf32>
382-
%1 = tosa.mul %arg0, %zeros : (tensor<2x3xf32>, tensor<1x1xf32>) -> tensor<2x3xf32>
384+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
385+
%1 = tosa.mul %arg0, %zeros, %shift : (tensor<2x3xf32>, tensor<1x1xf32>, tensor<1xi8>) -> tensor<2x3xf32>
383386

384387
// CHECK-NOT: tosa.mul
385388
// CHECK: return %[[ZERO]], %[[ZERO]]
386-
%2 = tosa.mul %zeros, %arg0 : (tensor<1x1xf32>, tensor<2x3xf32>) -> tensor<2x3xf32>
389+
%2 = tosa.mul %zeros, %arg0, %shift : (tensor<1x1xf32>, tensor<2x3xf32>, tensor<1xi8>) -> tensor<2x3xf32>
387390
return %1, %2 : tensor<2x3xf32>, tensor<2x3xf32>
388391
}
389392

@@ -966,7 +969,8 @@ func.func @mul_quant_nofold() -> tensor<1x!quant.uniform<i8:f32, 3.0757404601899
966969
// CHECK: tosa.mul
967970
%0 = "tosa.const"() {value = dense<0> : tensor<1xi8>} : () -> tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>
968971
%1 = "tosa.const"() {value = dense<1> : tensor<1xi8>} : () -> tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>
969-
%2 = tosa.mul %0, %1 : (tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>, tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>)-> tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>
972+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
973+
%2 = tosa.mul %0, %1, %shift : (tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>, tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>, tensor<1xi8>) -> tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>
970974
return %2 : tensor<1x!quant.uniform<i8:f32, 3.0757404601899907E-5:-128>>
971975
}
972976

mlir/test/Dialect/Tosa/constant-op-fold.mlir

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ func.func @fold_div_splat_i32() -> tensor<i32> {
238238
func.func @fold_mul_zero_rhs_f32(%arg0: tensor<f32>) -> tensor<f32> {
239239
%zero = "tosa.const"() {value = dense<0.0> : tensor<f32>} : () -> tensor<f32>
240240
// CHECK: %[[ZERO:.+]] = "tosa.const"() <{value = dense<0.000000e+00>
241-
%mul = tosa.mul %arg0, %zero : (tensor<f32>, tensor<f32>) -> tensor<f32>
241+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
242+
%mul = tosa.mul %arg0, %zero, %shift : (tensor<f32>, tensor<f32>, tensor<1xi8>) -> tensor<f32>
242243
// CHECK: return %[[ZERO]]
243244
return %mul : tensor<f32>
244245
}
@@ -249,7 +250,8 @@ func.func @fold_mul_zero_rhs_f32(%arg0: tensor<f32>) -> tensor<f32> {
249250
func.func @fold_mul_zero_lhs_f32(%arg0: tensor<f32>) -> tensor<f32> {
250251
%zero = "tosa.const"() {value = dense<0.0> : tensor<f32>} : () -> tensor<f32>
251252
// CHECK: %[[ZERO:.+]] = "tosa.const"() <{value = dense<0.000000e+00>
252-
%mul = tosa.mul %zero, %arg0 : (tensor<f32>, tensor<f32>) -> tensor<f32>
253+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
254+
%mul = tosa.mul %zero, %arg0, %shift : (tensor<f32>, tensor<f32>, tensor<1xi8>) -> tensor<f32>
253255
// CHECK: return %[[ZERO]]
254256
return %mul : tensor<f32>
255257
}
@@ -283,7 +285,8 @@ func.func @fold_mul_zero_lhs_i32(%arg0: tensor<i32>) -> tensor<i32> {
283285
// CHECK-LABEL: @fold_mul_one_rhs_f32
284286
func.func @fold_mul_one_rhs_f32(%arg0: tensor<f32>) -> tensor<f32> {
285287
%one = "tosa.const"() {value = dense<1.0> : tensor<f32>} : () -> tensor<f32>
286-
%mul = tosa.mul %arg0, %one : (tensor<f32>, tensor<f32>) -> tensor<f32>
288+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
289+
%mul = tosa.mul %arg0, %one, %shift : (tensor<f32>, tensor<f32>, tensor<1xi8>) -> tensor<f32>
287290
// CHECK: return %arg0
288291
return %mul : tensor<f32>
289292
}
@@ -293,7 +296,8 @@ func.func @fold_mul_one_rhs_f32(%arg0: tensor<f32>) -> tensor<f32> {
293296
// CHECK-LABEL: @fold_mul_one_lhs_f32
294297
func.func @fold_mul_one_lhs_f32(%arg0: tensor<f32>) -> tensor<f32> {
295298
%one = "tosa.const"() {value = dense<1.0> : tensor<f32>} : () -> tensor<f32>
296-
%mul = tosa.mul %one, %arg0 : (tensor<f32>, tensor<f32>) -> tensor<f32>
299+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
300+
%mul = tosa.mul %one, %arg0, %shift : (tensor<f32>, tensor<f32>, tensor<1xi8>) -> tensor<f32>
297301
// CHECK: return %arg0
298302
return %mul : tensor<f32>
299303
}
@@ -339,7 +343,8 @@ func.func @fold_mul_splat_i8() -> tensor<10xi32> {
339343
func.func @fold_mul_splat_f32() -> tensor<10xf32> {
340344
%one = "tosa.const"() {value = dense<3.0> : tensor<10xf32>} : () -> tensor<10xf32>
341345
%two = "tosa.const"() {value = dense<2.0> : tensor<10xf32>} : () -> tensor<10xf32>
342-
%mul = tosa.mul %one, %two : (tensor<10xf32>, tensor<10xf32>) -> tensor<10xf32>
346+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
347+
%mul = tosa.mul %one, %two, %shift : (tensor<10xf32>, tensor<10xf32>, tensor<1xi8>) -> tensor<10xf32>
343348
// CHECK: %[[THREE:.+]] = "tosa.const"() <{value = dense<6.000000e+00> : tensor<10xf32>}
344349
// CHECK: return %[[THREE]]
345350
return %mul : tensor<10xf32>

mlir/test/Dialect/Tosa/invalid.mlir

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -729,26 +729,27 @@ func.func @test_transpose_conv2d_invalid_outshape(%arg0: tensor<1x32x32x8xf32>,
729729

730730
// CHECK-LABEL: test_mul_type_mismatch
731731
func.func @test_mul_type_mismatch(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x1x3xf16>) -> tensor<13x21x3xf32> {
732+
%shift = "tosa.const"() {value = dense<0> : tensor<1xi8>} : () -> tensor<1xi8>
732733
// expected-error@+1 {{'tosa.mul' op requires the same element type for all operands}}
733-
%0 = tosa.mul %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<13x1x3xf16>) -> tensor<13x21x3xf32>
734+
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x3xf32>, tensor<13x1x3xf16>, tensor<1xi8>) -> tensor<13x21x3xf32>
734735
return %0 : tensor<13x21x3xf32>
735736
}
736737

737738
// -----
738739

739740
// CHECK-LABEL: test_mul_invalid_shift
740-
func.func @test_mul_invalid_shift(%arg0: tensor<13x21x3xi32>, %arg1: tensor<13x1x3xi32>) -> tensor<13x21x3xi32> {
741-
%shift = "tosa.const"() {value = dense<0.0> : tensor<f32>} : () -> tensor<f32>
742-
// expected-error@+1 {{'tosa.mul' op operand #2 must be 1D tensor of 8-bit signless integer values, but got 'tensor<f32>'}}
743-
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x3xi32>, tensor<13x1x3xi32>, tensor<f32>) -> tensor<13x21x3xi32>
744-
return %0 : tensor<13x21x3xi32>
741+
func.func @test_mul_invalid_shift(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x1x3xf32>) -> tensor<13x21x3xf32> {
742+
%shift = "tosa.const"() {value = dense<1> : tensor<1xi8>} : () -> tensor<1xi8>
743+
// expected-error@+1 {{'tosa.mul' op require shift to be 0 for float type}}
744+
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x3xf32>, tensor<13x1x3xf32>, tensor<1xi8>) -> tensor<13x21x3xf32>
745+
return %0 : tensor<13x21x3xf32>
745746
}
746747

747748
// -----
748749

749750
// CHECK-LABEL: test_mul_missing_shift
750751
func.func @test_mul_missing_shift(%arg0: tensor<13x21x3xi32>, %arg1: tensor<13x1x3xi32>) -> tensor<13x21x3xi32> {
751-
// this is ok because mul's shift operand is optional for now
752+
// expected-error@+1 {{'tosa.mul' op expected 3 operands, but found 2}}
752753
%0 = tosa.mul %arg0, %arg1 : (tensor<13x21x3xi32>, tensor<13x1x3xi32>) -> tensor<13x21x3xi32>
753754
return %0 : tensor<13x21x3xi32>
754755
}
@@ -1060,3 +1061,30 @@ func.func @test_sub_with_unequal_result_ranks(%arg0: tensor<1x21x3xf32>, %arg1:
10601061
%0 = tosa.sub %arg0, %arg1 : (tensor<1x21x3xf32>, tensor<13x21x3xf32>) -> tensor<1x13x21x3xf32>
10611062
return %0 : tensor<1x13x21x3xf32>
10621063
}
1064+
1065+
// -----
1066+
// CHECK-LABEL: test_mul_non_scalar_shift_2d
1067+
func.func @test_mul_non_scalar_shift_2d(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x1x3xf32>) -> tensor<13x21x3xf32> {
1068+
%shift = "tosa.const"() <{value = dense<0> : tensor<1x1xi8>}> : () -> tensor<1x1xi8>
1069+
// expected-error@+1 {{'tosa.mul' op operand #2 must be tosa-conformant scalar tensor of 8-bit signless integer values, but got 'tensor<1x1xi8>'}}
1070+
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x3xf32>, tensor<13x1x3xf32>, tensor<1x1xi8>) -> tensor<13x21x3xf32>
1071+
return %0 : tensor<13x21x3xf32>
1072+
}
1073+
1074+
// -----
1075+
// CHECK-LABEL: test_mul_non_scalar_shift_1d
1076+
func.func @test_mul_non_scalar_shift_1d(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x1x3xf32>) -> tensor<13x21x3xf32> {
1077+
%shift = "tosa.const"() <{value = dense<0> : tensor<2xi8>}> : () -> tensor<2xi8>
1078+
// expected-error@+1 {{'tosa.mul' op operand #2 must be tosa-conformant scalar tensor of 8-bit signless integer values, but got 'tensor<2xi8>'}}
1079+
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x3xf32>, tensor<13x1x3xf32>, tensor<2xi8>) -> tensor<13x21x3xf32>
1080+
return %0 : tensor<13x21x3xf32>
1081+
}
1082+
1083+
// -----
1084+
// CHECK-LABEL: test_mul_non_broadcast
1085+
func.func @test_mul_non_broadcast(%arg0: tensor<13x21x2xf32>, %arg1: tensor<3x1x3xf32>) -> tensor<13x21x3xf32> {
1086+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
1087+
// expected-error@+1 {{'tosa.mul' op operands don't have broadcast-compatible shapes}}
1088+
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x2xf32>, tensor<3x1x3xf32>, tensor<1xi8>) -> tensor<13x21x3xf32>
1089+
return %0 : tensor<13x21x3xf32>
1090+
}

mlir/test/Dialect/Tosa/ops.mlir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ func.func @test_mul_scalar_with_unranked_output(%arg0: tensor<f32>, %arg1: tenso
339339
// -----
340340
// CHECK-LABEL: mul
341341
func.func @test_mul(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x1x3xf32>) -> tensor<13x21x3xf32> {
342-
%0 = tosa.mul %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<13x1x3xf32>) -> tensor<13x21x3xf32>
342+
%shift = "tosa.const"() <{value = dense<0> : tensor<1xi8>}> : () -> tensor<1xi8>
343+
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x3xf32>, tensor<13x1x3xf32>, tensor<1xi8>) -> tensor<13x21x3xf32>
343344
return %0 : tensor<13x21x3xf32>
344345
}
345346

0 commit comments

Comments
 (0)