ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALAveragePool2D.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_AVERAGE_POOL_2D_H
19#define ONERT_MICRO_EXECUTE_PAL_AVERAGE_POOL_2D_H
20
21#include "PALAveragePool2DCommon.h"
22
23namespace onert_micro
24{
25namespace execute
26{
27namespace pal
28{
29
30OMStatus AveragePool(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) - params.pad_w;
57 const int in_y_origin = (out_y * stride_height) - params.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(static_cast<int32_t>(params.filter_w),
62 static_cast<int32_t>(input_width - in_x_origin));
63 const int filter_y_start = std::max(0, -in_y_origin);
64 const int filter_y_end = std::min(static_cast<int32_t>(params.filter_h),
65 static_cast<int32_t>(input_height - in_y_origin));
66 int32_t acc = 0;
67 int filter_count = 0;
68 for (int filter_y = filter_y_start; filter_y < filter_y_end; ++filter_y)
69 {
70 for (int filter_x = filter_x_start; filter_x < filter_x_end; ++filter_x)
71 {
72 const int in_x = in_x_origin + filter_x;
73 const int in_y = in_y_origin + filter_y;
74 acc += input_data[offset(input_shape.dimsData(), batch, in_y, in_x, channel)];
75 filter_count++;
76 }
77 }
78 if (filter_count == 0)
79 return UnknownError;
80 // Round to the closest integer value.
81 acc = acc > 0 ? (acc + filter_count / 2) / filter_count
82 : (acc - filter_count / 2) / filter_count;
83 acc = std::max(acc, params.quantized_activation_min);
84 acc = std::min(acc, params.quantized_activation_max);
85 output_data[offset(output_shape.dimsData(), batch, out_y, out_x, channel)] =
86 static_cast<int8_t>(acc);
87 }
88 }
89 }
90 }
91 return Ok;
92}
93
94} // namespace pal
95} // namespace execute
96} // namespace onert_micro
97
98#endif // ONERT_MICRO_EXECUTE_PAL_AVERAGE_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
list input_data
Definition infer.py:29
OMStatus AveragePool(const core::Pool2DParams &params, const core::OMRuntimeShape &input_shape, const int8_t *input_data, const core::OMRuntimeShape &output_shape, int8_t *output_data)
int MatchingDim(const core::OMRuntimeShape &shape1, int index1, const core::OMRuntimeShape &shape2, int index2)
Definition PALUtils.h:200
int offset(const int32_t *dims_data, int i0, int i1, int i2, int i3)
Definition PALUtils.h:220