150 assert(tensor_index >= 0);
153 auto graph = context->
graph();
154 auto reader = context->
reader();
155 const auto tensors = reader->
tensors();
156 const auto const_tensor = tensors[tensor_index];
157 assert(const_tensor !=
nullptr);
158 if (const_tensor->is_variable())
164 const auto r_buffers = reader->buffers();
165 const auto c_buffer = const_tensor->buffer();
166 const auto r_buffer = r_buffers[c_buffer];
167 assert(r_buffer !=
nullptr);
168 if (r_buffer->offset() == 1 || r_buffer->size() == 1)
171 throw std::runtime_error(
"CircleConst: Circle file with invalid extended Buffer.");
175 std::vector<uint8_t> temp_buffer;
177 if (r_buffer->offset() > 1)
179 if (r_buffer->size() >= std::numeric_limits<uint32_t>::max())
182 throw std::runtime_error(
"CircleConst: Circle file with invalid extended Buffer.");
184 uint32_t r_size =
static_cast<uint32_t
>(r_buffer->size());
186 temp_buffer.resize(r_size +
sizeof(uint32_t));
188 uint8_t *t_data = temp_buffer.data();
189 const uint8_t *f_data = reader->file_data(r_buffer->offset());
190 if (f_data ==
nullptr)
196 memcpy(t_data, &r_size,
sizeof(r_size));
197 t_data = t_data +
sizeof(r_size);
198 if (r_buffer->offset() + r_buffer->size() > reader->file_size())
204 memcpy(t_data, f_data, r_buffer->size());
207 const fbv_t *v_data =
reinterpret_cast<const fbv_t *
>(temp_buffer.data());
208 buffer =
wrap(v_data);
214 buffer =
wrap(r_buffer->data());
216 const auto const_dims =
wrap(const_tensor->shape());
217 if (const_dims.size() == 0 && buffer.
empty())
225 if (tensoroutputs->find(tensor_index))
231 uint32_t num_elements = 1;
232 for (uint32_t r = 0; r < const_dims.size(); ++r)
234 num_elements = num_elements * const_dims[r];
237 if (buffer.
empty() && num_elements > 0)
243 auto const_node = graph->nodes()->create<
CircleConst>();
246 INFO(l) <<
"[luci] NodeFinder const_node(" << tensor_index <<
") -> " << const_node <<
" "
247 << const_dims << std::endl;
248 if (num_elements > 0)
252 case loco::DataType::FLOAT32:
253 copy_data<loco::DataType::FLOAT32>(buffer, num_elements, const_node);
256 case loco::DataType::FLOAT16:
257 copy_data<loco::DataType::FLOAT16>(buffer, num_elements, const_node);
260 case loco::DataType::U4:
261 copy_data_4<loco::DataType::U4>(buffer, num_elements, const_node);
264 case loco::DataType::U8:
265 copy_data<loco::DataType::U8>(buffer, num_elements, const_node);
268 case loco::DataType::S4:
269 copy_data_4<loco::DataType::S4>(buffer, num_elements, const_node);
272 case loco::DataType::S8:
273 copy_data<loco::DataType::S8>(buffer, num_elements, const_node);
276 case loco::DataType::S16:
277 copy_data<loco::DataType::S16>(buffer, num_elements, const_node);
280 case loco::DataType::S32:
281 copy_data<loco::DataType::S32>(buffer, num_elements, const_node);
284 case loco::DataType::S64:
285 copy_data<loco::DataType::S64>(buffer, num_elements, const_node);
288 case loco::DataType::BOOL:
289 copy_data<loco::DataType::BOOL>(buffer, num_elements, const_node);
292 case loco::DataType::STRING:
293 copy_data<loco::DataType::STRING>(buffer, num_elements, const_node);
298 circle::EnumNameTensorType(const_tensor->type()));
Class to store context to build loco graph IR from TensorFlow.
IndexTensorOutputs * tensoroutputs()