1 /* Copyright 2017 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_MAXIMUM_MINIMUM_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_
17
18 #include "tensorflow/lite/kernels/internal/common.h"
19 #include "tensorflow/lite/kernels/internal/types.h"
20
21 namespace tflite {
22 namespace reference_ops {
23
24 template <typename T, typename Op, int N = 5>
MaximumMinimumBroadcastSlow(const RuntimeShape & unextended_input1_shape,const T * input1_data,const RuntimeShape & unextended_input2_shape,const T * input2_data,const RuntimeShape & unextended_output_shape,T * output_data,Op op)25 void MaximumMinimumBroadcastSlow(const RuntimeShape& unextended_input1_shape,
26 const T* input1_data,
27 const RuntimeShape& unextended_input2_shape,
28 const T* input2_data,
29 const RuntimeShape& unextended_output_shape,
30 T* output_data, Op op) {
31 // Uses element-wise calculation if broadcast is not required.
32 if (unextended_input1_shape == unextended_input2_shape) {
33 const int flat_size =
34 MatchingElementsSize(unextended_input1_shape, unextended_input2_shape,
35 unextended_output_shape);
36 for (int i = 0; i < flat_size; ++i) {
37 output_data[i] = op(input1_data[i], input2_data[i]);
38 }
39 } else {
40 TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), N);
41 TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), N);
42 TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), N);
43
44 NdArrayDesc<N> desc1;
45 NdArrayDesc<N> desc2;
46 NdArrayDesc<N> output_desc;
47 NdArrayDescsForElementwiseBroadcast(
48 unextended_input1_shape, unextended_input2_shape, &desc1, &desc2);
49 CopyDimsToDesc(RuntimeShape::ExtendedShape(N, unextended_output_shape),
50 &output_desc);
51
52 auto maxmin_func = [&](int indexes[N]) {
53 output_data[SubscriptToIndex(output_desc, indexes)] =
54 op(input1_data[SubscriptToIndex(desc1, indexes)],
55 input2_data[SubscriptToIndex(desc2, indexes)]);
56 };
57 NDOpsHelper<N>(output_desc, maxmin_func);
58 }
59 }
60
61 } // namespace reference_ops
62 } // namespace tflite
63
64 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MAXIMUM_MINIMUM_H_
65