Skip to content

Make TensorProto element sizes compile-time#29644

Open
GopalakrishnanN wants to merge 4 commits into
mainfrom
GopalakrishnanN/constexpr-data-type-map
Open

Make TensorProto element sizes compile-time#29644
GopalakrishnanN wants to merge 4 commits into
mainfrom
GopalakrishnanN/constexpr-data-type-map

Conversation

@GopalakrishnanN

Copy link
Copy Markdown
Contributor

Description

  • Replace the function-local unordered_map used by GetElementSizeOfTensor with a directly indexed constexpr table.
  • Validate at compile time that every numeric TensorProto_DataType has a nonzero entry while UNDEFINED and STRING remain unsupported.
  • Make GetElementSizeOfTensor usable in constant expressions and safely return zero for out-of-range enum values.
  • Add an independent exhaustive static_assert test covering every current ONNX tensor data type.

Motivation and Context

The previous map performed dynamic first-use initialization and heap-backed hash lookup for a fixed enum-to-size relation. A constexpr table moves completeness checking to compilation, removes runtime initialization, and reduces the generated framework object and library size.

Local Windows RelWithDebInfo measurements, using a stabilized build and forcing only tensorprotoutils.cc stale:

Metric Before After Change
Incremental build 14.01 s 12.04 s -14.1%
tensorprotoutils.obj 6,500,915 B 6,217,508 B -283,407 B (-4.36%)
onnxruntime_framework.lib 196,933,848 B 196,570,904 B -362,944 B (-0.18%)

Validation

  • lintrunner -a onnxruntime/core/framework/tensorprotoutils.h onnxruntime/core/framework/tensorprotoutils.cc onnxruntime/test/framework/tensorutils_test.cc
  • cmake --build build/Windows/RelWithDebInfo --config RelWithDebInfo --target onnxruntime_test_all --parallel 8
  • onnxruntime_test_all.exe --gtest_filter="TensorProtoUtilsTest.*" (10/10 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 refactors utils::GetElementSizeOfTensor from a runtime-initialized unordered_map lookup to a constexpr, directly-indexed table in tensorprotoutils.h, enabling constant-expression use and compile-time completeness validation for ONNX TensorProto_DataType element-size mappings.

Changes:

  • Replaces the function-local unordered_map with an inline constexpr std::array lookup table.
  • Makes GetElementSizeOfTensor constexpr noexcept, returning 0 for out-of-range enum values.
  • Adds an exhaustive compile-time (static_assert) test ensuring the mapping is constexpr and covers all current ONNX tensor data types.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/core/framework/tensorprotoutils.h Adds constexpr element-size table + compile-time completeness checking; inlines GetElementSizeOfTensor as constexpr.
onnxruntime/core/framework/tensorprotoutils.cc Removes the old unordered_map-based implementation of GetElementSizeOfTensor.
onnxruntime/test/framework/tensorutils_test.cc Adds an exhaustive constexpr static_assert test validating element sizes for all ONNX tensor data types and out-of-range behavior.

Comment thread onnxruntime/core/framework/tensorprotoutils.h
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