Skip to content

[mlir][quant] Bump up the MaxStorageBits from 32 to 64. #91706

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/Quant/QuantTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class QuantizedType : public Type {
using Type::Type;

/// The maximum number of bits supported for storage types.
static constexpr unsigned MaxStorageBits = 32;
/// NOTE: u64 storage type is not yet supported.
static constexpr unsigned MaxStorageBits = 64;

static LogicalResult verify(function_ref<InFlightDiagnostic()> emitError,
unsigned flags, Type storageType,
Expand Down
12 changes: 9 additions & 3 deletions mlir/lib/Dialect/Quant/IR/QuantTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ QuantizedType::verify(function_ref<InFlightDiagnostic()> emitError,
if (integralWidth == 0 || integralWidth > MaxStorageBits)
return emitError() << "illegal storage type size: " << integralWidth;

// Verify storageTypeMin and storageTypeMax.
bool isSigned =
(flags & QuantizationFlags::Signed) == QuantizationFlags::Signed;
// u64 is not yet supported because its full range cannot be represented
// by the type of `storageTypeMax`, making it difficult to verify the
// storage type.
if (!isSigned && integralWidth == 64)
return emitError()
<< "illegal storage type; u64 storage type is not supported";

// Verify storageTypeMin and storageTypeMax.
int64_t defaultIntegerMin =
getDefaultMinimumForInteger(isSigned, integralWidth);
int64_t defaultIntegerMax =
getDefaultMaximumForInteger(isSigned, integralWidth);
if (storageTypeMax - storageTypeMin <= 0 ||
storageTypeMin < defaultIntegerMin ||
if (storageTypeMax <= storageTypeMin || storageTypeMin < defaultIntegerMin ||
storageTypeMax > defaultIntegerMax) {
return emitError() << "illegal storage min and storage max: ("
<< storageTypeMin << ":" << storageTypeMax << ")";
Expand Down
8 changes: 4 additions & 4 deletions mlir/test/Dialect/Quant/parse-any-invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
!qalias = !quant.any<i<-4:3>:f32>

// -----
// Unrecognized storage type: storage size > 32
// expected-error@+1 {{illegal storage type size: 33}}
!qalias = !quant.any<i33:f32>
// Unrecognized storage type: storage size > 64
// expected-error@+1 {{illegal storage type size: 65}}
!qalias = !quant.any<i65:f32>

// -----
// Unrecognized storage type: storage size < 0
// Unrecognized storage type: storage size > 64
// expected-error@+1 {{illegal storage type size: 1024}}
!qalias = !quant.any<i1024<-4:3>:f32>

Expand Down
11 changes: 8 additions & 3 deletions mlir/test/Dialect/Quant/parse-uniform-invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
!qalias = !quant.uniform<i<-4:3>:f32, 0.99872:127>

// -----
// Unrecognized storage type: storage size > 32
// expected-error@+1 {{illegal storage type size: 33}}
!qalias = !quant.uniform<i33:f32, 0.99872:127>
// Unrecognized storage type: storage size > 64
// expected-error@+1 {{illegal storage type size: 65}}
!qalias = !quant.uniform<i65:f32, 0.99872:127>

// -----
// Unrecognized storage type: storage size < 0
Expand All @@ -60,6 +60,11 @@
// expected-error@+1 {{invalid integer width}}
!qalias = !quant.uniform<i123123123120<-4:3>:f32, 0.99872:127>

// -----
// Illegal storage type: u64
// expected-error@+1 {{illegal storage type; u64 storage type is not supported}}
!qalias = !quant.uniform<u64:f32, 0.99782:127>

// -----
// Illegal storage min/max: max - min < 0
// expected-error@+1 {{illegal storage min and storage max: (2:1)}}
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Dialect/Quant/parse-uniform.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ func.func @parse() -> !qalias {
return %0 : !qalias
}

// -----
// Storage type: i64
// CHECK: !quant.uniform<i64:f32, 2.000000e+02>
!qalias = !quant.uniform<i64:f32, 2.0e+2>
func.func @parse() -> !qalias {
%0 = "foo"() : () -> !qalias
return %0 : !qalias
}

// -----
// Expressed type: f32
// CHECK: !quant.uniform<u8:f32, 2.000000e+02>
Expand Down
Loading