-
Notifications
You must be signed in to change notification settings - Fork 35
[EISW-191805] Enable quantization for FP4 type #175
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
Jacenty-And-Intel
wants to merge
1
commit into
intel:npu/release/20.x
Choose a base branch
from
Jacenty-And-Intel:f4e2m1_mlir
base: npu/release/20.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.
+106
−33
Open
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -67,15 +67,18 @@ QuantizedType::verifyInvariants(function_ref<InFlightDiagnostic()> emitError, | |||||
| const auto width = llvm::dyn_cast<IntegerType>(storageType).getWidth(); | ||||||
| defaultMin = QuantizedType::getDefaultMinimumForInteger(isSigned, width); | ||||||
| defaultMax = QuantizedType::getDefaultMaximumForInteger(isSigned, width); | ||||||
| } else if (storageType.isa<Float8E5M2Type>()) { | ||||||
| } else if (mlir::isa<Float8E5M2Type>(storageType)) { | ||||||
| defaultMin = QuantizedType::getDefaultMinimumForF8E5M2(); | ||||||
| defaultMax = QuantizedType::getDefaultMaximumForF8E5M2(); | ||||||
| } else if (storageType.isa<Float8E4M3FNType>()) { | ||||||
| } else if (mlir::isa<Float8E4M3FNType>(storageType)) { | ||||||
| defaultMin = QuantizedType::getDefaultMinimumForF8E4M3FN(); | ||||||
| defaultMax = QuantizedType::getDefaultMaximumForF8E4M3FN(); | ||||||
| } else if (mlir::isa<Float4E2M1FNType>(storageType)) { | ||||||
| defaultMin = QuantizedType::getDefaultMinimumForF4E2M1FN(); | ||||||
| defaultMax = QuantizedType::getDefaultMaximumForF4E2M1FN(); | ||||||
| } else { | ||||||
| return emitError() << "illegal storage type, supported types are: integral " | ||||||
| "types, Float8E4M3FNType and Float8E5M2Type "; | ||||||
| "types, Float8E4M3FNType, Float8E5M2Type and Float4E2M1FNType "; | ||||||
| } | ||||||
|
|
||||||
| // Verify storageTypeMin and storageTypeMax. | ||||||
|
|
@@ -574,19 +577,18 @@ LogicalResult QuantileQuantizedType::verifyInvariants( | |||||
| unsigned typeWidth{}; | ||||||
| if (storageType.isa<IntegerType>()) { | ||||||
| typeWidth = llvm::dyn_cast<IntegerType>(storageType).getWidth(); | ||||||
| } else if (storageType.isa<Float8E5M2Type>() || | ||||||
| storageType.isa<Float8E4M3FNType>()) { | ||||||
| // Both Float8E5M2Type and Float8E4M3FNType derive from FloatType. | ||||||
| } else if (mlir::isa<Float8E5M2Type, Float8E4M3FNType, Float4E2M1FNType>(storageType)) { | ||||||
| // Float8E5M2Type, Float8E4M3FNType and Float4E2M1FNType derive from FloatType. | ||||||
| typeWidth = llvm::dyn_cast<FloatType>(storageType).getWidth(); | ||||||
| } else { | ||||||
| return emitError() << "illegal storage type, supported types are: integral " | ||||||
| "types, Float8E4M3FNType and Float8E5M2Type "; | ||||||
| "types, Float8E4M3FNType, Float8E5M2Type and Float4E2M1FNType "; | ||||||
| } | ||||||
|
|
||||||
| const size_t storageTypeRange = storageTypeMax - storageTypeMin + 1; | ||||||
| const size_t typeWidthSize = 1 << typeWidth; | ||||||
| const size_t expectedSize = | ||||||
| (storageTypeRange < typeWidthSize) ? storageTypeRange : typeWidthSize; | ||||||
| (storageTypeRange < typeWidthSize) && !mlir::isa<FloatType>(storageType) ? storageTypeRange : typeWidthSize; | ||||||
|
|
||||||
| const auto quantileArraySize = quantiles.size(); | ||||||
| if (quantileArraySize != expectedSize) { | ||||||
|
|
@@ -660,19 +662,18 @@ LogicalResult QuantileQuantizedPerAxisType::verifyInvariants( | |||||
| unsigned typeWidth{}; | ||||||
| if (storageType.isa<IntegerType>()) { | ||||||
| typeWidth = llvm::dyn_cast<IntegerType>(storageType).getWidth(); | ||||||
| } else if (storageType.isa<Float8E5M2Type>() || | ||||||
| storageType.isa<Float8E4M3FNType>()) { | ||||||
| // Both Float8E5M2Type and Float8E4M3FNType derive from FloatType. | ||||||
| } else if (mlir::isa<Float8E5M2Type, Float8E4M3FNType, Float4E2M1FNType>(storageType)) { | ||||||
|
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. nitpick:
Suggested change
this seems sufficient here. |
||||||
| // Float8E5M2Type, Float8E4M3FNType and Float4E2M1FNType derive from FloatType. | ||||||
| typeWidth = llvm::dyn_cast<FloatType>(storageType).getWidth(); | ||||||
| } else { | ||||||
| return emitError() << "illegal storage type, supported types are: integral " | ||||||
| "types, Float8E4M3FNType and Float8E5M2Type "; | ||||||
| "types, Float8E4M3FNType, Float8E5M2Type and Float4E2M1FNType "; | ||||||
| } | ||||||
|
|
||||||
| const size_t storageTypeRange = storageTypeMax - storageTypeMin + 1; | ||||||
| const size_t typeWidthSize = 1 << typeWidth; | ||||||
| const size_t expectedSize = | ||||||
| (storageTypeRange < typeWidthSize) ? storageTypeRange : typeWidthSize; | ||||||
| (storageTypeRange < typeWidthSize) && !mlir::isa<FloatType>(storageType) ? storageTypeRange : typeWidthSize; | ||||||
|
|
||||||
| const auto quantileArraySize = quantiles.size(); | ||||||
| if (quantileArraySize != expectedSize) { | ||||||
|
|
||||||
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 |
|---|---|---|
|
|
@@ -35,9 +35,8 @@ static Type parseStorageType(DialectAsmParser &parser, bool &isSigned) { | |
| if (auto intType = llvm::dyn_cast<IntegerType>(type)) { | ||
| isSigned = !intType.isUnsigned(); | ||
| storageTypeWidth = intType.getWidth(); | ||
| } else if (llvm::dyn_cast<Float8E5M2Type>(type) || | ||
| llvm::dyn_cast<Float8E4M3FNType>(type)) { | ||
| storageTypeWidth = 8; | ||
| } else if (mlir::isa<Float8E5M2Type, Float8E4M3FNType, Float4E2M1FNType>(type)) { | ||
|
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. ditto. just cast to FloatType? |
||
| storageTypeWidth = llvm::dyn_cast<FloatType>(type).getWidth(); | ||
| isSigned = true; | ||
| } else { | ||
| parser.emitError(typeLoc, "illegal quantized storage type alias"); | ||
|
|
@@ -132,12 +131,15 @@ static ParseResult parseStorageRange(DialectAsmParser &parser, Type storageType, | |
| const auto width = llvm::dyn_cast<IntegerType>(storageType).getWidth(); | ||
| defaultMin = QuantizedType::getDefaultMinimumForInteger(isSigned, width); | ||
| defaultMax = QuantizedType::getDefaultMaximumForInteger(isSigned, width); | ||
| } else if (storageType.isa<Float8E5M2Type>()) { | ||
| } else if (mlir::isa<Float8E5M2Type>(storageType)) { | ||
| defaultMin = QuantizedType::getDefaultMinimumForF8E5M2(); | ||
| defaultMax = QuantizedType::getDefaultMaximumForF8E5M2(); | ||
| } else if (storageType.isa<Float8E4M3FNType>()) { | ||
| } else if (mlir::isa<Float8E4M3FNType>(storageType)) { | ||
| defaultMin = QuantizedType::getDefaultMinimumForF8E4M3FN(); | ||
| defaultMax = QuantizedType::getDefaultMaximumForF8E4M3FN(); | ||
| } else if (mlir::isa<Float4E2M1FNType>(storageType)) { | ||
| defaultMin = QuantizedType::getDefaultMinimumForF4E2M1FN(); | ||
| defaultMax = QuantizedType::getDefaultMaximumForF4E2M1FN(); | ||
| } else { | ||
| defaultMin = std::numeric_limits<int64_t>::max(); | ||
| defaultMax = std::numeric_limits<int64_t>::min(); | ||
|
|
@@ -150,7 +152,7 @@ static ParseResult parseStorageRange(DialectAsmParser &parser, Type storageType, | |
| } | ||
|
|
||
| // Explicit storage min and storage max. | ||
| // F8 min and max values are integers, so parseInteger() is used. | ||
| // F8 and F4 min and max values are integers, so parseInteger() is used. | ||
| SMLoc minLoc = parser.getCurrentLocation(), maxLoc; | ||
| if (parser.parseInteger(storageTypeMin) || parser.parseColon() || | ||
| parser.getCurrentLocation(&maxLoc) || | ||
|
|
@@ -382,7 +384,7 @@ parseQuantParamListUntilRBrace(DialectAsmParser &parser, Type expressedType, | |
| /// block-size-info `,` scale-zero-tensor `>` | ||
| /// storage-spec ::= storage-type (`<` storage-range `>`)? | ||
| /// storage-range ::= integer-literal `:` integer-literal | ||
| /// storage-type ::= (`i` | `u`) integer-literal | ||
| /// storage-type ::= (`i` | `u`) integer-literal | `f8E5M2` | `f8E4M3FN` | `f4E2M1FN` | ||
| /// expressed-type-spec ::= `:` `f` integer-literal | ||
| /// axis-spec ::= `:` integer-literal | ||
| /// scale-zero ::= scale (`:` zero-point)? | ||
|
|
@@ -407,9 +409,9 @@ parseQuantParamListUntilRBrace(DialectAsmParser &parser, Type expressedType, | |
| /// scale-zero-list `>` | ||
| /// storage-spec ::= storage-type (`<` storage-range `>`)? | ||
| /// storage-range ::= integer-literal `:` integer-literal | ||
| /// storage-type ::= (`i` | `u`) integer-literal | ||
| /// storage-type ::= (`i` | `u`) integer-literal | `f8E5M2` | `f8E4M3FN` | `f4E2M1FN` | ||
| /// quantile-type-spec ::= `:` ((`i` | `u` | `f`) integer-literal | `f8E5M2` | | ||
| /// `f8E4M3FN`) | ||
| /// `f8E4M3FN` | `f4E2M1FN`) | ||
| /// expressed-type-spec ::= `:` `f` integer-literal axis-spec ::= | ||
| /// `:` integer-literal quantiles-list ::= `{` quantile (`,` quantile)* `}` | ||
| /// scale-zero ::= `:` float-literal `:` integer-literal | ||
|
|
@@ -641,6 +643,8 @@ static void printStorageType(QuantizedType type, DialectAsmPrinter &out) { | |
| out << "f8E5M2"; | ||
| } else if (type.getStorageType().isa<Float8E4M3FNType>()) { | ||
| out << "f8E4M3FN"; | ||
| } else if (type.getStorageType().isa<Float4E2M1FNType>()) { | ||
| out << "f4E2M1FN"; | ||
| } else if (isSigned) { | ||
| out << "i" << storageWidth; | ||
| } else { | ||
|
|
@@ -655,7 +659,9 @@ static void printStorageType(QuantizedType type, DialectAsmPrinter &out) { | |
| ? QuantizedType::getDefaultMinimumForF8E5M2() | ||
| : type.getStorageType().isa<Float8E4M3FNType>() | ||
| ? QuantizedType::getDefaultMinimumForF8E4M3FN() | ||
| : std::numeric_limits<int64_t>::max(); | ||
| : type.getStorageType().isa<Float4E2M1FNType>() | ||
| ? QuantizedType::getDefaultMinimumForF4E2M1FN() | ||
| : std::numeric_limits<int64_t>::max(); | ||
|
|
||
| int64_t defaultMax = | ||
| type.getStorageType().isa<IntegerType>() | ||
|
|
@@ -664,7 +670,9 @@ static void printStorageType(QuantizedType type, DialectAsmPrinter &out) { | |
| ? QuantizedType::getDefaultMaximumForF8E5M2() | ||
| : type.getStorageType().isa<Float8E4M3FNType>() | ||
| ? QuantizedType::getDefaultMaximumForF8E4M3FN() | ||
| : std::numeric_limits<int64_t>::min(); | ||
| : type.getStorageType().isa<Float4E2M1FNType>() | ||
| ? QuantizedType::getDefaultMaximumForF4E2M1FN() | ||
| : std::numeric_limits<int64_t>::min(); | ||
|
|
||
| if (defaultMin != type.getStorageTypeMin() || | ||
| defaultMax != type.getStorageTypeMax()) { | ||
|
|
@@ -685,6 +693,8 @@ static void printQuantileType(Type quantileType, DialectAsmPrinter &out) { | |
| out << ":f8E5M2"; | ||
| } else if (quantileType.isa<Float8E4M3FNType>()) { | ||
| out << ":f8E4M3FN"; | ||
| } else if (quantileType.isa<Float4E2M1FNType>()) { | ||
| out << ":f4E2M1FN"; | ||
| } else { | ||
| // Float types | ||
| out << ":" << quantileType; | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks like a separate bug fix. why is it required?