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

#include <Reduce.h>

Public Member Functions

 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 230 of file Reduce.h.

Constructor & Destructor Documentation

◆ Reduce()

nnfw::cker::Reduce::Reduce ( )
inline

Definition at line 233 of file Reduce.h.

233: _temp_index(), _resolved_axis(), _prepared(false) {}

Member Function Documentation

◆ prepare()

void nnfw::cker::Reduce::prepare ( size_t  temp_index_size,
size_t  resolved_axis_size 
)
inline

Definition at line 235 of file Reduce.h.

236 {
237 if (_prepared)
238 return;
239
240 // prepare space for temp_index and resolved_axis
241 if (temp_index_size > kMaxSmallSize)
242 _temp_index.resize(temp_index_size);
243 if (resolved_axis_size > kMaxSmallSize)
244 _resolved_axis.resize(resolved_axis_size);
245 _prepared = true;
246 }

Referenced by nnfw::cker::ReduceMean::PrepareforReduce().

◆ QuantizedMeanOrSum()

template<typename T , typename U >
bool nnfw::cker::Reduce::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   reducerconst U current, const T in 
)
inline

Definition at line 276 of file Reduce.h.

282 {
283 // Reset output data.
284 size_t num_outputs = 1;
285 for (int idx = 0; idx < output_shape.DimensionsCount(); ++idx)
286 {
287 size_t current = static_cast<size_t>(output_shape.Dims(idx));
288 // Overflow prevention.
289 if (num_outputs > std::numeric_limits<size_t>::max() / current)
290 {
291 return false;
292 }
293 num_outputs *= current;
294 }
295 for (size_t idx = 0; idx < num_outputs; ++idx)
296 {
297 output_data[idx] = T();
298 temp_sum[idx] = U();
299 }
300
301 // Resolve axis.
302 int num_resolved_axis = 0;
303 if (!ResolveAxis(input_shape.DimensionsCount(), axes, resolved_axis_data(), &num_resolved_axis))
304 {
305 return false;
306 }
307
308 if (!ReduceImpl<T, U>(input_data, input_shape, output_shape, resolved_axis_data(),
309 num_resolved_axis, temp_index_data(), reducer, temp_sum))
310 {
311 return false;
312 }
313
314 // Calculate mean by dividing output_data by num of aggregated element.
315 size_t num_elements_in_axis = 1;
316 for (int idx = 0; idx < num_resolved_axis; ++idx)
317 {
318 size_t current = static_cast<size_t>(input_shape.Dims(resolved_axis_data()[idx]));
319 // Overflow prevention.
320 if (current > static_cast<size_t>(std::numeric_limits<size_t>::max() / num_elements_in_axis))
321 {
322 return false;
323 }
324 num_elements_in_axis *= current;
325 }
326
327 if (num_elements_in_axis > 0)
328 {
329 const float scale = input_scale / output_scale;
330 if (compute_sum)
331 {
332 // TODO(b/116341117): Eliminate float and do this completely in 8bit.
333 const float bias = -input_zero_point * scale * num_elements_in_axis;
334 for (size_t idx = 0; idx < num_outputs; ++idx)
335 {
336 const U value =
337 static_cast<U>(std::round(temp_sum[idx] * scale + bias)) + output_zero_point;
338 output_data[idx] = static_cast<T>(value);
339 }
340 }
341 else
342 {
343 const float bias = -input_zero_point * scale;
344 for (size_t idx = 0; idx < num_outputs; ++idx)
345 {
346 float float_mean =
347 static_cast<float>(temp_sum[idx]) / static_cast<float>(num_elements_in_axis);
348 float result = std::min(std::round(float_mean * scale + bias) + output_zero_point,
349 static_cast<float>(std::numeric_limits<T>::max()));
350 result = std::max(result, static_cast<float>(std::numeric_limits<T>::min()));
351 output_data[idx] = static_cast<T>(result);
352 }
353 }
354 }
355 return true;
356 }
int32_t * resolved_axis_data(void)
Definition Reduce.h:358
int32_t * temp_index_data(void)
Definition Reduce.h:362
const luci_interpreter::RuntimeShape output_shape
result
Definition infer.py:103
bool ResolveAxis(const int num_dims, const std::vector< int > &axes, int *out_axis, int *out_num_axis)
Definition Reduce.h:169

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::Shape::Dims(), output_shape, nnfw::cker::ResolveAxis(), resolved_axis_data(), and temp_index_data().

◆ ReduceGeneric()

template<typename T >
bool nnfw::cker::Reduce::ReduceGeneric ( const Shape input_shape,
const T *  input_data,
const Shape output_shape,
T *  output_data,
const std::vector< int > &  axes,
bool  ,
init_value,
T   reducerconst T current, const T in 
)
inline

Definition at line 251 of file Reduce.h.

254 {
255 // Reset output data.
256 if (!InitTensorDataForReduce(output_shape, init_value, output_data))
257 {
258 return false;
259 }
260
261 // Resolve axis.
262 int num_resolved_axis = 0;
263 if (!ResolveAxis(input_shape.DimensionsCount(), axes, resolved_axis_data(), &num_resolved_axis))
264 {
265 return false;
266 }
267
268 return ReduceImpl<T, T>(input_data, input_shape, output_shape, resolved_axis_data(),
269 num_resolved_axis, temp_index_data(), reducer, output_data);
270 }
bool InitTensorDataForReduce(const Shape &shape, const T init_value, T *data)
Definition Reduce.h:208

References nnfw::cker::Shape::DimensionsCount(), nnfw::cker::InitTensorDataForReduce(), output_shape, nnfw::cker::ResolveAxis(), resolved_axis_data(), and temp_index_data().

◆ resolved_axis_data()

int32_t * nnfw::cker::Reduce::resolved_axis_data ( void  )
inline

Definition at line 358 of file Reduce.h.

359 {
360 return _resolved_axis.size() ? _resolved_axis.data() : _resolved_axis_small;
361 }

Referenced by nnfw::cker::ReduceMean::PrepareforReduce(), QuantizedMeanOrSum(), ReduceGeneric(), nnfw::cker::ReduceMean::ReduceOp(), and nnfw::cker::ReduceMean::ReduceOp().

◆ temp_index_data()

int32_t * nnfw::cker::Reduce::temp_index_data ( void  )
inline

Definition at line 362 of file Reduce.h.

363 {
364 return _temp_index.size() ? _temp_index.data() : _temp_index_small;
365 }

Referenced by QuantizedMeanOrSum(), ReduceGeneric(), nnfw::cker::ReduceMean::ReduceOp(), and nnfw::cker::ReduceMean::ReduceOp().


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