This repository was archived by the owner on Nov 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Extending UniformQuantizedType with interface-based support for new storage types in Quant dialect #149
Draft
Roman-Pevnyi
wants to merge
1
commit into
intel:npu/release/19.x
Choose a base branch
from
Roman-Pevnyi:EISW-158454_refactor_palletization
base: npu/release/19.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Extending UniformQuantizedType with interface-based support for new storage types in Quant dialect #149
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| include "mlir/IR/AttrTypeBase.td" | ||
| include "mlir/IR/BuiltinDialect.td" | ||
| include "mlir/IR/BuiltinTypeInterfaces.td" | ||
| include "mlir/IR/QuantizationInterface.td" | ||
|
|
||
| // TODO: Currently the types defined in this file are prefixed with `Builtin_`. | ||
| // This is to differentiate the types here with the ones in OpBase.td. We should | ||
|
|
@@ -78,8 +79,8 @@ def Builtin_Complex : Builtin_Type<"Complex", "complex"> { | |
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // Base class for Builtin dialect float types. | ||
| class Builtin_FloatType<string name, string mnemonic> | ||
| : Builtin_Type<name, mnemonic, /*traits=*/[], "::mlir::FloatType"> { | ||
| class Builtin_FloatType<string name, string mnemonic, list<Trait> traits = []> | ||
| : Builtin_Type<name, mnemonic, traits, "::mlir::FloatType"> { | ||
| let extraClassDeclaration = [{ | ||
| static }] # name # [{Type get(MLIRContext *context); | ||
| }]; | ||
|
|
@@ -88,7 +89,8 @@ class Builtin_FloatType<string name, string mnemonic> | |
| //===----------------------------------------------------------------------===// | ||
| // Float8E5M2Type | ||
|
|
||
| def Builtin_Float8E5M2 : Builtin_FloatType<"Float8E5M2", "f8E5M2"> { | ||
| def Builtin_Float8E5M2 : Builtin_FloatType<"Float8E5M2", "f8E5M2", | ||
| [QuantizationInterface]> { | ||
| let summary = "8-bit floating point with 2 bit mantissa"; | ||
| let description = [{ | ||
| An 8-bit floating point type with 1 sign bit, 5 bits exponent and 2 bits | ||
|
|
@@ -104,6 +106,23 @@ def Builtin_Float8E5M2 : Builtin_FloatType<"Float8E5M2", "f8E5M2"> { | |
|
|
||
| Described in: https://arxiv.org/abs/2209.05433 | ||
| }]; | ||
|
|
||
| let extraClassDeclaration = [{ | ||
| static Float8E5M2Type get(MLIRContext *context); | ||
|
|
||
| /// QuantizationInterface method implementations | ||
| bool isStorageSigned() const { return true; } | ||
| unsigned getStorageWidth() const { return 8; } | ||
| int64_t getDefaultMaximum([[maybe_unused]] bool isSigned, [[maybe_unused]] unsigned integralWidth) const { | ||
| return 448; | ||
| } | ||
| int64_t getDefaultMinimum(bool isSigned, unsigned integralWidth) const { | ||
| return -getDefaultMaximum(isSigned, integralWidth); | ||
| } | ||
| std::string printStorageType([[maybe_unused]] bool isSigned, [[maybe_unused]] unsigned storageWidth) const { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure we should have print method here. Isn't there more canonical way to stringify type name? |
||
| return "f8E5M2"; | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
|
|
@@ -128,7 +147,8 @@ def Builtin_Float8E4M3 : Builtin_FloatType<"Float8E4M3", "f8E4M3"> { | |
| //===----------------------------------------------------------------------===// | ||
| // Float8E4M3FNType | ||
|
|
||
| def Builtin_Float8E4M3FN : Builtin_FloatType<"Float8E4M3FN", "f8E4M3FN"> { | ||
| def Builtin_Float8E4M3FN : Builtin_FloatType<"Float8E4M3FN", "f8E4M3FN", | ||
| [QuantizationInterface]> { | ||
| let summary = "8-bit floating point with 3 bit mantissa"; | ||
| let description = [{ | ||
| An 8-bit floating point type with 1 sign bit, 4 bits exponent and 3 bits | ||
|
|
@@ -145,6 +165,23 @@ def Builtin_Float8E4M3FN : Builtin_FloatType<"Float8E4M3FN", "f8E4M3FN"> { | |
|
|
||
| Described in: https://arxiv.org/abs/2209.05433 | ||
| }]; | ||
|
|
||
| let extraClassDeclaration = [{ | ||
| static Float8E4M3FNType get(MLIRContext *context); | ||
|
|
||
| /// QuantizationInterface method implementations | ||
| bool isStorageSigned() const { return true; } | ||
| unsigned getStorageWidth() const { return 8; } | ||
| int64_t getDefaultMaximum([[maybe_unused]] bool isSigned, [[maybe_unused]] unsigned integralWidth) const { | ||
| return 57344; | ||
| } | ||
| int64_t getDefaultMinimum(bool isSigned, unsigned integralWidth) const{ | ||
| return -getDefaultMaximum(isSigned, integralWidth); | ||
| } | ||
| std::string printStorageType([[maybe_unused]] bool isSigned, [[maybe_unused]] unsigned storageWidth) const { | ||
| return "f8E4M3FN"; | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
|
|
@@ -358,7 +395,8 @@ def Builtin_Index : Builtin_Type<"Index", "index"> { | |
| // IntegerType | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| def Builtin_Integer : Builtin_Type<"Integer", "integer"> { | ||
| def Builtin_Integer : Builtin_Type<"Integer", "integer", | ||
| [QuantizationInterface]> { | ||
| let summary = "Integer type with arbitrary precision up to a fixed limit"; | ||
| let description = [{ | ||
| Syntax: | ||
|
|
@@ -415,6 +453,25 @@ def Builtin_Integer : Builtin_Type<"Integer", "integer"> { | |
| /// Integer representation maximal bitwidth. | ||
| /// Note: This is aligned with the maximum width of llvm::IntegerType. | ||
| static constexpr unsigned kMaxWidth = (1 << 24) - 1; | ||
|
|
||
| /// QuantizationInterface method implementations | ||
| bool isStorageSigned() const { return !isUnsigned(); } | ||
| unsigned getStorageWidth() const { return getWidth(); } | ||
| int64_t getDefaultMinimum(bool isSigned, unsigned integralWidth) const { | ||
| if (isSigned) { | ||
| return llvm::minIntN(integralWidth); | ||
| } | ||
| return 0; | ||
| } | ||
| int64_t getDefaultMaximum(bool isSigned, unsigned integralWidth) const { | ||
| if (isSigned) { | ||
| return llvm::maxIntN(integralWidth); | ||
| } | ||
| return llvm::maxUIntN(integralWidth); | ||
| } | ||
| std::string printStorageType(bool isSigned, unsigned storageWidth) const { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to pass these argument from outside? |
||
| return (isSigned ? "i" : "u") + std::to_string(storageWidth); | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| //===- QuantizationInterface.h - Quantile Float Interfaces --------*- C++ | ||
| //-*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef MLIR_IR_QuantizationInterface_H | ||
| #define MLIR_IR_QuantizationInterface_H | ||
|
|
||
| #include "mlir/IR/Types.h" | ||
|
|
||
| // Forward declarations for the types we need in the implementation | ||
| namespace mlir { | ||
| class IntegerType; | ||
| class FloatType; | ||
| } // namespace mlir | ||
|
|
||
| #include "mlir/IR/QuantizationInterface.h.inc" | ||
|
|
||
| #endif // MLIR_IR_QuantizationInterface_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #ifndef MLIR_IR_QUANTIZATIONINTERFACE | ||
| #define MLIR_IR_QUANTIZATIONINTERFACE | ||
|
|
||
| include "mlir/IR/OpBase.td" | ||
|
|
||
| def QuantizationInterface : TypeInterface<"QuantizationInterface"> { | ||
| let description = [{ | ||
| Interface for types that can be used as quantile storage types. | ||
| This interface provides methods to determine storage characteristics | ||
| like width and signedness for quantization purposes. | ||
| }]; | ||
| let cppNamespace = "::mlir"; | ||
|
|
||
| let methods = [ | ||
| InterfaceMethod<[{ | ||
| Get the storage type width in bits. | ||
| Returns the number of bits used to store values of this type. | ||
| }], | ||
| "unsigned", "getStorageWidth", (ins)>, | ||
|
|
||
| InterfaceMethod<[{ | ||
| Check if the storage type is signed. | ||
| Returns true if the type represents signed values, false for unsigned. | ||
| }], | ||
| "bool", "isStorageSigned", (ins)>, | ||
|
|
||
| InterfaceMethod<[{ | ||
| Get the default minimum value for the storage type. | ||
| }], | ||
| "int64_t", "getDefaultMinimum", (ins "bool":$isSigned, "unsigned":$integralWidth)>, | ||
|
|
||
| InterfaceMethod<[{ | ||
| Get the default maximum value for the storage type. | ||
| }], | ||
| "int64_t", "getDefaultMaximum", (ins "bool":$isSigned, "unsigned":$integralWidth)>, | ||
|
|
||
| InterfaceMethod<[{ | ||
| Get the name of the storage type. | ||
| }], | ||
| "std::string", "printStorageType", (ins "bool":$isSigned, "unsigned":$storageWidth)> | ||
| ]; | ||
|
|
||
| } | ||
|
|
||
| #endif // MLIR_IR_QUANTIZATIONINTERFACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.