This is an archive of the discontinued LLVM Phabricator instance.

Add FP8 E4M3 support to APFloat.
ClosedPublic

Authored by reedwm on Nov 9 2022, 8:28 PM.

Details

Summary

NVIDIA, ARM, and Intel recently introduced two new FP8 formats, as described in the paper: https://arxiv.org/abs/2209.05433. The first of the two FP8 dtypes, E5M2, was added in https://reviews.llvm.org/D133823. This change adds the second of the two: E4M3.

There is an RFC for adding the FP8 dtypes here: https://discourse.llvm.org/t/rfc-add-apfloat-and-mlir-type-support-for-fp8-e5m2/65279. I spoke with the RFC's author, Stella, and she gave me the go ahead to implement the E4M3 type. The name of the E4M3 type in APFloat is Float8E4M3FN, as discussed in the RFC. The "FN" means only Finite and NaN values are supported.

Unlike E5M2, E4M3 has different behavior from IEEE types in regards to Inf and NaN values. There are no Inf values, and NaN is represented when the exponent and mantissa bits are all 1s. To represent these differences in APFloat, I added an enum field, fltNonfiniteBehavior, to the fltSemantics struct. The possible enum values are IEEE754 and NanOnly. Only Float8E4M3FN has the NanOnly behavior.

After this change is submitted, I plan on adding the Float8E4M3FN type to MLIR, in the same way as E5M2 was added in https://reviews.llvm.org/D133823.

Diff Detail

Event Timeline

reedwm created this revision.Nov 9 2022, 8:28 PM
reedwm requested review of this revision.Nov 9 2022, 8:28 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptNov 9 2022, 8:28 PM
This revision is now accepted and ready to land.Nov 15 2022, 10:53 AM
stellaraccident accepted this revision.Nov 15 2022, 11:21 AM

Thanks for this. Patch lgtm. Has a couple of format issues but probably ok as-is (this file is very inconsistently formatted and it looks like you overrode clang-format a bit for consistency with adjacent code).

This revision was landed with ongoing or failed builds.Nov 15 2022, 11:27 AM
Closed by commit rG88eb3c62f25d: Add FP8 E4M3 support to APFloat. (authored by reedwm, committed by bkramer). · Explain Why
This revision was automatically updated to reflect the committed changes.

Thanks for this. Patch lgtm. Has a couple of format issues but probably ok as-is (this file is very inconsistently formatted and it looks like you overrode clang-format a bit for consistency with adjacent code).

Yeah, I ran clang-format-diff.py, but reverted some changes when inconsistent with the rest of the file. In particular, clang-format-diff.py would unindent parts of structs within a namespace (since clang-format wants no indentation in a namespace but the file uses indentation), and in the test file I ensured added/modified lines in a test function were consistent with the unmodified lines in the function.