ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw::cker::ReduceMean Class Reference

#include <ReduceMean.h>

Collaboration diagram for nnfw::cker::ReduceMean:

Public Member Functions

 ReduceMean ()
 
template<typename T >
int PrepareforReduce (const Shape &input_shape, const Shape &output_shape, const std::vector< int > &axes, T *output_data, T init_value)
 
template<typename In , typename Out >
bool ReduceOp (const Shape &input_shape, const In *input_data, const Shape &output_shape, Out *output_data, const std::vector< int > &axes, bool, Out init_value, Out reducer(const Out current, const Out in, int normalizer))
 
template<typename In , typename Out >
bool ReduceOp (const Shape &input_shape, const In *input_data, float input_scale, int32_t input_offset, const Shape &output_shape, Out *output_data, float output_scale, int32_t output_offset, const std::vector< int > &axes, bool, Out init_value, int reducer(const int current, const In in))
 
- Public Member Functions inherited from nnfw::cker::Reduce
 Reduce ()
 
void prepare (size_t temp_index_size, size_t resolved_axis_size)
 
template<typename T >
bool ReduceGeneric (const Shape &input_shape, const T *input_data, const Shape &output_shape, T *output_data, const std::vector< int > &axes, bool, T init_value, T reducer(const T current, const T in))
 
template<typename T , typename U >
bool QuantizedMeanOrSum (const T *input_data, int32_t input_zero_point, float input_scale, const Shape &input_shape, T *output_data, int32_t output_zero_point, float output_scale, const Shape &output_shape, const std::vector< int > &axes, bool, U *temp_sum, bool compute_sum, U reducer(const U current, const T in))
 
int32_t * resolved_axis_data (void)
 
int32_t * temp_index_data (void)
 

Detailed Description

Definition at line 111 of file ReduceMean.h.

Constructor & Destructor Documentation

◆ ReduceMean()

nnfw::cker::ReduceMean::ReduceMean ( )
inline

Definition at line 114 of file ReduceMean.h.

114: Reduce(){};

Member Function Documentation

◆ PrepareforReduce()

template<typename T >
int nnfw::cker::ReduceMean::PrepareforReduce ( const Shape input_shape,
const Shape output_shape,
const std::vector< int > &  axes,
T *  output_data,
init_value 
)
inline

Definition at line 117 of file ReduceMean.h.

119 {
120 // Reset output data.
121 if (!InitTensorDataForReduce(output_shape, init_value, output_data))
122 {
123 return -1;
124 }
125 const auto input_dims = input_shape.DimsData();
126 const int num_dims = input_shape.DimensionsCount();
127 int resolved_axis_size = 1;
128 const auto num_axes = axes.size();
129
130 for (size_t idx = 0; idx < num_axes; idx++)
131 {
132 int current = axes[idx] < 0 ? (axes[idx] + num_dims) : axes[idx];
133 assert(current >= 0 && current < num_dims);
134 resolved_axis_size *= input_dims[current];
135 }
136
137 prepare(num_dims, resolved_axis_size);
138
139 // Resolve axis.
140 int num_resolved_axis = 0;
141 if (!ResolveAxis(input_shape.DimensionsCount(), axes, resolved_axis_data(), &num_resolved_axis))
142 {
143 return -1;
144 }
145
146 return num_resolved_axis;
147 }
int32_t * resolved_axis_data(void)
Definition Reduce.h:358
void prepare(size_t temp_index_size, size_t resolved_axis_size)
Definition Reduce.h:235
const luci_interpreter::RuntimeShape output_shape
bool ResolveAxis(const int num_dims, const std::vector< int > &axes, int *out_axis, int *out_num_axis)
Definition Reduce.h:169
bool InitTensorDataForReduce(const Shape &shape, const T init_value, T *data)
Definition Reduce.h:208

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::DimsData(), nnfw::cker::InitTensorDataForReduce(), output_shape, nnfw::cker::Reduce::prepare(), nnfw::cker::ResolveAxis(), and nnfw::cker::Reduce::resolved_axis_data().

Referenced by ReduceOp(), and ReduceOp().

◆ ReduceOp() [1/2]

template<typename In , typename Out >
bool nnfw::cker::ReduceMean::ReduceOp ( const Shape input_shape,
const In *  input_data,
const Shape output_shape,
Out *  output_data,
const std::vector< int > &  axes,
bool  ,
Out  init_value,
Out   reducerconst Out current, const Out in, int normalizer 
)
inline

Definition at line 152 of file ReduceMean.h.

155 {
156 int num_resolved_axis;
157 num_resolved_axis = PrepareforReduce(input_shape, output_shape, axes, output_data, init_value);
158 if (num_resolved_axis == -1)
159 {
160 return false;
161 }
162 return ReduceMeanImpl<In, Out>(input_data, input_shape, resolved_axis_data(), num_resolved_axis,
163 temp_index_data(), reducer, output_data);
164 }
int32_t * temp_index_data(void)
Definition Reduce.h:362
int PrepareforReduce(const Shape &input_shape, const Shape &output_shape, const std::vector< int > &axes, T *output_data, T init_value)
Definition ReduceMean.h:117

References output_shape, PrepareforReduce(), nnfw::cker::Reduce::resolved_axis_data(), and nnfw::cker::Reduce::temp_index_data().

Referenced by nnfw::cker::Mean(), and nnfw::cker::MeanQ8Asymm().

◆ ReduceOp() [2/2]

template<typename In , typename Out >
bool nnfw::cker::ReduceMean::ReduceOp ( const Shape input_shape,
const In *  input_data,
float  input_scale,
int32_t  input_offset,
const Shape output_shape,
Out *  output_data,
float  output_scale,
int32_t  output_offset,
const std::vector< int > &  axes,
bool  ,
Out  init_value,
int   reducerconst int current, const In in 
)
inline

Definition at line 167 of file ReduceMean.h.

171 {
172 size_t num_outputs = 1;
173 auto output_dims = output_shape.DimsData();
174
175 for (size_t idx = 0; idx < static_cast<size_t>(output_shape.DimensionsCount()); idx++)
176 {
177 num_outputs *= output_dims[idx];
178 }
179 _temp_sum.resize(num_outputs, 0);
180 int num_resolved_axis;
181 num_resolved_axis = PrepareforReduce(input_shape, output_shape, axes, output_data, init_value);
182 if (num_resolved_axis == -1)
183 {
184 return false;
185 }
186
187 size_t normalizer =
188 ReduceSumQuantImpl<In>(input_data, input_shape, resolved_axis_data(), num_resolved_axis,
189 temp_index_data(), reducer, _temp_sum.data());
190 if (num_outputs > 0)
191 {
192 float scale = input_scale / output_scale;
193 float bias = -input_offset * scale;
194 for (size_t idx = 0; idx < num_outputs; idx++)
195 {
196 float float_mean = static_cast<float>(_temp_sum[idx]) / normalizer;
197 float result = std::min(round_nearest(float_mean * scale + bias + output_offset),
198 static_cast<float>(std::numeric_limits<Out>::max()));
199 result = std::max(result, static_cast<float>(std::numeric_limits<Out>::min()));
200 output_data[idx] = static_cast<Out>(result);
201 }
202 }
203 return false;
204 }
result
Definition infer.py:103
float round_nearest(float value)
Definition ReduceMean.h:29

References output_shape, PrepareforReduce(), nnfw::cker::Reduce::resolved_axis_data(), nnfw::cker::round_nearest(), and nnfw::cker::Reduce::temp_index_data().


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