Skip to content

Commit 7895bd3

Browse files
authored
[MLIR] Add f4E2M1FN type (#108877)
This PR adds `f4E2M1FN` type to mlir. `f4E2M1FN` type is proposed in [OpenCompute MX Specification](https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf). It defines a 4-bit floating point number with bit layout S1E2M1. Unlike IEEE-754 types, there are no infinity or NaN values. ```c f4E2M1FN - 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.0 - Max normal number: S.11.1 = ±2^(2) x (1 + 0.5) = ±6.0 - Min normal number: S.01.0 = ±2^(0) = ±1.0 - Min subnormal number: S.00.1 = ±2^(0) x 0.5 = ±0.5 ``` Related PRs: - [PR-95392](llvm/llvm-project#95392) [APFloat] Add APFloat support for FP4 data type - [PR-105573](llvm/llvm-project#105573) [MLIR] Add f6E3M2FN type - was used as a template for this PR - [PR-107999](llvm/llvm-project#107999) [MLIR] Add f6E2M3FN type
1 parent c5566c7 commit 7895bd3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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 - Float4E2M1FNType.
128+
class PyFloat4E2M1FNType
129+
: public PyConcreteType<PyFloat4E2M1FNType, PyFloatType> {
130+
public:
131+
static constexpr IsAFunctionTy isaFunction = mlirTypeIsAFloat4E2M1FN;
132+
static constexpr GetTypeIDFunctionTy getTypeIdFunction =
133+
mlirFloat4E2M1FNTypeGetTypeID;
134+
static constexpr const char *pyClassName = "Float4E2M1FNType";
135+
using PyConcreteType::PyConcreteType;
136+
137+
static void bindDerived(ClassTy &c) {
138+
c.def_static(
139+
"get",
140+
[](DefaultingPyMlirContext context) {
141+
MlirType t = mlirFloat4E2M1FNTypeGet(context->get());
142+
return PyFloat4E2M1FNType(context->getRef(), t);
143+
},
144+
py::arg("context") = py::none(), "Create a float4_e2m1fn type.");
145+
}
146+
};
147+
127148
/// Floating Point Type subclass - Float6E2M3FNType.
128149
class PyFloat6E2M3FNType
129150
: public PyConcreteType<PyFloat6E2M3FNType, PyFloatType> {
@@ -922,6 +943,7 @@ void mlir::python::populateIRTypes(py::module &m) {
922943
PyIntegerType::bind(m);
923944
PyFloatType::bind(m);
924945
PyIndexType::bind(m);
946+
PyFloat4E2M1FNType::bind(m);
925947
PyFloat6E2M3FNType::bind(m);
926948
PyFloat6E3M2FNType::bind(m);
927949
PyFloat8E4M3FNType::bind(m);

0 commit comments

Comments
 (0)