ONE - On-device Neural Engine
Loading...
Searching...
No Matches
circle_eval_diff::MAPEPrinter Class Referencefinal

#include <MetricPrinter.h>

Collaboration diagram for circle_eval_diff::MAPEPrinter:

Public Member Functions

void init (const luci::Module *first, const luci::Module *second)
 
void accumulate (const std::vector< std::shared_ptr< Tensor > > &first, const std::vector< std::shared_ptr< Tensor > > &second)
 
void dump (std::ostream &os) const
 
- Public Member Functions inherited from circle_eval_diff::MetricPrinter
virtual ~MetricPrinter ()=default
 

Detailed Description

Definition at line 111 of file MetricPrinter.h.

Member Function Documentation

◆ accumulate()

void circle_eval_diff::MAPEPrinter::accumulate ( const std::vector< std::shared_ptr< Tensor > > &  first,
const std::vector< std::shared_ptr< Tensor > > &  second 
)
virtual

Implements circle_eval_diff::MetricPrinter.

Definition at line 254 of file MetricPrinter.cpp.

256{
257 assert(first.size() == second.size()); // FIX_CALLER_UNLESS
258 assert(first.size() == _intermediate.size()); // FIX_CALLER_UNLESS
259
260 for (uint32_t output_idx = 0; output_idx < _intermediate.size(); output_idx++)
261 {
262 const auto first_output = first[output_idx];
263 const auto second_output = second[output_idx];
264
265 // Cast data to fp32 and then compute absolute error
266 const auto fp32_first_output = fp32(first_output);
267 const auto fp32_second_output = fp32(second_output);
268
269 accum_mean_absolute_error(output_idx, fp32_first_output, fp32_second_output);
270 }
271
272 _num_data++;
273}

◆ dump()

void circle_eval_diff::MAPEPrinter::dump ( std::ostream &  os) const
virtual

Implements circle_eval_diff::MetricPrinter.

Definition at line 275 of file MetricPrinter.cpp.

276{
277 os << "Mean Absolute Percentage Error (MAPE)" << std::endl;
278
279 for (uint32_t output_idx = 0; output_idx < _intermediate.size(); output_idx++)
280 {
281 const auto name = _output_names.at(output_idx);
282 const auto &inter = _intermediate.at(output_idx);
283 assert(inter.dtype() == loco::DataType::FLOAT32); // FIX_ME_UNLESS
284 const auto elem_count = inter.size<loco::DataType::FLOAT32>();
285
286 // Compute MAPE
287 float mape = 0.0;
288 for (uint32_t elem_idx = 0; elem_idx < elem_count; elem_idx++)
289 mape += inter.at<loco::DataType::FLOAT32>(elem_idx);
290
291 mape = mape / elem_count;
292 mape = mape / _num_data;
293 mape *= 100.0;
294
295 os << "MAPE for " << name << " is " << mape << "%" << std::endl;
296 }
297}

◆ init()

void circle_eval_diff::MAPEPrinter::init ( const luci::Module first,
const luci::Module second 
)
virtual

Reimplemented from circle_eval_diff::MetricPrinter.

Definition at line 196 of file MetricPrinter.cpp.

197{
198 THROW_UNLESS(first != nullptr, "Invalid module.");
199 THROW_UNLESS(second != nullptr, "Invalid module.");
200
201 const auto first_output = loco::output_nodes(first->graph());
202 const auto second_output = loco::output_nodes(second->graph());
203
204 assert(first_output.size() == second_output.size()); // FIX_CALLER_UNLESS
205
206 for (uint32_t i = 0; i < first_output.size(); i++)
207 {
208 const auto first_node = loco::must_cast<luci::CircleNode *>(first_output[i]);
209 const auto second_node = loco::must_cast<luci::CircleNode *>(second_output[i]);
210
211 // Create tensors to store intermediate results
212 _intermediate.emplace_back();
213 _intermediate.at(i).dtype(loco::DataType::FLOAT32);
214 // NOTE Use both first_node and second_node to avoid release build break
215 _intermediate.at(i).rank(first_node->rank());
216 uint32_t num_elems = 1;
217 for (uint32_t j = 0; j < second_node->rank(); j++)
218 {
219 _intermediate.at(i).dim(j) = second_node->dim(j);
220 num_elems *= second_node->dim(j).value();
221 }
222 _intermediate.at(i).size<loco::DataType::FLOAT32>(num_elems);
223
224 // Check the buffer is initilized with zero
225 for (uint32_t j = 0; j < num_elems; j++)
226 assert(_intermediate.at(i).at<loco::DataType::FLOAT32>(j) == 0.0);
227
228 // Save output names for logging
229 _output_names.emplace_back(first_node->name());
230 }
231}
#define THROW_UNLESS(COND, MSG)
loco::Graph * graph(void) const
provide main graph
Definition Module.cpp:32
std::vector< Node * > output_nodes(Graph *)
Definition Graph.cpp:101
uint64_t num_elems(const nnfw_tensorinfo *tensor_info)
Get the total number of elements in nnfw_tensorinfo->dims.

References luci::Module::graph(), num_elems(), loco::output_nodes(), and THROW_UNLESS.


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