xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/slice.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_SLICE_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SLICE_H_
17 
18 #include "tensorflow/lite/kernels/internal/portable_tensor.h"
19 #include "tensorflow/lite/kernels/internal/types.h"
20 
21 namespace tflite {
22 
23 namespace reference_ops {
24 
25 template <typename T>
Slice(const tflite::SliceParams & op_params,const RuntimeShape & input_shape,const RuntimeShape & output_shape,SequentialTensorWriter<T> * writer)26 inline void Slice(const tflite::SliceParams& op_params,
27                   const RuntimeShape& input_shape,
28                   const RuntimeShape& output_shape,
29                   SequentialTensorWriter<T>* writer) {
30   const RuntimeShape ext_shape = RuntimeShape::ExtendedShape(5, input_shape);
31   TFLITE_DCHECK_LE(op_params.begin_count, 5);
32   TFLITE_DCHECK_LE(op_params.size_count, 5);
33   const int begin_count = op_params.begin_count;
34   const int size_count = op_params.size_count;
35   // We front-pad the begin and size vectors.
36   int start[5];
37   int stop[5];
38   for (int i = 0; i < 5; ++i) {
39     int padded_i = 5 - i;
40     start[i] =
41         begin_count < padded_i ? 0 : op_params.begin[begin_count - padded_i];
42     stop[i] =
43         (size_count < padded_i || op_params.size[size_count - padded_i] == -1)
44             ? ext_shape.Dims(i)
45             : start[i] + op_params.size[size_count - padded_i];
46   }
47 
48   for (int i0 = start[0]; i0 < stop[0]; ++i0) {
49     for (int i1 = start[1]; i1 < stop[1]; ++i1) {
50       for (int i2 = start[2]; i2 < stop[2]; ++i2) {
51         for (int i3 = start[3]; i3 < stop[3]; ++i3) {
52           for (int i4 = start[4]; i4 < stop[4]; ++i4) {
53             writer->Write(Offset(ext_shape, i0, i1, i2, i3, i4));
54           }
55         }
56       }
57     }
58   }
59 }
60 
61 template <typename T>
Slice(const tflite::SliceParams & op_params,const RuntimeShape & input_shape,const T * input_data,const RuntimeShape & output_shape,T * output_data)62 inline void Slice(const tflite::SliceParams& op_params,
63                   const RuntimeShape& input_shape, const T* input_data,
64                   const RuntimeShape& output_shape, T* output_data) {
65   SequentialTensorWriter<T> writer(input_data, output_data);
66   return Slice(op_params, input_shape, output_shape, &writer);
67 }
68 
69 template <typename T>
Slice(const tflite::SliceParams & op_params,const RuntimeShape & input_shape,const TfLiteTensor * input,const RuntimeShape & output_shape,TfLiteTensor * output)70 inline void Slice(const tflite::SliceParams& op_params,
71                   const RuntimeShape& input_shape, const TfLiteTensor* input,
72                   const RuntimeShape& output_shape, TfLiteTensor* output) {
73   SequentialTensorWriter<T> writer(input, output);
74   return Slice(op_params, input_shape, output_shape, &writer);
75 }
76 
77 }  // namespace reference_ops
78 }  // namespace tflite
79 
80 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SLICE_H_
81