Make TensorProto element sizes compile-time#29644
Open
GopalakrishnanN wants to merge 4 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
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_mapwith aninline constexpr std::arraylookup table. - Makes
GetElementSizeOfTensorconstexpr noexcept, returning0for 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. |
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
unordered_mapused byGetElementSizeOfTensorwith a directly indexedconstexprtable.TensorProto_DataTypehas a nonzero entry whileUNDEFINEDandSTRINGremain unsupported.GetElementSizeOfTensorusable in constant expressions and safely return zero for out-of-range enum values.static_asserttest 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.ccstale:tensorprotoutils.objonnxruntime_framework.libValidation
lintrunner -a onnxruntime/core/framework/tensorprotoutils.h onnxruntime/core/framework/tensorprotoutils.cc onnxruntime/test/framework/tensorutils_test.cccmake --build build/Windows/RelWithDebInfo --config RelWithDebInfo --target onnxruntime_test_all --parallel 8onnxruntime_test_all.exe --gtest_filter="TensorProtoUtilsTest.*"(10/10 passed)