Skip to content

Commit 3f9caba

Browse files
authored
[MLIR] Add f8E8M0FNU type (llvm#111028)
This PR adds `f8E8M0FNU` type to MLIR. `f8E8M0FNU` type is proposed in [OpenCompute MX Specification](https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf). It defines a 8-bit floating point number with bit layout S0E8M0. Unlike IEEE-754 types, there are no infinity, denormals, zeros or negative values. ```c f8E8M0FNU - Exponent bias: 127 - Maximum stored exponent value: 254 (binary 1111'1110) - Maximum unbiased exponent value: 254 - 127 = 127 - Minimum stored exponent value: 0 (binary 0000'0000) - Minimum unbiased exponent value: 0 − 127 = -127 - Doesn't have zero - Doesn't have infinity - NaN is encoded as binary 1111'1111 Additional details: - Zeros cannot be represented - Negative values cannot be represented - Mantissa is always 1 ``` Related PRs: - [PR-107127](llvm#107127) [APFloat] Add APFloat support for E8M0 type - [PR-105573](llvm#105573) [MLIR] Add f6E3M2FN type - was used as a template for this PR - [PR-107999](llvm#107999) [MLIR] Add f6E2M3FN type - [PR-108877](llvm#108877) [MLIR] Add f4E2M1FN type
1 parent ccca3c6 commit 3f9caba

File tree

24 files changed

+140
-7
lines changed

24 files changed

+140
-7
lines changed

mlir/include/mlir-c/BuiltinTypes.h

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

182+
/// Returns the typeID of an Float8E8M0FNU type.
183+
MLIR_CAPI_EXPORTED MlirTypeID mlirFloat8E8M0FNUTypeGetTypeID(void);
184+
185+
/// Checks whether the given type is an f8E8M0FNU type.
186+
MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat8E8M0FNU(MlirType type);
187+
188+
/// Creates an f8E8M0FNU type in the given context. The type is owned by the
189+
/// context.
190+
MLIR_CAPI_EXPORTED MlirType mlirFloat8E8M0FNUTypeGet(MlirContext ctx);
191+
182192
/// Returns the typeID of an BFloat16 type.
183193
MLIR_CAPI_EXPORTED MlirTypeID mlirBFloat16TypeGetTypeID(void);
184194

mlir/include/mlir/IR/Builders.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Builder {
7070
FloatType getFloat8E4M3FNUZType();
7171
FloatType getFloat8E4M3B11FNUZType();
7272
FloatType getFloat8E3M4Type();
73+
FloatType getFloat8E8M0FNUType();
7374
FloatType getBF16Type();
7475
FloatType getF16Type();
7576
FloatType getTF32Type();

mlir/include/mlir/IR/BuiltinTypes.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class FloatType : public Type {
7070
static FloatType getFloat4E2M1FN(MLIRContext *ctx);
7171
static FloatType getFloat6E2M3FN(MLIRContext *ctx);
7272
static FloatType getFloat6E3M2FN(MLIRContext *ctx);
73+
static FloatType getFloat8E8M0FNU(MLIRContext *ctx);
7374

7475
/// Methods for support type inquiry through isa, cast, and dyn_cast.
7576
static bool classof(Type type);
@@ -416,11 +417,12 @@ inline bool BaseMemRefType::isValidElementType(Type type) {
416417
}
417418

418419
inline bool FloatType::classof(Type type) {
419-
return llvm::isa<
420-
Float4E2M1FNType, Float6E2M3FNType, Float6E3M2FNType, Float8E5M2Type,
421-
Float8E4M3Type, Float8E4M3FNType, Float8E5M2FNUZType, Float8E4M3FNUZType,
422-
Float8E4M3B11FNUZType, Float8E3M4Type, BFloat16Type, Float16Type,
423-
FloatTF32Type, Float32Type, Float64Type, Float80Type, Float128Type>(type);
420+
return llvm::isa<Float4E2M1FNType, Float6E2M3FNType, Float6E3M2FNType,
421+
Float8E5M2Type, Float8E4M3Type, Float8E4M3FNType,
422+
Float8E5M2FNUZType, Float8E4M3FNUZType,
423+
Float8E4M3B11FNUZType, Float8E3M4Type, Float8E8M0FNUType,
424+
BFloat16Type, Float16Type, FloatTF32Type, Float32Type,
425+
Float64Type, Float80Type, Float128Type>(type);
424426
}
425427

426428
inline FloatType FloatType::getFloat4E2M1FN(MLIRContext *ctx) {
@@ -463,6 +465,10 @@ inline FloatType FloatType::getFloat8E3M4(MLIRContext *ctx) {
463465
return Float8E3M4Type::get(ctx);
464466
}
465467

468+
inline FloatType FloatType::getFloat8E8M0FNU(MLIRContext *ctx) {
469+
return Float8E8M0FNUType::get(ctx);
470+
}
471+
466472
inline FloatType FloatType::getBF16(MLIRContext *ctx) {
467473
return BFloat16Type::get(ctx);
468474
}

mlir/include/mlir/IR/BuiltinTypes.td

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,29 @@ def Builtin_Float6E3M2FN : Builtin_FloatType<"Float6E3M2FN", "f6E3M2FN"> {
296296
}];
297297
}
298298

299+
//===----------------------------------------------------------------------===//
300+
// Float8E8M0FNUType
301+
302+
def Builtin_Float8E8M0FNU : Builtin_FloatType<"Float8E8M0FNU", "f8E8M0FNU"> {
303+
let summary = "8-bit floating point with 8-bit exponent, no mantissa or sign";
304+
let description = [{
305+
An 8-bit floating point type with no sign bit, 8 bits exponent and no
306+
mantissa. This is not a standard type as defined by IEEE-754; it is intended
307+
to be used for representing scaling factors, so it cannot represent zeros
308+
and negative numbers. The values it can represent are powers of two in the
309+
range [-127,127] and NaN.
310+
311+
* bit encoding: S0E8M0
312+
* exponent bias: 127
313+
* infinities: Not supported
314+
* NaNs: Supported with all bits set to 1
315+
* denormals: Not supported
316+
317+
Open Compute Project (OCP) microscaling formats (MX) specification:
318+
https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf
319+
}];
320+
}
321+
299322
//===----------------------------------------------------------------------===//
300323
// BFloat16Type
301324

mlir/include/mlir/IR/CommonTypeConstraints.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ def F6E2M3FN : Type<CPred<"$_self.isFloat6E2M3FN()">, "f6E2M3FN type">,
353353
BuildableType<"$_builder.getFloat6E2M3FNType()">;
354354
def F6E3M2FN : Type<CPred<"$_self.isFloat6E3M2FN()">, "f6E3M2FN type">,
355355
BuildableType<"$_builder.getFloat6E3M2FNType()">;
356+
def F8E8M0FNU : Type<CPred<"$_self.isFloat8E8M0FNU()">, "f8E8M0FNU type">,
357+
BuildableType<"$_builder.getFloat8E8M0FNUType()">;
356358

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

mlir/include/mlir/IR/Types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class Type {
135135
bool isFloat8E4M3FNUZ() const;
136136
bool isFloat8E4M3B11FNUZ() const;
137137
bool isFloat8E3M4() const;
138+
bool isFloat8E8M0FNU() const;
138139
bool isBF16() const;
139140
bool isF16() const;
140141
bool isTF32() const;

mlir/lib/AsmParser/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ TOK_KEYWORD(f8E3M4)
104104
TOK_KEYWORD(f4E2M1FN)
105105
TOK_KEYWORD(f6E2M3FN)
106106
TOK_KEYWORD(f6E3M2FN)
107+
TOK_KEYWORD(f8E8M0FNU)
107108
TOK_KEYWORD(f128)
108109
TOK_KEYWORD(false)
109110
TOK_KEYWORD(floordiv)

mlir/lib/AsmParser/TypeParser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ OptionalParseResult Parser::parseOptionalType(Type &type) {
4949
case Token::kw_f8E4M3FNUZ:
5050
case Token::kw_f8E4M3B11FNUZ:
5151
case Token::kw_f8E3M4:
52+
case Token::kw_f8E8M0FNU:
5253
case Token::kw_bf16:
5354
case Token::kw_f16:
5455
case Token::kw_tf32:
@@ -336,6 +337,9 @@ Type Parser::parseNonFunctionType() {
336337
case Token::kw_f8E3M4:
337338
consumeToken(Token::kw_f8E3M4);
338339
return builder.getFloat8E3M4Type();
340+
case Token::kw_f8E8M0FNU:
341+
consumeToken(Token::kw_f8E8M0FNU);
342+
return builder.getFloat8E8M0FNUType();
339343
case Token::kw_bf16:
340344
consumeToken(Token::kw_bf16);
341345
return builder.getBF16Type();

mlir/lib/Bindings/Python/IRTypes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,27 @@ class PyFloat8E3M4Type : public PyConcreteType<PyFloat8E3M4Type, PyFloatType> {
331331
}
332332
};
333333

334+
/// Floating Point Type subclass - Float8E8M0FNUType.
335+
class PyFloat8E8M0FNUType
336+
: public PyConcreteType<PyFloat8E8M0FNUType, PyFloatType> {
337+
public:
338+
static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat8E8M0FNU;
339+
static constexpr GetTypeIDFunctionTy getTypeIdFunction =
340+
mlirFloat8E8M0FNUTypeGetTypeID;
341+
static constexpr const char *pyClassName = "Float8E8M0FNUType";
342+
using PyConcreteType::PyConcreteType;
343+
344+
static void bindDerived(ClassTy &c) {
345+
c.def_static(
346+
"get",
347+
[](DefaultingPyMlirContext context) {
348+
MlirType t = mlirFloat8E8M0FNUTypeGet(context->get());
349+
return PyFloat8E8M0FNUType(context->getRef(), t);
350+
},
351+
py::arg("context") = py::none(), "Create a float8_e8m0fnu type.");
352+
}
353+
};
354+
334355
/// Floating Point Type subclass - BF16Type.
335356
class PyBF16Type : public PyConcreteType<PyBF16Type, PyFloatType> {
336357
public:
@@ -953,6 +974,7 @@ void mlir::python::populateIRTypes(py::module &m) {
953974
PyFloat8E4M3B11FNUZType::bind(m);
954975
PyFloat8E5M2FNUZType::bind(m);
955976
PyFloat8E3M4Type::bind(m);
977+
PyFloat8E8M0FNUType::bind(m);
956978
PyBF16Type::bind(m);
957979
PyF16Type::bind(m);
958980
PyTF32Type::bind(m);

mlir/lib/CAPI/IR/BuiltinTypes.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ MlirType mlirFloat8E3M4TypeGet(MlirContext ctx) {
205205
return wrap(FloatType::getFloat8E3M4(unwrap(ctx)));
206206
}
207207

208+
MlirTypeID mlirFloat8E8M0FNUTypeGetTypeID() {
209+
return wrap(Float8E8M0FNUType::getTypeID());
210+
}
211+
212+
bool mlirTypeIsAFloat8E8M0FNU(MlirType type) {
213+
return unwrap(type).isFloat8E8M0FNU();
214+
}
215+
216+
MlirType mlirFloat8E8M0FNUTypeGet(MlirContext ctx) {
217+
return wrap(FloatType::getFloat8E8M0FNU(unwrap(ctx)));
218+
}
219+
208220
MlirTypeID mlirBFloat16TypeGetTypeID() {
209221
return wrap(BFloat16Type::getTypeID());
210222
}

0 commit comments

Comments
 (0)