Skip to content

Commit d73f0c6

Browse files
ZoranZomboratsramasit
authored andcommitted
Relax negative scales conditions in Quant Dialect (#61)
* relax negative scale conditions * adapt tests now handling negative scales
1 parent cdf8e3e commit d73f0c6

File tree

5 files changed

+40
-44
lines changed

5 files changed

+40
-44
lines changed

mlir/lib/Dialect/Quant/IR/QuantTypes.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,8 @@ LogicalResult UniformQuantizedType::verifyInvariants(
330330
return emitError() << "expressed type must be floating point";
331331

332332
// Verify scale.
333-
double minScale = getMinScale(expressedType);
334-
double maxScale = getMaxScale(expressedType);
335-
if (scale < minScale || scale > maxScale)
336-
return emitError() << "scale out of expressed type range [" << minScale
337-
<< ", " << maxScale << "]";
333+
if (std::isinf(scale) || std::isnan(scale))
334+
return emitError() << "illegal scale: " << scale;
338335

339336
return success();
340337
}
@@ -398,12 +395,9 @@ LogicalResult UniformQuantizedPerAxisType::verifyInvariants(
398395
<< scales.size() << ", " << zeroPoints.size();
399396

400397
// Verify scale.
401-
double minScale = getMinScale(expressedType);
402-
double maxScale = getMaxScale(expressedType);
403398
for (double scale : scales) {
404-
if (scale < minScale || scale > maxScale)
405-
return emitError() << "scale out of expressed type range [" << minScale
406-
<< ", " << maxScale << "]";
399+
if (std::isinf(scale) || std::isnan(scale))
400+
return emitError() << "illegal scale: " << scale;
407401
}
408402

409403
// Verify quantized dimension.

mlir/test/Dialect/Quant/parse-quantile-invalid.mlir

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,6 @@ func.func @parse() -> !qalias {
142142
// expected-error@+1 {{expected non-function type}}
143143
!qalias = !quant.quantile<i8<-4:3>:f16:f33, {-1.0,1.0}:0.99872:127>
144144

145-
// -----
146-
// Illegal scale: negative
147-
// expected-error@+1 {{scale out of expressed type range}}
148-
!qalias = !quant.quantile<i8<-4:3>:f16:f32, {-1.0,1.0}:-1.0:127>
149-
150145
// -----
151146
// Illegal uniform params: missing quantized dimension
152147
// expected-error@+1 {{expected integer value}}

mlir/test/Dialect/Quant/parse-quantile.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,21 @@ func.func @parse() -> !qalias {
163163
%0 = "foo"() : () -> !qalias
164164
return %0 : !qalias
165165
}
166+
167+
// -----
168+
// Negative scale checking
169+
// CHECK: !quant.quantile<i4:f16:f32, {-1.000000e+00,-8.667000e-01,-7.333000e-01,-6.000000e-01,-4.667000e-01,-3.333000e-01,-2.000000e-01,-0.066699999999999995,0.066699999999999995,2.000000e-01,3.333000e-01,4.667000e-01,6.000000e-01,7.333000e-01,8.667000e-01,1.000000e+00}:-2.000000e+02>
170+
!qalias = !quant.quantile<i4:f16:f32, {-1.0000,-0.8667,-0.7333,-0.6000,-0.4667,-0.3333,-0.2000,-0.0667,0.0667,0.2000,0.3333,0.4667,0.6000,0.7333,0.8667,1.0000}:-2.0e+2>
171+
func.func @parse() -> !qalias {
172+
%0 = "foo"() : () -> !qalias
173+
return %0 : !qalias
174+
}
175+
176+
// -----
177+
// Per-axis negative scale checking
178+
// CHECK: !quant.quantile<i4:f16:f32:1, {-1.000000e+00,-8.667000e-01,-7.333000e-01,-6.000000e-01,-4.667000e-01,-3.333000e-01,-2.000000e-01,-0.066699999999999995,0.066699999999999995,2.000000e-01,3.333000e-01,4.667000e-01,6.000000e-01,7.333000e-01,8.667000e-01,1.000000e+00}:{-2.000000e+02,-9.987200e-01:120}>
179+
!qalias = !quant.quantile<i4:f16:f32:1, {-1.0000,-0.8667,-0.7333,-0.6000,-0.4667,-0.3333,-0.2000,-0.0667,0.0667,0.2000,0.3333,0.4667,0.6000,0.7333,0.8667,1.0000}:{-2.0e+2,-0.99872:120}>
180+
func.func @parse() -> !qalias {
181+
%0 = "foo"() : () -> !qalias
182+
return %0 : !qalias
183+
}

mlir/test/Dialect/Quant/parse-uniform-invalid.mlir

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@
125125
// expected-error@+1 {{expected non-function type}}
126126
!qalias = !quant.uniform<i8<-4:3>:f33, 0.99872:127>
127127

128-
// -----
129-
// Illegal scale: negative
130-
// expected-error@+1 {{scale out of expressed type range}}
131-
!qalias = !quant.uniform<i8<-4:3>:f32, -1.0:127>
132-
133128
// -----
134129
// Illegal uniform params: missing quantized dimension
135130
// expected-error@+1 {{expected integer value}}
@@ -141,27 +136,3 @@
141136
// expected-error@+1 {{expected floating point literal}}
142137
!qalias = !quant.uniform<i8<-4:3>:f32, {2.000000e+02,-19.987200e-01:1}>
143138

144-
// -----
145-
// Illegal negative axis in per-axis quantization
146-
// expected-error@+1 {{illegal quantized dimension: -1}}
147-
!qalias = !quant.uniform<i8:f32:-1, {2.0,3.0:1}>
148-
149-
// -----
150-
// Scale f16 underflow
151-
// expected-error@+1 {{scale out of expressed type range}}
152-
!qalias = !quant.uniform<i8:f16, 5.8e-8>
153-
154-
// -----
155-
// Scale f16 overflow
156-
// expected-error@+1 {{scale out of expressed type range}}
157-
!qalias = !quant.uniform<i8:f16, 6.6e4>
158-
159-
// -----
160-
// Scale f16 underflow in per-axis quantization
161-
// expected-error@+1 {{scale out of expressed type range}}
162-
!qalias = !quant.uniform<i8:f16:1, {2.0,5.8e-8}>
163-
164-
// -----
165-
// Scale f16 overflow in per-axis quantization
166-
// expected-error@+1 {{scale out of expressed type range}}
167-
!qalias = !quant.uniform<i8:f16:1, {2.0,6.6e4}>

mlir/test/Dialect/Quant/parse-uniform.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,21 @@ func.func @parse() -> !qalias {
199199
%0 = "foo"() : () -> !qalias
200200
return %0 : !qalias
201201
}
202+
203+
// -----
204+
// Negative scale checking
205+
// CHECK: !quant.uniform<i8:f32, -2.000000e+02>
206+
!qalias = !quant.uniform<i8:f32, -2.0e+2>
207+
func.func @parse() -> !qalias {
208+
%0 = "foo"() : () -> !qalias
209+
return %0 : !qalias
210+
}
211+
212+
// -----
213+
// Per axis negative scale checking
214+
// CHECK: !quant.uniform<i8:f32:1, {-2.000000e+02,-9.987200e-01:120}>
215+
!qalias = !quant.uniform<i8:f32:1, {-2.0e+2,-0.99872:120}>
216+
func.func @parse() -> !qalias {
217+
%0 = "foo"() : () -> !qalias
218+
return %0 : !qalias
219+
}

0 commit comments

Comments
 (0)