Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creation of string tensor sequence. #3048

Merged
merged 1 commit into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion onnxruntime/core/session/onnxruntime_c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ static OrtStatus* OrtCreateValueImplSeqHelper(const OrtValue* const* in, size_t
}

OrtStatus* st{};
utils::MLTypeCallDispatcherRet<OrtStatus*, CallCreateValueImpl, bool, float, double,
utils::MLTypeCallDispatcherRet<OrtStatus*, CallCreateValueImpl, bool, float, double, std::string,
Copy link
Member

Choose a reason for hiding this comment

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

This dispatcher is so great!

MLFloat16, BFloat16, int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t>
t_disp(one_tensor.GetElementType());

Expand Down
39 changes: 39 additions & 0 deletions onnxruntime/test/shared_lib/test_nontensor_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,42 @@ TEST(CApiTest, CreateGetSeqTensors) {
std::set<int64_t>(std::begin(vals), std::end(vals)));
}
}

TEST(CApiTest, CreateGetSeqStringTensors) {
// Creation
auto default_allocator = onnxruntime::make_unique<MockedOrtAllocator>();
Ort::MemoryInfo info("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault);

std::vector<Ort::Value> in;
const char* string_input_data[] = {"abs", "def"};
const int N = 2;
for (int i = 0; i < N; ++i) {
// create tensor
std::vector<int64_t> shape{2};
auto value = Ort::Value::CreateTensor(Ort::AllocatorWithDefaultOptions(), shape.data(), shape.size(), ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING);

Ort::GetApi().FillStringTensor(value, string_input_data, 2);
in.push_back(std::move(value));
}

Ort::Value seq_ort = Ort::Value::CreateSequence(in);

// Fetch
std::set<std::string> string_set;
for (int idx = 0; idx < N; ++idx) {
Ort::Value out = seq_ort.GetValue(idx, default_allocator.get());
size_t data_len = out.GetStringTensorDataLength();
std::string result(data_len, '\0');
std::vector<size_t> offsets(N);
out.GetStringTensorContent((void*)result.data(), data_len, offsets.data(), offsets.size());

const char* s = result.data();
for (size_t i = 0; i < offsets.size(); ++i) {
size_t start = offsets[i];
size_t count = (i + 1) < offsets.size() ? offsets[i + 1] - start : data_len - start;
std::string stemp(s + start, count);
string_set.insert(stemp);
}
}
ASSERT_EQ(string_set, std::set<std::string>(std::begin(string_input_data), std::end(string_input_data)));
}