Browse Source

!7549 [MSLITE][Develop] add lite string interface

Merge pull request !7549 from sunsuodong/add_lite_interface
tags/v1.1.0
mindspore-ci-bot Gitee 5 years ago
parent
commit
a75b77c843
3 changed files with 46 additions and 7 deletions
  1. +14
    -0
      mindspore/lite/include/lite_utils.h
  2. +27
    -1
      mindspore/lite/src/common/string_util.cc
  3. +5
    -6
      mindspore/lite/src/common/string_util.h

+ 14
- 0
mindspore/lite/include/lite_utils.h View File

@@ -20,6 +20,7 @@
#include <string>
#include <memory>
#include "schema/model_generated.h"
#include "include/ms_tensor.h"

namespace mindspore::lite {
/// \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 NodeType = schema::NodeType;
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
#endif // MINDSPORE_LITE_INCLUDE_LITE_UTILS_H_

+ 27
- 1
mindspore/lite/src/common/string_util.cc View File

@@ -15,6 +15,8 @@
*/

#include "src/common/string_util.h"
#include <algorithm>
#include "include/ms_tensor.h"

namespace mindspore {
namespace lite {
@@ -28,9 +30,13 @@ std::vector<StringPack> ParseTensorBuffer(Tensor *tensor) {
}

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);
int32_t num = *offset;
std::vector<StringPack> buffer;
for (int i = 0; i < num; i++) {
offset += 1;
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 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
static uint64_t k0 = 0xc3a5c85c97cb3127ULL;
static uint64_t k1 = 0xb492b66fbe98f273ULL;


+ 5
- 6
mindspore/lite/src/common/string_util.h View File

@@ -33,12 +33,11 @@ typedef struct {
} StringPack;

// 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> ParseStringBuffer(const void *data);



Loading…
Cancel
Save