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

#include <MetricPrinter.h>

Collaboration diagram for circle_eval_diff::MSEPrinter:

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 89 of file MetricPrinter.h.

Member Function Documentation

◆ accumulate()

void circle_eval_diff::MSEPrinter::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 616 of file MetricPrinter.cpp.

618{
619 assert(first.size() == second.size()); // FIX_CALLER_UNLESS
620 assert(first.size() == _intermediate.size()); // FIX_CALLER_UNLESS
621
622 for (uint32_t output_idx = 0; output_idx < _intermediate.size(); output_idx++)
623 {
624 const auto first_output = first[output_idx];
625 const auto second_output = second[output_idx];
626
627 // Cast data to fp32 and then compute absolute error
628 const auto fp32_first_output = fp32(first_output);
629 const auto fp32_second_output = fp32(second_output);
630
631 accum_squared_error(output_idx, fp32_first_output, fp32_second_output);
632 }
633
634 _num_data++;
635}

◆ dump()

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

Implements circle_eval_diff::MetricPrinter.

Definition at line 637 of file MetricPrinter.cpp.

638{
639 os << "Mean Squared Error (MSE)" << std::endl;
640
641 for (uint32_t output_idx = 0; output_idx < _intermediate.size(); output_idx++)
642 {
643 const auto name = _output_names.at(output_idx);
644 const auto &inter = _intermediate.at(output_idx);
645 assert(inter.dtype() == loco::DataType::FLOAT32); // FIX_ME_UNLESS
646 const auto elem_count = inter.size<loco::DataType::FLOAT32>();
647
648 // Compute MSE
649 float mse = 0.0;
650 for (uint32_t elem_idx = 0; elem_idx < elem_count; elem_idx++)
651 mse += inter.at<loco::DataType::FLOAT32>(elem_idx);
652
653 mse = mse / elem_count;
654 mse = mse / _num_data;
655
656 os << "MSE for " << name << " is " << mse << std::endl;
657 }
658}

◆ init()

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

Reimplemented from circle_eval_diff::MetricPrinter.

Definition at line 563 of file MetricPrinter.cpp.

564{
565 THROW_UNLESS(first != nullptr, "Invalid module.");
566 THROW_UNLESS(second != nullptr, "Invalid module.");
567
568 const auto first_output = loco::output_nodes(first->graph());
569 const auto second_output = loco::output_nodes(second->graph());
570
571 assert(first_output.size() == second_output.size()); // FIX_CALLER_UNLESS
572
573 for (uint32_t i = 0; i < first_output.size(); i++)
574 {
575 const auto first_node = loco::must_cast<luci::CircleNode *>(first_output[i]);
576 const auto second_node = loco::must_cast<luci::CircleNode *>(second_output[i]);
577
578 // Create tensors to store intermediate results
579 _intermediate.emplace_back();
580 _intermediate.at(i).dtype(loco::DataType::FLOAT32);
581 // NOTE Use both first_node and second_node to avoid release build break
582 _intermediate.at(i).rank(first_node->rank());
583 uint32_t num_elems = 1;
584 for (uint32_t j = 0; j < second_node->rank(); j++)
585 {
586 _intermediate.at(i).dim(j) = second_node->dim(j);
587 num_elems *= second_node->dim(j).value();
588 }
589 _intermediate.at(i).size<loco::DataType::FLOAT32>(num_elems);
590
591 // Check the buffer is initilized with zero
592 for (uint32_t j = 0; j < num_elems; j++)
593 assert(_intermediate.at(i).at<loco::DataType::FLOAT32>(j) == 0.0);
594
595 // Save output names for logging
596 _output_names.emplace_back(first_node->name());
597 }
598}
#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: