Skip to content

Commit b39ab7a

Browse files
authored
[mlir][tosa] Add error_if checks to clamp op verifier (#134224)
Specifically it introduces checks for: - ERROR_IF(max_val < min_val) - ERROR_IF(isNaN(min_val) || isNaN(max_val)) Signed-off-by: Luke Hutton <[email protected]>
1 parent 1cec5ff commit b39ab7a

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,13 @@ LogicalResult tosa::ClampOp::verify() {
653653
(intMaxValAttr.getType() != inputETy))
654654
return emitOpError("min/max attributes types are incompatible with "
655655
"input/output element types.");
656+
657+
const bool isUnsigned = cast<IntegerType>(inputETy).isUnsigned();
658+
const APInt minVal = intMinValAttr.getValue();
659+
const APInt maxVal = intMaxValAttr.getValue();
660+
if (isUnsigned ? maxVal.ult(minVal) : maxVal.slt(minVal))
661+
return emitOpError("expected min_val <= max_val, got min_val=")
662+
<< minValAttr << ", max_val=" << maxValAttr;
656663
} else {
657664
// otherwise, input datatype is float, check that the min_val/max_val
658665
// attributes share the same type and that their type is the same as the
@@ -664,6 +671,16 @@ LogicalResult tosa::ClampOp::verify() {
664671
(floatMaxValAttr.getType() != inputETy))
665672
return emitOpError("min/max attributes types are incompatible with "
666673
"input/output element types.");
674+
675+
const APFloat minVal = floatMinValAttr.getValue();
676+
const APFloat maxVal = floatMaxValAttr.getValue();
677+
if (minVal.isNaN() || maxVal.isNaN())
678+
return emitOpError("min/max attributes should not be 'NaN', got min_val=")
679+
<< minValAttr << ", max_val=" << maxValAttr;
680+
681+
if (maxVal < minVal)
682+
return emitOpError("expected min_val <= max_val, got min_val=")
683+
<< minValAttr << ", max_val=" << maxValAttr;
667684
}
668685

669686
return success();

mlir/test/Dialect/Tosa/invalid.mlir

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,3 +2084,39 @@ func.func @test_mul_out_i16(%arg0: tensor<13x21x3xi8>, %arg1: tensor<13x1x3xi8>,
20842084
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x3xi8>, tensor<13x1x3xi8>, tensor<1xi8>) -> tensor<13x21x3xi16>
20852085
return %0 : tensor<13x21x3xi16>
20862086
}
2087+
2088+
// -----
2089+
2090+
// CHECK-LABEL: test_clamp_nan_min_val
2091+
func.func @test_clamp_nan_min_val(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
2092+
// expected-error@+1 {{'tosa.clamp' op min/max attributes should not be 'NaN', got min_val=0xFFFFFFFF : f32, max_val=1.000000e+00 : f32}}
2093+
%0 = tosa.clamp %arg0 {min_val = 0xFFFFFFFF : f32, max_val = 1.0: f32} : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
2094+
return %0 : tensor<13x21x3xf32>
2095+
}
2096+
2097+
// -----
2098+
2099+
// CHECK-LABEL: test_clamp_nan_max_val
2100+
func.func @test_clamp_nan_max_val(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
2101+
// expected-error@+1 {{'tosa.clamp' op min/max attributes should not be 'NaN', got min_val=2.300000e+00 : f32, max_val=0x7FFFFFFF : f32}}
2102+
%0 = tosa.clamp %arg0 {min_val = 2.3 : f32, max_val = 0x7FFFFFFF: f32} : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
2103+
return %0 : tensor<13x21x3xf32>
2104+
}
2105+
2106+
// -----
2107+
2108+
// CHECK-LABEL: test_clamp_min_larger_than_max_int8
2109+
func.func @test_clamp_min_larger_than_max_int8(%arg0: tensor<13x21x3xi8>) -> tensor<13x21x3xi8> {
2110+
// expected-error@+1 {{'tosa.clamp' op expected min_val <= max_val, got min_val=127 : i8, max_val=-128 : i8}}
2111+
%0 = tosa.clamp %arg0 {min_val = 127 : i8, max_val = -128: i8} : (tensor<13x21x3xi8>) -> tensor<13x21x3xi8>
2112+
return %0 : tensor<13x21x3xi8>
2113+
}
2114+
2115+
// -----
2116+
2117+
// CHECK-LABEL: test_clamp_min_larger_than_max_fp32
2118+
func.func @test_clamp_min_larger_than_max_fp32(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
2119+
// expected-error@+1 {{'tosa.clamp' op expected min_val <= max_val, got min_val=2.000000e+00 : f32, max_val=-1.100000e+00 : f32}}
2120+
%0 = tosa.clamp %arg0 {min_val = 2.0 : f32, max_val = -1.1: f32} : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
2121+
return %0 : tensor<13x21x3xf32>
2122+
}

0 commit comments

Comments
 (0)