ONE - On-device Neural Engine
Loading...
Searching...
No Matches
tflread::Reader Class Reference

Loads TF lite file and provides helpers to access attributes. More...

#include <Read.h>

Public Member Functions

 Reader (const tflite::Model *model)
 
 Reader (const tflite::Model *model, const std::vector< char > *rawdata)
 
 Reader ()=delete
 
uint32_t version () const
 
const std::vector< const tflite::OperatorCode * > & opcodes ()
 
const TFliteBuffers_tbuffers ()
 
const TFliteTensors_ttensors ()
 
const TFliteOperators_toperators ()
 
const std::vector< int32_t > & inputs () const
 
const std::vector< int32_t > & outputs () const
 
const TFliteMetadata_tmetadata () const
 
const TFliteSignatureDef_tsignaturedefs () const
 
uint32_t num_subgraph () const
 
size_t buffer_info (uint32_t buf_idx, const uint8_t **buff_data)
 
size_t buffer_info (uint32_t buf_idx, const uint8_t **buff_data, bool &ext_offset)
 
tflite::BuiltinOperator builtin_code (const tflite::Operator *op) const
 
std::string opcode_name (const tflite::Operator *op) const
 
bool select_subgraph (uint32_t subgraph)
 
const std::string & subgraph_name (void) const
 
uint32_t subgraph_index (void) const
 

Detailed Description

Loads TF lite file and provides helpers to access attributes.

Definition at line 42 of file Read.h.

Constructor & Destructor Documentation

◆ Reader() [1/3]

tflread::Reader::Reader ( const tflite::Model *  model)

Definition at line 28 of file Read.cpp.

29{
30 _version = model->version();
31 _subgraphs = model->subgraphs();
32 _buffers = model->buffers();
33 _metadata = model->metadata();
34 _signaturedefs = model->signature_defs();
35
36 auto opcodes = model->operator_codes();
37 for (const ::tflite::OperatorCode *opcode : *opcodes)
38 {
39 _op_codes.push_back(opcode);
40 }
41}
const std::vector< const tflite::OperatorCode * > & opcodes()
Definition Read.h:61

References opcodes().

◆ Reader() [2/3]

tflread::Reader::Reader ( const tflite::Model *  model,
const std::vector< char > *  rawdata 
)

Definition at line 43 of file Read.cpp.

44{
45 _rawdata = rawdata;
46
47 _version = model->version();
48 _subgraphs = model->subgraphs();
49 _buffers = model->buffers();
50 _metadata = model->metadata();
51 _signaturedefs = model->signature_defs();
52
53 auto opcodes = model->operator_codes();
54 for (const ::tflite::OperatorCode *opcode : *opcodes)
55 {
56 _op_codes.push_back(opcode);
57 }
58}

References opcodes().

◆ Reader() [3/3]

tflread::Reader::Reader ( )
delete

Member Function Documentation

◆ buffer_info() [1/2]

size_t tflread::Reader::buffer_info ( uint32_t  buf_idx,
const uint8_t **  buff_data 
)

Definition at line 60 of file Read.cpp.

61{
62 *buff_data = nullptr;
63
64 if (buf_idx == 0)
65 return 0;
66
67 if (auto *buffer = (*_buffers)[buf_idx])
68 {
69 if (auto *array = buffer->data())
70 {
71 if (size_t size = array->size())
72 {
73 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
74 return size;
75 }
76 }
77 }
78
79 return 0;
80}
int32_t size[5]
Definition Slice.cpp:35

References size.

Referenced by tfldump::dump_model().

◆ buffer_info() [2/2]

size_t tflread::Reader::buffer_info ( uint32_t  buf_idx,
const uint8_t **  buff_data,
bool &  ext_offset 
)

Definition at line 82 of file Read.cpp.

83{
84 *buff_data = nullptr;
85 ext_offset = false;
86
87 if (buf_idx == 0)
88 return 0;
89
90 if (auto *buffer = (*_buffers)[buf_idx])
91 {
92 auto buffer_offset = buffer->offset();
93 if (buffer->offset() > 1)
94 {
95 assert(_rawdata); // make debug break for invalid case
96 if (_rawdata == nullptr)
97 return 0;
98
99 ext_offset = true;
100 *buff_data = reinterpret_cast<const uint8_t *>(&_rawdata->at(buffer_offset));
101 return buffer->size();
102 }
103 else if (auto *array = buffer->data())
104 {
105 if (size_t size = array->size())
106 {
107 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
108 return size;
109 }
110 }
111 else
112 {
113 if (buffer->offset() == 1 && buffer->size() == 1)
114 {
115 std::cerr << "Buffer " << buf_idx << " has invalid offset/size." << std::endl;
116 }
117 }
118 }
119
120 return 0;
121}

References size.

◆ buffers()

const TFliteBuffers_t * tflread::Reader::buffers ( )
inline

Definition at line 62 of file Read.h.

62{ return _buffers; }

Referenced by tfldump::dump_model().

◆ builtin_code()

tflite::BuiltinOperator tflread::Reader::builtin_code ( const tflite::Operator *  op) const

Definition at line 123 of file Read.cpp.

124{
125 uint32_t index = op->opcode_index();
126 assert(index < _op_codes.size());
127 const tflite::OperatorCode *opcode = _op_codes.at(index);
128
130}
::tflite::BuiltinOperator builtin_code_neutral(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:33
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54

References mio::tflite::builtin_code_neutral().

Referenced by tfldump::dump_sub_graph().

◆ inputs()

const std::vector< int32_t > & tflread::Reader::inputs ( ) const
inline

◆ metadata()

const TFliteMetadata_t * tflread::Reader::metadata ( ) const
inline

Definition at line 67 of file Read.h.

67{ return _metadata; }

Referenced by tfldump::dump_model().

◆ num_subgraph()

uint32_t tflread::Reader::num_subgraph ( ) const
inline

Definition at line 70 of file Read.h.

70{ return _subgraphs->size(); }
uoffset_t size() const

References flatbuffers::Vector< T >::size().

Referenced by tfldump::dump_model().

◆ opcode_name()

std::string tflread::Reader::opcode_name ( const tflite::Operator *  op) const

Definition at line 132 of file Read.cpp.

133{
134 uint32_t index = op->opcode_index();
135 assert(index < _op_codes.size());
136 const tflite::OperatorCode *opcode = _op_codes.at(index);
137
138 if (!mio::tflite::is_valid(opcode))
139 {
140 std::ostringstream oss;
141 oss << "(invalid: " << index << ")";
142 return oss.str();
143 }
144
145 return mio::tflite::opcode_name(opcode);
146}
std::string opcode_name(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:61
bool is_valid(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:40

References mio::tflite::is_valid(), and mio::tflite::opcode_name().

Referenced by tfldump::dump_sub_graph().

◆ opcodes()

const std::vector< const tflite::OperatorCode * > & tflread::Reader::opcodes ( )
inline

Definition at line 61 of file Read.h.

61{ return _op_codes; }

Referenced by tfldump::dump_model(), Reader(), and Reader().

◆ operators()

const TFliteOperators_t * tflread::Reader::operators ( )
inline

Definition at line 64 of file Read.h.

64{ return _operators; }

Referenced by tfldump::dump_sub_graph().

◆ outputs()

const std::vector< int32_t > & tflread::Reader::outputs ( ) const
inline

◆ select_subgraph()

bool tflread::Reader::select_subgraph ( uint32_t  subgraph)

Definition at line 148 of file Read.cpp.

149{
150 _subgraph_index = sgindex;
151 _tensors = nullptr;
152 _operators = nullptr;
153
154 _inputs.clear();
155 _outputs.clear();
156
157 if (_subgraphs->size() <= sgindex)
158 {
159 assert(false);
160 return false;
161 }
162
163 const tflite::SubGraph *subgraph = (*_subgraphs)[sgindex];
164
165 auto name = subgraph->name();
166 _subgraph_name = name ? name->c_str() : "(noname)";
167
168 _tensors = subgraph->tensors();
169 _operators = subgraph->operators();
170
171 _inputs = as_index_vector(subgraph->inputs());
172 _outputs = as_index_vector(subgraph->outputs());
173
174 return true;
175}
std::vector< T > as_index_vector(const flatbuffers::Vector< T > *flat_array)
Definition Read.h:29

References tflread::as_index_vector(), and flatbuffers::Vector< T >::size().

Referenced by tfldump::dump_model().

◆ signaturedefs()

const TFliteSignatureDef_t * tflread::Reader::signaturedefs ( ) const
inline

Definition at line 68 of file Read.h.

68{ return _signaturedefs; }

Referenced by tfldump::dump_model().

◆ subgraph_index()

uint32_t tflread::Reader::subgraph_index ( void  ) const
inline

Definition at line 80 of file Read.h.

80{ return _subgraph_index; }

Referenced by tfldump::dump_sub_graph().

◆ subgraph_name()

const std::string & tflread::Reader::subgraph_name ( void  ) const
inline

Definition at line 79 of file Read.h.

79{ return _subgraph_name; }

Referenced by tfldump::dump_model().

◆ tensors()

const TFliteTensors_t * tflread::Reader::tensors ( )
inline

Definition at line 63 of file Read.h.

63{ return _tensors; }

Referenced by tfldump::dump_sub_graph().

◆ version()

uint32_t tflread::Reader::version ( ) const
inline

Definition at line 59 of file Read.h.

59{ return _version; }

Referenced by tfldump::dump_model().


The documentation for this class was generated from the following files: