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_ARG_MIN_MAX_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ARG_MIN_MAX_H_
17
18 #include <functional>
19
20 #include "tensorflow/lite/kernels/internal/types.h"
21
22 namespace tflite {
23
24 namespace reference_ops {
25
26 template <typename T>
GetComparefunction(bool is_arg_max)27 std::function<bool(T, T)> GetComparefunction(bool is_arg_max) {
28 if (is_arg_max) {
29 return std::greater<T>();
30 } else {
31 return std::less<T>();
32 }
33 }
34
35 template <typename T1, typename T2, typename T3, typename Cmp>
ArgMinMax(const RuntimeShape & input1_shape,const T1 * input1_data,const T3 * input2_data,const RuntimeShape & output_shape,T2 * output_data,const Cmp & cmp)36 void ArgMinMax(const RuntimeShape& input1_shape, const T1* input1_data,
37 const T3* input2_data, const RuntimeShape& output_shape,
38 T2* output_data, const Cmp& cmp) {
39 TFLITE_DCHECK_GT(input1_shape.DimensionsCount(), 0);
40 TFLITE_DCHECK_EQ(input1_shape.DimensionsCount() - 1,
41 output_shape.DimensionsCount());
42 int axis = input2_data[0];
43 if (axis < 0) {
44 axis += input1_shape.DimensionsCount();
45 }
46 const int axis_size = input1_shape.Dims(axis);
47
48 int outer_size = 1;
49 for (int i = 0; i < axis; ++i) {
50 TFLITE_DCHECK_EQ(input1_shape.Dims(i), output_shape.Dims(i));
51 outer_size *= input1_shape.Dims(i);
52 }
53
54 int inner_size = 1;
55 const int dims_count = input1_shape.DimensionsCount();
56 for (int i = axis + 1; i < dims_count; ++i) {
57 TFLITE_DCHECK_EQ(input1_shape.Dims(i), output_shape.Dims(i - 1));
58 inner_size *= input1_shape.Dims(i);
59 }
60 for (int outer = 0; outer < outer_size; ++outer) {
61 for (int inner = 0; inner < inner_size; ++inner) {
62 auto min_max_value = input1_data[outer * axis_size * inner_size + inner];
63 T2 min_max_index = 0;
64 for (int i = 1; i < axis_size; ++i) {
65 const auto& curr_value =
66 input1_data[(outer * axis_size + i) * inner_size + inner];
67 if (cmp(curr_value, min_max_value)) {
68 min_max_value = curr_value;
69 min_max_index = static_cast<T2>(i);
70 }
71 }
72 output_data[outer * inner_size + inner] = min_max_index;
73 }
74 }
75 }
76
77 template <typename T1, typename T2, typename T3>
ArgMinMax(const RuntimeShape & input1_shape,const T1 * input1_data,const T3 * input2_data,const RuntimeShape & output_shape,T2 * output_data,const bool is_arg_max)78 void ArgMinMax(const RuntimeShape& input1_shape, const T1* input1_data,
79 const T3* input2_data, const RuntimeShape& output_shape,
80 T2* output_data, const bool is_arg_max) {
81 ArgMinMax(input1_shape, input1_data, input2_data, output_shape, output_data,
82 GetComparefunction<T1>(is_arg_max));
83 }
84
85 } // namespace reference_ops
86 } // namespace tflite
87
88 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ARG_MIN_MAX_H_
89