Fix incorrect string tensor creation in test - #29219
Conversation
…oat test The test was passing std::string* as raw memory to CreateTensor, which is invalid for string tensors. Use CreateTensor with allocator + FillStringTensor instead (matching the pattern in CreateGetSeqStringTensors). Also remove redundant keys vector and use std::array for fixed-size dims and values. Fixes #13246
There was a problem hiding this comment.
Pull request overview
This PR fixes the CreateGetVectorOfMapsStringFloat shared-lib C++ API test by switching to the correct mechanism for creating and populating a string tensor (allocate as ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING and then fill via FillStringTensor), and cleans up fixed-size test data by using std::array.
Changes:
- Use
std::arrayfor the fixed-size shape (dims) and float payload (values). - Correct string tensor creation by allocating a string tensor via
Ort::AllocatorWithDefaultOptions()and populating it withValue::FillStringTensor. - Fix the final key-set assertion to compare against
keys_arr(the actual source of truth).
Review: PR #29219 — Fix incorrect string tensor creation in testVerdict: Approve. Clean, correct bug fix for issue #13246. What it doesThe pre-fix code created a Ort::Value::CreateTensor(info, keys.data(), keys.size() * sizeof(std::string),
dims.data(), dims.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING);That overload is for tensors that wrap a caller-owned numeric buffer. String tensors in ORT don't store std::string objects — they have their own internal string storage that must be allocated by ORT and populated via The fix uses the correct two-step API: Ort::Value::CreateTensor(Ort::AllocatorWithDefaultOptions(), dims.data(), dims.size(),
ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING); // ORT-owned string storage
keys_tensor.FillStringTensor(keys_arr, NUM_KV_PAIRS); // populate the stringsThat's the canonical pattern (matches everywhere else in the codebase that creates Other changes
Non-blocking observations
CI22 / 73 checks OK at time of review. Safe to approve now; merge once CI fully clears. |
This pull request refactors the
CreateGetVectorOfMapsStringFloattest intest_nontensor_types.ccto improve type safety and clarity by switching fromstd::vectortostd::arrayfor fixed-size data and updating tensor creation logic. The changes also fix the string comparison to use the correct array.Test improvements and type safety:
std::vectorwithstd::arrayfordimsandvaluesvariables, making the code safer and more efficient for fixed-size data.Ort::AllocatorWithDefaultOptions()and filling the tensor withFillStringTensor, aligning with best practices for string tensor creation.Test correctness:
keys_arrinstead of the undefinedkeysvector, ensuring the test checks the correct set of keys.Header inclusion:
#include <array>at the top of the file to support the use ofstd::array.