From 312620a81ecb775e5b86d8c7f36c6daa4835d2c7 Mon Sep 17 00:00:00 2001 From: youzi27 Date: Thu, 19 Nov 2026 05:20:30 +0000 Subject: [PATCH] sw/common: encode fp8 E4M3 values as OCP E4M3 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. --- sw/common/softfloat_ext.cpp | 22 ++++++- tests/unittest/Makefile | 2 +- tests/unittest/fp8_cvt/Makefile | 13 ++++ tests/unittest/fp8_cvt/main.cpp | 107 ++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 tests/unittest/fp8_cvt/Makefile create mode 100644 tests/unittest/fp8_cvt/main.cpp diff --git a/sw/common/softfloat_ext.cpp b/sw/common/softfloat_ext.cpp index fdb002def0..2e02d45ff4 100644 --- a/sw/common/softfloat_ext.cpp +++ b/sw/common/softfloat_ext.cpp @@ -493,7 +493,10 @@ uint32_t cvt_f32_to_custom(float value, uint32_t exp_bits, uint32_t sig_bits, const uint32_t mant_mask = sig_bits ? ((1u< emax) { + // Overflow? (for E4M3, S.1111.111 is NaN, so 480 and above overflow) + if (exponent > emax + || (is_e4m3 && exponent == emax && ((uint32_t)main & mant_mask) == mant_mask)) { flags |= (FLAG_OF | FLAG_NX); + if (is_e4m3) { + if (fflags) { + *fflags |= flags; + } + return pack_custom(exp_max, mant_mask - 1u); // max finite + } int to_inf = (frm == RNE || frm == RMM) ? 1 : (frm == RTZ) ? 0 : diff --git a/tests/unittest/Makefile b/tests/unittest/Makefile index 5ebe4bfd2c..433a969c06 100644 --- a/tests/unittest/Makefile +++ b/tests/unittest/Makefile @@ -1,7 +1,7 @@ ROOT_DIR := $(realpath ../..) include $(ROOT_DIR)/config.mk -PROJECTS := vx_malloc fpcvt linked_list dram_sim dram_bank_model dxa_ooo rvc_decompress gfx_binsort gfx_tex_sw gfx_rast_sw gfx_msaa gfx_om_mrt +PROJECTS := vx_malloc fpcvt fp8_cvt linked_list dram_sim dram_bank_model dxa_ooo rvc_decompress gfx_binsort gfx_tex_sw gfx_rast_sw gfx_msaa gfx_om_mrt all: $(foreach p,$(PROJECTS),$(MAKE) -C $(p) &&) true diff --git a/tests/unittest/fp8_cvt/Makefile b/tests/unittest/fp8_cvt/Makefile new file mode 100644 index 0000000000..adbf9aac66 --- /dev/null +++ b/tests/unittest/fp8_cvt/Makefile @@ -0,0 +1,13 @@ +ROOT_DIR := $(realpath ../../..) +include $(ROOT_DIR)/config.mk + +PROJECT := fp8_cvt + +SRC_DIR := $(VORTEX_HOME)/tests/unittest/$(PROJECT) + +SRCS := $(SRC_DIR)/main.cpp $(SW_COMMON_DIR)/rvfloats.cpp $(SW_COMMON_DIR)/softfloat_ext.cpp + +CXXFLAGS += -I$(THIRD_PARTY_DIR)/softfloat/source/include +LDFLAGS += $(THIRD_PARTY_DIR)/softfloat/build/Linux-x86_64-GCC/softfloat.a + +include ../common.mk diff --git a/tests/unittest/fp8_cvt/main.cpp b/tests/unittest/fp8_cvt/main.cpp new file mode 100644 index 0000000000..81a07d3a8d --- /dev/null +++ b/tests/unittest/fp8_cvt/main.cpp @@ -0,0 +1,107 @@ +// Copyright © 2019-2023 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Host fp8 conversions (sw/common/rvfloats): every E4M3 and E5M2 code must +// survive decode + encode, and the E4M3 encoder must follow OCP E4M3 (no +// infinity, max finite 448, NaN = S.1111.111; out-of-range values saturate). + +#include +#include +#include +#include + +#include "rvfloats.h" + +namespace { + +int failures = 0; + +uint32_t f2u(float f) { + uint32_t u; + std::memcpy(&u, &f, sizeof(u)); + return u; +} + +float u2f(uint32_t u) { + float f; + std::memcpy(&f, &u, sizeof(f)); + return f; +} + +void check_round_trip(const char *fmt, uint32_t (*dec)(uint8_t, uint32_t, uint32_t *), + uint8_t (*enc)(uint32_t, uint32_t, uint32_t *)) { + for (int c = 0; c < 256; ++c) { + uint32_t f = dec(uint8_t(c), 0, nullptr); + uint8_t back = enc(f, 0, nullptr); + if (std::isnan(u2f(f))) { + if (!std::isnan(u2f(dec(back, 0, nullptr)))) { + printf("FAILED: %s 0x%02x (NaN) encoded back as 0x%02x\n", fmt, c, back); + ++failures; + } + } else if (back != c) { + printf("FAILED: %s 0x%02x (%g) encoded back as 0x%02x\n", fmt, c, u2f(f), back); + ++failures; + } + } +} + +void expect_e4m3(float x, uint8_t expect) { + uint8_t c = rv_ftoe4m3_s(f2u(x), 0, nullptr); + if (c != expect) { + printf("FAILED: e4m3(%g) = 0x%02x, expected 0x%02x\n", x, c, expect); + ++failures; + } +} + +void expect_mxfp8(float x, uint8_t expect) { + uint8_t c = rv_ftomxfp8_s(f2u(x), 127, 0, nullptr); + if (c != expect) { + printf("FAILED: mxfp8(%g, scale 2^0) = 0x%02x, expected 0x%02x\n", x, c, expect); + ++failures; + } +} + +} // namespace + +int main() { + check_round_trip("e4m3", rv_e4m3tof_s, rv_ftoe4m3_s); + check_round_trip("e5m2", rv_e5m2tof_s, rv_ftoe5m2_s); + + // rounding near the top of the E4M3 range (round to nearest even) + expect_e4m3(247.0f, 0x77); + expect_e4m3(248.0f, 0x78); + expect_e4m3(300.0f, 0x79); + expect_e4m3(304.0f, 0x7a); + expect_e4m3(447.0f, 0x7e); + expect_e4m3(464.0f, 0x7e); + // out of range: saturate; NaN keeps its sign + expect_e4m3(480.0f, 0x7e); + expect_e4m3(1.0e6f, 0x7e); + expect_e4m3(-1.0e6f, 0xfe); + expect_e4m3(INFINITY, 0x7e); + expect_e4m3(-INFINITY, 0xfe); + expect_e4m3(NAN, 0x7f); + expect_e4m3(-NAN, 0xff); + + // MX quantiser (E4M3 elements, scale 2^0) + expect_mxfp8(384.0f, 0x7c); + expect_mxfp8(448.0f, 0x7e); + + if (failures) { + printf("%d check(s) FAILED!\n", failures); + return 1; + } + printf("PASSED!\n"); + return 0; +}