73 std::unique_ptr<ModelRecipe> model_recipe{
new ModelRecipe()};
80 auto tensors = circle_import.
tensors();
81 auto buffers = circle_import.
buffers();
82 auto operators = circle_import.
operators();
85 for (uint32_t i = 0; i < operators->size(); ++i)
87 const auto *op = operators->
Get(i);
88 circle::BuiltinOperator builtincode = circle_import.
builtin_code(op);
92 graph_builder->filler(op, &circle_import, model_recipe.get());
96 std::string opcodename = circle_import.
opcode_name(op);
97 throw std::runtime_error{
"Not supported: " + opcodename};
102 for (uint32_t i = 0; i < tensors->size(); ++i)
104 auto tensor = tensors->Get(i);
107 if (tensor->buffer() >= buffers->size())
108 throw std::runtime_error{
"file load failed"};
110 ::circlechef::Operand *operand = model_recipe->add_operand();
116 ::circlechef::TensorShape *shape = operand->mutable_shape();
117 for (
auto dim : dims)
123 std::vector<int32_t> expvalues;
124 std::vector<float> expfvalues;
127 circlechef::TensorFiller *filler = operand->mutable_filler();
129 filler->set_tag(
"gaussian");
130 filler->add_arg(
"0.0");
131 filler->add_arg(
"0.1");
135 circlechef::TensorFiller *filler = operand->mutable_filler();
136 filler->set_tag(
"explicit");
137 for (
auto value : expvalues)
139 std::ostringstream ss;
141 filler->add_arg(ss.str());
146 circlechef::TensorFiller *filler = operand->mutable_filler();
147 filler->set_tag(
"explicit");
148 for (
auto value : expfvalues)
150 std::ostringstream ss;
152 filler->add_arg(ss.str());
156 auto quant = tensor->quantization();
157 if (quant !=
nullptr)
161 if (quant->min() !=
nullptr && quant->min()->size() > 0)
163 circlechef::TensorQuantization *chef_quant = operand->mutable_quant();
164 for (uint32_t idx = 0; idx < quant->min()->size(); ++idx)
165 chef_quant->add_min(quant->min()->Get(idx));
167 if (quant->max() !=
nullptr && quant->max()->size() > 0)
169 circlechef::TensorQuantization *chef_quant = operand->mutable_quant();
170 for (uint32_t idx = 0; idx < quant->max()->size(); idx++)
171 chef_quant->add_max(quant->max()->Get(idx));
173 if (quant->scale() !=
nullptr && quant->scale()->size() > 0)
175 circlechef::TensorQuantization *chef_quant = operand->mutable_quant();
176 for (uint32_t idx = 0; idx < quant->scale()->size(); ++idx)
177 chef_quant->add_scale(quant->scale()->Get(idx));
179 if (quant->zero_point() !=
nullptr && quant->zero_point()->size() > 0)
181 circlechef::TensorQuantization *chef_quant = operand->mutable_quant();
182 for (uint32_t idx = 0; idx < quant->zero_point()->size(); ++idx)
183 chef_quant->add_zero_point(quant->zero_point()->Get(idx));
185 circlechef::TensorQuantization *chef_quant = operand->mutable_quant();
186 chef_quant->set_quantized_dimension(quant->quantized_dimension());
189 auto shape_signature = tensor->shape_signature();
190 if (shape_signature !=
nullptr)
192 circlechef::ShapeSignature *chef_shape_signature = operand->mutable_shape_signature();
193 for (uint32_t i = 0; i < shape_signature->size(); ++i)
195 chef_shape_signature->add_dim(shape_signature->Get(i));
201 for (uint32_t i = 0; i < operators->size(); ++i)
203 const auto *op = operators->Get(i);
204 circle::BuiltinOperator builtincode = circle_import.
builtin_code(op);
208 auto operation = graph_builder->build(op, &circle_import, model_recipe.get());
216 std::string opcodename = circle_import.
opcode_name(op);
217 throw std::runtime_error{
"Not supported: " + opcodename};
222 const std::vector<int32_t> &inputs = circle_import.
inputs();
223 const std::vector<int32_t> &outputs = circle_import.
outputs();
225 for (
const auto input : inputs)
227 auto tensor = tensors->Get(input);
230 model_recipe->add_input(name);
232 for (
const auto output : outputs)
234 auto tensor = tensors->Get(output);
237 model_recipe->add_output(name);
240 return std::move(model_recipe);