• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "include/lite_utils.h"
18 #include <algorithm>
19 #include <vector>
20 #include <string>
21 #include <utility>
22 #include "src/tensor.h"
23 #include "src/common/log_adapter.h"
24 #ifndef STRING_KERNEL_CLIP
25 #include "src/common/string_util.h"
26 #endif
27 #include "tools/common/option.h"
28 #include "include/errorcode.h"
29 #include "include/ms_tensor.h"
30 
31 namespace mindspore {
32 namespace lite {
StringsToMSTensor(const std::vector<std::string> & inputs,tensor::MSTensor * tensor)33 int StringsToMSTensor(const std::vector<std::string> &inputs, tensor::MSTensor *tensor) {
34 #ifndef STRING_KERNEL_CLIP
35   if (tensor == nullptr) {
36     return RET_PARAM_INVALID;
37   }
38 
39   std::vector<StringPack> all_pack;
40   for (auto &input : inputs) {
41     StringPack pack = {static_cast<int>(input.length()), input.data()};
42     all_pack.push_back(pack);
43   }
44   return WriteStringsToTensor(static_cast<Tensor *>(tensor), all_pack);
45 #else
46   MS_LOG(ERROR) << unsupport_string_tensor_log;
47   return RET_ERROR;
48 #endif
49 }
50 
MSTensorToStrings(const tensor::MSTensor * tensor)51 std::vector<std::string> MSTensorToStrings(const tensor::MSTensor *tensor) {
52 #ifndef STRING_KERNEL_CLIP
53   if (tensor == nullptr) {
54     return {""};
55   }
56   const void *ptr = const_cast<Tensor *>(static_cast<const Tensor *>(tensor))->data();
57   std::vector<StringPack> all_pack = ParseStringBuffer(ptr);
58   std::vector<std::string> result(all_pack.size());
59   std::transform(all_pack.begin(), all_pack.end(), result.begin(), [](StringPack &pack) {
60     std::string str(pack.data, pack.len);
61     return str;
62   });
63   return result;
64 #else
65   MS_LOG(ERROR) << unsupport_string_tensor_log;
66   return {""};
67 #endif
68 }
69 }  // namespace lite
70 }  // namespace mindspore
71