Validate tensor element enum conversion at compile time#29646
Open
GopalakrishnanN wants to merge 3 commits into
Open
Validate tensor element enum conversion at compile time#29646GopalakrishnanN wants to merge 3 commits into
GopalakrishnanN wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens and speeds up the ONNX TensorProto_DataType → ORT C API ONNXTensorElementDataType conversion by making it usable in constant expressions and enforcing (at compile time) that the ONNX and ORT enums remain fully and uniquely mappable, including handling the ordering divergence introduced by FLOAT8E8M0.
Changes:
- Move the conversion logic into a
constexprhelper (header-visible) and explicitly remapFLOAT8E8M0/UINT2/INT2to account for the ONNX vs ORT ordering difference. - Add a
constevalcompile-time validator +static_assertensuring the mapping is complete and bijective. - Add an independent exhaustive
static_asserttest that also checks invalid values map toUNDEFINED.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| onnxruntime/core/framework/onnxruntime_map_type_info.h | Adds constexpr conversion + consteval bijection validator with a compile-time static_assert. |
| onnxruntime/core/framework/onnxruntime_map_type_info.cc | Switches map key-type conversion to the new header helper and removes the old runtime switch. |
| onnxruntime/test/framework/type_info_test.cc | Adds an exhaustive compile-time test for the conversion mapping, including invalid inputs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
TensorProto_DataTypeto ORT C APIONNXTensorElementDataTypeconversion usable in constant expressions.FLOAT8E8M0beforeUINT2andINT2.constevalvalidator requiring a complete one-to-one mapping between the two enums.static_asserttest, including invalid values.Motivation and Context
The previous conversion was a runtime 26-case switch. More importantly, there was no compile-time guard ensuring future ONNX and ORT enum additions remained complete and bijective. This change turns that compatibility contract into a compilation invariant while preserving invalid-value fallback to
UNDEFINED.Local Windows RelWithDebInfo measurements, using a stabilized build and forcing only
onnxruntime_map_type_info.ccstale:onnxruntime_map_type_info.objonnxruntime_framework.libThese measurements are toolchain-specific and primarily guard against regressions; the compile-time compatibility check is the main benefit.
Validation
lintrunner -a onnxruntime/core/framework/onnxruntime_map_type_info.h onnxruntime/core/framework/onnxruntime_map_type_info.cc onnxruntime/test/framework/type_info_test.cccmake --build build/Windows/RelWithDebInfo --config RelWithDebInfo --target onnxruntime_test_all --parallel 8onnxruntime_test_all.exe --gtest_filter="TypeInfoTests.*"(4/4 passed)