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   for (int b = 0; b < output_shape.Dims(0); ++b) {
47     for (int y = 0; y < output_shape.Dims(1); ++y) {
48       for (int x = 0; x < output_shape.Dims(2); ++x) {
49         for (int c = 0; c < output_shape.Dims(3); ++c) {
50           auto out_idx = Offset(output_shape, b, y, x, c);
51           auto in1_idx = SubscriptToIndex(desc1, b, y, x, c);
52           auto in2_idx = SubscriptToIndex(desc2, b, y, x, c);
53           auto in1_val = input1_data[in1_idx];
54           auto in2_val = input2_data[in2_idx];
55           output_data[out_idx] = func(in1_val, in2_val);
56         }
57       }
58     }
59   }
60 }
61 
62 // R: Result type. T1: Input 1 type. T2: Input 2 type.
63 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))64 inline void BinaryFunction(const RuntimeShape& input1_shape,
65                            const T1* input1_data,
66                            const RuntimeShape& input2_shape,
67                            const T2* input2_data,
68                            const RuntimeShape& output_shape, R* output_data,
69                            R (*func)(T1, T2)) {
70   const int flat_size =
71       MatchingFlatSize(input1_shape, input2_shape, output_shape);
72   for (int i = 0; i < flat_size; ++i) {
73     output_data[i] = func(input1_data[i], input2_data[i]);
74   }
75 }
76 
77 }  // namespace reference_ops
78 }  // namespace tflite
79 
80 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_BINARY_FUNCTION_H_
81