onnxruntime
7caa6448 - Make TensorProto element sizes compile-time (#29644)

Commit
32 days ago
Make TensorProto element sizes compile-time (#29644) ### 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) --------- Co-authored-by: Gopalakrishnan Nallasamy <gopalakrishnan.nallasamy@microsoft.com>
Parents
Loading