ONE - On-device Neural Engine
Loading...
Searching...
No Matches
mir_interpreter::AvgPool2DImpl< uint8_t > Struct Reference

Static Public Member Functions

static void run (const mir::ops::AvgPool2DOp &op, const mir::TensorVariant &input, mir::TensorVariant &output)
 

Detailed Description

Definition at line 93 of file AvgPool2D.cpp.

Member Function Documentation

◆ run()

void mir_interpreter::AvgPool2DImpl< uint8_t >::run ( const mir::ops::AvgPool2DOp op,
const mir::TensorVariant input,
mir::TensorVariant output 
)
static

Definition at line 99 of file AvgPool2D.cpp.

101{
102 const auto &input_type = input.getType();
103 const auto &output_type = op.getOutput(0)->getType();
104 (void)input_type;
105
106 assert(input_type.isQuantized());
107 assert(output_type.isQuantized());
108 assert(input_type.getElementType() == DataType::UINT8);
109
110 const auto &input_shape = op.getInputShape(0);
111 const auto &output_shape = op.getOutputShape(0);
112 const auto &window_size = op.getWindowSize();
113 const auto &strides = op.getStrides();
114 const auto &padding_before = op.getPaddingBefore();
115 const auto &padding_after = op.getPaddingAfter();
116 (void)padding_after;
117
118 constexpr int num_spatial_dims = 2;
119 assert(input.getShape().rank() == 4);
120 assert(window_size.size() == num_spatial_dims);
121 assert(strides.size() == num_spatial_dims);
122 assert(padding_before.size() == num_spatial_dims);
123 assert(padding_after.size() == num_spatial_dims);
124
125 Tensor<uint8_t> input_accessor(input);
126 Tensor<uint8_t> res_accessor(output);
127
128 ShapeRange in_range(input_shape);
129 Index in_index(input_shape.rank());
130
131 int32_t output_min = std::numeric_limits<uint8_t>::min();
132 int32_t output_max = std::numeric_limits<uint8_t>::max();
133
134 for (const auto &out_index : ShapeRange(output_shape))
135 {
136 int32_t result = 0;
137 size_t num_elements = 0;
138
139 // Assuming NHWC format.
140 in_index.at(0) = out_index.at(0);
141 in_index.at(3) = out_index.at(3);
142
143 for (const auto &window_index : ShapeRange(Shape(window_size)))
144 {
145 // Assuming NHWC format.
146 for (int i = 0; i < num_spatial_dims; ++i)
147 in_index.at(1 + i) =
148 out_index.at(1 + i) * strides[i] + window_index.at(i) - padding_before[i];
149
150 if (in_range.contains(in_index))
151 {
152 num_elements++;
153 result += input_accessor.at(in_index);
154 }
155 else if (op.getIncludePad())
156 {
157 num_elements++;
158 }
159 }
160 result = (result + num_elements / 2) / num_elements;
161 result = std::max(result, output_min);
162 result = std::min(result, output_max);
163 res_accessor.at(out_index) = static_cast<uint8_t>(result);
164 }
165}
const TensorType & getType() const
Gets the type of this output.
Definition Operation.h:91
Output * getOutput(std::size_t index)
Definition Operation.h:149
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< std::int32_t > & getWindowSize() const
Definition AvgPool2DOp.h:45
const std::vector< std::int32_t > & getPaddingBefore() const
Definition AvgPool2DOp.h:49
const std::vector< std::int32_t > & getPaddingAfter() const
Definition AvgPool2DOp.h:51
bool getIncludePad() const
Definition AvgPool2DOp.h:53
const std::vector< std::int32_t > & getStrides() const
Definition AvgPool2DOp.h:47
const luci_interpreter::RuntimeShape output_shape
result
Definition infer.py:103
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59
Definition Shape.h:28

References mir::Tensor< T >::at(), mir::Index::at(), mir::ShapeRange::contains(), mir::ops::AvgPool2DOp::getIncludePad(), mir::Operation::getInputShape(), mir::Operation::getOutput(), mir::Operation::getOutputShape(), mir::ops::AvgPool2DOp::getPaddingAfter(), mir::ops::AvgPool2DOp::getPaddingBefore(), mir::ops::AvgPool2DOp::getStrides(), mir::Operation::Output::getType(), mir::ops::AvgPool2DOp::getWindowSize(), and output_shape.

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


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