Skip to content

Commit e5597e3

Browse files
authored
[MLIR] Add f6E2M3FN type (#107999)
This PR adds `f6E2M3FN` type to mlir. `f6E2M3FN` 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 S1E2M3. Unlike IEEE-754 types, there are no infinity or NaN values. ```c f6E2M3FN - Exponent bias: 1 - Maximum stored exponent value: 3 (binary 11) - Maximum unbiased exponent value: 3 - 1 = 2 - Minimum stored exponent value: 1 (binary 01) - Minimum unbiased exponent value: 1 − 1 = 0 - Has Positive and Negative zero - Doesn't have infinity - Doesn't have NaNs Additional details: - Zeros (+/-): S.00.000 - Max normal number: S.11.111 = ±2^(2) x (1 + 0.875) = ±7.5 - Min normal number: S.01.000 = ±2^(0) = ±1.0 - Max subnormal number: S.00.111 = ±2^(0) x 0.875 = ±0.875 - Min subnormal number: S.00.001 = ±2^(0) x 0.125 = ±0.125 ``` Related PRs: - [PR-94735](llvm/llvm-project#94735) [APFloat] Add APFloat support for FP6 data types - [PR-105573](llvm/llvm-project#105573) [MLIR] Add f6E3M2FN type - was used as a template for this PR
1 parent f05b24f commit e5597e3

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

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 Float6E2M3FN type.
83+
MLIR_CAPI_EXPORTED MlirTypeID mlirFloat6E2M3FNTypeGetTypeID(void);
84+
85+
/// Checks whether the given type is an f6E2M3FN type.
86+
MLIR_CAPI_EXPORTED bool mlirTypeIsAFloat6E2M3FN(MlirType type);
87+
88+
/// Creates an f6E2M3FN type in the given context. The type is owned by the
89+
/// context.
90+
MLIR_CAPI_EXPORTED MlirType mlirFloat6E2M3FNTypeGet(MlirContext ctx);
91+
8292
/// Returns the typeID of an Float6E3M2FN type.
8393
MLIR_CAPI_EXPORTED MlirTypeID mlirFloat6E3M2FNTypeGetTypeID(void);
8494

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 - Float6E2M3FNType.
128+
class PyFloat6E2M3FNType
129+
: public PyConcreteType<PyFloat6E2M3FNType, PyFloatType> {
130+
public:
131+
static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat6E2M3FN;
132+
static constexpr GetTypeIDFunctionTy getTypeIdFunction =
133+
mlirFloat6E2M3FNTypeGetTypeID;
134+
static constexpr const char *pyClassName = "Float6E2M3FNType";
135+
using PyConcreteType::PyConcreteType;
136+
137+
static void bindDerived(ClassTy &c) {
138+
c.def_static(
139+
"get",
140+
[](DefaultingPyMlirContext context) {
141+
MlirType t = mlirFloat6E2M3FNTypeGet(context->get());
142+
return PyFloat6E2M3FNType(context->getRef(), t);
143+
},
144+
py::arg("context") = py::none(), "Create a float6_e2m3fn type.");
145+
}
146+
};
147+
127148
/// Floating Point Type subclass - Float6E3M2FNType.
128149
class PyFloat6E3M2FNType
129150
: public PyConcreteType<PyFloat6E3M2FNType, PyFloatType> {
@@ -901,6 +922,7 @@ void mlir::python::populateIRTypes(py::module &m) {
901922
PyIntegerType::bind(m);
902923
PyFloatType::bind(m);
903924
PyIndexType::bind(m);
925+
PyFloat6E2M3FNType::bind(m);
904926
PyFloat6E3M2FNType::bind(m);
905927
PyFloat8E4M3FNType::bind(m);
906928
PyFloat8E5M2Type::bind(m);

mlir/lib/CAPI/IR/BuiltinTypes.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ unsigned mlirFloatTypeGetWidth(MlirType type) {
8585
return llvm::cast<FloatType>(unwrap(type)).getWidth();
8686
}
8787

88+
MlirTypeID mlirFloat6E2M3FNTypeGetTypeID() {
89+
return wrap(Float6E2M3FNType::getTypeID());
90+
}
91+
92+
bool mlirTypeIsAFloat6E2M3FN(MlirType type) {
93+
return unwrap(type).isFloat6E2M3FN();
94+
}
95+
96+
MlirType mlirFloat6E2M3FNTypeGet(MlirContext ctx) {
97+
return wrap(FloatType::getFloat6E2M3FN(unwrap(ctx)));
98+
}
99+
88100
MlirTypeID mlirFloat6E3M2FNTypeGetTypeID() {
89101
return wrap(Float6E3M2FNType::getTypeID());
90102
}

mlir/python/mlir/_mlir_libs/_mlir/ir.pyi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ __all__ = [
120120
"F32Type",
121121
"F64Type",
122122
"FlatSymbolRefAttr",
123+
"Float6E2M3FNType",
123124
"Float6E3M2FNType",
124125
"Float8E3M4Type",
125126
"Float8E4M3B11FNUZType",
@@ -1541,6 +1542,19 @@ class FlatSymbolRefAttr(Attribute):
15411542
Returns the value of the FlatSymbolRef attribute as a string
15421543
"""
15431544

1545+
class Float6E2M3FNType(FloatType):
1546+
static_typeid: ClassVar[TypeID]
1547+
@staticmethod
1548+
def get(context: Optional[Context] = None) -> Float6E2M3FNType:
1549+
"""
1550+
Create a float6_e2m3fn type.
1551+
"""
1552+
@staticmethod
1553+
def isinstance(other: Type) -> bool: ...
1554+
def __init__(self, cast_from_type: Type) -> None: ...
1555+
@property
1556+
def typeid(self) -> TypeID: ...
1557+
15441558
class Float6E3M2FNType(FloatType):
15451559
static_typeid: ClassVar[TypeID]
15461560
@staticmethod

mlir/python/mlir/extras/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
F16Type,
1313
F32Type,
1414
F64Type,
15+
Float6E2M3FNType,
1516
Float6E3M2FNType,
1617
Float8E3M4Type,
1718
Float8E4M3B11FNUZType,
@@ -75,6 +76,7 @@ def ui(width):
7576
f8E4M3FN = lambda: Float8E4M3FNType.get()
7677
f8E4M3B11FNUZ = lambda: Float8E4M3B11FNUZType.get()
7778
f8E3M4 = lambda: Float8E3M4Type.get()
79+
f6E2M3FN = lambda: Float6E2M3FNType.get()
7880
f6E3M2FN = lambda: Float6E3M2FNType.get()
7981

8082
none = lambda: NoneType.get()

0 commit comments

Comments
 (0)