Skip to content

Commit abf34b0

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](llvm/llvm-project#94735) [APFloat] Add APFloat support for FP6 data types - [PR-97118](llvm/llvm-project#97118) [MLIR] Add f8E4M3 type - was used as a template for this PR
1 parent da59957 commit abf34b0

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 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/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);

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 mlirFloat6E3M2FNTypeGetTypeID() {
89+
return wrap(Float6E3M2FNType::getTypeID());
90+
}
91+
92+
bool mlirTypeIsAFloat6E3M2FN(MlirType type) {
93+
return unwrap(type).isFloat6E3M2FN();
94+
}
95+
96+
MlirType mlirFloat6E3M2FNTypeGet(MlirContext ctx) {
97+
return wrap(FloatType::getFloat6E3M2FN(unwrap(ctx)));
98+
}
99+
88100
MlirTypeID mlirFloat8E5M2TypeGetTypeID() {
89101
return wrap(Float8E5M2Type::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+
"Float6E3M2FNType",
123124
"Float8E3M4Type",
124125
"Float8E4M3B11FNUZType",
125126
"Float8E4M3FNType",
@@ -1539,6 +1540,19 @@ class FlatSymbolRefAttr(Attribute):
15391540
Returns the value of the FlatSymbolRef attribute as a string
15401541
"""
15411542

1543+
class Float6E3M2FNType(FloatType):
1544+
static_typeid: ClassVar[TypeID]
1545+
@staticmethod
1546+
def get(context: Optional[Context] = None) -> Float6E3M2FNType:
1547+
"""
1548+
Create a float6_e3m2fn type.
1549+
"""
1550+
@staticmethod
1551+
def isinstance(other: Type) -> bool: ...
1552+
def __init__(self, cast_from_type: Type) -> None: ...
1553+
@property
1554+
def typeid(self) -> TypeID: ...
1555+
15421556
class Float8E3M4Type(FloatType):
15431557
static_typeid: ClassVar[TypeID]
15441558
@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+
Float6E3M2FNType,
1516
Float8E3M4Type,
1617
Float8E4M3B11FNUZType,
1718
Float8E4M3FNType,
@@ -74,6 +75,7 @@ def ui(width):
7475
f8E4M3FN = lambda: Float8E4M3FNType.get()
7576
f8E4M3B11FNUZ = lambda: Float8E4M3B11FNUZType.get()
7677
f8E3M4 = lambda: Float8E3M4Type.get()
78+
f6E3M2FN = lambda: Float6E3M2FNType.get()
7779

7880
none = lambda: NoneType.get()
7981

0 commit comments

Comments
 (0)