Skip to content

Commit 72403f4

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 a4cc048 commit 72403f4

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

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 mlirFloat4E2M1FNTypeGetTypeID() {
89+
return wrap(Float4E2M1FNType::getTypeID());
90+
}
91+
92+
bool mlirTypeIsAFloat4E2M1FN(MlirType type) {
93+
return unwrap(type).isFloat4E2M1FN();
94+
}
95+
96+
MlirType mlirFloat4E2M1FNTypeGet(MlirContext ctx) {
97+
return wrap(FloatType::getFloat4E2M1FN(unwrap(ctx)));
98+
}
99+
88100
MlirTypeID mlirFloat6E2M3FNTypeGetTypeID() {
89101
return wrap(Float6E2M3FNType::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+
"Float4E2M1FNType",
123124
"Float6E2M3FNType",
124125
"Float6E3M2FNType",
125126
"Float8E3M4Type",
@@ -1542,6 +1543,19 @@ class FlatSymbolRefAttr(Attribute):
15421543
Returns the value of the FlatSymbolRef attribute as a string
15431544
"""
15441545

1546+
class Float4E2M1FNType(FloatType):
1547+
static_typeid: ClassVar[TypeID]
1548+
@staticmethod
1549+
def get(context: Optional[Context] = None) -> Float4E2M1FNType:
1550+
"""
1551+
Create a float4_e2m1fn type.
1552+
"""
1553+
@staticmethod
1554+
def isinstance(other: Type) -> bool: ...
1555+
def __init__(self, cast_from_type: Type) -> None: ...
1556+
@property
1557+
def typeid(self) -> TypeID: ...
1558+
15451559
class Float6E2M3FNType(FloatType):
15461560
static_typeid: ClassVar[TypeID]
15471561
@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+
Float4E2M1FNType,
1516
Float6E2M3FNType,
1617
Float6E3M2FNType,
1718
Float8E3M4Type,
@@ -76,6 +77,7 @@ def ui(width):
7677
f8E4M3FN = lambda: Float8E4M3FNType.get()
7778
f8E4M3B11FNUZ = lambda: Float8E4M3B11FNUZType.get()
7879
f8E3M4 = lambda: Float8E3M4Type.get()
80+
f4E2M1FN = lambda: Float4E2M1FNType.get()
7981
f6E2M3FN = lambda: Float6E2M3FNType.get()
8082
f6E3M2FN = lambda: Float6E3M2FNType.get()
8183

0 commit comments

Comments
 (0)