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