xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/binary_function.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_
17 
18 #include "tensorflow/lite/kernels/internal/common.h"
19 #include "tensorflow/lite/kernels/internal/compatibility.h"
20 #include "tensorflow/lite/kernels/internal/types.h"
21 
22 namespace tflite {
23 
24 namespace reference_ops {
25 
26 // Also appears to duplicate MinimumMaximum.
27 //
28 // R: Result type. T1: Input 1 type. T2: Input 2 type.
29 template <typename R, typename T1, typename T2>
BroadcastBinaryFunction4DSlow(const RuntimeShape & unextended_input1_shape,const T1 * input1_data,const RuntimeShape & unextended_input2_shape,const T2 * input2_data,const RuntimeShape & unextended_output_shape,R * output_data,R (* func)(T1,T2))30 inline void BroadcastBinaryFunction4DSlow(
31     const RuntimeShape& unextended_input1_shape, const T1* input1_data,
32     const RuntimeShape& unextended_input2_shape, const T2* input2_data,
33     const RuntimeShape& unextended_output_shape, R* output_data,
34     R (*func)(T1, T2)) {
35   TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4);
36   TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), 4);
37   TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4);
38   const RuntimeShape output_shape =
39       RuntimeShape::ExtendedShape(4, unextended_output_shape);
40 
41   NdArrayDesc<4> desc1;
42   NdArrayDesc<4> desc2;
43   NdArrayDescsForElementwiseBroadcast(unextended_input1_shape,
44                                       unextended_input2_shape, &desc1, &desc2);
45 
46   const int* dims_data =
47       reinterpret_cast<const int*>(output_shape.DimsDataUpTo5D());
48   for (int b = 0; b < output_shape.Dims(0); ++b) {
49     int out_idx_b = b * dims_data[1];
50     int in_idx1_b = desc1.strides[0] * b;
51     int in_idx2_b = desc2.strides[0] * b;
52     for (int y = 0; y < output_shape.Dims(1); ++y) {
53       int out_idx_y = (out_idx_b + y) * dims_data[2];
54       int in_idx1_y = in_idx1_b + desc1.strides[1] * y;
55       int in_idx2_y = in_idx2_b + desc2.strides[1] * y;
56       for (int x = 0; x < output_shape.Dims(2); ++x) {
57         int out_idx_x = (out_idx_y + x) * dims_data[3];
58         int in1_idx = in_idx1_y + desc1.strides[2] * x;
59         int in2_idx = in_idx2_y + desc2.strides[2] * x;
60         for (int c = 0; c < output_shape.Dims(3); ++c) {
61           auto out_idx = out_idx_x + c;
62           auto in1_val = input1_data[in1_idx];
63           auto in2_val = input2_data[in2_idx];
64           output_data[out_idx] = func(in1_val, in2_val);
65           in1_idx += desc1.strides[3];
66           in2_idx += desc2.strides[3];
67         }
68       }
69     }
70   }
71 }
72 
73 // R: Result type. T1: Input 1 type. T2: Input 2 type.
74 template <typename R, typename T1, typename T2>
BinaryFunction(const RuntimeShape & input1_shape,const T1 * input1_data,const RuntimeShape & input2_shape,const T2 * input2_data,const RuntimeShape & output_shape,R * output_data,R (* func)(T1,T2))75 inline void BinaryFunction(const RuntimeShape& input1_shape,
76                            const T1* input1_data,
77                            const RuntimeShape& input2_shape,
78                            const T2* input2_data,
79                            const RuntimeShape& output_shape, R* output_data,
80                            R (*func)(T1, T2)) {
81   const int flat_size =
82       MatchingFlatSize(input1_shape, input2_shape, output_shape);
83   for (int i = 0; i < flat_size; ++i) {
84     output_data[i] = func(input1_data[i], input2_data[i]);
85   }
86 }
87 
88 }  // namespace reference_ops
89 }  // namespace tflite
90 
91 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_
92