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