ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::odc::MinMaxReader Class Reference

#include <MinMaxReader.h>

Public Member Functions

 MinMaxReader (const std::string &filepath)
 
MinMaxVectors readOP (uint32_t model_idx, uint32_t subg_idx, uint32_t op_idx) const
 Returns minmax recording for op {model_idx, subg_idx, op_idx}.
 
MinMaxVectors readInput (uint32_t model_idx, uint32_t subg_idx, uint32_t input_idx) const
 Returns minmax recording for input {model_idx, subg_idx, input_idx}.
 
uint32_t readNumRuns () const
 Returns minmax recording count.
 

Detailed Description

Definition at line 58 of file MinMaxReader.h.

Constructor & Destructor Documentation

◆ MinMaxReader()

onert::odc::MinMaxReader::MinMaxReader ( const std::string &  filepath)

Definition at line 80 of file MinMaxReader.cc.

80 : _filepath(filepath)
81{
82 // DO NOTHING
83}

Member Function Documentation

◆ readInput()

MinMaxVectors onert::odc::MinMaxReader::readInput ( uint32_t  model_idx,
uint32_t  subg_idx,
uint32_t  input_idx 
) const

Returns minmax recording for input {model_idx, subg_idx, input_idx}.

Returns
MinMaxVectors

Definition at line 162 of file MinMaxReader.cc.

164{
165 // Find file to read
166 auto file = std::fopen(_filepath.c_str(), "rb");
167 if (!file)
168 throw std::runtime_error("Cannot open file: " + _filepath);
169
170 checkHeader(file);
171
172 // Read num_run
173 uint32_t num_run = 0;
174 readMMFile(&num_run, sizeof(uint32_t), 1, file, "Cannot read num_run from file");
175
176 MinMaxVectors mmv;
177 float minmax[2];
178 const int64_t data_size = sizeof(float) * 2 + sizeof(uint32_t) * 3;
179
180 // Check num_run overflow
181 if (num_run > std::numeric_limits<uint32_t>::max() / data_size)
182 throw std::runtime_error("num_run overflow");
183
184 for (uint32_t r = 0; r < num_run; ++r)
185 {
186 // Read num of operations and num of inputs
187 uint32_t num_op = 0;
188 readMMFile(&num_op, sizeof(uint32_t), 1, file, "Cannot read num of operations");
189 uint32_t num_input = 0;
190 readMMFile(&num_input, sizeof(uint32_t), 1, file, "Cannot read num of inputs");
191
192 // Check num_op overflow
193 if (num_op > std::numeric_limits<uint32_t>::max() / data_size / num_run)
194 throw std::runtime_error("num_op overflow");
195 // Check num_input overflow
196 if (num_input > std::numeric_limits<uint32_t>::max() / data_size / num_run)
197 throw std::runtime_error("num_input overflow");
198
199 // Skip operation minmax data
200 seekMMFile(file, static_cast<int64_t>(data_size * num_op), SEEK_CUR,
201 "Cannot skip operation minmax data");
202
203 // Find operation
204 for (uint32_t i = 0; i < num_input; ++i)
205 {
206 uint32_t model_id_from_file = 0;
207 uint32_t subg_idx_from_file = 0;
208 uint32_t input_idx_from_file = 0;
209
210 readMMFile(&model_id_from_file, sizeof(uint32_t), 1, file, "Cannot read model_id from file");
211 readMMFile(&subg_idx_from_file, sizeof(uint32_t), 1, file, "Cannot read subg_idx from file");
212 readMMFile(&input_idx_from_file, sizeof(uint32_t), 1, file,
213 "Cannot read input_idx from file");
214
215 if (model_id_from_file == model_idx && subg_idx_from_file == subg_idx &&
216 input_idx_from_file == input_idx)
217 {
218 // Read minmax data
219 readMMFile(minmax, sizeof(float), 2, file, "Cannot read minmax data from file");
220 mmv.min_vector.emplace_back(minmax[0]);
221 mmv.max_vector.emplace_back(minmax[1]);
222
223 // Skip remain input minmax data
224 const uint32_t remain_elem = num_input - i - 1;
225 seekMMFile(file, static_cast<int64_t>(data_size * remain_elem), SEEK_CUR,
226 "Cannot skip remain minmax data from file");
227 break;
228 }
229
230 // Skip minmax data
231 seekMMFile(file, sizeof(float) * 2, SEEK_CUR, "Cannot skip minmax data from file");
232 }
233 }
234
235 std::fclose(file);
236 return mmv;
237}

References onert::odc::MinMaxVectors::max_vector, and onert::odc::MinMaxVectors::min_vector.

◆ readNumRuns()

uint32_t onert::odc::MinMaxReader::readNumRuns ( ) const

Returns minmax recording count.

Returns
minmax recording count

Definition at line 239 of file MinMaxReader.cc.

240{
241 // Find file to read
242 auto file = std::fopen(_filepath.c_str(), "rb");
243 if (!file)
244 throw std::runtime_error("Cannot open file: " + _filepath);
245
246 checkHeader(file);
247
248 // Read num_run
249 uint32_t num_run = 0;
250 readMMFile(&num_run, sizeof(uint32_t), 1, file, "Cannot read num_run from file");
251
252 std::fclose(file);
253
254 return num_run;
255}

Referenced by onert::odc::Quantizer::readyForQuantize().

◆ readOP()

MinMaxVectors onert::odc::MinMaxReader::readOP ( uint32_t  model_idx,
uint32_t  subg_idx,
uint32_t  op_idx 
) const

Returns minmax recording for op {model_idx, subg_idx, op_idx}.

Returns
MinMaxVectors

Definition at line 86 of file MinMaxReader.cc.

87{
88 // Find file to read
89 auto file = std::fopen(_filepath.c_str(), "rb");
90 if (!file)
91 throw std::runtime_error("Cannot open file: " + _filepath);
92
93 checkHeader(file);
94
95 // Read num_run
96 uint32_t num_run = 0;
97 readMMFile(&num_run, sizeof(uint32_t), 1, file, "Cannot read num_run from file");
98
99 MinMaxVectors mmv;
100 float minmax[2];
101 const int64_t data_size = sizeof(float) * 2 + sizeof(uint32_t) * 3;
102
103 // Check num_run overflow
104 if (num_run > std::numeric_limits<uint32_t>::max() / data_size)
105 throw std::runtime_error("num_run overflow");
106
107 for (uint32_t r = 0; r < num_run; ++r)
108 {
109 // Read num of operations and num of inputs
110 uint32_t num_op = 0;
111 readMMFile(&num_op, sizeof(uint32_t), 1, file, "Cannot read num of operations");
112 uint32_t num_input = 0;
113 readMMFile(&num_input, sizeof(uint32_t), 1, file, "Cannot read num of inputs");
114
115 // Check num_op overflow
116 if (num_op > std::numeric_limits<uint32_t>::max() / data_size / num_run)
117 throw std::runtime_error("num_op overflow");
118 // Check num_input overflow
119 if (num_input > std::numeric_limits<uint32_t>::max() / data_size / num_run)
120 throw std::runtime_error("num_input overflow");
121
122 // Find operation
123 for (uint32_t i = 0; i < num_op; ++i)
124 {
125 uint32_t model_id_from_file = 0;
126 uint32_t subg_idx_from_file = 0;
127 uint32_t op_idx_from_file = 0;
128
129 readMMFile(&model_id_from_file, sizeof(uint32_t), 1, file, "Cannot read model_id from file");
130 readMMFile(&subg_idx_from_file, sizeof(uint32_t), 1, file, "Cannot read subg_idx from file");
131 readMMFile(&op_idx_from_file, sizeof(uint32_t), 1, file, "Cannot read op_idx from file");
132
133 if (model_id_from_file == model_idx && subg_idx_from_file == subg_idx &&
134 op_idx_from_file == op_idx)
135 {
136 // Read minmax data
137 readMMFile(minmax, sizeof(float), 2, file, "Cannot read minmax data from file");
138 mmv.min_vector.emplace_back(minmax[0]);
139 mmv.max_vector.emplace_back(minmax[1]);
140
141 // Skip remain operation minmax data
142 const uint32_t remain_elem = num_op - i - 1;
143 seekMMFile(file, static_cast<int64_t>(data_size * remain_elem), SEEK_CUR,
144 "Failed to skip remain minmax data");
145
146 break;
147 }
148
149 // Skip minmax data
150 seekMMFile(file, sizeof(float) * 2, SEEK_CUR, "Failed to skip minmax data");
151 }
152
153 // Skip input minmax data
154 seekMMFile(file, static_cast<int64_t>(data_size * num_input), SEEK_CUR,
155 "Failed to skip input minmax data");
156 }
157
158 std::fclose(file);
159 return mmv;
160}

References onert::odc::MinMaxVectors::max_vector, and onert::odc::MinMaxVectors::min_vector.


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