Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / pal / common / PALSub.h
1 /*
2  * Copyright (c) 2021 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 LUCI_INTERPRETER_PAL_SUB_COMMON_H
18 #define LUCI_INTERPRETER_PAL_SUB_COMMON_H
19
20 #include "PALUtils.h"
21
22 namespace luci_interpreter_pal
23 {
24 template <typename T>
25 static inline void Sub(const ArithmeticParams &params, const int flat_size, const T *input1_data,
26                        const T *input2_data, T *output_data)
27 {
28   T activation_min, activation_max;
29   getActivationParams(params, &activation_min, &activation_max);
30
31   for (int i = 0; i < flat_size; ++i)
32     output_data[i] =
33       std::min(std::max(input1_data[i] - input2_data[i], activation_min), activation_max);
34 }
35
36 template <typename T>
37 inline void
38 BroadcastSub4DSlow(const ArithmeticParams &params,
39                    const luci_interpreter::RuntimeShape &input1_shape, const T *input1_data,
40                    const luci_interpreter::RuntimeShape &input2_shape, const T *input2_data,
41                    const luci_interpreter::RuntimeShape &output_shape, T *output_data)
42 {
43   NdArrayDesc<4> desc1;
44   NdArrayDesc<4> desc2;
45   NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, &desc2);
46   const luci_interpreter::RuntimeShape extended_output_shape =
47     luci_interpreter::RuntimeShape::extendedShape(4, output_shape);
48
49   T activation_min, activation_max;
50   getActivationParams(params, &activation_min, &activation_max);
51
52   // In Tensorflow, the dimensions are canonically named (batch_number, row,
53   // col, channel), with extents (batches, height, width, depth), with the
54   // trailing dimension changing most rapidly (channels has the smallest stride,
55   // typically 1 element).
56   //
57   // In generated C code, we store arrays with the dimensions reversed. The
58   // first dimension has smallest stride.
59   //
60   // We name our variables by their Tensorflow convention, but generate C code
61   // nesting loops such that the innermost loop has the smallest stride for the
62   // best cache behavior.
63   for (int b = 0; b < extended_output_shape.dims(0); ++b)
64   {
65     for (int y = 0; y < extended_output_shape.dims(1); ++y)
66     {
67       for (int x = 0; x < extended_output_shape.dims(2); ++x)
68       {
69         for (int c = 0; c < extended_output_shape.dims(3); ++c)
70         {
71           const int output_data_offset =
72             ((b * extended_output_shape.dims(1) + y) * extended_output_shape.dims(2) + x) *
73               extended_output_shape.dims(3) +
74             c;
75
76           output_data[output_data_offset] =
77             std::min(std::max(input1_data[subscriptToIndex(desc1, b, y, x, c)] -
78                                 input2_data[subscriptToIndex(desc2, b, y, x, c)],
79                               activation_min),
80                      activation_max);
81         }
82       }
83     }
84   }
85 }
86
87 } // namespace luci_interpreter_pal
88
89 #endif // LUCI_INTERPRETER_PAL_SUB_COMMON_H