ONE - On-device Neural Engine
Loading...
Searching...
No Matches
mir_interpreter::ReduceMeanImpl< T > Struct Template Reference

Static Public Member Functions

static void run (const mir::TensorVariant &inputv, const mir::ops::ReduceMeanOp &op, mir::TensorVariant &output)
 

Detailed Description

template<typename T>
struct mir_interpreter::ReduceMeanImpl< T >

Definition at line 30 of file ReduceMean.cpp.

Member Function Documentation

◆ run()

template<typename T >
void mir_interpreter::ReduceMeanImpl< T >::run ( const mir::TensorVariant inputv,
const mir::ops::ReduceMeanOp op,
mir::TensorVariant output 
)
static

Definition at line 37 of file ReduceMean.cpp.

39{
40 const auto &input_shape = op.getInputShape(0);
41 const auto &output_shape = op.getOutputShape(0);
42 const auto &reduction_dims = op.getReductionDims();
43 const bool keep_dims = op.getKeepDims();
44
45 const auto reductor = [](T result, T x) { return result + x; };
46
47 mir::Tensor<T> input(inputv);
48 mir::Tensor<T> res_accessor(output);
49
50 erase<T>(output);
51
52 // This mask contains 'true' for dimensions that should be reduced. For example, if we want
53 // to reduce dimensions 1 and 3 with total number of dimensions of 4, the mask will be
54 // [false, true, false, true].
55 std::vector<bool> reduction_dims_mask(input_shape.rank(), false);
56 for (const int dim : reduction_dims)
57 {
58 reduction_dims_mask[dim] = true;
59 }
60
61 mir::Index out_index(output_shape.rank());
62 for (const mir::Index &in_index : mir::ShapeRange(input_shape))
63 {
64 int out_index_dim = 0;
65 for (int dim = 0; dim < input_shape.rank(); ++dim)
66 {
67 if (keep_dims)
68 {
69 out_index.at(out_index_dim++) = reduction_dims_mask[dim] ? 0 : in_index.at(dim);
70 }
71 else
72 {
73 if (!reduction_dims_mask[dim])
74 {
75 out_index.at(out_index_dim++) = in_index.at(dim);
76 }
77 }
78 }
79 res_accessor.at(out_index) = reductor(res_accessor.at(out_index), input.at(in_index));
80 }
81
82 const std::int32_t reduction_factor = input_shape.numElements() / output_shape.numElements();
83
84 for (const auto &index : mir::ShapeRange(output_shape))
85 {
86 res_accessor.at(index) /= reduction_factor;
87 }
88}
const Shape & getInputShape(std::size_t index) const
Definition Operation.h:161
const Shape & getOutputShape(std::size_t index) const
Definition Operation.h:163
const std::vector< int > & getReductionDims() const
Definition ReduceOp.h:38
bool getKeepDims() const
Definition ReduceOp.h:40
const luci_interpreter::RuntimeShape output_shape
result
Definition infer.py:103

References mir::Tensor< T >::at(), mir::Index::at(), mir::Operation::getInputShape(), mir::ops::ReduceOp::getKeepDims(), mir::Operation::getOutputShape(), mir::ops::ReduceOp::getReductionDims(), and output_shape.

Referenced by package.infer.session::inference().


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