2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef LUCI_INTERPRETER_PAL_SUB_COMMON_H
18 #define LUCI_INTERPRETER_PAL_SUB_COMMON_H
22 namespace luci_interpreter_pal
25 static inline void Sub(const ArithmeticParams ¶ms, const int flat_size, const T *input1_data,
26 const T *input2_data, T *output_data)
28 T activation_min, activation_max;
29 getActivationParams(params, &activation_min, &activation_max);
31 for (int i = 0; i < flat_size; ++i)
33 std::min(std::max(input1_data[i] - input2_data[i], activation_min), activation_max);
38 BroadcastSub4DSlow(const ArithmeticParams ¶ms,
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)
45 NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, &desc2);
46 const luci_interpreter::RuntimeShape extended_output_shape =
47 luci_interpreter::RuntimeShape::extendedShape(4, output_shape);
49 T activation_min, activation_max;
50 getActivationParams(params, &activation_min, &activation_max);
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).
57 // In generated C code, we store arrays with the dimensions reversed. The
58 // first dimension has smallest stride.
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)
65 for (int y = 0; y < extended_output_shape.dims(1); ++y)
67 for (int x = 0; x < extended_output_shape.dims(2); ++x)
69 for (int c = 0; c < extended_output_shape.dims(3); ++c)
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) +
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)],
87 } // namespace luci_interpreter_pal
89 #endif // LUCI_INTERPRETER_PAL_SUB_COMMON_H