-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathCircleConst.cpp
More file actions
321 lines (272 loc) · 9 KB
/
CircleConst.cpp
File metadata and controls
321 lines (272 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "luci/Import/Nodes/CircleConst.h"
#include "luci/Import/CircleReader.h"
#include <luci/IR/Nodes/CircleConst.h>
#include <luci/Log.h>
#include <loco.h>
#include <oops/UserExn.h>
#include <cassert>
#include <limits>
#include <ostream>
#include <string>
#include <vector>
#include <string.h>
namespace
{
std::ostream &operator<<(std::ostream &os, const luci::VectorWrapper<int32_t> &vect)
{
uint32_t seq = 0;
for (const auto &v : vect)
{
if (seq)
os << ", ";
os << v;
seq++;
}
return os;
}
using namespace luci;
template <loco::DataType DT>
void copy_data(const VectorWrapper<uint8_t> &raw_data, uint32_t num_elements,
CircleConst *const_node)
{
using T = typename loco::DataTypeImpl<DT>::Type;
// TODO calculate the exact buffer size of sparse tensor
if (const_node->sparsityparam())
{
num_elements = raw_data.size() / sizeof(T);
}
assert(raw_data.size() == num_elements * sizeof(T));
const auto *data = reinterpret_cast<const T *>(raw_data.data());
const_node->size<DT>(num_elements);
for (uint32_t i = 0; i < num_elements; ++i)
{
const_node->at<DT>(i) = data[i];
}
}
template <>
void copy_data<loco::DataType::STRING>(const VectorWrapper<uint8_t> &raw_data,
uint32_t num_elements, CircleConst *const_node)
{
assert(const_node->sparsityparam() == nullptr);
const auto *data = reinterpret_cast<const char *>(raw_data.data());
const auto *i32d = reinterpret_cast<const int32_t *>(raw_data.data());
// de-serialize string data
// int32_t count
// int32_t offsets[count + 1]
// string values[count]
assert(static_cast<uint32_t>(*i32d) == num_elements);
i32d++; // skip count
std::vector<int32_t> offsets;
offsets.push_back(*i32d++);
for (uint32_t i = 0; i < num_elements; ++i)
{
offsets.push_back(*i32d++);
}
assert(offsets.size() == num_elements + 1);
// Validate STRING offsets as non-negative, monotonic, and bounded within data buffer
for (uint32_t i = 0; i < offsets.size(); ++i)
{
if (offsets[i] < 0)
{
throw std::runtime_error("String offset is negative");
}
if (i > 0 && offsets[i] < offsets[i - 1])
{
throw std::runtime_error("String offsets are not monotonic");
}
if (offsets[i] > static_cast<int32_t>(raw_data.size()))
{
throw std::runtime_error("String offset is out of bounds");
}
}
const_node->size<loco::DataType::STRING>(num_elements);
for (uint32_t i = 0; i < num_elements; ++i)
{
int32_t start = offsets[i];
int32_t next = offsets[i + 1];
std::string value(data + start, next - start);
const_node->at<loco::DataType::STRING>(i) = value;
}
}
// NOTE copy_data for S4, U4.
// this method will unpack two 4bit elements, packed in 8bit,
// to two 8bit elements, having values -8~7, for S4 and 0~15 for U4.
template <loco::DataType DT>
void copy_data_4(const VectorWrapper<uint8_t> &raw_data, uint32_t num_elements,
CircleConst *const_node)
{
using T = typename loco::DataTypeImpl<DT>::Type;
// TODO support sparse?
assert(const_node->sparsityparam() == nullptr);
if (const_node->sparsityparam())
return;
uint32_t raw_size = (num_elements + 1) / 2;
assert(raw_data.size() == raw_size);
const uint8_t *data = raw_data.data();
const_node->size<DT>(num_elements);
for (uint32_t i = 0; i < raw_size; ++i)
{
uint32_t idx = i * 2;
// for S4, 1bit for sign, 3bit for value
const_node->at<DT>(idx) = static_cast<T>(data[i] << 4) >> 4;
if (idx + 1 < num_elements)
{
const_node->at<DT>(idx + 1) = static_cast<T>(data[i]) >> 4;
}
}
}
} // namespace
namespace luci
{
CircleNode *CircleConstNodeBuilder::build(TensorIndex tensor_index,
GraphBuilderContext *context) const
{
assert(tensor_index >= 0);
LOGGER(l);
auto graph = context->graph();
auto reader = context->reader();
const auto tensors = reader->tensors();
const auto const_tensor = tensors[tensor_index];
assert(const_tensor != nullptr);
if (const_tensor->is_variable())
{
// Create CircleVariable for variable
return nullptr;
}
const auto r_buffers = reader->buffers();
const auto c_buffer = const_tensor->buffer();
const auto r_buffer = r_buffers[c_buffer];
assert(r_buffer != nullptr);
if (r_buffer->offset() == 1 || r_buffer->size() == 1)
{
// NOTE this shouldn't happen
throw std::runtime_error("CircleConst: Circle file with invalid extended Buffer.");
}
// temporary buffer to provide raw data from file
// must have life time same or longer than 'buffer' variable
std::vector<uint8_t> temp_buffer;
luci::VectorWrapper<uint8_t> buffer(nullptr);
if (r_buffer->offset() > 1)
{
if (r_buffer->size() >= std::numeric_limits<uint32_t>::max())
{
// NOTE uint32_t limit is to match "uoffset_t flatbuffers::Vector::size()"
throw std::runtime_error("CircleConst: Circle file with invalid extended Buffer.");
}
uint32_t r_size = static_cast<uint32_t>(r_buffer->size());
// match binary level to flatbuffers::Vector
temp_buffer.resize(r_size + sizeof(uint32_t));
uint8_t *t_data = temp_buffer.data();
const uint8_t *f_data = reader->file_data(r_buffer->offset());
if (f_data == nullptr)
{
// NOTE this shouldn't happen
assert(false);
return nullptr;
}
memcpy(t_data, &r_size, sizeof(r_size));
t_data = t_data + sizeof(r_size);
if (r_buffer->offset() + r_buffer->size() > reader->file_size())
{
// NOTE this shouldn't happen
assert(false);
return nullptr;
}
memcpy(t_data, f_data, r_buffer->size());
using fbv_t = flatbuffers::Vector<uint8_t>;
const fbv_t *v_data = reinterpret_cast<const fbv_t *>(temp_buffer.data());
buffer = wrap(v_data);
context->ext_buffer(true);
}
else
{
buffer = wrap(r_buffer->data());
}
const auto const_dims = wrap(const_tensor->shape()); // in NHWC
if (const_dims.size() == 0 && buffer.empty())
{
// unknown shape tensor and scalar tensor
return nullptr;
}
// if tensor_index is used as output to some other operator, this is not a constant
auto tensoroutputs = context->tensoroutputs();
if (tensoroutputs->find(tensor_index))
{
// other operator output tensor
return nullptr;
}
uint32_t num_elements = 1;
for (uint32_t r = 0; r < const_dims.size(); ++r)
{
num_elements = num_elements * const_dims[r];
}
if (buffer.empty() && num_elements > 0)
{
// normal empty tensor
return nullptr;
}
auto const_node = graph->nodes()->create<CircleConst>();
copy_tensor_attributes(const_tensor, const_node);
const_node->shape_status(luci::ShapeStatus::VALID);
INFO(l) << "[luci] NodeFinder const_node(" << tensor_index << ") -> " << const_node << " "
<< const_dims << std::endl;
if (num_elements > 0)
{
switch (luci_datatype(const_tensor->type()))
{
case loco::DataType::FLOAT32:
copy_data<loco::DataType::FLOAT32>(buffer, num_elements, const_node);
break;
case loco::DataType::FLOAT16:
copy_data<loco::DataType::FLOAT16>(buffer, num_elements, const_node);
break;
case loco::DataType::U4:
copy_data_4<loco::DataType::U4>(buffer, num_elements, const_node);
break;
case loco::DataType::U8:
copy_data<loco::DataType::U8>(buffer, num_elements, const_node);
break;
case loco::DataType::S4:
copy_data_4<loco::DataType::S4>(buffer, num_elements, const_node);
break;
case loco::DataType::S8:
copy_data<loco::DataType::S8>(buffer, num_elements, const_node);
break;
case loco::DataType::S16:
copy_data<loco::DataType::S16>(buffer, num_elements, const_node);
break;
case loco::DataType::S32:
copy_data<loco::DataType::S32>(buffer, num_elements, const_node);
break;
case loco::DataType::S64:
copy_data<loco::DataType::S64>(buffer, num_elements, const_node);
break;
case loco::DataType::BOOL:
copy_data<loco::DataType::BOOL>(buffer, num_elements, const_node);
break;
case loco::DataType::STRING:
copy_data<loco::DataType::STRING>(buffer, num_elements, const_node);
break;
default:
throw oops::UserExn("Unsupported tensor type",
circle::EnumNameTensorType(const_tensor->type()));
}
}
return const_node;
}
} // namespace luci