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

#include <MetricPrinter.h>

Collaboration diagram for circle_eval_diff::MAEPrinter:

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

Member Function Documentation

◆ accumulate()

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

153{
154 assert(first.size() == second.size()); // FIX_CALLER_UNLESS
155 assert(first.size() == _intermediate.size()); // FIX_CALLER_UNLESS
156
157 for (uint32_t output_idx = 0; output_idx < _intermediate.size(); output_idx++)
158 {
159 const auto first_output = first[output_idx];
160 const auto second_output = second[output_idx];
161
162 // Cast data to fp32 and then compute absolute error
163 const auto fp32_first_output = fp32(first_output);
164 const auto fp32_second_output = fp32(second_output);
165
166 accum_absolute_error(output_idx, fp32_first_output, fp32_second_output);
167 }
168
169 _num_data++;
170}

◆ dump()

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

Implements circle_eval_diff::MetricPrinter.

Definition at line 172 of file MetricPrinter.cpp.

173{
174 os << "Mean Absolute Error (MAE)" << std::endl;
175
176 for (uint32_t output_idx = 0; output_idx < _intermediate.size(); output_idx++)
177 {
178 const auto name = _output_names.at(output_idx);
179 const auto &inter = _intermediate.at(output_idx);
180 assert(inter.dtype() == loco::DataType::FLOAT32); // FIX_ME_UNLESS
181 const auto elem_count = inter.size<loco::DataType::FLOAT32>();
182
183 // Compute MAE
184 float mae = 0.0;
185 for (uint32_t elem_idx = 0; elem_idx < elem_count; elem_idx++)
186 mae += inter.at<loco::DataType::FLOAT32>(elem_idx);
187
188 mae = mae / elem_count;
189 mae = mae / _num_data;
190
191 os << "MAE for " << name << " is " << mae << std::endl;
192 }
193}

◆ init()

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

Reimplemented from circle_eval_diff::MetricPrinter.

Definition at line 99 of file MetricPrinter.cpp.

100{
101 THROW_UNLESS(first != nullptr, "Invalid module.");
102 THROW_UNLESS(second != nullptr, "Invalid module.");
103
104 const auto first_output = loco::output_nodes(first->graph());
105 const auto second_output = loco::output_nodes(second->graph());
106
107 assert(first_output.size() == second_output.size()); // FIX_CALLER_UNLESS
108
109 for (uint32_t i = 0; i < first_output.size(); i++)
110 {
111 const auto first_node = loco::must_cast<luci::CircleNode *>(first_output[i]);
112 const auto second_node = loco::must_cast<luci::CircleNode *>(second_output[i]);
113
114 // Create tensors to store intermediate results
115 _intermediate.emplace_back();
116 _intermediate.at(i).dtype(loco::DataType::FLOAT32);
117 // NOTE Use both first_node and second_node to avoid release build break
118 _intermediate.at(i).rank(first_node->rank());
119 uint32_t num_elems = 1;
120 for (uint32_t j = 0; j < second_node->rank(); j++)
121 {
122 _intermediate.at(i).dim(j) = second_node->dim(j);
123 num_elems *= second_node->dim(j).value();
124 }
125 _intermediate.at(i).size<loco::DataType::FLOAT32>(num_elems);
126
127 // Check the buffer is initilized with zero
128 for (uint32_t j = 0; j < num_elems; j++)
129 assert(_intermediate.at(i).at<loco::DataType::FLOAT32>(j) == 0.0);
130
131 // Save output names for logging
132 _output_names.emplace_back(first_node->name());
133 }
134}
#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: