ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALL2Pool2D.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 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 LUCI_INTERPRETER_PAL_L2_POOL_2D_COMMON_H
19#define LUCI_INTERPRETER_PAL_L2_POOL_2D_COMMON_H
20
21#include "Params.h"
22#include "PALUtils.h"
23
25{
26
27inline void L2Pool(const PoolParams &params, const luci_interpreter::RuntimeShape &input_shape,
28 const float *input_data, const luci_interpreter::RuntimeShape &output_shape,
29 float *output_data)
30{
31 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
32 const int depth = MatchingDim(input_shape, 3, output_shape, 3);
33 const int input_height = input_shape.dims(1);
34 const int input_width = input_shape.dims(2);
35 const int output_height = output_shape.dims(1);
36 const int output_width = output_shape.dims(2);
37 const int stride_height = params.stride_height;
38 const int stride_width = params.stride_width;
39 for (int batch = 0; batch < batches; ++batch)
40 {
41 for (int out_y = 0; out_y < output_height; ++out_y)
42 {
43 for (int out_x = 0; out_x < output_width; ++out_x)
44 {
45 for (int channel = 0; channel < depth; ++channel)
46 {
47 const int in_x_origin = (out_x * stride_width) - params.padding_values.width;
48 const int in_y_origin = (out_y * stride_height) - params.padding_values.height;
49 // Compute the boundaries of the filter region clamped so as to
50 // ensure that the filter window fits in the input array.
51 const int filter_x_start = std::max(0, -in_x_origin);
52 const int filter_x_end = std::min(params.filter_width, input_width - in_x_origin);
53 const int filter_y_start = std::max(0, -in_y_origin);
54 const int filter_y_end = std::min(params.filter_height, input_height - in_y_origin);
55 float sum_squares = 0.f;
56 int filter_count = 0;
57 for (int filter_y = filter_y_start; filter_y < filter_y_end; ++filter_y)
58 {
59 for (int filter_x = filter_x_start; filter_x < filter_x_end; ++filter_x)
60 {
61 const int in_x = in_x_origin + filter_x;
62 const int in_y = in_y_origin + filter_y;
63 const float val =
64 input_data[offset(input_shape.dimsData(), batch, in_y, in_x, channel)];
65 sum_squares += val * val;
66 filter_count++;
67 }
68 }
69 assert(filter_count != 0);
70 const float l2pool_result = std::sqrt(sum_squares / filter_count);
71 output_data[offset(output_shape.dimsData(), batch, out_y, out_x, channel)] =
74 }
75 }
76 }
77 }
78}
79
80} // namespace luci_interpreter_pal
81
82#endif // LUCI_INTERPRETER_PAL_L2_POOL_2D_COMMON_H
int32_t dims(int i) const
Definition Tensor.h:108
const luci_interpreter::RuntimeShape output_shape
int offset(const int32_t *dims_data, int i0, int i1, int i2, int i3)
Definition PALUtils.h:193
int MatchingDim(const luci_interpreter::RuntimeShape &shape1, int index1, const luci_interpreter::RuntimeShape &shape2, int index2)
Definition PALUtils.h:173
void L2Pool(const PoolParams &params, const luci_interpreter::RuntimeShape &input_shape, const float *input_data, const luci_interpreter::RuntimeShape &output_shape, float *output_data)
Definition PALL2Pool2D.h:27
T activationFunctionWithMinMax(T x, T output_activation_min, T output_activation_max)
Definition PALUtils.h:204