xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/gather.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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_GATHER_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_GATHER_H_
17 
18 #include <cstring>
19 
20 #include "tensorflow/lite/kernels/internal/common.h"
21 
22 namespace tflite {
23 namespace reference_ops {
24 
25 template <typename T, typename CoordsT = int32>
Gather(const tflite::GatherParams & op_params,const RuntimeShape & input_shape,const T * input_data,const RuntimeShape & coords_shape,const CoordsT * coords_data,const RuntimeShape & output_shape,T * output_data)26 inline void Gather(const tflite::GatherParams& op_params,
27                    const RuntimeShape& input_shape, const T* input_data,
28                    const RuntimeShape& coords_shape, const CoordsT* coords_data,
29                    const RuntimeShape& output_shape, T* output_data) {
30   ruy::profiler::ScopeLabel label("Gather");
31   int axis = op_params.axis;
32   if (axis < 0) {
33     axis += input_shape.DimensionsCount();
34   }
35   TFLITE_DCHECK_GE(axis, 0);
36   TFLITE_DCHECK_LT(axis, input_shape.DimensionsCount());
37 
38   int batch_dims = op_params.batch_dims;
39   if (batch_dims < 0) {
40     batch_dims += coords_shape.DimensionsCount();
41   }
42   TFLITE_DCHECK_GE(batch_dims, 0);
43   TFLITE_DCHECK_LT(batch_dims, input_shape.DimensionsCount());
44   TFLITE_DCHECK_LE(batch_dims, coords_shape.DimensionsCount());
45   TFLITE_DCHECK_GE(axis, batch_dims);
46   for (int i = 0; i < batch_dims; ++i) {
47     TFLITE_DCHECK_EQ(input_shape.Dims(i), coords_shape.Dims(i));
48   }
49 
50   const int axis_size = input_shape.Dims(axis);
51 
52   int batch_size = 1;
53   for (int i = 0; i < batch_dims; ++i) {
54     batch_size *= input_shape.Dims(i);
55   }
56 
57   int outer_size = 1;
58   for (int i = batch_dims; i < axis; ++i) {
59     outer_size *= input_shape.Dims(i);
60   }
61 
62   int inner_size = 1;
63   for (int i = axis + 1; i < input_shape.DimensionsCount(); ++i) {
64     inner_size *= input_shape.Dims(i);
65   }
66 
67   int coord_size = 1;
68   for (int i = batch_dims; i < coords_shape.DimensionsCount(); ++i) {
69     coord_size *= coords_shape.Dims(i);
70   }
71 
72   for (int batch = 0; batch < batch_size; ++batch) {
73     for (int outer = 0; outer < outer_size; ++outer) {
74       for (int i = 0; i < coord_size; ++i) {
75         TFLITE_DCHECK_GE(coords_data[i], 0);
76         TFLITE_DCHECK_LT(coords_data[i], axis_size);
77         // TODO(rsun): replace memcpy with a for loop
78         std::memcpy(
79             output_data +
80                 (((batch * outer_size) + outer) * coord_size + i) * inner_size,
81             input_data + (((batch * outer_size) + outer) * axis_size +
82                           coords_data[batch * coord_size + i]) *
83                              inner_size,
84             sizeof(T) * inner_size);
85       }
86     }
87   }
88 }
89 
90 }  // namespace reference_ops
91 }  // namespace tflite
92 
93 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_GATHER_H_
94