1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // Contains the implementation of the operations.
18
19 #define LOG_TAG "Operations"
20
21 #include <vector>
22
23 #include "OperationResolver.h"
24 #include "Operations.h"
25 #include "Tracing.h"
26
27 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
28 #include <tensorflow/lite/kernels/internal/reference/legacy_reference_ops.h>
29
30 #include "CpuOperationUtils.h"
31 #endif // NN_INCLUDE_CPU_IMPLEMENTATION
32
33 namespace android {
34 namespace nn {
35 namespace strided_slice {
36
37 constexpr uint32_t kNumInputs = 7;
38 constexpr uint32_t kInputTensor = 0;
39 constexpr uint32_t kBeginTensor = 1;
40 constexpr uint32_t kEndTensor = 2;
41 constexpr uint32_t kStridesTensor = 3;
42 constexpr uint32_t kBeginMask = 4;
43 constexpr uint32_t kEndMask = 5;
44 constexpr uint32_t kShrinkAxisMask = 6;
45
46 constexpr uint32_t kNumOutputs = 1;
47 constexpr uint32_t kOutputTensor = 0;
48
49 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
50 namespace {
51
52 template <typename T>
compute(const T * inputData,const Shape & inputShape,const int32_t * beginData,const int32_t * endData,const int32_t * stridesData,int32_t beginMask,int32_t endMask,int32_t shrinkAxisMask,T * outputData,const Shape & outputShape)53 bool compute(const T* inputData, const Shape& inputShape, const int32_t* beginData,
54 const int32_t* endData, const int32_t* stridesData, int32_t beginMask, int32_t endMask,
55 int32_t shrinkAxisMask, T* outputData, const Shape& outputShape) {
56 NNTRACE_TRANS("stridedSlice");
57 // This Op only supports 1-4D cases and since we use the reference 4D
58 // implementation, the 1-3D tensors are mapped to 4D.
59 const int kMaxDim = 4;
60
61 std::vector<int> starts;
62 std::vector<int> stops;
63 std::vector<int> strides;
64
65 int32_t numInputDims = static_cast<int32_t>(getNumberOfDimensions(inputShape));
66 for (int32_t idx = numInputDims - 1; idx >= 0; --idx) {
67 starts.emplace_back(beginData[idx]);
68 stops.emplace_back(endData[idx]);
69 strides.emplace_back(stridesData[idx]);
70 }
71
72 for (int i = numInputDims; i < kMaxDim; i++) {
73 starts.emplace_back(0);
74 stops.emplace_back(1);
75 strides.emplace_back(1);
76 }
77
78 beginMask = ReverseMaskBits(beginMask, numInputDims);
79 endMask = ReverseMaskBits(endMask, numInputDims);
80 shrinkAxisMask = ReverseMaskBits(shrinkAxisMask, numInputDims);
81
82 tflite::reference_ops::StridedSlice(inputData, convertShapeToDims(inputShape), beginMask,
83 endMask, shrinkAxisMask, starts, stops, strides, outputData,
84 convertShapeToDims(outputShape));
85
86 return true;
87 }
88
89 template <typename T>
executeTyped(IOperationExecutionContext * context)90 bool executeTyped(IOperationExecutionContext* context) {
91 return compute<T>(
92 context->getInputBuffer<T>(kInputTensor), context->getInputShape(kInputTensor),
93 context->getInputBuffer<int32_t>(kBeginTensor),
94 context->getInputBuffer<int32_t>(kEndTensor),
95 context->getInputBuffer<int32_t>(kStridesTensor),
96 context->getInputValue<int32_t>(kBeginMask), context->getInputValue<int32_t>(kEndMask),
97 context->getInputValue<int32_t>(kShrinkAxisMask),
98 context->getOutputBuffer<T>(kOutputTensor), context->getOutputShape(kOutputTensor));
99 }
100
101 } // namespace
102 #endif // NN_INCLUDE_CPU_IMPLEMENTATION
103
validate(const IOperationValidationContext * context)104 Result<Version> validate(const IOperationValidationContext* context) {
105 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
106 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
107 OperandType inputType = context->getInputType(kInputTensor);
108 NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
109 inputType == OperandType::TENSOR_FLOAT32 ||
110 inputType == OperandType::TENSOR_QUANT8_ASYMM ||
111 inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
112 << "Unsupported input operand type for STRIDED_SLICE op: " << inputType;
113
114 Version minSupportedVersion;
115 if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
116 minSupportedVersion = Version::ANDROID_R;
117 } else if (inputType == OperandType::TENSOR_FLOAT16) {
118 minSupportedVersion = Version::ANDROID_Q;
119 } else {
120 minSupportedVersion = Version::ANDROID_P;
121 }
122
123 NN_RET_CHECK(validateInputTypes(context, {
124 inputType,
125 OperandType::TENSOR_INT32,
126 OperandType::TENSOR_INT32,
127 OperandType::TENSOR_INT32,
128 OperandType::INT32,
129 OperandType::INT32,
130 OperandType::INT32,
131 }));
132 NN_RET_CHECK(validateOutputTypes(context, {inputType}));
133 const Shape& input = context->getInputShape(kInputTensor);
134 if (hasKnownRank(input)) {
135 NN_RET_CHECK_LE(getNumberOfDimensions(input), 4);
136 }
137 return minSupportedVersion;
138 }
139
140 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
prepare(IOperationExecutionContext * context)141 bool prepare(IOperationExecutionContext* context) {
142 // StridedSlice op only supports 1D-4D input arrays.
143 const Shape& inputShape = context->getInputShape(kInputTensor);
144 uint32_t numInputDims = getNumberOfDimensions(inputShape);
145 NN_OPS_CHECK(numInputDims <= 4);
146
147 const Shape& beginShape = context->getInputShape(kBeginTensor);
148 const Shape& endShape = context->getInputShape(kEndTensor);
149 const Shape& stridesShape = context->getInputShape(kStridesTensor);
150
151 NN_OPS_CHECK(getNumberOfDimensions(beginShape) == 1);
152 NN_OPS_CHECK(getNumberOfDimensions(endShape) == 1);
153 NN_OPS_CHECK(getNumberOfDimensions(stridesShape) == 1);
154
155 NN_OPS_CHECK(getSizeOfDimension(beginShape, 0) == numInputDims);
156 NN_OPS_CHECK(getSizeOfDimension(endShape, 0) == numInputDims);
157 NN_OPS_CHECK(getSizeOfDimension(stridesShape, 0) == numInputDims);
158
159 NN_OPS_CHECK(beginShape.type == OperandType::TENSOR_INT32);
160 NN_OPS_CHECK(endShape.type == OperandType::TENSOR_INT32);
161 NN_OPS_CHECK(stridesShape.type == OperandType::TENSOR_INT32);
162
163 const int32_t* beginData = context->getInputBuffer<int32_t>(kBeginTensor);
164 const int32_t* endData = context->getInputBuffer<int32_t>(kEndTensor);
165 const int32_t* stridesData = context->getInputBuffer<int32_t>(kStridesTensor);
166
167 const int32_t beginMask = context->getInputValue<int32_t>(kBeginMask);
168 const int32_t endMask = context->getInputValue<int32_t>(kEndMask);
169 const int32_t shrinkAxisMask = context->getInputValue<int32_t>(kShrinkAxisMask);
170
171 // Determine size of output tensor and map indices
172 std::vector<uint32_t> outDims;
173 for (int32_t idx = 0; idx < static_cast<int32_t>(numInputDims); idx++) {
174 int32_t dim = static_cast<int32_t>(getSizeOfDimension(inputShape, idx));
175 int32_t stride = stridesData[idx];
176 // stride value has to be non-zero
177 NN_OPS_CHECK(stride != 0);
178 bool positiveStride = stride > 0;
179
180 int32_t begin = beginMask & (1 << idx) ? positiveStride ? 0 : dim - 1
181 : ClampedIndex(beginData[idx], dim, positiveStride);
182 int32_t end = endMask & (1 << idx) ? positiveStride ? dim : -1
183 : ClampedIndex(endData[idx], dim, positiveStride);
184
185 // This is valid for both positive and negative strides
186 int32_t outDim = ceil((end - begin) / static_cast<float>(stride));
187 outDim = outDim < 0 ? 0 : static_cast<uint32_t>(outDim);
188 if (!(shrinkAxisMask & (1 << idx))) {
189 outDims.push_back(outDim);
190 } else {
191 // Only positive stride is allowed on non-range indexing (i.e. shrinkMask is set).
192 NN_RET_CHECK_GT(stride, 0) << "index = " << idx;
193 NN_RET_CHECK_EQ(outDim, 1) << "index = " << idx;
194 }
195 }
196
197 // Handle the case when all dimensions are removed
198 if (outDims.empty()) {
199 outDims.push_back(1);
200 }
201
202 Shape outputShape = context->getOutputShape(kOutputTensor);
203 NN_RET_CHECK(SetShape(inputShape, &outputShape));
204 outputShape.dimensions = outDims;
205 return context->setOutputShape(kOutputTensor, outputShape);
206 }
207
execute(IOperationExecutionContext * context)208 bool execute(IOperationExecutionContext* context) {
209 switch (context->getInputType(kInputTensor)) {
210 case OperandType::TENSOR_FLOAT16:
211 return executeTyped<_Float16>(context);
212 case OperandType::TENSOR_FLOAT32:
213 return executeTyped<float>(context);
214 case OperandType::TENSOR_QUANT8_ASYMM:
215 return executeTyped<uint8_t>(context);
216 case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
217 return executeTyped<int8_t>(context);
218 default:
219 NN_RET_CHECK_FAIL() << "Unsupported tensor type for STRIDED_SLICE op.";
220 }
221 }
222 #endif // NN_INCLUDE_CPU_IMPLEMENTATION
223
224 } // namespace strided_slice
225
226 NN_REGISTER_OPERATION(STRIDED_SLICE, "STRIDED_SLICE", strided_slice::validate,
227 strided_slice::prepare, strided_slice::execute);
228
229 } // namespace nn
230 } // namespace android
231