Skip to content

Validate tensor element enum conversion at compile time#29646

Open
GopalakrishnanN wants to merge 3 commits into
mainfrom
GopalakrishnanN/constexpr-enum-conversion
Open

Validate tensor element enum conversion at compile time#29646
GopalakrishnanN wants to merge 3 commits into
mainfrom
GopalakrishnanN/constexpr-enum-conversion

Conversation

@GopalakrishnanN

Copy link
Copy Markdown
Contributor

Description

  • Make the ONNX TensorProto_DataType to ORT C API ONNXTensorElementDataType conversion usable in constant expressions.
  • Preserve the shared numeric prefix and explicitly map the final three values, whose ordering differs after ONNX inserted FLOAT8E8M0 before UINT2 and INT2.
  • Add a consteval validator requiring a complete one-to-one mapping between the two enums.
  • Add an independent exhaustive static_assert test, 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.cc stale:

Metric Before After Change
Incremental build 7.294 s 6.535 s -10.4%
onnxruntime_map_type_info.obj 765,269 B 762,123 B -3,146 B (-0.41%)
onnxruntime_framework.lib 196,933,848 B 196,931,236 B -2,612 B

These 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.cc
  • cmake --build build/Windows/RelWithDebInfo --config RelWithDebInfo --target onnxruntime_test_all --parallel 8
  • onnxruntime_test_all.exe --gtest_filter="TypeInfoTests.*" (4/4 passed)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 constexpr helper (header-visible) and explicitly remap FLOAT8E8M0 / UINT2 / INT2 to account for the ONNX vs ORT ordering difference.
  • Add a consteval compile-time validator + static_assert ensuring the mapping is complete and bijective.
  • Add an independent exhaustive static_assert test that also checks invalid values map to UNDEFINED.

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.

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.

2 participants