ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALUtils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 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_UTILS_H
19#define ONERT_MICRO_EXECUTE_PAL_UTILS_H
20
21#include <cassert>
22
23namespace onert_micro
24{
25namespace execute
26{
27namespace pal
28{
29
30inline std::pair<uint32_t, uint32_t> getUpLowerWeightTensorDepth(core::OpTrainableRankType rank,
31 const uint32_t output_depth)
32{
33 std::pair<uint32_t, uint32_t> result(0u, output_depth);
34
35 switch (rank)
36 {
37 case core::ALL:
38 break;
40 result.second = static_cast<uint32_t>(static_cast<float>(output_depth) / 2.f);
41 break;
43 result.first = static_cast<uint32_t>(static_cast<float>(output_depth) / 2.f);
44 break;
45 default:
46 assert("Unsupported type");
47 break;
48 }
49
50 return result;
51}
52
53// Table of sigmoid(i/24) at 0.16 format - 256 elements.
54// We use combined sigmoid and tanh look-up table, since
55// tanh(x) = 2*sigmoid(2*x) -1.
56// Both functions are symmetric, so the LUT table is only needed
57// for the absolute value of the input.
58static const uint16_t sigmoid_table_uint16[256] = {
59 32768, 33451, 34133, 34813, 35493, 36169, 36843, 37513, 38180, 38841, 39498, 40149, 40794, 41432,
60 42064, 42688, 43304, 43912, 44511, 45102, 45683, 46255, 46817, 47369, 47911, 48443, 48964, 49475,
61 49975, 50464, 50942, 51409, 51865, 52311, 52745, 53169, 53581, 53983, 54374, 54755, 55125, 55485,
62 55834, 56174, 56503, 56823, 57133, 57433, 57724, 58007, 58280, 58544, 58800, 59048, 59288, 59519,
63 59743, 59959, 60168, 60370, 60565, 60753, 60935, 61110, 61279, 61441, 61599, 61750, 61896, 62036,
64 62172, 62302, 62428, 62549, 62666, 62778, 62886, 62990, 63090, 63186, 63279, 63368, 63454, 63536,
65 63615, 63691, 63765, 63835, 63903, 63968, 64030, 64090, 64148, 64204, 64257, 64308, 64357, 64405,
66 64450, 64494, 64536, 64576, 64614, 64652, 64687, 64721, 64754, 64786, 64816, 64845, 64873, 64900,
67 64926, 64950, 64974, 64997, 65019, 65039, 65060, 65079, 65097, 65115, 65132, 65149, 65164, 65179,
68 65194, 65208, 65221, 65234, 65246, 65258, 65269, 65280, 65291, 65301, 65310, 65319, 65328, 65337,
69 65345, 65352, 65360, 65367, 65374, 65381, 65387, 65393, 65399, 65404, 65410, 65415, 65420, 65425,
70 65429, 65433, 65438, 65442, 65445, 65449, 65453, 65456, 65459, 65462, 65465, 65468, 65471, 65474,
71 65476, 65479, 65481, 65483, 65485, 65488, 65489, 65491, 65493, 65495, 65497, 65498, 65500, 65501,
72 65503, 65504, 65505, 65507, 65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517,
73 65517, 65518, 65519, 65520, 65520, 65521, 65522, 65522, 65523, 65523, 65524, 65524, 65525, 65525,
74 65526, 65526, 65526, 65527, 65527, 65528, 65528, 65528, 65529, 65529, 65529, 65529, 65530, 65530,
75 65530, 65530, 65531, 65531, 65531, 65531, 65531, 65532, 65532, 65532, 65532, 65532, 65532, 65533,
76 65533, 65533, 65533, 65533, 65533, 65533, 65533, 65534, 65534, 65534, 65534, 65534, 65534, 65534,
77 65534, 65534, 65534, 65535};
78
79inline std::int32_t saturatingRoundingDoublingHighMul(std::int32_t a, std::int32_t b)
80{
81 bool overflow = a == b && a == std::numeric_limits<std::int32_t>::min();
82 std::int64_t a_64(a);
83 std::int64_t b_64(b);
84 std::int64_t ab_64 = a_64 * b_64;
85 std::int32_t nudge = ab_64 >= 0 ? (1 << 30) : (1 - (1 << 30));
86 std::int32_t ab_x2_high32 = static_cast<std::int32_t>((ab_64 + nudge) / (1ll << 31));
87 return overflow ? std::numeric_limits<std::int32_t>::max() : ab_x2_high32;
88}
89
90// Correctly-rounded-to-nearest division by a power-of-two.
91// Also known as a rounding arithmetic right shift.
92inline int32_t roundingDivideByPOT(int32_t x, int32_t exponent)
93{
94 assert(exponent >= 0);
95 assert(exponent <= 31);
96 const int32_t mask = int32_t((1ll << exponent) - 1);
97 const int32_t zero = int32_t(0);
98 const int32_t one = int32_t(1);
99 const int32_t remainder = x & mask;
100 const int32_t threshold = (mask >> 1) + ((x < zero ? one : zero) & one);
101 return (x >> exponent) + ((remainder > threshold ? one : zero) & one);
102}
103
104inline int32_t multiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
105{
106 int left_shift = shift > 0 ? shift : 0;
107 int right_shift = shift > 0 ? 0 : -shift;
108 return roundingDivideByPOT(
109 saturatingRoundingDoublingHighMul(x * (1 << left_shift), quantized_multiplier), right_shift);
110}
111
113 int32_t quantized_multiplier,
114 int left_shift)
115{
116 return roundingDivideByPOT(saturatingRoundingDoublingHighMul(x, quantized_multiplier),
117 -left_shift);
118}
119
120template <typename P> inline void getActivationParams(const P &params, int32_t *min, int32_t *max)
121{
122 *min = params.int32_activation_min;
123 *max = params.int32_activation_max;
124}
125
126template <typename P> inline void getActivationParams(const P &params, float *min, float *max)
127{
128 *min = params.float_activation_min;
129 *max = params.float_activation_max;
130}
131
132template <typename P> inline void getActivationParams(const P &params, int64_t *min, int64_t *max)
133{
134 *min = params.int64_activation_min;
135 *max = params.int64_activation_max;
136}
137
138// Get common shape dim, assert that they all agree.
139inline int MatchingDim(const core::OMRuntimeShape &shape1, int index1,
140 const core::OMRuntimeShape &shape2, int index2)
141{
142 assert(shape1.dims(index1) == shape2.dims(index2));
143 return shape1.dims(index1);
144}
145
146// Data is required to be contiguous, and so many operators can use either the
147// full array flat size or the flat size with one dimension skipped (commonly
148// the depth).
149inline int flatSizeSkipDim(const int32_t *dims_data, int skip_dim, int num_dims)
150{
151 int flat_size = 1;
152 for (int i = 0; i < num_dims; ++i)
153 {
154 flat_size *= (i == skip_dim) ? 1 : dims_data[i];
155 }
156 return flat_size;
157}
158
159inline int offset(const int32_t *dims_data, int i0, int i1, int i2, int i3)
160{
161 return ((i0 * dims_data[1] + i1) * dims_data[2] + i2) * dims_data[3] + i3;
162}
163
164inline int offset(const int32_t *dims_data, int i0, int i1, int i2, int i3, int i4)
165{
166 return (((i0 * dims_data[1] + i1) * dims_data[2] + i2) * dims_data[3] + i3) * dims_data[4] + i4;
167}
168
169template <typename T>
170inline T activationFunctionWithMinMax(T x, T output_activation_min, T output_activation_max)
171{
172 using std::max;
173 using std::min;
174 return min(max(x, output_activation_min), output_activation_max);
175}
176
177// Reduces and compresses dimensions so that broadcast handling becomes more
178// efficient. Returns true if the output shape is broadcastable; it doesn't
179// contain any degenerate dimension, i.e. shape dimension = 0. False otherwise.
180template <int MAX_DIM = 6>
182 const core::OMRuntimeShape &input2_shape,
183 size_t *compressed_input1_stride,
184 size_t *compressed_input2_stride, size_t *compressed_output_shape)
185{
186 size_t num_compressed_dims = 0;
187 size_t compressed_input1_shape[MAX_DIM];
188 size_t compressed_input2_shape[MAX_DIM];
189 std::fill(compressed_input1_shape, compressed_input1_shape + MAX_DIM, 1);
190 std::fill(compressed_input2_shape, compressed_input2_shape + MAX_DIM, 1);
191 std::fill(compressed_output_shape, compressed_output_shape + MAX_DIM, 1);
192 bool broadcast_input1 = false;
193 bool broadcast_input2 = false;
194 bool first_nonunit = true;
195
196 if (input1_shape.dimensionsCount() < 0 || input2_shape.dimensionsCount() < 0)
197 {
198 return false;
199 }
200 const size_t num_input1_dims = input1_shape.dimensionsCount();
201 const size_t num_input2_dims = input2_shape.dimensionsCount();
202 const int32_t *input1_dims = input1_shape.dimsData();
203 const int32_t *input2_dims = input2_shape.dimsData();
204 const size_t num_common_dims = std::min(num_input1_dims, num_input2_dims);
205 for (size_t i = 1; i <= num_common_dims; i++)
206 {
207 if (input1_dims[num_input1_dims - i] < 0 || input2_dims[num_input2_dims - i] < 0)
208 {
209 return false;
210 }
211 const size_t input1_dim = input1_dims[num_input1_dims - i];
212 const size_t input2_dim = input2_dims[num_input2_dims - i];
213 if (input1_dim == 0 || input2_dim == 0)
214 {
215 return false;
216 }
217 if (input1_dim == 1 && input2_dim == 1)
218 {
219 continue;
220 }
221 assert(!broadcast_input1 || !broadcast_input2);
222
223 if (input1_dim == 1)
224 {
225 if (!broadcast_input1)
226 {
227 broadcast_input1 = true;
228 broadcast_input2 = false;
229 num_compressed_dims++;
230 }
231 compressed_input2_shape[num_compressed_dims - 1] *= input2_dim;
232 compressed_output_shape[num_compressed_dims - 1] *= input2_dim;
233 }
234 else if (input2_dim == 1)
235 {
236 if (!broadcast_input2)
237 {
238 broadcast_input1 = false;
239 broadcast_input2 = true;
240 num_compressed_dims++;
241 }
242 compressed_input1_shape[num_compressed_dims - 1] *= input1_dim;
243 compressed_output_shape[num_compressed_dims - 1] *= input1_dim;
244 }
245 else
246 {
247 assert(input1_dim == input2_dim);
248 if (broadcast_input1 || broadcast_input2 || first_nonunit)
249 {
250 broadcast_input1 = false;
251 broadcast_input2 = false;
252 num_compressed_dims++;
253 }
254 compressed_input1_shape[num_compressed_dims - 1] *= input1_dim;
255 compressed_input2_shape[num_compressed_dims - 1] *= input1_dim;
256 compressed_output_shape[num_compressed_dims - 1] *= input1_dim;
257 }
258 first_nonunit = false;
259 }
260 if (num_input1_dims > num_input2_dims)
261 {
262 if (!broadcast_input2)
263 {
264 num_compressed_dims++;
265 }
266 for (size_t i = 0; i < num_input1_dims - num_input2_dims; i++)
267 {
268 if (input1_dims[i] < 0)
269 return false;
270 const size_t input1_dim = input1_dims[i];
271 if (input1_dim == 0)
272 {
273 return false;
274 }
275 compressed_input1_shape[num_compressed_dims - 1] *= input1_dim;
276 compressed_output_shape[num_compressed_dims - 1] *= input1_dim;
277 }
278 }
279 else if (num_input2_dims > num_input1_dims)
280 {
281 if (!broadcast_input1)
282 {
283 num_compressed_dims++;
284 }
285 for (size_t i = 0; i < num_input2_dims - num_input1_dims; i++)
286 {
287 if (input2_dims[i] < 0)
288 return false;
289 const size_t input2_dim = input2_dims[i];
290 if (input2_dim == 0)
291 {
292 return false;
293 }
294 compressed_input2_shape[num_compressed_dims - 1] *= input2_dim;
295 compressed_output_shape[num_compressed_dims - 1] *= input2_dim;
296 }
297 }
298 num_compressed_dims = (num_compressed_dims > 1) ? num_compressed_dims : 1;
299
300 int input1_stride = 1;
301 int input2_stride = 1;
302 for (int i = 0; i < MAX_DIM; ++i)
303 {
304 compressed_input1_stride[i] = input1_stride;
305 input1_stride *= compressed_input1_shape[i];
306 compressed_input2_stride[i] = input2_stride;
307 input2_stride *= compressed_input2_shape[i];
308 }
309 for (int i = 0; i < MAX_DIM; ++i)
310 {
311 if (compressed_input1_shape[i] != compressed_input2_shape[i])
312 {
313 if (compressed_input1_shape[i] == 1)
314 {
315 compressed_input1_stride[i] = 0;
316 }
317 else
318 {
319 assert(compressed_input2_shape[i] == 1);
320 compressed_input2_stride[i] = 0;
321 }
322 }
323 }
324 return true;
325}
326
327} // namespace pal
328} // namespace execute
329} // namespace onert_micro
330
331#endif // ONERT_MICRO_EXECUTE_PAL_UTILS_H
size_t dimensionsCount() const noexcept
std::pair< uint32_t, uint32_t > getUpLowerWeightTensorDepth(core::OpTrainableRankType rank, const uint32_t output_depth)
Definition PALUtils.h:30
int flatSizeSkipDim(const int32_t *dims_data, int skip_dim, int num_dims)
Definition PALUtils.h:149
std::int32_t saturatingRoundingDoublingHighMul(std::int32_t a, std::int32_t b)
Definition PALUtils.h:79
void getActivationParams(const P &params, int32_t *min, int32_t *max)
Definition PALUtils.h:120
int32_t multiplyByQuantizedMultiplierSmallerThanOneExp(int32_t x, int32_t quantized_multiplier, int left_shift)
Definition PALUtils.h:112
bool ReduceDimensionsForBroadcast(const core::OMRuntimeShape &input1_shape, const core::OMRuntimeShape &input2_shape, size_t *compressed_input1_stride, size_t *compressed_input2_stride, size_t *compressed_output_shape)
Definition PALUtils.h:181
int MatchingDim(const core::OMRuntimeShape &shape1, int index1, const core::OMRuntimeShape &shape2, int index2)
Definition PALUtils.h:139
int offset(const int32_t *dims_data, int i0, int i1, int i2, int i3)
Definition PALUtils.h:159
int32_t multiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
Definition PALUtils.h:104
int32_t roundingDivideByPOT(int32_t x, int32_t exponent)
Definition PALUtils.h:92
T activationFunctionWithMinMax(T x, T output_activation_min, T output_activation_max)
Definition PALUtils.h:170