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

Static Public Member Functions

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

Detailed Description

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

Definition at line 30 of file MaxPool2D.cpp.

Member Function Documentation

◆ run()

template<typename T >
void mir_interpreter::MaxPool2DImpl< T >::run ( const mir::TensorVariant inputv,
const mir::ops::MaxPool2DOp op,
mir::TensorVariant result 
)
static

Definition at line 37 of file MaxPool2D.cpp.

39{
40 const auto &input_shape = op.getInputShape(0);
41 const auto &output_shape = op.getOutputShape(0);
42 const auto &window_size = op.getWindowSize();
43 const auto &strides = op.getStrides();
44 const auto &padding_before = op.getPaddingBefore();
45 const auto &padding_after = op.getPaddingAfter();
46 (void)padding_after;
47
48 Tensor<T> input(inputv);
49
50 constexpr int num_spatial_dims = 2;
51 assert(input.getShape().rank() == 4);
52 assert(window_size.size() == num_spatial_dims);
53 assert(strides.size() == num_spatial_dims);
54 assert(padding_before.size() == num_spatial_dims);
55 assert(padding_after.size() == num_spatial_dims);
56
57 Tensor<T> res_accessor(result);
58
59 ShapeRange in_range(input_shape);
60 Index in_index(input_shape.rank());
61
62 for (const auto &out_index : ShapeRange(output_shape))
63 {
64 T result = std::numeric_limits<T>::lowest();
65
66 // Assuming NHWC format.
67 in_index.at(0) = out_index.at(0);
68 in_index.at(3) = out_index.at(3);
69
70 for (const auto &window_index : ShapeRange(Shape(window_size)))
71 {
72 // Assuming NHWC format.
73 for (int i = 0; i < num_spatial_dims; ++i)
74 in_index.at(1 + i) =
75 out_index.at(1 + i) * strides[i] + window_index.at(i) - padding_before[i];
76
77 if (in_range.contains(in_index))
78 {
79 result = std::max(result, input.at(in_index));
80 }
81 }
82
83 res_accessor.at(out_index) = result;
84 }
85}
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::getOutputShape(), mir::ops::MaxPool2DOp::getPaddingAfter(), mir::ops::MaxPool2DOp::getPaddingBefore(), mir::ops::MaxPool2DOp::getStrides(), mir::ops::MaxPool2DOp::getWindowSize(), and output_shape.

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


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