Merge pull request !7549 from sunsuodong/add_lite_interfacetags/v1.1.0
| @@ -20,6 +20,7 @@ | |||||
| #include <string> | #include <string> | ||||
| #include <memory> | #include <memory> | ||||
| #include "schema/model_generated.h" | #include "schema/model_generated.h" | ||||
| #include "include/ms_tensor.h" | |||||
| namespace mindspore::lite { | namespace mindspore::lite { | ||||
| /// \brief Allocator defined a memory pool for malloc memory and free memory dynamically. | /// \brief Allocator defined a memory pool for malloc memory and free memory dynamically. | ||||
| @@ -36,5 +37,18 @@ using Uint32Vector = std::vector<uint32_t>; | |||||
| using String = std::string; | using String = std::string; | ||||
| using NodeType = schema::NodeType; | using NodeType = schema::NodeType; | ||||
| using AllocatorPtr = std::shared_ptr<Allocator>; | using AllocatorPtr = std::shared_ptr<Allocator>; | ||||
| /// \brief Set data of MSTensor from string vector. | |||||
| /// | |||||
| /// \param[in] input string vector. | |||||
| /// \param[out] MSTensor. | |||||
| /// | |||||
| /// \return STATUS as an error code of this interface, STATUS is defined in errorcode.h. | |||||
| int StringsToMSTensor(const std::vector<std::string> &inputs, tensor::MSTensor *tensor); | |||||
| /// \brief Get string vector from MSTensor. | |||||
| /// \param[in] MSTensor. | |||||
| /// \return string vector. | |||||
| std::vector<std::string> MSTensorToStrings(const tensor::MSTensor *tensor); | |||||
| } // namespace mindspore::lite | } // namespace mindspore::lite | ||||
| #endif // MINDSPORE_LITE_INCLUDE_LITE_UTILS_H_ | #endif // MINDSPORE_LITE_INCLUDE_LITE_UTILS_H_ | ||||
| @@ -15,6 +15,8 @@ | |||||
| */ | */ | ||||
| #include "src/common/string_util.h" | #include "src/common/string_util.h" | ||||
| #include <algorithm> | |||||
| #include "include/ms_tensor.h" | |||||
| namespace mindspore { | namespace mindspore { | ||||
| namespace lite { | namespace lite { | ||||
| @@ -28,9 +30,13 @@ std::vector<StringPack> ParseTensorBuffer(Tensor *tensor) { | |||||
| } | } | ||||
| std::vector<StringPack> ParseStringBuffer(const void *data) { | std::vector<StringPack> ParseStringBuffer(const void *data) { | ||||
| std::vector<StringPack> buffer; | |||||
| if (data == nullptr) { | |||||
| MS_LOG(ERROR) << "data is nullptr"; | |||||
| return buffer; | |||||
| } | |||||
| const int32_t *offset = reinterpret_cast<const int32_t *>(data); | const int32_t *offset = reinterpret_cast<const int32_t *>(data); | ||||
| int32_t num = *offset; | int32_t num = *offset; | ||||
| std::vector<StringPack> buffer; | |||||
| for (int i = 0; i < num; i++) { | for (int i = 0; i < num; i++) { | ||||
| offset += 1; | offset += 1; | ||||
| buffer.push_back(StringPack{(*(offset + 1)) - (*offset), reinterpret_cast<const char *>(data) + (*offset)}); | buffer.push_back(StringPack{(*(offset + 1)) - (*offset), reinterpret_cast<const char *>(data) + (*offset)}); | ||||
| @@ -108,6 +114,26 @@ int GetStringCount(const void *data) { return *(static_cast<const int32_t *>(dat | |||||
| int GetStringCount(Tensor *tensor) { return GetStringCount(tensor->MutableData()); } | int GetStringCount(Tensor *tensor) { return GetStringCount(tensor->MutableData()); } | ||||
| int StringsToMSTensor(const std::vector<std::string> &inputs, tensor::MSTensor *tensor) { | |||||
| std::vector<StringPack> all_pack; | |||||
| for (auto &input : inputs) { | |||||
| StringPack pack = {static_cast<int>(input.length()), input.data()}; | |||||
| all_pack.push_back(pack); | |||||
| } | |||||
| return WriteStringsToTensor(static_cast<Tensor *>(tensor), all_pack); | |||||
| } | |||||
| std::vector<std::string> MSTensorToStrings(const tensor::MSTensor *tensor) { | |||||
| const void *ptr = static_cast<const Tensor *>(tensor)->data_c(); | |||||
| std::vector<StringPack> all_pack = ParseStringBuffer(ptr); | |||||
| std::vector<std::string> result(all_pack.size()); | |||||
| std::transform(all_pack.begin(), all_pack.end(), result.begin(), [](StringPack &pack) { | |||||
| std::string str(pack.data, pack.len); | |||||
| return str; | |||||
| }); | |||||
| return result; | |||||
| } | |||||
| // Some primes between 2^63 and 2^64 | // Some primes between 2^63 and 2^64 | ||||
| static uint64_t k0 = 0xc3a5c85c97cb3127ULL; | static uint64_t k0 = 0xc3a5c85c97cb3127ULL; | ||||
| static uint64_t k1 = 0xb492b66fbe98f273ULL; | static uint64_t k1 = 0xb492b66fbe98f273ULL; | ||||
| @@ -33,12 +33,11 @@ typedef struct { | |||||
| } StringPack; | } StringPack; | ||||
| // example of string tensor: | // example of string tensor: | ||||
| // 3, 0, 0, 0 # int32, num of strings | |||||
| // 20, 0, 0, 0 # int32, offset of 0-th string | |||||
| // 23, 0, 0, 0 # int32, offset of 0-th string | |||||
| // 26, 0, 0, 0 # int32, offset of 0-th string | |||||
| // 29, 0, 0, 0 # int32, total length of tensor data | |||||
| // 'h', 'o', 'w', 'a', 'r', 'e', 'y', 'o', 'u' # char, how are you | |||||
| // 2, 0, 0, 0 # int32, num of strings | |||||
| // 16, 0, 0, 0 # int32, offset of 0-th string | |||||
| // 21, 0, 0, 0 # int32, offset of 1-th string | |||||
| // 30, 0, 0, 0 # int32, total length of tensor data | |||||
| // 'h', 'e', 'l', 'l', 'o', 'h', 'o', 'w', 'a', 'r', 'e', 'y', 'o', 'u' # char, "hello", "how are you" | |||||
| std::vector<StringPack> ParseTensorBuffer(Tensor *tensor); | std::vector<StringPack> ParseTensorBuffer(Tensor *tensor); | ||||
| std::vector<StringPack> ParseStringBuffer(const void *data); | std::vector<StringPack> ParseStringBuffer(const void *data); | ||||