Skip to content

Commit 34a4e53

Browse files
committed
[mlir][tosa] Add error_if checks to clamp op verifier
Specifically it introduces checks for: - ERROR_IF(max_val < min_val) - ERROR_IF(isNaN(min_val) || isNaN(max_val)) Change-Id: Id3fd81868df7ce7096c219bb61f903f1105039c5 Signed-off-by: Luke Hutton <[email protected]>
1 parent 536fe74 commit 34a4e53

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
@@ -663,6 +663,13 @@ LogicalResult tosa::ClampOp::verify() {
663663
(intMaxValAttr.getType() != inputETy))
664664
return emitOpError("min/max attributes types are incompatible with "
665665
"input/output element types.");
666+
667+
const bool isUnsigned = cast<IntegerType>(inputETy).isUnsigned();
668+
const APInt minVal = intMinValAttr.getValue();
669+
const APInt maxVal = intMaxValAttr.getValue();
670+
if (isUnsigned ? maxVal.ult(minVal) : maxVal.slt(minVal))
671+
return emitOpError("expected min_val <= max_val, got min_val=")
672+
<< minValAttr << ", max_val=" << maxValAttr;
666673
} else {
667674
// otherwise, input datatype is float, check that the min_val/max_val
668675
// attributes share the same type and that their type is the same as the
@@ -674,6 +681,16 @@ LogicalResult tosa::ClampOp::verify() {
674681
(floatMaxValAttr.getType() != inputETy))
675682
return emitOpError("min/max attributes types are incompatible with "
676683
"input/output element types.");
684+
685+
const APFloat minVal = floatMinValAttr.getValue();
686+
const APFloat maxVal = floatMaxValAttr.getValue();
687+
if (minVal.isNaN() || maxVal.isNaN())
688+
return emitOpError("min/max attributes should not be 'NaN', got min_val=")
689+
<< minValAttr << ", max_val=" << maxValAttr;
690+
691+
if (maxVal < minVal)
692+
return emitOpError("expected min_val <= max_val, got min_val=")
693+
<< minValAttr << ", max_val=" << maxValAttr;
677694
}
678695

679696
return success();

mlir/test/Dialect/Tosa/invalid.mlir

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,3 +1939,39 @@ func.func @test_mul_out_i16(%arg0: tensor<13x21x3xi8>, %arg1: tensor<13x1x3xi8>,
19391939
%0 = tosa.mul %arg0, %arg1, %shift : (tensor<13x21x3xi8>, tensor<13x1x3xi8>, tensor<1xi8>) -> tensor<13x21x3xi16>
19401940
return %0 : tensor<13x21x3xi16>
19411941
}
1942+
1943+
// -----
1944+
1945+
// CHECK-LABEL: test_clamp_nan_min_val
1946+
func.func @test_clamp_nan_min_val(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
1947+
// expected-error@+1 {{'tosa.clamp' op min/max attributes should not be 'NaN', got min_val=0xFFFFFFFF : f32, max_val=1.000000e+00 : f32}}
1948+
%0 = tosa.clamp %arg0 {min_val = 0xFFFFFFFF : f32, max_val = 1.0: f32} : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
1949+
return %0 : tensor<13x21x3xf32>
1950+
}
1951+
1952+
// -----
1953+
1954+
// CHECK-LABEL: test_clamp_nan_max_val
1955+
func.func @test_clamp_nan_max_val(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
1956+
// expected-error@+1 {{'tosa.clamp' op min/max attributes should not be 'NaN', got min_val=2.300000e+00 : f32, max_val=0x7FFFFFFF : f32}}
1957+
%0 = tosa.clamp %arg0 {min_val = 2.3 : f32, max_val = 0x7FFFFFFF: f32} : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
1958+
return %0 : tensor<13x21x3xf32>
1959+
}
1960+
1961+
// -----
1962+
1963+
// CHECK-LABEL: test_clamp_min_larger_than_max_int8
1964+
func.func @test_clamp_min_larger_than_max_int8(%arg0: tensor<13x21x3xi8>) -> tensor<13x21x3xi8> {
1965+
// expected-error@+1 {{'tosa.clamp' op expected min_val <= max_val, got min_val=127 : i8, max_val=-128 : i8}}
1966+
%0 = tosa.clamp %arg0 {min_val = 127 : i8, max_val = -128: i8} : (tensor<13x21x3xi8>) -> tensor<13x21x3xi8>
1967+
return %0 : tensor<13x21x3xi8>
1968+
}
1969+
1970+
// -----
1971+
1972+
// CHECK-LABEL: test_clamp_min_larger_than_max_fp32
1973+
func.func @test_clamp_min_larger_than_max_fp32(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
1974+
// expected-error@+1 {{'tosa.clamp' op expected min_val <= max_val, got min_val=2.000000e+00 : f32, max_val=-1.100000e+00 : f32}}
1975+
%0 = tosa.clamp %arg0 {min_val = 2.0 : f32, max_val = -1.1: f32} : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
1976+
return %0 : tensor<13x21x3xf32>
1977+
}

0 commit comments

Comments
 (0)