Skip to content

Commit 918222b

Browse files
authored
[MLIR] Add f6E3M2FN type (#105573)
This PR adds `f6E3M2FN` type to mlir. `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 7e07c1d commit 918222b

File tree

25 files changed

+137
-7
lines changed

25 files changed

+137
-7
lines changed

llvm/unittests/ADT/APFloatTest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,16 +2084,17 @@ 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");
2089-
2090-
test = APFloat::getSmallestNormalized(APFloat::Float4E2M1FN(), false);
2091-
expected = APFloat(APFloat::Float4E2M1FN(), "0x1p0");
20922090
EXPECT_FALSE(test.isNegative());
20932091
EXPECT_TRUE(test.isFiniteNonZero());
20942092
EXPECT_FALSE(test.isDenormal());
20952093
EXPECT_TRUE(test.bitwiseIsEqual(expected));
20962094
EXPECT_TRUE(test.isSmallestNormalized());
2095+
2096+
test = APFloat::getSmallestNormalized(APFloat::Float4E2M1FN(), false);
2097+
expected = APFloat(APFloat::Float4E2M1FN(), "0x1p0");
20972098
EXPECT_FALSE(test.isNegative());
20982099
EXPECT_TRUE(test.isFiniteNonZero());
20992100
EXPECT_FALSE(test.isDenormal());

mlir/include/mlir-c/BuiltinTypes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat(MlirType type);
7979
/// Returns the bitwidth of a floating-point type.
8080
MLIR_CAPI_EXPORTED unsigned mlirFloatTypeGetWidth(MlirType type);
8181

82+
/// Returns the typeID of an Float6E3M2FN type.
83+
MLIR_CAPI_EXPORTED MlirTypeID mlirFloat6E3M2FNTypeGetTypeID(void);
84+
85+
/// Checks whether the given type is an f6E3M2FN type.
86+
MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat6E3M2FN(MlirType type);
87+
88+
/// Creates an f6E3M2FN type in the given context. The type is owned by the
89+
/// context.
90+
MLIR_CAPI_EXPORTED MlirType mlirFloat6E3M2FNTypeGet(MlirContext ctx);
91+
8292
/// Returns the typeID of an Float8E5M2 type.
8393
MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E5M2TypeGetTypeID(void);
8494

mlir/include/mlir/IR/Builders.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Builder {
6060
Attribute metadata = Attribute());
6161

6262
// Types.
63+
FloatType getFloat6E3M2FNType();
6364
FloatType getFloat8E5M2Type();
6465
FloatType getFloat8E4M3Type();
6566
FloatType getFloat8E4M3FNType();

mlir/include/mlir/IR/BuiltinTypes.h

Lines changed: 7 additions & 2 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);
@@ -413,13 +414,17 @@ inline bool BaseMemRefType::isValidElementType(Type type) {
413414
}
414415

415416
inline bool FloatType::classof(Type type) {
416-
return llvm::isa<Float8E5M2Type, Float8E4M3Type, Float8E4M3FNType,
417-
Float8E5M2FNUZType, Float8E4M3FNUZType,
417+
return llvm::isa<Float6E3M2FNType, Float8E5M2Type, Float8E4M3Type,
418+
Float8E4M3FNType, Float8E5M2FNUZType, Float8E4M3FNUZType,
418419
Float8E4M3B11FNUZType, Float8E3M4Type, BFloat16Type,
419420
Float16Type, FloatTF32Type, Float32Type, Float64Type,
420421
Float80Type, Float128Type>(type);
421422
}
422423

424+
inline FloatType FloatType::getFloat6E3M2FN(MLIRContext *ctx) {
425+
return Float6E3M2FNType::get(ctx);
426+
}
427+
423428
inline FloatType FloatType::getFloat8E5M2(MLIRContext *ctx) {
424429
return Float8E5M2Type::get(ctx);
425430
}

mlir/include/mlir/IR/BuiltinTypes.td

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,27 @@ 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+
Open Compute Project (OCP) microscaling formats (MX) specification:
253+
https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
254+
}];
255+
}
256+
236257
//===----------------------------------------------------------------------===//
237258
// BFloat16Type
238259

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
@@ -125,6 +125,7 @@ class Type {
125125
// Convenience predicates. This is only for floating point types,
126126
// derived types should use isa/dyn_cast.
127127
bool isIndex() const;
128+
bool isFloat6E3M2FN() const;
128129
bool isFloat8E5M2() const;
129130
bool isFloat8E4M3() const;
130131
bool isFloat8E4M3FN() 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
@@ -39,6 +39,7 @@ OptionalParseResult Parser::parseOptionalType(Type &type) {
3939
case Token::kw_tuple:
4040
case Token::kw_vector:
4141
case Token::inttype:
42+
case Token::kw_f6E3M2FN:
4243
case Token::kw_f8E5M2:
4344
case Token::kw_f8E4M3:
4445
case Token::kw_f8E4M3FN:
@@ -303,6 +304,9 @@ Type Parser::parseNonFunctionType() {
303304
}
304305

305306
// float-type
307+
case Token::kw_f6E3M2FN:
308+
consumeToken(Token::kw_f6E3M2FN);
309+
return builder.getFloat6E3M2FNType();
306310
case Token::kw_f8E5M2:
307311
consumeToken(Token::kw_f8E5M2);
308312
return builder.getFloat8E5M2Type();

mlir/lib/Bindings/Python/IRTypes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ class PyFloatType : public PyConcreteType<PyFloatType> {
124124
}
125125
};
126126

127+
/// Floating Point Type subclass - Float6E3M2FNType.
128+
class PyFloat6E3M2FNType
129+
: public PyConcreteType<PyFloat6E3M2FNType, PyFloatType> {
130+
public:
131+
static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat6E3M2FN;
132+
static constexpr GetTypeIDFunctionTy getTypeIdFunction =
133+
mlirFloat6E3M2FNTypeGetTypeID;
134+
static constexpr const char *pyClassName = "Float6E3M2FNType";
135+
using PyConcreteType::PyConcreteType;
136+
137+
static void bindDerived(ClassTy &c) {
138+
c.def_static(
139+
"get",
140+
[](DefaultingPyMlirContext context) {
141+
MlirType t = mlirFloat6E3M2FNTypeGet(context->get());
142+
return PyFloat6E3M2FNType(context->getRef(), t);
143+
},
144+
py::arg("context") = py::none(), "Create a float6_e3m2fn type.");
145+
}
146+
};
147+
127148
/// Floating Point Type subclass - Float8E4M3FNType.
128149
class PyFloat8E4M3FNType
129150
: public PyConcreteType<PyFloat8E4M3FNType, PyFloatType> {
@@ -880,6 +901,7 @@ void mlir::python::populateIRTypes(py::module &m) {
880901
PyIntegerType::bind(m);
881902
PyFloatType::bind(m);
882903
PyIndexType::bind(m);
904+
PyFloat6E3M2FNType::bind(m);
883905
PyFloat8E4M3FNType::bind(m);
884906
PyFloat8E5M2Type::bind(m);
885907
PyFloat8E4M3Type::bind(m);

0 commit comments

Comments
 (0)