ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALMaxPool2D.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2020 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef ONERT_MICRO_EXECUTE_PAL_MAX_POOL_2D_H
19#define ONERT_MICRO_EXECUTE_PAL_MAX_POOL_2D_H
20
21#include "PALMaxPool2DCommon.h"
22
23namespace onert_micro
24{
25namespace execute
26{
27namespace pal
28{
29
30OMStatus MaxPool(const core::Pool2DParams &params, const core::OMRuntimeShape &input_shape,
31 const int8_t *input_data, const core::OMRuntimeShape &output_shape,
32 int8_t *output_data)
33{
34 assert(input_shape.dimensionsCount() == 4);
35 assert(output_shape.dimensionsCount() == 4);
36 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
37 const int depth = MatchingDim(input_shape, 3, output_shape, 3);
38 const int input_height = input_shape.dims(1);
39 const int input_width = input_shape.dims(2);
40 const int output_height = output_shape.dims(1);
41 const int output_width = output_shape.dims(2);
42 const int stride_height = params.stride_h;
43 const int stride_width = params.stride_w;
44 const int pad_w = params.pad_w;
45 const int pad_h = params.pad_h;
46 const int filter_h = params.filter_h;
47 const int filter_w = params.filter_w;
48 for (int batch = 0; batch < batches; ++batch)
49 {
50 for (int out_y = 0; out_y < output_height; ++out_y)
51 {
52 for (int out_x = 0; out_x < output_width; ++out_x)
53 {
54 for (int channel = 0; channel < depth; ++channel)
55 {
56 const int in_x_origin = (out_x * stride_width) - pad_w;
57 const int in_y_origin = (out_y * stride_height) - pad_h;
58 // Compute the boundaries of the filter region clamped so as to
59 // ensure that the filter window fits in the input array.
60 const int filter_x_start = std::max(0, -in_x_origin);
61 const int filter_x_end = std::min(filter_w, input_width - in_x_origin);
62 const int filter_y_start = std::max(0, -in_y_origin);
63 const int filter_y_end = std::min(filter_h, input_height - in_y_origin);
64 int8_t max = std::numeric_limits<int8_t>::lowest();
65 for (int filter_y = filter_y_start; filter_y < filter_y_end; ++filter_y)
66 {
67 for (int filter_x = filter_x_start; filter_x < filter_x_end; ++filter_x)
68 {
69 const int in_x = in_x_origin + filter_x;
70 const int in_y = in_y_origin + filter_y;
71 max = std::max(
72 max, input_data[offset(input_shape.dimsData(), batch, in_y, in_x, channel)]);
73 }
74 }
75 max = std::max<int8_t>(max, params.quantized_activation_min);
76 max = std::min<int8_t>(max, params.quantized_activation_max);
77 output_data[offset(output_shape.dimsData(), batch, out_y, out_x, channel)] =
78 static_cast<int8_t>(max);
79 }
80 }
81 }
82 }
83 return Ok;
84}
85
86} // namespace pal
87} // namespace execute
88} // namespace onert_micro
89
90#endif // ONERT_MICRO_EXECUTE_PAL_MAX_POOL_2D_H
int32_t dimensionsCount() const
Definition Tensor.h:106
int32_t dims(int i) const
Definition Tensor.h:108
const luci_interpreter::RuntimeShape output_shape
int MatchingDim(const core::OMRuntimeShape &shape1, int index1, const core::OMRuntimeShape &shape2, int index2)
Definition PALUtils.h:200
OMStatus MaxPool(const core::Pool2DParams &params, const core::OMRuntimeShape &input_shape, const int8_t *input_data, const core::OMRuntimeShape &output_shape, int8_t *output_data)
int offset(const int32_t *dims_data, int i0, int i1, int i2, int i3)
Definition PALUtils.h:220