73 std::unique_ptr<ModelRecipe> model_recipe{
new ModelRecipe()};
80 for (uint32_t n = 0; n < num_subgraph; ++n)
84 auto tensors = tflite_import.
tensors();
85 auto buffers = tflite_import.
buffers();
86 auto operators = tflite_import.
operators();
88 tflchef::Graph *graph =
nullptr;
90 graph = model_recipe->add_graph();
93 for (uint32_t i = 0; i < operators->size(); ++i)
95 const auto *op = operators->Get(i);
96 tflite::BuiltinOperator builtincode = tflite_import.
builtin_code(op);
100 graph_builder->filler(op, &tflite_import, model_recipe.get());
104 std::string opcodename = tflite_import.
opcode_name(op);
105 throw std::runtime_error{
"Not supported: " + opcodename};
110 for (uint32_t i = 0; i < tensors->size(); ++i)
112 auto tensor = tensors->Get(i);
115 if (tensor->buffer() >= buffers->size())
116 throw std::runtime_error{
"file load failed"};
118 ::tflchef::Operand *operand;
119 if (graph !=
nullptr)
120 operand = graph->add_operand();
122 operand = model_recipe->add_operand();
125 operand->set_is_variable(tensor->is_variable());
130 ::tflchef::TensorShape *shape = operand->mutable_shape();
131 for (
auto dim : dims)
138 std::vector<int32_t> expvalues;
139 std::vector<float> expfvalues;
142 tflchef::TensorFiller *filler = operand->mutable_filler();
144 filler->set_tag(
"gaussian");
145 filler->add_arg(
"0.0");
146 filler->add_arg(
"0.1");
150 tflchef::TensorFiller *filler = operand->mutable_filler();
151 filler->set_tag(
"explicit");
152 for (
auto value : expvalues)
154 std::ostringstream ss;
156 filler->add_arg(ss.str());
161 tflchef::TensorFiller *filler = operand->mutable_filler();
162 filler->set_tag(
"explicit");
163 for (
auto value : expfvalues)
165 std::ostringstream ss;
167 filler->add_arg(ss.str());
171 auto quant = tensor->quantization();
172 if (quant !=
nullptr)
176 if (quant->min() !=
nullptr && quant->min()->size() > 0)
178 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
179 for (uint32_t idx = 0; idx < quant->min()->size(); ++idx)
180 chef_quant->add_min(quant->min()->Get(idx));
182 if (quant->max() !=
nullptr && quant->max()->size() > 0)
184 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
185 for (uint32_t idx = 0; idx < quant->max()->size(); idx++)
186 chef_quant->add_max(quant->max()->Get(idx));
188 if (quant->scale() !=
nullptr && quant->scale()->size() > 0)
190 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
191 for (uint32_t idx = 0; idx < quant->scale()->size(); ++idx)
192 chef_quant->add_scale(quant->scale()->Get(idx));
194 if (quant->zero_point() !=
nullptr && quant->zero_point()->size() > 0)
196 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
197 for (uint32_t idx = 0; idx < quant->zero_point()->size(); ++idx)
198 chef_quant->add_zero_point(quant->zero_point()->Get(idx));
200 tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
201 chef_quant->set_quantized_dimension(quant->quantized_dimension());
207 tflchef::TensorSparsity *chef_sparsity = operand->mutable_sparsity();
209 auto chef_traversal_order = chef_sparsity->mutable_traversal_order();
210 for (
const auto &to : *(
sparsity->traversal_order()))
212 chef_traversal_order->add_dim(to);
215 auto chef_block_map = chef_sparsity->mutable_block_map();
216 for (
const auto &bm : *(
sparsity->block_map()))
218 chef_block_map->add_dim(bm);
221 for (
const auto &dm : *(
sparsity->dim_metadata()))
223 auto chef_dm = chef_sparsity->add_dim_metadata();
227 chef_dm->set_dense_size(dm->dense_size());
229 auto chef_array_segments = chef_dm->mutable_array_segments();
230 switch (dm->array_segments_type())
232 case tflite::SparseIndexVector_NONE:
235 case tflite::SparseIndexVector_Int32Vector:
236 for (
const auto &as : *(dm->array_segments_as_Int32Vector()->values()))
238 chef_array_segments->add_dim(as);
241 case tflite::SparseIndexVector_Uint16Vector:
242 for (
const auto &as : *(dm->array_segments_as_Uint16Vector()->values()))
244 chef_array_segments->add_dim(as);
247 case tflite::SparseIndexVector_Uint8Vector:
248 for (
const auto &as : *(dm->array_segments_as_Uint8Vector()->values()))
250 chef_array_segments->add_dim(as);
254 throw std::runtime_error(
"unsupported sparse index vector type");
257 auto chef_array_indices = chef_dm->mutable_array_indices();
258 switch (dm->array_indices_type())
260 case tflite::SparseIndexVector_NONE:
263 case tflite::SparseIndexVector_Int32Vector:
264 for (
const auto &as : *(dm->array_indices_as_Int32Vector()->values()))
266 chef_array_indices->add_dim(as);
269 case tflite::SparseIndexVector_Uint16Vector:
270 for (
const auto &as : *(dm->array_indices_as_Uint16Vector()->values()))
272 chef_array_indices->add_dim(as);
275 case tflite::SparseIndexVector_Uint8Vector:
276 for (
const auto &as : *(dm->array_indices_as_Uint8Vector()->values()))
278 chef_array_indices->add_dim(as);
282 throw std::runtime_error(
"unsupported sparse index vector type");
287 auto shape_signature = tensor->shape_signature();
288 if (shape_signature !=
nullptr)
290 tflchef::ShapeSignature *chef_shape_signature = operand->mutable_shape_signature();
291 for (uint32_t j = 0; j < shape_signature->size(); ++j)
293 chef_shape_signature->add_dim(shape_signature->Get(j));
299 for (uint32_t i = 0; i < operators->size(); ++i)
301 const auto *op = operators->Get(i);
302 tflite::BuiltinOperator builtincode = tflite_import.
builtin_code(op);
306 tflchef::Operation *operation =
307 graph ? graph->add_operation() : model_recipe->add_operation();
310 graph_builder->build(&ctx);
318 std::string opcodename = tflite_import.
opcode_name(op);
319 throw std::runtime_error{
"Not supported: " + opcodename};
324 const std::vector<int32_t> &inputs = tflite_import.
inputs();
325 const std::vector<int32_t> &outputs = tflite_import.
outputs();
327 for (
const auto input : inputs)
329 auto tensor = tensors->Get(input);
332 if (graph !=
nullptr)
333 graph->add_input(name);
335 model_recipe->add_input(name);
337 for (
const auto output : outputs)
339 auto tensor = tensors->Get(output);
342 if (graph !=
nullptr)
343 graph->add_output(name);
345 model_recipe->add_output(name);
349 return std::move(model_recipe);