• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STRIDED_SLICE_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_STRIDED_SLICE_H_
17 
18 #include "ruy/profiler/instrumentation.h"  // from @ruy
19 #include "tensorflow/lite/kernels/internal/common.h"
20 #include "tensorflow/lite/kernels/internal/compatibility.h"
21 #include "tensorflow/lite/kernels/internal/portable_tensor.h"
22 #include "tensorflow/lite/kernels/internal/strided_slice_logic.h"
23 #include "tensorflow/lite/kernels/internal/types.h"
24 
25 namespace tflite {
26 
27 namespace reference_ops {
28 
29 template <typename T>
StridedSlice(const tflite::StridedSliceParams & op_params,const RuntimeShape & unextended_input_shape,const RuntimeShape & unextended_output_shape,SequentialTensorWriter<T> * writer)30 inline void StridedSlice(const tflite::StridedSliceParams& op_params,
31                          const RuntimeShape& unextended_input_shape,
32                          const RuntimeShape& unextended_output_shape,
33                          SequentialTensorWriter<T>* writer) {
34   using strided_slice::LoopCondition;
35   using strided_slice::StartForAxis;
36   using strided_slice::StopForAxis;
37 
38   ruy::profiler::ScopeLabel label("StridedSlice");
39 
40   // Note that the output_shape is not used herein.
41   tflite::StridedSliceParams params_copy = op_params;
42 
43   TFLITE_DCHECK_LE(unextended_input_shape.DimensionsCount(), 5);
44   TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 5);
45   const RuntimeShape input_shape =
46       RuntimeShape::ExtendedShape(5, unextended_input_shape);
47   const RuntimeShape output_shape =
48       RuntimeShape::ExtendedShape(5, unextended_output_shape);
49 
50   // Reverse and pad to 5 dimensions because that is what the runtime code
51   // requires (ie. all shapes must be 5D and are given backwards).
52   strided_slice::StridedSlicePadIndices(&params_copy, 5);
53 
54   const int start_0 = StartForAxis(params_copy, input_shape, 0);
55   const int stop_0 = StopForAxis(params_copy, input_shape, 0, start_0);
56   const int start_1 = StartForAxis(params_copy, input_shape, 1);
57   const int stop_1 = StopForAxis(params_copy, input_shape, 1, start_1);
58   const int start_2 = StartForAxis(params_copy, input_shape, 2);
59   const int stop_2 = StopForAxis(params_copy, input_shape, 2, start_2);
60   const int start_3 = StartForAxis(params_copy, input_shape, 3);
61   const int stop_3 = StopForAxis(params_copy, input_shape, 3, start_3);
62   const int start_4 = StartForAxis(params_copy, input_shape, 4);
63   const int stop_4 = StopForAxis(params_copy, input_shape, 4, start_4);
64 
65   for (int offset_0 = start_0 * input_shape.Dims(1),
66            end_0 = stop_0 * input_shape.Dims(1),
67            step_0 = params_copy.strides[0] * input_shape.Dims(1);
68        !LoopCondition(offset_0, end_0, params_copy.strides[0]);
69        offset_0 += step_0) {
70     for (int offset_1 = (offset_0 + start_1) * input_shape.Dims(2),
71              end_1 = (offset_0 + stop_1) * input_shape.Dims(2),
72              step_1 = params_copy.strides[1] * input_shape.Dims(2);
73          !LoopCondition(offset_1, end_1, params_copy.strides[1]);
74          offset_1 += step_1) {
75       for (int offset_2 = (offset_1 + start_2) * input_shape.Dims(3),
76                end_2 = (offset_1 + stop_2) * input_shape.Dims(3),
77                step_2 = params_copy.strides[2] * input_shape.Dims(3);
78            !LoopCondition(offset_2, end_2, params_copy.strides[2]);
79            offset_2 += step_2) {
80         for (int offset_3 = (offset_2 + start_3) * input_shape.Dims(4),
81                  end_3 = (offset_2 + stop_3) * input_shape.Dims(4),
82                  step_3 = params_copy.strides[3] * input_shape.Dims(4);
83              !LoopCondition(offset_3, end_3, params_copy.strides[3]);
84              offset_3 += step_3) {
85           for (int offset_4 = offset_3 + start_4, end_4 = offset_3 + stop_4;
86                !LoopCondition(offset_4, end_4, params_copy.strides[4]);
87                offset_4 += params_copy.strides[4]) {
88             writer->Write(offset_4);
89           }
90         }
91       }
92     }
93   }
94 }
95 
96 template <typename T>
StridedSlice(const tflite::StridedSliceParams & op_params,const RuntimeShape & unextended_input_shape,const T * input_data,const RuntimeShape & unextended_output_shape,T * output_data)97 inline void StridedSlice(const tflite::StridedSliceParams& op_params,
98                          const RuntimeShape& unextended_input_shape,
99                          const T* input_data,
100                          const RuntimeShape& unextended_output_shape,
101                          T* output_data) {
102   SequentialTensorWriter<T> writer(input_data, output_data);
103   StridedSlice<T>(op_params, unextended_input_shape, unextended_output_shape,
104                   &writer);
105 }
106 
107 template <typename T>
StridedSlice(const tflite::StridedSliceParams & op_params,const RuntimeShape & unextended_input_shape,const TfLiteTensor * input,const RuntimeShape & unextended_output_shape,TfLiteTensor * output)108 inline void StridedSlice(const tflite::StridedSliceParams& op_params,
109                          const RuntimeShape& unextended_input_shape,
110                          const TfLiteTensor* input,
111                          const RuntimeShape& unextended_output_shape,
112                          TfLiteTensor* output) {
113   SequentialTensorWriter<T> writer(input, output);
114   StridedSlice<T>(op_params, unextended_input_shape, unextended_output_shape,
115                   &writer);
116 }
117 
118 }  // namespace reference_ops
119 }  // namespace tflite
120 
121 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_STRIDED_SLICE_H_
122