Skip to content

Commit b67d8c7

Browse files
committed
[MLIR] Add f6E3M2FN type (#105573).
`f6E3M2FN` type is proposed in [OpenCompute MX Specification](https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf). It defines a 6-bit floating point number with bit layout S1E3M2. Unlike IEEE-754 types, there are no infinity or NaN values. ```c f6E3M2FN - Exponent bias: 3 - Maximum stored exponent value: 7 (binary 111) - Maximum unbiased exponent value: 7 - 3 = 4 - Minimum stored exponent value: 1 (binary 001) - Minimum unbiased exponent value: 1 − 3 = −2 - Has Positive and Negative zero - Doesn't have infinity - Doesn't have NaNs Additional details: - Zeros (+/-): S.000.00 - Max normal number: S.111.11 = ±2^(4) x (1 + 0.75) = ±28 - Min normal number: S.001.00 = ±2^(-2) = ±0.25 - Max subnormal number: S.000.11 = ±2^(-2) x 0.75 = ±0.1875 - Min subnormal number: S.000.01 = ±2^(-2) x 0.25 = ±0.0625 ``` Related PRs: - [PR-94735](#94735) [APFloat] Add APFloat support for FP6 data types - [PR-97118](#97118) [MLIR] Add f8E4M3 type - was used as a template for this PR
1 parent a105877 commit b67d8c7

File tree

25 files changed

+137
-5
lines changed

25 files changed

+137
-5
lines changed

llvm/unittests/ADT/APFloatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,8 +2084,14 @@ TEST(APFloatTest, getSmallestNormalized) {
20842084
EXPECT_FALSE(test.isDenormal());
20852085
EXPECT_TRUE(test.bitwiseIsEqual(expected));
20862086
EXPECT_TRUE(test.isSmallestNormalized());
2087+
20872088
test = APFloat::getSmallestNormalized(APFloat::Float6E3M2FN(), false);
20882089
expected = APFloat(APFloat::Float6E3M2FN(), "0x1p-2");
2090+
EXPECT_FALSE(test.isNegative());
2091+
EXPECT_TRUE(test.isFiniteNonZero());
2092+
EXPECT_FALSE(test.isDenormal());
2093+
EXPECT_TRUE(test.bitwiseIsEqual(expected));
2094+
EXPECT_TRUE(test.isSmallestNormalized());
20892095

20902096
test = APFloat::getSmallestNormalized(APFloat::Float4E2M1FN(), false);
20912097
expected = APFloat(APFloat::Float4E2M1FN(), "0x1p0");

mlir/include/mlir-c/BuiltinTypes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E3M4(MlirType type);
149149
/// context.
150150
MLIR_CAPI_EXPORTED MlirType mlirFloat8E3M4TypeGet(MlirContext ctx);
151151

152+
/// Returns the typeID of an Float6E3M2FN type.
153+
MLIR_CAPI_EXPORTED MlirTypeID mlirFloat6E3M2FNTypeGetTypeID(void);
154+
155+
/// Checks whether the given type is an f6E3M2FN type.
156+
MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat6E3M2FN(MlirType type);
157+
158+
/// Creates an f8E3M2FN type in the given context. The type is owned by the
159+
/// context.
160+
MLIR_CAPI_EXPORTED MlirType mlirFloat6E3M2FNTypeGet(MlirContext ctx);
161+
152162
/// Returns the typeID of an BFloat16 type.
153163
MLIR_CAPI_EXPORTED MlirTypeID mlirBFloat16TypeGetTypeID(void);
154164

mlir/include/mlir/IR/Builders.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Builder {
6767
FloatType getFloat8E4M3FNUZType();
6868
FloatType getFloat8E4M3B11FNUZType();
6969
FloatType getFloat8E3M4Type();
70+
FloatType getFloat6E3M2FNType();
7071
FloatType getBF16Type();
7172
FloatType getF16Type();
7273
FloatType getTF32Type();

mlir/include/mlir/IR/BuiltinTypes.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class FloatType : public Type {
6767
static FloatType getFloat8E4M3FNUZ(MLIRContext *ctx);
6868
static FloatType getFloat8E4M3B11FNUZ(MLIRContext *ctx);
6969
static FloatType getFloat8E3M4(MLIRContext *ctx);
70+
static FloatType getFloat6E3M2FN(MLIRContext *ctx);
7071

7172
/// Methods for support type inquiry through isa, cast, and dyn_cast.
7273
static bool classof(Type type);
@@ -415,9 +416,9 @@ inline bool BaseMemRefType::isValidElementType(Type type) {
415416
inline bool FloatType::classof(Type type) {
416417
return llvm::isa<Float8E5M2Type, Float8E4M3Type, Float8E4M3FNType,
417418
Float8E5M2FNUZType, Float8E4M3FNUZType,
418-
Float8E4M3B11FNUZType, Float8E3M4Type, BFloat16Type,
419-
Float16Type, FloatTF32Type, Float32Type, Float64Type,
420-
Float80Type, Float128Type>(type);
419+
Float8E4M3B11FNUZType, Float8E3M4Type, Float6E3M2FNType,
420+
BFloat16Type, Float16Type, FloatTF32Type, Float32Type,
421+
Float64Type, Float80Type, Float128Type>(type);
421422
}
422423

423424
inline FloatType FloatType::getFloat8E5M2(MLIRContext *ctx) {
@@ -448,6 +449,10 @@ inline FloatType FloatType::getFloat8E3M4(MLIRContext *ctx) {
448449
return Float8E3M4Type::get(ctx);
449450
}
450451

452+
inline FloatType FloatType::getFloat6E3M2FN(MLIRContext *ctx) {
453+
return Float6E3M2FNType::get(ctx);
454+
}
455+
451456
inline FloatType FloatType::getBF16(MLIRContext *ctx) {
452457
return BFloat16Type::get(ctx);
453458
}

mlir/include/mlir/IR/BuiltinTypes.td

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,24 @@ def Builtin_Float8E3M4 : Builtin_FloatType<"Float8E3M4", "f8E3M4"> {
233233
}];
234234
}
235235

236+
//===----------------------------------------------------------------------===//
237+
// Float6E3M2FNType
238+
239+
def Builtin_Float6E3M2FN : Builtin_FloatType<"Float6E3M2FN", "f6E3M2FN"> {
240+
let summary = "6-bit floating point with 3 bits exponent and 2 bit mantissa";
241+
let description = [{
242+
An 6-bit floating point type with 1 sign bit, 3 bits exponent and 2 bits
243+
mantissa. This is not a standard type as defined by IEEE-754, but it
244+
follows similar conventions with the following characteristics:
245+
246+
* bit encoding: S1E3M2
247+
* exponent bias: 3
248+
* infinities: Not supported
249+
* NaNs: Not supported
250+
* denormals when exponent is 0
251+
}];
252+
}
253+
236254
//===----------------------------------------------------------------------===//
237255
// BFloat16Type
238256

mlir/include/mlir/IR/CommonTypeConstraints.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ def F8E5M2FNUZ : Type<CPred<"$_self.isFloat8E5M2FNUZ()">, "f8E5M2FNUZ type">,
344344
BuildableType<"$_builder.getFloat8E5M2FNUZType()">;
345345
def F8E3M4 : Type<CPred<"$_self.isFloat8E3M4()">, "f8E3M4 type">,
346346
BuildableType<"$_builder.getFloat8E3M4Type()">;
347+
def F6E3M2FN : Type<CPred<"$_self.isFloat6E3M2FN()">, "f6E3M2FN type">,
348+
BuildableType<"$_builder.getFloat6E3M2FNType()">;
347349

348350
def AnyComplex : Type<CPred<"::llvm::isa<::mlir::ComplexType>($_self)">,
349351
"complex-type", "::mlir::ComplexType">;

mlir/include/mlir/IR/Types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class Type {
132132
bool isFloat8E4M3FNUZ() const;
133133
bool isFloat8E4M3B11FNUZ() const;
134134
bool isFloat8E3M4() const;
135+
bool isFloat6E3M2FN() const;
135136
bool isBF16() const;
136137
bool isF16() const;
137138
bool isTF32() const;

mlir/lib/AsmParser/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ TOK_KEYWORD(f8E5M2FNUZ)
101101
TOK_KEYWORD(f8E4M3FNUZ)
102102
TOK_KEYWORD(f8E4M3B11FNUZ)
103103
TOK_KEYWORD(f8E3M4)
104+
TOK_KEYWORD(f6E3M2FN)
104105
TOK_KEYWORD(f128)
105106
TOK_KEYWORD(false)
106107
TOK_KEYWORD(floordiv)

mlir/lib/AsmParser/TypeParser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ OptionalParseResult Parser::parseOptionalType(Type &type) {
4646
case Token::kw_f8E4M3FNUZ:
4747
case Token::kw_f8E4M3B11FNUZ:
4848
case Token::kw_f8E3M4:
49+
case Token::kw_f6E3M2FN:
4950
case Token::kw_bf16:
5051
case Token::kw_f16:
5152
case Token::kw_tf32:
@@ -324,6 +325,9 @@ Type Parser::parseNonFunctionType() {
324325
case Token::kw_f8E3M4:
325326
consumeToken(Token::kw_f8E3M4);
326327
return builder.getFloat8E3M4Type();
328+
case Token::kw_f6E3M2FN:
329+
consumeToken(Token::kw_f6E3M2FN);
330+
return builder.getFloat6E3M2FNType();
327331
case Token::kw_bf16:
328332
consumeToken(Token::kw_bf16);
329333
return builder.getBF16Type();

mlir/lib/Bindings/Python/IRTypes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,27 @@ class PyFloat8E3M4Type : public PyConcreteType<PyFloat8E3M4Type, PyFloatType> {
266266
}
267267
};
268268

269+
/// Floating Point Type subclass - Float6E3M2FNType.
270+
class PyFloat6E3M2FNType
271+
: public PyConcreteType<PyFloat6E3M2FNType, PyFloatType> {
272+
public:
273+
static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat6E3M2FN;
274+
static constexpr GetTypeIDFunctionTy getTypeIdFunction =
275+
mlirFloat6E3M2FNTypeGetTypeID;
276+
static constexpr const char *pyClassName = "Float6E3M2FNType";
277+
using PyConcreteType::PyConcreteType;
278+
279+
static void bindDerived(ClassTy &c) {
280+
c.def_static(
281+
"get",
282+
[](DefaultingPyMlirContext context) {
283+
MlirType t = mlirFloat6E3M2FNTypeGet(context->get());
284+
return PyFloat6E3M2FNType(context->getRef(), t);
285+
},
286+
py::arg("context") = py::none(), "Create a float6_e3m2fn type.");
287+
}
288+
};
289+
269290
/// Floating Point Type subclass - BF16Type.
270291
class PyBF16Type : public PyConcreteType<PyBF16Type, PyFloatType> {
271292
public:
@@ -885,6 +906,7 @@ void mlir::python::populateIRTypes(py::module &m) {
885906
PyFloat8E4M3B11FNUZType::bind(m);
886907
PyFloat8E5M2FNUZType::bind(m);
887908
PyFloat8E3M4Type::bind(m);
909+
PyFloat6E3M2FNType::bind(m);
888910
PyBF16Type::bind(m);
889911
PyF16Type::bind(m);
890912
PyTF32Type::bind(m);

0 commit comments

Comments
 (0)