Skip to content

sw/common: encode fp8 E4M3 values as OCP E4M3 - #494

Open
youzi27 wants to merge 1 commit into
vortexgpgpu:masterfrom
youzi27:fix/fp8-e4m3-encoding
Open

youzi27 wants to merge 1 commit into
vortexgpgpu:masterfrom
youzi27:fix/fp8-e4m3-encoding

Conversation

@youzi27

@youzi27 youzi27 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #493.

Summary

cvt_f32_to_custom now has the E4M3 case that cvt_custom_to_f32 already has: exponent 1111 holds normal values up to 448, NaN encodes as S.1111.111, and values whose rounded magnitude would exceed 448, as well as infinities, saturate to ±448 (OCP SAT; finite overflow still raises OF and NX). Other formats are unchanged. This fixes rv_ftoe4m3_s and the mxfp8 quantiser rv_ftomxfp8_s. No RTL produces E4M3, so saturation follows the OCP MX quantisation rule; NaN on overflow (OVF) would be a two-line change.

The new tests/unittest/fp8_cvt checks that all 256 E4M3 and E5M2 codes survive decode and encode, plus rounding near 448, saturation, NaN and two mxfp8 values. Subnormal results are still rounded twice (to four significant bits, then to the subnormal grid), on master too; this change does not touch that path.

Impact and evidence

Impact class: R2

Contracts touched: none

evidence status detail
functional tests (simx / rtlsim / xrt) run (simx, rtlsim) fp8_cvt: 27 failures on master, passes here; the rest of make -C tests/unittest run passes; the 37 CI cases whose data go through this encoder (fp8 sgemm_tcu, mxfp8/nvfp4 MX tests) at XLEN 32 and 64: same verdicts and the same instruction and cycle counts as master, though some mxfp8 inputs change (up to 35 of 8192 codes); a host check over all 2^32 fp32 inputs (RNE) gives the nearest code for every input of magnitude 2^-6 or more and every NaN
model_parity run model_parity-mxfp8, -mxfp8-lsu-lanes: pass, as on master
perf_gate run perf_gate-mxfp8: same counts as master; both miss the baseline with my local toolchain (v3.0.1, not the pinned v3.0.2)
Beacon logic_gate n/a host code only
synthesis (fpga_gate / asic_gate) n/a host code only
XLEN 64 build run the CI cases above
  • No test silenced (known_issue:, wider tolerance, smaller args, removed case) without a root-cause plan.
  • Golden data changed only through its update command, named in the commit message.
  • Cycle-moving RTL changes carry the matching SimX change, or the parity case that shows none moved.
  • A toolchain pin moves only after the prebuilt tag exists.

cvt_f32_to_custom handled E4M3 like an IEEE format with an infinity:
every value from 248 up became 0x78, which the decoder (and the tensor
datapaths) read as 256, and NaN became 0x7c, which decodes as 384. The
codes 0x79..0x7e (288..448) could not be produced at all. Exponent 1111
now holds normal values, values beyond 448 and infinities saturate to
+-448, and NaN encodes as S.1111.111. This covers rv_ftoe4m3_s and the
mxfp8 quantiser rv_ftomxfp8_s.

Add tests/unittest/fp8_cvt: round trip of all E4M3 and E5M2 codes plus
E4M3 rounding, saturation and NaN cases.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fp8 E4M3 encoder cannot produce 288 to 448, NaN becomes 384

1 participant