Skip to content

fix(onnx-graphsurgeon): export float8_e4m3fnuz with the correct ONNX type - #4857

Open
luicarus wants to merge 1 commit into
NVIDIA:mainfrom
luicarus:fix-fnuz-dtype-export
Open

luicarus wants to merge 1 commit into
NVIDIA:mainfrom
luicarus:fix-fnuz-dtype-export

Conversation

@luicarus

Copy link
Copy Markdown

Description

dtype_to_onnx incorrectly mapped ml_dtypes.float8_e4m3fnuz to "FLOAT8E4M3FN", causing constants with float8_e4m3fnuz values to be exported with the wrong ONNX data type.

Because FLOAT8E4M3FN and FLOAT8E4M3FNUZ have the same storage width, this failed silently: the exported raw_data was unchanged, but the tensor was labeled with the wrong type.

Fixes #4856.

What changed

  • onnx_graphsurgeon/exporters/onnx_exporter.py
    • map float8_e4m3fnuz to "FLOAT8E4M3FNUZ".
  • tests/test_exporters.py
    • add regression coverage for the affected dtype and import/export round trip.
  • CHANGELOG.md
    • add a Fixed entry under v0.6.2.

Tests

I verified that the new regression tests detect the original bug by reverting only the source fix and re-running them:

Test Before fix After fix
test_should_export_constant_tensor_with_each_ml_float8_dtype[float8_e4m3fnuz-18] FAIL PASS
test_should_distinguish_e4m3fn_from_e4m3fnuz FAIL PASS
test_should_preserve_e4m3fnuz_across_import_and_export_round_trip FAIL PASS

Full tools/onnx-graphsurgeon test suite:

before: 12 failed, 279 passed, 1 skipped
after:  12 failed, 285 passed, 1 skipped

The 12 remaining failures are unchanged and unrelated to this patch. They come from tests/test_examples.py, which invokes examples through:

bash -c 'python3 example.py'

In my Windows environment, python3 is not available under that command name. No test that passed before this change fails afterward.

Notes

The regression tests assert the exported ONNX data_type explicitly rather than relying only on raw_data. Since FLOAT8E4M3FN and FLOAT8E4M3FNUZ use the same byte width, a bytes-only assertion would not detect this bug.

Relationship to #4843

#4843 fixes the same defect and is technically correct. It has been open since 2026-09-01 with no linked issue and no maintainer response, and its DCO check fails because its commit carries no Signed-off-by line, which I believe is the only thing blocking it.

I am opening this separately rather than waiting because it adds two things #4843 does not have: a CHANGELOG.md entry (following the precedent of the merged #4836) and a regression test for the import/export round trip described in #4856, which #4843's PR description mentions but does not cover. Its existing test also relies on a raw_data round-trip assertion that cannot distinguish a correct label from an incorrect one.

This commit is signed off. If the maintainers would rather land #4843 once it is signed off, that is entirely fine by me — please close this one.

…type

dtype_to_onnx mapped ml_dtypes.float8_e4m3fnuz to "FLOAT8E4M3FN", so a
Constant holding float8_e4m3fnuz values was exported with the wrong ONNX
data type. FLOAT8E4M3FN and FLOAT8E4M3FNUZ have the same storage width, so
this failed silently: the exported raw_data was byte-identical and only the
type label was wrong, which makes any consumer that decodes the bytes
according to that label read different values.

The mismatch also made the import/export round trip asymmetric. Import
already handles FLOAT8E4M3FNUZ correctly, but materializing a lazy
initializer and exporting it again relabelled the tensor from 18 to 17.

float8_e5m2fnuz is mapped correctly in the same table, so only the
e4m3fnuz entry was affected.

The new tests assert the exported ONNX data_type for each ml_dtypes float8
variant, plus the round trip, rather than relying on raw_data: since both
e4m3 variants share a byte width, a bytes-only assertion cannot tell a
correct label from an incorrect one. Reverting only the source change makes
the three new regression tests fail, which confirms they detect the bug.

Signed-off-by: luxing <luicarus@users.noreply.github.com>
@luicarus
luicarus requested a review from a team as a code owner September 21, 2026 14:21
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.

onnx_graphsurgeon: Constant with float8_e4m3fnuz values is exported as FLOAT8E4M3FN

1 participant