Skip to content

onnx-graphsurgeon - fold value_float constants as float32 in fold_constants - #4834

Open
VaggelisGian wants to merge 1 commit into
NVIDIA:mainfrom
VaggelisGian:fix-gs-fold-constant-float32
Open

onnx-graphsurgeon - fold value_float constants as float32 in fold_constants#4834
VaggelisGian wants to merge 1 commit into
NVIDIA:mainfrom
VaggelisGian:fix-gs-fold-constant-float32

Conversation

@VaggelisGian

Copy link
Copy Markdown

What does this PR do?

Graph.fold_constants() lowered Constant nodes carrying the value_float or value_floats attributes into float64 (DOUBLE) tensors, because np.array defaults to double precision for Python floats. ONNX defines both attributes as float32, so the folded initializers had the wrong type and models produced from them were rejected by ONNX Runtime with a type error on consumers such as Add.

Reproduction before this fix: build a graph with a Constant(value_float=...) node, run fold_constants(), export. The folded initializer comes out with data_type = 11 (DOUBLE) and ONNX Runtime refuses to load the model. After the fix it is data_type = 1 (FLOAT) and loads cleanly.

The fix lowers both attributes explicitly as float32. value_int and value_ints already land on int64 through the default path, which matches their ONNX types, so they are left alone.

Testing

New regression test tests/ir/test_graph.py::TestFoldConstants::test_value_float_attrs_fold_as_float32, parametrized over both attributes; asserts dtype, shape and values of the folded constant. It fails on unpatched main (folded initializer dtype DOUBLE) and passes after.

Commands and output on this machine:

$ cd tools/onnx-graphsurgeon
$ python -m pytest "tests/ir/test_graph.py::TestFoldConstants::test_value_float_attrs_fold_as_float32" -v

tests\ir\test_graph.py::TestFoldConstants::test_value_float_attrs_fold_as_float32[attrs0] PASSED [ 50%]
tests\ir\test_graph.py::TestFoldConstants::test_value_float_attrs_fold_as_float32[attrs1] PASSED [100%]
============================== 2 passed in 0.36s ==============================

$ python -m pytest tests --ignore=tests/test_examples.py -q
281 passed, 1 skipped in 3.46s

test_examples.py is excluded only because its harness spawns bash -c python3, unavailable on Windows; unrelated to this change.

Environment

TensorRT OSS main (10d15ae), onnx-graphsurgeon sources run from the repo.
Python 3.12, onnx 1.20.0, numpy 2.5.2, Windows 11 Pro.

Issue

Per CONTRIBUTING.md, bugfixes start as an issue approved by TensorRT engineers before code review. I could not find an existing issue covering this; happy to file one and link it here if maintainers prefer that flow, or please advise whether this PR can proceed directly.

Constant nodes carrying value_float or value_floats attributes were lowered
by fold_constants() into float64 tensors because np.array defaults to double
for Python floats. ONNX defines both attributes as float32, so the folded
initializers had the wrong type and models produced from them were rejected
by ONNX Runtime with a type error on consumers such as Add.

Lower the two attributes explicitly as float32 and cover both with a
regression test asserting dtype, shape and values of the folded constant.
value_int and value_ints already land on int64 through the default path,
which matches their ONNX types, so they are left alone.

Test Plan:
  cd tools/onnx-graphsurgeon
  PYTHONPATH=$PWD python -m pytest "tests/ir/test_graph.py::TestFoldConstants::test_value_float_attrs_fold_as_float32"
    fails before the fix (initializer dtype DOUBLE), passes after
  PYTHONPATH=$PWD python -m pytest tests --ignore=tests/test_examples.py
    281 passed, 1 skipped (test_examples excluded: its harness spawns
    bash with python3, which does not exist on Windows)

Signed-off-by: Vaggelis <baggelis100@gmail.com>
@VaggelisGian
VaggelisGian requested a review from a team as a code owner August 24, 2026 10:55
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.

1 participant