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

Static Public Member Functions

static void run (const mir::TensorVariant &input, const mir::ops::MaxPool2DOp &op, mir::TensorVariant &result)
 

Detailed Description

Definition at line 87 of file MaxPool2D.cpp.

Member Function Documentation

◆ run()

void mir_interpreter::MaxPool2DImpl< uint8_t >::run ( const mir::TensorVariant input,
const mir::ops::MaxPool2DOp op,
mir::TensorVariant result 
)
static

Definition at line 93 of file MaxPool2D.cpp.

95{
96 const auto &input_type = input.getType();
97 const auto &output_type = op.getOutput(0)->getType();
98 (void)input_type;
99
100 assert(input_type.isQuantized());
101 assert(output_type.isQuantized());
102 assert(input_type.getElementType() == DataType::UINT8);
103
104 const auto &input_shape = op.getInputShape(0);
105 const auto &output_shape = op.getOutputShape(0);
106 const auto &window_size = op.getWindowSize();
107 const auto &strides = op.getStrides();
108 const auto &padding_before = op.getPaddingBefore();
109 const auto &padding_after = op.getPaddingAfter();
110 (void)padding_after;
111
112 constexpr int num_spatial_dims = 2;
113 assert(input.getShape().rank() == 4);
114 assert(window_size.size() == num_spatial_dims);
115 assert(strides.size() == num_spatial_dims);
116 assert(padding_before.size() == num_spatial_dims);
117 assert(padding_after.size() == num_spatial_dims);
118
119 Tensor<uint8_t> input_accessor(input);
120
121 TensorType res_type(mir::DataType::UINT8, output_shape, output_type.getQuantization());
122 TensorVariant res(res_type);
123 Tensor<uint8_t> res_accessor(res);
124
125 ShapeRange in_range(input_shape);
126 Index in_index(input_shape.rank());
127
128 for (const auto &out_index : ShapeRange(output_shape))
129 {
130 // Assuming NHWC format.
131 in_index.at(0) = out_index.at(0);
132 in_index.at(3) = out_index.at(3);
133
134 uint8_t result = 0;
135 for (const auto &window_index : ShapeRange(Shape(window_size)))
136 {
137 // Assuming NHWC format.
138 for (int i = 0; i < num_spatial_dims; ++i)
139 in_index.at(1 + i) =
140 out_index.at(1 + i) * strides[i] + window_index.at(i) - padding_before[i];
141
142 if (in_range.contains(in_index))
143 {
144 result = std::max(result, input_accessor.at(in_index));
145 }
146 }
147 res_accessor.at(out_index) = result;
148 }
149}
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 > & getPaddingBefore() const
Definition MaxPool2DOp.h:49
const std::vector< std::int32_t > & getPaddingAfter() const
Definition MaxPool2DOp.h:51
const std::vector< std::int32_t > & getWindowSize() const
Definition MaxPool2DOp.h:45
const std::vector< std::int32_t > & getStrides() const
Definition MaxPool2DOp.h:47
const luci_interpreter::RuntimeShape output_shape
result
Definition infer.py:103
Definition Shape.h:28

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

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


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