ONE - On-device Neural Engine
Loading...
Searching...
No Matches
exo::tflite_detail Namespace Reference

Data Structures

struct  OpCode
 
struct  SerializedModelData
 
struct  SubGraphContext
 Record the information of T/F Lite SubGraph and its mapping to loco. More...
 

Typedefs

using TFLTensorIndex = int32_t
 

Functions

tflite::Padding getOpPadding (const loco::Padding2D *pad, const loco::Stride< 2 > *stride, const ShapeDescription &ifm, const ShapeDescription &ofm)
 
tflite::Padding getOpPadding (const locoex::Padding pad)
 
void registerGraphIOName (loco::Graph *graph, SerializedModelData &gd)
 Register graph input and output names to SerializedModelData.
 
void set_tensor_index (loco::Node *node, const TFLTensorIndex &tensor_id)
 
TFLTensorIndex get_tensor_index (loco::Node *node)
 
void exportOpDefinedTensor (const TFLTensorInfo &info, FlatBufferBuilder &builder, SerializedModelData &gd)
 
void exportOpDefinedTensors (loco::Graph *g, flatbuffers::FlatBufferBuilder &builder, SerializedModelData &gd)
 create Tensors corresponding to results of all nodes in graph
 

Typedef Documentation

◆ TFLTensorIndex

using exo::tflite_detail::TFLTensorIndex = typedef int32_t

Definition at line 110 of file TFLExporterUtils.h.

Function Documentation

◆ exportOpDefinedTensor()

void exo::tflite_detail::exportOpDefinedTensor ( const TFLTensorInfo &  info,
FlatBufferBuilder builder,
SerializedModelData gd 
)

Definition at line 209 of file TFLTensorExporter.cpp.

211{
212 // Create and register output tensor shape
213 auto shape_offset = encodeShape(builder, info.shape());
214
215 // encode and register output tensor buffer
216 auto buffer = info.tfl_content() == nullptr ? encodeOpBuffer(builder)
217 : encodeOpBuffer(builder, info.tfl_content());
218
219 auto buffer_id = static_cast<uint32_t>(gd._buffers.size());
220 gd._buffers.push_back(buffer);
221
222 auto name_offset = builder.CreateString(info.name());
223 auto tensor_offset = CreateTensor(builder, shape_offset, info.dtype(), buffer_id, name_offset,
224 /*quantization*/ 0, /*is_variable*/ false);
225 gd._tensors.push_back(tensor_offset);
226}
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
volatile const char info[]
std::vector< flatbuffers::Offset< tflite::Tensor > > _tensors
std::vector< flatbuffers::Offset< tflite::Buffer > > _buffers

References exo::tflite_detail::SerializedModelData::_buffers, exo::tflite_detail::SerializedModelData::_tensors, flatbuffers::FlatBufferBuilder::CreateString(), and info.

Referenced by exportOpDefinedTensors().

◆ exportOpDefinedTensors()

void exo::tflite_detail::exportOpDefinedTensors ( loco::Graph g,
flatbuffers::FlatBufferBuilder builder,
SerializedModelData gd 
)

create Tensors corresponding to results of all nodes in graph

Parameters
computationalgraph
gdinformation about serialized parts of model

Definition at line 228 of file TFLTensorExporter.cpp.

229{
230 TFLTensorContext tensor_ctx;
231
232 for (auto node : loco::postorder_traversal(loco::output_nodes(g)))
233 {
234 allocateTFLiteTensor(node, tensor_ctx);
235 }
236
237 // add one empty buffer
238 // note: there's a comment in tflite fbs file
239 // - Note the 0th entry of this array must be an empty buffer (sentinel).
240 // - This is a convention so that tensors without a buffer can provide 0 as
241 // - their buffer.
242 auto buffer = encodeOpBuffer(builder);
243 gd._buffers.push_back(buffer);
244
245 for (const auto &tensor_info : tensor_ctx)
246 {
247 exportOpDefinedTensor(tensor_info, builder, gd);
248 }
249}
void exportOpDefinedTensor(const TFLTensorInfo &info, FlatBufferBuilder &builder, SerializedModelData &gd)

References exo::tflite_detail::SerializedModelData::_buffers, exportOpDefinedTensor(), loco::output_nodes(), and loco::postorder_traversal().

◆ get_tensor_index()

TFLTensorIndex exo::tflite_detail::get_tensor_index ( loco::Node node)

Definition at line 152 of file TFLExporterUtils.cpp.

153{
154 assert(node->annot<TFLTensorIndexAnnotation>() != nullptr);
155 return node->annot<TFLTensorIndexAnnotation>()->index();
156}
const T * annot(void) const
Retrieve a stored annotation of type T.

References loco::AnnotatedItem< Annotation >::annot().

◆ getOpPadding() [1/2]

tflite::Padding exo::tflite_detail::getOpPadding ( const loco::Padding2D pad,
const loco::Stride< 2 > *  stride,
const ShapeDescription ifm,
const ShapeDescription ofm 
)

Definition at line 66 of file TFLExporterUtils.cpp.

68{
69 // VALID padding
70 if (pad->top() == 0 && pad->bottom() == 0 && pad->left() == 0 && pad->right() == 0)
71 return tflite::Padding_VALID;
72
73 // SAME padding
74 //
75 // For same padding, by definition, following equation should hold:
76 // O = floor((I - 1) / S) + 1
77 // where input size I, output size O, stride S
78 //
79 // NOTE input and output 'feature' map are shape of NHWC
80 bool same_padding_criterion_1 =
81 (static_cast<uint32_t>(ofm._dims[1]) == (ifm._dims[1] - 1) / stride->vertical() + 1) &&
82 (static_cast<uint32_t>(ofm._dims[2]) == (ifm._dims[2] - 1) / stride->horizontal() + 1);
83
84 // For same padding, rear padding is same or bigger than front padding by at most 1
85 bool same_padding_criterion_2 =
86 (pad->top() <= pad->bottom()) && (pad->bottom() <= pad->top() + 1) &&
87 (pad->left() <= pad->right()) && (pad->right() <= pad->left() + 1);
88
89 if (same_padding_criterion_1 && same_padding_criterion_2)
90 return tflite::Padding_SAME;
91
92 INTERNAL_EXN("NYI for custom PAD");
93}
#define INTERNAL_EXN(msg)
@ brief throw internal exception with message
Definition InternalExn.h:25
uint32_t left(void) const
Definition Padding2D.h:49
uint32_t top(void) const
Definition Padding2D.h:41
uint32_t bottom(void) const
Definition Padding2D.h:45
uint32_t right(void) const
Definition Padding2D.h:53
uint32_t horizontal(void) const
Definition Stride.h:40
uint32_t vertical(void) const
Definition Stride.h:36
std::vector< int32_t > _dims

References exo::ShapeDescription::_dims, loco::Padding2D::bottom(), INTERNAL_EXN, loco::Padding2D::left(), loco::Padding2D::right(), and loco::Padding2D::top().

◆ getOpPadding() [2/2]

tflite::Padding exo::tflite_detail::getOpPadding ( const locoex::Padding  pad)

Definition at line 95 of file TFLExporterUtils.cpp.

96{
97 if (pad == locoex::Padding::VALID)
98 return tflite::Padding_VALID;
99 if (pad == locoex::Padding::SAME)
100 return tflite::Padding_SAME;
101
102 INTERNAL_EXN_V("Unknown padding", oops::to_uint32(pad));
103}
#define INTERNAL_EXN_V(msg, val)
@ brief throw internal exception with message and value
Definition InternalExn.h:28
uint32_t to_uint32(T a)
Definition InternalExn.h:33

References INTERNAL_EXN_V, locoex::SAME, oops::to_uint32(), and locoex::VALID.

◆ registerGraphIOName()

void exo::tflite_detail::registerGraphIOName ( loco::Graph graph,
SerializedModelData gd 
)

Register graph input and output names to SerializedModelData.

Definition at line 105 of file TFLExporterUtils.cpp.

106{
107 for (uint32_t in = 0; in < graph->inputs()->size(); ++in)
108 {
109 auto pull = loco::pull_node(graph, in);
110 auto name = graph->inputs()->at(in)->name();
111
112 gd._pull_to_name[pull] = name;
113 }
114 for (uint32_t out = 0; out < graph->outputs()->size(); ++out)
115 {
116 auto push = loco::push_node(graph, out);
117 auto name = graph->outputs()->at(out)->name();
118
119 gd._push_to_name[push] = name;
120 }
121}
Pull * pull_node(Graph *g, const GraphInputIndex &index)
Find a Pull node with a given input index.
Definition Nodes.cpp:162
Push * push_node(Graph *g, const GraphOutputIndex &index)
Find a Push node with a given output index.
Definition Nodes.cpp:67
int32_t size[5]
Definition Slice.cpp:35
std::unordered_map< loco::Pull *, std::string > _pull_to_name
std::unordered_map< loco::Push *, std::string > _push_to_name

References exo::tflite_detail::SerializedModelData::_pull_to_name, exo::tflite_detail::SerializedModelData::_push_to_name, loco::pull_node(), loco::push_node(), and size.

◆ set_tensor_index()

void exo::tflite_detail::set_tensor_index ( loco::Node node,
const TFLTensorIndex tensor_id 
)

Definition at line 146 of file TFLExporterUtils.cpp.

147{
148 assert(node->annot<TFLTensorIndexAnnotation>() == nullptr);
149 node->annot(std::make_unique<TFLTensorIndexAnnotation>(tensor_id));
150}

References loco::AnnotatedItem< Annotation >::annot().