ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __KBENCHMARK_KERNELS_ACL_COMMON_UTILS_H__
18#define __KBENCHMARK_KERNELS_ACL_COMMON_UTILS_H__
19
20#include <algorithm>
21
22using namespace arm_compute;
23
24namespace kbenchmark
25{
26namespace kernels
27{
28namespace acl_common
29{
30
32{
33 uint32_t top;
34 uint32_t bottom;
35 uint32_t left;
36 uint32_t right;
37};
38
39PaddingInfo calculatePadding(const std::string &padding_name, const uint32_t ifm_H,
40 const uint32_t ifm_W, const uint32_t ofm_H, const uint32_t ofm_W,
41 const uint32_t vertical_stride, const uint32_t horizontal_stride,
42 const uint32_t ker_H, const uint32_t ker_W)
43{
44 uint32_t top;
45 uint32_t bottom;
46 uint32_t left;
47 uint32_t right;
48
49 if (padding_name == "VALID")
50 {
51 top = bottom = left = right = 0;
52 }
53 else if (padding_name == "SAME")
54 {
55 const int32_t vertical_needed_input = (ofm_H - 1) * vertical_stride + ker_H;
56 const int32_t vertical_total_padding = std::max(0, vertical_needed_input - (int32_t)ifm_H);
57
58 const int32_t horizontal_needed_input = (ofm_W - 1) * horizontal_stride + ker_W;
59 const int32_t horizontal_total_padding = std::max(0, horizontal_needed_input - (int32_t)ifm_W);
60
61 top = vertical_total_padding / 2;
62 bottom = (vertical_total_padding + 1) / 2;
63 left = horizontal_total_padding / 2;
64 right = (horizontal_total_padding + 1) / 2;
65 }
66
67 return PaddingInfo{top, bottom, left, right};
68}
69
70PadStrideInfo asPadStrideInfo(const PaddingInfo &padding, uint32_t vertical_stride,
71 uint32_t horizontal_stride)
72{
73 return PadStrideInfo{horizontal_stride,
74 vertical_stride,
75 padding.left,
76 padding.right,
77 padding.top,
78 padding.bottom,
79 DimensionRoundingType::FLOOR};
80}
81
82ActivationLayerInfo asActivationLayerInfo(const std::string &act_name)
83{
84 if (act_name == "NONE")
85 {
86 return ActivationLayerInfo{};
87 }
88 else if (act_name == "RELU")
89 {
90 return ActivationLayerInfo{ActivationLayerInfo::ActivationFunction::RELU};
91 }
92 else
93 {
94 throw std::runtime_error{"Not support activation layer info"};
95 }
96}
97
98} // namespace acl_common
99} // namespace kernels
100} // namespace kbenchmark
101
102#endif // __KBENCHMARK_KERNELS_ACL_COMMON_UTILS_H__
PadStrideInfo asPadStrideInfo(const PaddingInfo &padding, uint32_t vertical_stride, uint32_t horizontal_stride)
Definition Utils.h:70
ActivationLayerInfo asActivationLayerInfo(const std::string &act_name)
Definition Utils.h:82
PaddingInfo calculatePadding(const std::string &padding_name, const uint32_t ifm_H, const uint32_t ifm_W, const uint32_t ofm_H, const uint32_t ofm_W, const uint32_t vertical_stride, const uint32_t horizontal_stride, const uint32_t ker_H, const uint32_t ker_W)
Definition Utils.h:39