microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
operators/string_tensor.cc
57lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | #include "string_utils.h" |
| 4 | #include "string_tensor.h" |
| 5 | |
| 6 | void GetTensorMutableDataString(const OrtApi& api, OrtW::CustomOpApi& ort, OrtKernelContext* context, |
| 7 | const OrtValue* value, std::vector<std::string>& output) { |
| 8 | (void)context; |
| 9 | OrtTensorDimensions dimensions(ort, value); |
| 10 | size_t len = static_cast<size_t>(dimensions.Size()); |
| 11 | size_t data_len; |
| 12 | OrtW::ThrowOnError(api, api.GetStringTensorDataLength(value, &data_len)); |
| 13 | output.resize(len); |
| 14 | std::vector<char> result(data_len + len + 1, '\0'); |
| 15 | std::vector<size_t> offsets(len); |
| 16 | OrtW::ThrowOnError(api, api.GetStringTensorContent(value, (void*)result.data(), data_len, offsets.data(), offsets.size())); |
| 17 | output.resize(len); |
| 18 | for (int64_t i = (int64_t)len - 1; i >= 0; --i) { |
| 19 | if (i < static_cast<int64_t>(len) - 1) |
| 20 | result[offsets[static_cast<size_t>(i + (int64_t)1)]] = '\0'; |
| 21 | output[static_cast<size_t>(i)] = result.data() + offsets[static_cast<size_t>(i)]; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | void FillTensorDataString(const OrtApi& api, OrtW::CustomOpApi& ort, OrtKernelContext* context, |
| 26 | const std::vector<std::string>& value, OrtValue* output) { |
| 27 | (void)ort; |
| 28 | (void)context; |
| 29 | std::vector<const char*> temp(value.size()); |
| 30 | for (size_t i = 0; i < value.size(); ++i) { |
| 31 | temp[i] = value[i].c_str(); |
| 32 | } |
| 33 | |
| 34 | OrtW::ThrowOnError(api,api.FillStringTensor(output, temp.data(), value.size())); |
| 35 | } |
| 36 | |
| 37 | void GetTensorMutableDataString(const OrtApi& api, OrtW::CustomOpApi& ort, OrtKernelContext* context, |
| 38 | const OrtValue* value, std::vector<ustring>& output) { |
| 39 | std::vector<std::string> utf8_strings; |
| 40 | GetTensorMutableDataString(api, ort, context, value, utf8_strings); |
| 41 | |
| 42 | output.reserve(utf8_strings.size()); |
| 43 | for (auto& str : utf8_strings) { |
| 44 | output.emplace_back(str); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | void FillTensorDataString(const OrtApi& api, OrtW::CustomOpApi& ort, OrtKernelContext* context, |
| 50 | const std::vector<ustring>& value, OrtValue* output) { |
| 51 | std::vector<std::string> utf8_strings; |
| 52 | utf8_strings.reserve(value.size()); |
| 53 | for (const auto& str: value) { |
| 54 | utf8_strings.push_back(std::string(str)); |
| 55 | } |
| 56 | FillTensorDataString(api, ort, context, utf8_strings, output); |
| 57 | } |
| 58 | |