• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tensorflow/lite/kernels/internal/types.h"
19 
20 namespace tflite {
21 
22 namespace reference_ops {
23 
24 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)25 void ArgMinMax(const RuntimeShape& input1_shape, const T1* input1_data,
26                const T3* input2_data, const RuntimeShape& output_shape,
27                T2* output_data, const Cmp& cmp) {
28   TFLITE_DCHECK_GT(input1_shape.DimensionsCount(), 0);
29   TFLITE_DCHECK_EQ(input1_shape.DimensionsCount() - 1,
30                    output_shape.DimensionsCount());
31   int axis = input2_data[0];
32   if (axis < 0) {
33     axis += input1_shape.DimensionsCount();
34   }
35   const int axis_size = input1_shape.Dims(axis);
36 
37   int outer_size = 1;
38   for (int i = 0; i < axis; ++i) {
39     TFLITE_DCHECK_EQ(input1_shape.Dims(i), output_shape.Dims(i));
40     outer_size *= input1_shape.Dims(i);
41   }
42 
43   int inner_size = 1;
44   const int dims_count = input1_shape.DimensionsCount();
45   for (int i = axis + 1; i < dims_count; ++i) {
46     TFLITE_DCHECK_EQ(input1_shape.Dims(i), output_shape.Dims(i - 1));
47     inner_size *= input1_shape.Dims(i);
48   }
49   for (int outer = 0; outer < outer_size; ++outer) {
50     for (int inner = 0; inner < inner_size; ++inner) {
51       auto min_max_value = input1_data[outer * axis_size * inner_size + inner];
52       T2 min_max_index = 0;
53       for (int i = 1; i < axis_size; ++i) {
54         const auto& curr_value =
55             input1_data[(outer * axis_size + i) * inner_size + inner];
56         if (cmp(curr_value, min_max_value)) {
57           min_max_value = curr_value;
58           min_max_index = static_cast<T2>(i);
59         }
60       }
61       output_data[outer * inner_size + inner] = min_max_index;
62     }
63   }
64 }
65 }  // namespace reference_ops
66 }  // namespace tflite
67 
68 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_ARG_MIN_MAX_H_
69