Skip to content

Remove redundant checks related to quantized type #110604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

sdasgup3
Copy link
Contributor

@sdasgup3 sdasgup3 commented Oct 1, 2024

APFloat::getSmallest (and similarly APFloat:getLargest)

APFloat getSmallest(const fltSemantics &Sem, bool Negative = false); 

return the positive number when the default value for the second argument is used.

With that being said, the check QuantTypes.cpp#L325

 if (scale <= 0.0 || std::isinf(scale) || std::isnan(scale))
    return emitError() << "illegal scale: " << scale;

is already covered by the check which follows QuantTypes.cpp#L327

  if (scale < minScale || scale > maxScale)
    return emitError() << "scale out of expressed type range [" << minScale
                       << ", " << maxScale << "]";

given that range [positive-smallest-finite-number, positive-largest-finite-number] does not include inf and nans.

I propose to remove the redundant check. Any suggestion for improving the error message is welcome.

@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2024

@llvm/pr-subscribers-mlir

Author: Sandeep Dasgupta (sdasgup3)

Changes

APFloat::getSmallest (and similarly APFloat:getLargest)

APFloat getSmallest(const fltSemantics &amp;Sem, bool Negative = false); 

return the positive number when the default value for the second argument is used.

With that being said, the check QuantTypes.cpp#L325

 if (scale &lt;= 0.0 || std::isinf(scale) || std::isnan(scale))
    return emitError() &lt;&lt; "illegal scale: " &lt;&lt; scale;

is already covered by the check which follows QuantTypes.cpp#L327

  if (scale &lt; minScale || scale &gt; maxScale)
    return emitError() &lt;&lt; "scale out of expressed type range [" &lt;&lt; minScale
                       &lt;&lt; ", " &lt;&lt; maxScale &lt;&lt; "]";

given that range [positive-smallest-finite-number, positive-largest-finite-number] does not include inf and nans.

I propose to remove the redundant check. Any suggestion for improving the error message is welcome.


Full diff: https://github.com/llvm/llvm-project/pull/110604.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/Quant/IR/QuantTypes.cpp (-4)
  • (modified) mlir/test/Dialect/Quant/parse-uniform-invalid.mlir (+1-1)
diff --git a/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp b/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp
index ac01b37a553077..7c0d3696486515 100644
--- a/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp
+++ b/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp
@@ -322,8 +322,6 @@ LogicalResult UniformQuantizedType::verifyInvariants(
   // Verify scale.
   double minScale = getMinScale(expressedType);
   double maxScale = getMaxScale(expressedType);
-  if (scale <= 0.0 || std::isinf(scale) || std::isnan(scale))
-    return emitError() << "illegal scale: " << scale;
   if (scale < minScale || scale > maxScale)
     return emitError() << "scale out of expressed type range [" << minScale
                        << ", " << maxScale << "]";
@@ -388,8 +386,6 @@ LogicalResult UniformQuantizedPerAxisType::verifyInvariants(
   double minScale = getMinScale(expressedType);
   double maxScale = getMaxScale(expressedType);
   for (double scale : scales) {
-    if (scale <= 0.0 || std::isinf(scale) || std::isnan(scale))
-      return emitError() << "illegal scale: " << scale;
     if (scale < minScale || scale > maxScale)
       return emitError() << "scale out of expressed type range [" << minScale
                          << ", " << maxScale << "]";
diff --git a/mlir/test/Dialect/Quant/parse-uniform-invalid.mlir b/mlir/test/Dialect/Quant/parse-uniform-invalid.mlir
index 7613a344cf2b8f..4528d2826a850c 100644
--- a/mlir/test/Dialect/Quant/parse-uniform-invalid.mlir
+++ b/mlir/test/Dialect/Quant/parse-uniform-invalid.mlir
@@ -107,7 +107,7 @@
 
 // -----
 // Illegal scale: negative
-// expected-error@+1 {{illegal scale: -1.000000}}
+// expected-error@+1 {{scale out of expressed type range}}
 !qalias = !quant.uniform<i8<-4:3>:f32, -1.0:127>
 
 // -----

@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2024

@llvm/pr-subscribers-mlir-quant

Author: Sandeep Dasgupta (sdasgup3)

Changes

APFloat::getSmallest (and similarly APFloat:getLargest)

APFloat getSmallest(const fltSemantics &amp;Sem, bool Negative = false); 

return the positive number when the default value for the second argument is used.

With that being said, the check QuantTypes.cpp#L325

 if (scale &lt;= 0.0 || std::isinf(scale) || std::isnan(scale))
    return emitError() &lt;&lt; "illegal scale: " &lt;&lt; scale;

is already covered by the check which follows QuantTypes.cpp#L327

  if (scale &lt; minScale || scale &gt; maxScale)
    return emitError() &lt;&lt; "scale out of expressed type range [" &lt;&lt; minScale
                       &lt;&lt; ", " &lt;&lt; maxScale &lt;&lt; "]";

given that range [positive-smallest-finite-number, positive-largest-finite-number] does not include inf and nans.

I propose to remove the redundant check. Any suggestion for improving the error message is welcome.


Full diff: https://github.com/llvm/llvm-project/pull/110604.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/Quant/IR/QuantTypes.cpp (-4)
  • (modified) mlir/test/Dialect/Quant/parse-uniform-invalid.mlir (+1-1)
diff --git a/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp b/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp
index ac01b37a553077..7c0d3696486515 100644
--- a/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp
+++ b/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp
@@ -322,8 +322,6 @@ LogicalResult UniformQuantizedType::verifyInvariants(
   // Verify scale.
   double minScale = getMinScale(expressedType);
   double maxScale = getMaxScale(expressedType);
-  if (scale <= 0.0 || std::isinf(scale) || std::isnan(scale))
-    return emitError() << "illegal scale: " << scale;
   if (scale < minScale || scale > maxScale)
     return emitError() << "scale out of expressed type range [" << minScale
                        << ", " << maxScale << "]";
@@ -388,8 +386,6 @@ LogicalResult UniformQuantizedPerAxisType::verifyInvariants(
   double minScale = getMinScale(expressedType);
   double maxScale = getMaxScale(expressedType);
   for (double scale : scales) {
-    if (scale <= 0.0 || std::isinf(scale) || std::isnan(scale))
-      return emitError() << "illegal scale: " << scale;
     if (scale < minScale || scale > maxScale)
       return emitError() << "scale out of expressed type range [" << minScale
                          << ", " << maxScale << "]";
diff --git a/mlir/test/Dialect/Quant/parse-uniform-invalid.mlir b/mlir/test/Dialect/Quant/parse-uniform-invalid.mlir
index 7613a344cf2b8f..4528d2826a850c 100644
--- a/mlir/test/Dialect/Quant/parse-uniform-invalid.mlir
+++ b/mlir/test/Dialect/Quant/parse-uniform-invalid.mlir
@@ -107,7 +107,7 @@
 
 // -----
 // Illegal scale: negative
-// expected-error@+1 {{illegal scale: -1.000000}}
+// expected-error@+1 {{scale out of expressed type range}}
 !qalias = !quant.uniform<i8<-4:3>:f32, -1.0:127>
 
 // -----

@sdasgup3
Copy link
Contributor Author

sdasgup3 commented Oct 3, 2024

Hi Rafael (@rafaelubalmw),
Can you please take a look?

abhigunj pushed a commit to openxla/stablehlo that referenced this pull request Oct 3, 2024
The recent upstream
[change](llvm/llvm-project#100667) have
introduced quantization checks that are
already present in the StableHLO core library. This commit removes these
duplicate
checks to avoid redundancy and potential inconsistencies.


|Checks proposed to be removed| StableHLO Code | Upstream MLIR | 
|-|-|-|
| `channel-axis >= 0`|
[cs](https://github.com/openxla/stablehlo/blob/1c0547f391dff5ac71d36dc20a916260afa78c61/stablehlo/dialect/Base.cpp#L795)
|
[cs](https://github.com/llvm/llvm-project/blob/96f37ae45310885e09195be09d9c05e1c1dff86b/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp#L399)
|
| scale within smallest and largest finite numbers determined by
`expressed_type`|
[cs](https://github.com/openxla/stablehlo/blob/1c0547f391dff5ac71d36dc20a916260afa78c61/stablehlo/dialect/Base.cpp#L765)
|
[cs1](https://github.com/llvm/llvm-project/blob/96f37ae45310885e09195be09d9c05e1c1dff86b/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp#L327)
[cs2](https://github.com/llvm/llvm-project/blob/96f37ae45310885e09195be09d9c05e1c1dff86b/mlir/lib/Dialect/Quant/IR/QuantTypes.cpp#L393C9-L393C45)
|


Note that StableHLO has checks like `quantization_dimension <
rank(self)` and
`dim(self, quantization_dimension) = size(scales)` implemented at
[cs](https://github.com/openxla/stablehlo/blob/1c0547f391dff5ac71d36dc20a916260afa78c61/stablehlo/dialect/Base.cpp#L795).
In upstream MLIR similar checks
[cs](https://github.com/llvm/llvm-project/blob/96f37ae45310885e09195be09d9c05e1c1dff86b/mlir/lib/Dialect/Quant/IR/QuantOps.cpp#L51)
are encoded as part of
[dcast](https://github.com/llvm/llvm-project/blob/96f37ae45310885e09195be09d9c05e1c1dff86b/mlir/lib/Dialect/Quant/IR/QuantOps.cpp#L110)
and
[qcast](https://github.com/llvm/llvm-project/blob/96f37ae45310885e09195be09d9c05e1c1dff86b/mlir/lib/Dialect/Quant/IR/QuantOps.cpp#L139)
ops and hence cannot be claimed as duplicate.

related upstream clean-up
llvm/llvm-project#110604
@sdasgup3 sdasgup3 force-pushed the simplify-quant-type-checks branch from a5365a2 to 2461430 Compare October 4, 2024 16:08
@sdasgup3
Copy link
Contributor Author

sdasgup3 commented Oct 4, 2024

@rafaelubalmw Can you please merge this? Sorry I do not have the merge access.

@GleasonK GleasonK merged commit 90a5744 into llvm:main Oct 7, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants