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 "src/common/string_util.h"
18 #include <algorithm>
19 #include <vector>
20 #include <string>
21 #include <fstream>
22 #include <climits>
23 #include "include/ms_tensor.h"
24
25 namespace mindspore {
26 namespace lite {
ParseTensorBuffer(Tensor * tensor)27 std::vector<StringPack> ParseTensorBuffer(Tensor *tensor) {
28 if (tensor == nullptr) {
29 MS_LOG(ERROR) << "tensor is nullptr.";
30 return std::vector<StringPack>{};
31 }
32 if (tensor->data() == nullptr) {
33 MS_LOG(ERROR) << "Tensor data is null, cannot be parsed";
34 return std::vector<StringPack>{};
35 }
36 return ParseStringBuffer(tensor->MutableData());
37 }
38
ParseStringBuffer(const void * data)39 std::vector<StringPack> ParseStringBuffer(const void *data) {
40 std::vector<StringPack> buffer;
41 if (data == nullptr) {
42 MS_LOG(ERROR) << "data is nullptr";
43 return buffer;
44 }
45 const auto *offset = reinterpret_cast<const int32_t *>(data);
46 int32_t num = *offset;
47 for (int i = 0; i < num; i++) {
48 offset += 1;
49 buffer.push_back(StringPack{(*(offset + 1)) - (*offset), reinterpret_cast<const char *>(data) + (*offset)});
50 }
51 return buffer;
52 }
53
WriteStringsToTensor(Tensor * tensor,const std::vector<StringPack> & string_buffer)54 int WriteStringsToTensor(Tensor *tensor, const std::vector<StringPack> &string_buffer) {
55 if (tensor == nullptr) {
56 MS_LOG(ERROR) << "tensor is nullptr.";
57 return RET_ERROR;
58 }
59 size_t num = string_buffer.size();
60 std::vector<int32_t> offset(num + 1);
61 offset[0] = 4 * (num + 2);
62 for (size_t i = 0; i < num; i++) {
63 offset[i + 1] = offset[i] + string_buffer[i].len;
64 }
65 std::vector<int> shape = {offset[num]};
66 tensor->set_shape(shape);
67 tensor->set_data_type(kObjectTypeString);
68 tensor->FreeData();
69 void *data = tensor->MutableData();
70 if (data == nullptr) {
71 return RET_ERROR;
72 }
73
74 auto *string_info = reinterpret_cast<int32_t *>(data);
75 char *string_data = reinterpret_cast<char *>(data);
76
77 string_info[0] = num;
78 for (size_t i = 0; i <= num; i++) {
79 string_info[i + 1] = offset[i];
80 }
81 for (size_t i = 0; i < num; i++) {
82 memcpy(string_data + offset[i], string_buffer[i].data, string_buffer[i].len);
83 }
84 return RET_OK;
85 }
86
WriteSeperatedStringsToTensor(Tensor * tensor,const std::vector<std::vector<StringPack>> & string_buffer)87 int WriteSeperatedStringsToTensor(Tensor *tensor, const std::vector<std::vector<StringPack>> &string_buffer) {
88 if (tensor == nullptr) {
89 MS_LOG(ERROR) << "tensor is nullptr.";
90 return RET_ERROR;
91 }
92 size_t num = string_buffer.size();
93 std::vector<int32_t> offset(num + 1);
94 offset[0] = 4 * (num + 2);
95 std::vector<int> len(num);
96 for (size_t i = 0; i < num; i++) {
97 len[i] = 0;
98 for (int j = 0; j < static_cast<int>(string_buffer[i].size()); j++) {
99 len[i] += string_buffer[i][j].len;
100 }
101 offset[i + 1] = offset[i] + len[i];
102 }
103
104 std::vector<int> shape = {offset[num]};
105 tensor->set_shape(shape);
106 tensor->FreeData();
107 void *data = tensor->MutableData();
108 if (data == nullptr) {
109 return RET_ERROR;
110 }
111
112 auto *string_info = reinterpret_cast<int32_t *>(data);
113 auto *string_data = reinterpret_cast<char *>(data);
114
115 string_info[0] = num;
116 for (size_t i = 0; i <= num; i++) {
117 string_info[i + 1] = offset[i];
118 }
119 for (size_t i = 0; i < num; i++) {
120 auto *dst = string_data + offset[i];
121 for (auto string_part : string_buffer[i]) {
122 memcpy(dst, string_part.data, string_part.len);
123 dst += string_part.len;
124 }
125 }
126 return RET_OK;
127 }
128
GetStringCount(const void * data)129 int GetStringCount(const void *data) { return *(static_cast<const int32_t *>(data)); }
130
GetStringCount(Tensor * tensor)131 int GetStringCount(Tensor *tensor) {
132 if (tensor == nullptr) {
133 MS_LOG(ERROR) << "tensor is nullptr.";
134 return RET_ERROR;
135 }
136 return GetStringCount(tensor->MutableData());
137 }
138
139 // Some primes between 2^63 and 2^64
140 namespace {
141 static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
142 static const uint64_t k1 = 0xb492b66fbe98f273ULL;
143 static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
144
Fetch64Bit(const char * p)145 uint64_t Fetch64Bit(const char *p) {
146 uint64_t result = 0;
147 memcpy(&result, p, sizeof(uint64_t));
148 return result;
149 }
150
Fetch32Bit(const char * p)151 uint32_t Fetch32Bit(const char *p) {
152 uint32_t result = 0;
153 memcpy(&result, p, sizeof(uint32_t));
154 return result;
155 }
156
Rotate64(uint64_t value,int shift)157 uint64_t Rotate64(uint64_t value, int shift) {
158 return shift == 0 ? value : ((value >> shift) | (value << (64 - shift)));
159 }
160
HashLen16(uint64_t u,uint64_t v,uint64_t multiple)161 uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t multiple) {
162 uint64_t a = (u ^ v) * multiple;
163 a ^= (a >> 47);
164 uint64_t b = (v ^ a) * multiple;
165 b ^= (b >> 47);
166 b *= multiple;
167 return b;
168 }
169
ShiftMix(uint64_t value)170 uint64_t ShiftMix(uint64_t value) { return value ^ (value >> 47); }
171
HashStringLen0to16(const char * s,size_t len)172 uint64_t HashStringLen0to16(const char *s, size_t len) {
173 if (len >= 8) {
174 uint64_t mul = k2 + len * 2;
175 uint64_t a = Fetch64Bit(s) + k2;
176 uint64_t b = Fetch64Bit(s + len - 8);
177 uint64_t c = Rotate64(b, 37) * mul + a;
178 uint64_t d = (Rotate64(a, 25) + b) * mul;
179 return HashLen16(c, d, mul);
180 }
181 if (len >= 4) {
182 uint64_t mul = k2 + len * 2;
183 uint64_t a = Fetch32Bit(s);
184 return HashLen16(len + (a << 3), Fetch32Bit(s + len - 4), mul);
185 }
186 if (len > 0) {
187 uint8_t a = s[0];
188 uint8_t b = s[len >> 1];
189 uint8_t c = s[len - 1];
190 uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
191 uint32_t z = len + (static_cast<uint32_t>(c) << 2);
192 return ShiftMix((y * k2) ^ (z * k0)) * k2;
193 }
194 return k2;
195 }
196
HashStringLen17to32(const char * s,size_t len)197 uint64_t HashStringLen17to32(const char *s, size_t len) {
198 uint64_t mul = k2 + len * 2;
199 uint64_t a = Fetch64Bit(s) * k1;
200 uint64_t b = Fetch64Bit(s + 8);
201 uint64_t c = Fetch64Bit(s + len - 8) * mul;
202 uint64_t d = Fetch64Bit(s + len - 16) * k2;
203 return HashLen16(Rotate64(a + b, 43) + Rotate64(c, 30) + d, a + Rotate64(b + k2, 18) + c, mul);
204 }
205
HashStringLen33to64(const char * s,size_t len)206 uint64_t HashStringLen33to64(const char *s, size_t len) {
207 uint64_t mul = k2 + len * 2;
208 uint64_t a = Fetch64Bit(s) * k2;
209 uint64_t b = Fetch64Bit(s + 8);
210 uint64_t c = Fetch64Bit(s + len - 8) * mul;
211 uint64_t d = Fetch64Bit(s + len - 16) * k2;
212 uint64_t y = Rotate64(a + b, 43) + Rotate64(c, 30) + d;
213 uint64_t z = HashLen16(y, a + Rotate64(b + k2, 18) + c, mul);
214 uint64_t e = Fetch64Bit(s + 16) * mul;
215 uint64_t f = Fetch64Bit(s + 24);
216 uint64_t g = (y + Fetch64Bit(s + len - 32)) * mul;
217 uint64_t h = (z + Fetch64Bit(s + len - 24)) * mul;
218 return HashLen16(Rotate64(e + f, 43) + Rotate64(g, 30) + h, e + Rotate64(f + a, 18) + g, mul);
219 }
220
HashLen32WithSeeds(const char * s,uint64_t a,uint64_t b)221 std::pair<uint64_t, uint64_t> HashLen32WithSeeds(const char *s, uint64_t a, uint64_t b) {
222 a += Fetch64Bit(s);
223 b = Rotate64(b + a + Fetch64Bit(s + 24), 21);
224 uint64_t c = a;
225 a += Fetch64Bit(s + 8);
226 a += Fetch64Bit(s + 16);
227 b += Rotate64(a, 44);
228 return std::make_pair(a + Fetch64Bit(s + 24), b + c);
229 }
230 } // namespace
231
StringHash64(const char * s,size_t len)232 uint64_t StringHash64(const char *s, size_t len) {
233 if (s == nullptr) {
234 return 0;
235 }
236 const uint64_t seed_value = 81;
237 if (len <= 16) {
238 return HashStringLen0to16(s, len);
239 } else if (len <= 32) {
240 return HashStringLen17to32(s, len);
241 } else if (len <= 64) {
242 return HashStringLen33to64(s, len);
243 }
244
245 uint64_t x = seed_value;
246 uint64_t y = seed_value * k1 + 113;
247 uint64_t tmp = y * k2 + 113;
248 uint64_t z = (tmp ^ (tmp >> 47)) * k2;
249 std::pair<uint64_t, uint64_t> v = std::make_pair(0, 0);
250 std::pair<uint64_t, uint64_t> w = std::make_pair(0, 0);
251 x = x * k2 + Fetch64Bit(s);
252
253 const char *end = s + ((len - 1) / 64) * 64;
254 const char *last64 = end + ((len - 1) & 63) - 63;
255 MS_ASSERT(s + len - 64 == last64);
256 do {
257 x = Rotate64(x + y + v.first + Fetch64Bit(s + 8), 37) * k1;
258 y = Rotate64(y + v.second + Fetch64Bit(s + 48), 42) * k1;
259 x ^= w.second;
260 y += v.first + Fetch64Bit(s + 40);
261 z = Rotate64(z + w.first, 33) * k1;
262 v = HashLen32WithSeeds(s, v.second * k1, x + w.first);
263 w = HashLen32WithSeeds(s + 32, z + w.second, y + Fetch64Bit(s + 16));
264 std::swap(z, x);
265 s += 64;
266 } while (s != end);
267 uint64_t mul = k1 + ((z & 0xff) << 1);
268 s = last64;
269 w.first += ((len - 1) & 63);
270 v.first += w.first;
271 w.first += v.first;
272 x = Rotate64(x + y + v.first + Fetch64Bit(s + 8), 37) * mul;
273 y = Rotate64(y + v.second + Fetch64Bit(s + 48), 42) * mul;
274 x ^= w.second * 9;
275 y += v.first * 9 + Fetch64Bit(s + 40);
276 z = Rotate64(z + w.first, 33) * mul;
277 v = HashLen32WithSeeds(s, v.second * mul, x + w.first);
278 w = HashLen32WithSeeds(s + 32, z + w.second, y + Fetch64Bit(s + 16));
279 std::swap(z, x);
280 return HashLen16(HashLen16(v.first, w.first, mul) + ShiftMix(y) * k0 + z, HashLen16(v.second, w.second, mul) + x,
281 mul);
282 }
283 } // namespace lite
284 } // namespace mindspore
285