32#include <unordered_map>
43 for (uint32_t n = 0; n <
graph->inputs()->
size(); ++n)
46 assert(node !=
nullptr);
53 for (uint32_t n = 0; n <
graph->outputs()->
size(); ++n)
56 assert(push !=
nullptr);
57 auto node = push->from();
58 assert(node !=
nullptr);
72encodeOperatorCodes(
FlatBufferBuilder &builder, std::unordered_map<OpCode, uint32_t> &opcodes,
73 std::unordered_map<OpCode, std::string> &custom_opcodes)
75 std::vector<Offset<OperatorCode>> operator_codes_vec(opcodes.size());
76 for (
auto it : opcodes)
78 uint32_t idx = it.second;
79 if (it.first.opcode != BuiltinOperator_CUSTOM)
81 operator_codes_vec[idx] = CreateOperatorCode(builder, it.first.opcode);
85 auto opCode = it.first;
86 auto custom_code = custom_opcodes.find(opCode);
87 if (custom_code == custom_opcodes.end())
88 INTERNAL_EXN(
"Cannot find code for customop even though opcode is BuiltinOperator_CUSTOM");
90 operator_codes_vec[idx] =
91 CreateOperatorCode(builder, it.first.opcode, builder.
CreateString(custom_code->second));
111 auto tensors = _builder.CreateVector(gd.
_tensors);
112 auto inputs = _builder.CreateVector(gd.
_inputs);
113 auto outputs = _builder.CreateVector(gd.
_outputs);
114 auto operators = _builder.CreateVector(gd.
_operators);
116 auto subgraph = CreateSubGraph(_builder, tensors, inputs, outputs, operators, df);
120void CircleExporter::Impl::exportGraph(
loco::Graph *graph)
136 constexpr uint32_t version = 0;
144 registerGraphInputTensors(graph, gd);
145 registerGraphOutputTensors(graph, gd);
147 exportNodes(graph, _builder, gd);
150 auto operator_codes =
155 auto subgraphs = _builder.CreateVector(std::vector<
Offset<SubGraph>>{subgraph});
158 std::string description_str =
"nnpackage";
159 auto description = _builder.CreateString(description_str);
162 auto buffers = _builder.CreateVector(gd.
_buffers);
165 std::vector<int> metadata_buffer_vec;
166 auto metadata_buffer = _builder.CreateVector(metadata_buffer_vec);
169 auto model_offset = CreateModel(_builder, version, operator_codes, subgraphs, description,
170 buffers, metadata_buffer);
171 FinishModelBuffer(_builder, model_offset);
176 return reinterpret_cast<const char *
>(_builder.GetBufferPointer());
#define INTERNAL_EXN(msg)
@ brief throw internal exception with message
const char * getBufferPointer() const
size_t getBufferSize() const
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.
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
void exportOpDefinedTensors(loco::Graph *g, FlatBufferBuilder &builder, SerializedModelData &gd)
create Tensors corresponding to results of all nodes in graph
void registerGraphIOName(loco::Graph *graph, SerializedModelData &gd)
Register graph input and output names to SerializedModelData.
TFLTensorIndex get_tensor_index(loco::Node *node)
void convert_to_TFLNodes(loco::Graph *graph)
void optimize(loco::Graph *g)
Run passes for a graph after completion of converting canonical nodes into TFL nodes.
Pull * pull_node(Graph *g, const GraphInputIndex &index)
Find a Pull node with a given input index.
Push * push_node(Graph *g, const GraphOutputIndex &index)
Find a Push node with a given output index.
std::unordered_map< OpCode, std::string > _custom_operator_codes
std::vector< flatbuffers::Offset< circle::Tensor > > _tensors
std::vector< flatbuffers::Offset< circle::Operator > > _operators
std::vector< flatbuffers::Offset< circle::Buffer > > _buffers
std::unordered_map< OpCode, uint32_t > _operator_codes
Record the information of T/F Lite SubGraph and its mapping to loco.
std::vector< int32_t > _outputs
SubGraph output tensor id.
circle::DataFormat _data_format
@DataFormat for SubGraph
std::vector< int32_t > _inputs
SubGraph input tensor id.