31#include <mio/circle/schema_generated.h>
35#include <unordered_map>
44 for (uint32_t n = 0; n <
graph->inputs()->
size(); ++n)
47 assert(node !=
nullptr);
54 for (uint32_t n = 0; n <
graph->outputs()->
size(); ++n)
57 assert(push !=
nullptr);
58 auto node = push->from();
59 assert(node !=
nullptr);
80encodeOperatorCodes(
FlatBufferBuilder &builder, std::unordered_map<luci::OpCode, uint32_t> &opcodes)
82 std::vector<Offset<OperatorCode>> operator_codes_vec(opcodes.size());
83 for (
const auto &it : opcodes)
85 uint32_t idx = it.second;
86 int8_t dep_code = 127;
87 if (it.first.opcode < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES)
88 dep_code =
static_cast<int8_t
>(it.first.opcode);
89 if (it.first.opcode != BuiltinOperator_CUSTOM)
91 operator_codes_vec[idx] =
92 CreateOperatorCode(builder, dep_code, 0, it.first.version, it.first.opcode);
96 operator_codes_vec[idx] =
97 CreateOperatorCode(builder, dep_code, builder.
CreateString(it.first.custom_code),
98 it.first.version, it.first.opcode);
115 phase.emplace_back(std::make_unique<luci::CircleShapeInferencePass>());
116 phase.emplace_back(std::make_unique<luci::CircleTypeInferencePass>());
124 phase_runner.attach(&prog);
125 phase_runner.run(phase);
146 auto subgraph = CreateSubGraph(_builder, tensors, inputs, outputs, operators, name);
150void CircleExporterImpl::exportModule(Module *module)
152 assert(module->size() > 0);
155 SerializedModelData md;
163 md._ext_buffer =
module->ext_buffer();
165 if (!exportModuleData(module, md) && md._require_ext_buffer)
167 assert(md._ext_buffer ==
false);
171 for (
size_t g = 0; g <
module->size(); ++g)
173 auto graph =
module->graph(g);
179 md._ext_buffer =
true;
180 exportModuleData(module, md);
183 finalizeWithExtendedBuffer(md);
186bool CircleExporterImpl::exportModuleData(Module *module, SerializedModelData &md)
188 std::vector<flatbuffers::Offset<circle::SubGraph>> subgraph_vec;
190 for (
size_t g = 0;
g <
module->size(); ++
g)
192 auto graph =
module->graph(g);
196 SerializedGraphData gd;
205 registerGraphInputTensors(graph, gd);
206 registerGraphOutputTensors(graph, gd);
212 subgraph_vec.push_back(subgraph);
218 auto operator_codes = encodeOperatorCodes(_builder, md._operator_codes);
221 std::string description_str =
"ONE-luci/export";
222 auto description = _builder.
CreateString(description_str);
225 md._metadata.source_table(
module->source_table());
235 md._require_ext_buffer =
true;
240 constexpr uint32_t version = 0;
243 auto model_offset = CreateModel(_builder, version, operator_codes, subgraphs, description,
244 buffers, 0 , metadata);
245 FinishModelBuffer(_builder, model_offset);
250void CircleExporterImpl::finalizeWithExtendedBuffer(SerializedModelData &md)
252 _ext_buffer = md._ext_buffer;
256 _fb_data_with_ext.clear();
258 auto align16 = [](
size_t &v) {
264 size_t result_size = _builder.
GetSize();
265 align16(result_size);
266 for (
auto &it : md._buffer_data_map)
269 result_size += buffer_data.size();
270 align16(result_size);
272 align16(result_size);
276 const char *buff_ptr =
reinterpret_cast<const char *
>(_builder.
GetBufferPointer());
278 auto padalign16 = [](std::string &
str) {
279 while (
str.size() % 16 != 0)
283 result.reserve(result_size);
286 auto mutable_model = circle::GetMutableModel(
result.data());
287 auto mutable_buffers = mutable_model->mutable_buffers();
291 for (
auto &it : md._buffer_data_map)
293 int32_t buffer_index = it.first;
296 uint64_t
size = buffer_data.size();
298 circle::Buffer *mutable_buffer = mutable_buffers->GetMutableObject(buffer_index);
299 mutable_buffer->mutate_offset(
offset);
300 mutable_buffer->mutate_size(
size);
302 result.append(buffer_data.begin(), buffer_data.end());
308 _fb_data_with_ext =
result;
314 return reinterpret_cast<const char *
>(_fb_data_with_ext.data());
321 return _fb_data_with_ext.size();
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
uoffset_t GetSize() const
The current size of the serialized buffer, counting from the end.
uint8_t * GetBufferPointer() const
Get the serialized buffer (after you call Finish()).
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
void Clear()
Reset all the state in this FlatBufferBuilder so it can be reused to construct another buffer.
CircleExporterImpl()=delete
const char * getBufferPointer() const
size_t getBufferSize() const
CircleOutputExclude is used to specifying not exported nodes.
Collection of 'loco::Graph's.
__global uchar * offset(const Image *img, int x, int y)
void optimize(loco::Graph *g)
Run passes for a graph after completion of converting canonical nodes into TFL nodes.
std::vector< std::unique_ptr< Pass > > Phase
bool check_size_limit(const flatbuffers::FlatBufferBuilder &fb, const uint64_t data_size)
void exportNodes(loco::Graph *g, flatbuffers::FlatBufferBuilder &builder, SerializedModelData &md, SerializedGraphData &gd)
create Operators corresponding to model nodes
void exportOpDefinedTensors(loco::Graph *g, FlatBufferBuilder &builder, SerializedModelData &md, SerializedGraphData &gd)
create Tensors corresponding to results of all nodes in graph
void prepareModelData(FlatBufferBuilder &builder, SerializedModelData &md)
one time preparation for SerializedModelData
CircleTensorIndex get_tensor_index(loco::Node *node)
CircleOutput * output_node(loco::Graph *g, const loco::GraphOutputIndex &index)
Find a CircleOutput node with a given output index.
CircleInput * input_node(loco::Graph *g, const loco::GraphInputIndex &index)
Find a Pull node with a given input index.
void clearExportInfo(loco::Graph *g)
clear temporary export information annotated to graph nodes
std::vector< flatbuffers::Offset< circle::Metadata > > createCircleMetadataVector(flatbuffers::FlatBufferBuilder &builder, luci::SerializedModelData &md)
Create Metadata corresponding to model metadata.
std::vector< flatbuffers::Offset< circle::Operator > > _operators
std::vector< flatbuffers::Offset< circle::Tensor > > _tensors
std::vector< uint8_t > BufferData
Record the information of T/F Lite SubGraph and its mapping to loco.
std::string _name
SubGraph name.
std::vector< int32_t > _inputs
SubGraph input tensor id.
std::vector< int32_t > _outputs
SubGraph output tensor id.