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 #define LOG_TAG "Operations"
18 
19 #include <vector>
20 
21 #include "IndexedShapeWrapper.h"
22 #include "OperationResolver.h"
23 
24 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
25 #include "CpuOperationUtils.h"
26 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
27 
28 namespace android {
29 namespace nn {
30 namespace slice {
31 
32 constexpr char kOperationName[] = "SLICE";
33 
34 constexpr uint32_t kNumInputs = 3;
35 constexpr uint32_t kInputTensor = 0;
36 constexpr uint32_t kBeginTensor = 1;
37 constexpr uint32_t kSizeTensor = 2;
38 
39 constexpr uint32_t kNumOutputs = 1;
40 constexpr uint32_t kOutputTensor = 0;
41 
42 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
43 namespace {
44 
45 template <typename T>
addVectors(const std::vector<T> & a,const std::vector<T> & b,std::vector<T> * res)46 void addVectors(const std::vector<T>& a, const std::vector<T>& b, std::vector<T>* res) {
47     for (int i = 0; i < res->size(); ++i) {
48         res->at(i) = a[i] + b[i];
49     }
50 }
51 
52 template <typename T>
evalGeneric(const T * inputData,const Shape & inputShape,const int32_t * beginData,const Shape & beginShape,const int32_t * sizeData,const Shape & sizeShape,T * outputData,const Shape & outputShape)53 bool evalGeneric(const T* inputData, const Shape& inputShape, const int32_t* beginData,
54                  const Shape& beginShape, const int32_t* sizeData, const Shape& sizeShape,
55                  T* outputData, const Shape& outputShape) {
56     const int outputSize = getNumberOfElements(outputShape);
57     const IndexedShapeWrapper indexedOutput = IndexedShapeWrapper(outputShape);
58     const IndexedShapeWrapper indexedInput = IndexedShapeWrapper(inputShape);
59     std::vector<uint32_t> outputIndex(getNumberOfDimensions(outputShape), 0);
60     std::vector<uint32_t> beginIndex(getSizeOfDimension(beginShape, 0));
61     std::vector<uint32_t> inputIndex(getNumberOfDimensions(inputShape));
62 
63     for (int i = 0; i < beginIndex.size(); ++i) {
64         beginIndex[i] = static_cast<uint32_t>(beginData[i]);
65     }
66 
67     bool lastIndex = false;
68     uint32_t outputOffset;
69     uint32_t inputOffset;
70 
71     do {
72         addVectors(outputIndex, beginIndex, &inputIndex);
73 
74         NN_RET_CHECK(indexedOutput.indexToFlatIndex(outputIndex, &outputOffset));
75         NN_RET_CHECK(indexedInput.indexToFlatIndex(inputIndex, &inputOffset));
76 
77         outputData[outputOffset] = inputData[inputOffset];
78         NN_RET_CHECK(indexedOutput.nextIndexInplace(&outputIndex, &lastIndex));
79     } while (!lastIndex);
80     return true;
81 }
82 
83 }  // namespace
84 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
85 
validate(const IOperationValidationContext * context)86 Result<Version> validate(const IOperationValidationContext* context) {
87     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
88     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
89 
90     const OperandType inputType = context->getInputType(kInputTensor);
91     NN_RET_CHECK(inputType == OperandType::TENSOR_FLOAT16 ||
92                  inputType == OperandType::TENSOR_FLOAT32 ||
93                  inputType == OperandType::TENSOR_INT32 ||
94                  inputType == OperandType::TENSOR_QUANT8_ASYMM ||
95                  inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
96             << "Unsupported tensor type for operation " << kOperationName;
97     auto minSupportedVersion = Version::ANDROID_OC_MR1;
98     if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
99         minSupportedVersion = Version::ANDROID_R;
100     } else {
101         minSupportedVersion = Version::ANDROID_Q;
102     }
103     NN_RET_CHECK(validateInputTypes(
104             context, {inputType, OperandType::TENSOR_INT32, OperandType::TENSOR_INT32}));
105     NN_RET_CHECK(validateOutputTypes(context, {inputType}));
106     return minSupportedVersion;
107 }
108 
109 #ifdef NN_INCLUDE_CPU_IMPLEMENTATION
prepare(IOperationExecutionContext * context)110 bool prepare(IOperationExecutionContext* context) {
111     const Shape& inputShape = context->getInputShape(kInputTensor);
112     const int32_t n_dims = getNumberOfDimensions(inputShape);
113     NN_RET_CHECK(n_dims > 0);
114 
115     const Shape& beginShape = context->getInputShape(kBeginTensor);
116     NN_RET_CHECK_EQ(getNumberOfDimensions(beginShape), 1);
117     NN_RET_CHECK_EQ(getSizeOfDimension(beginShape, 0), n_dims);
118 
119     const Shape& sizeShape = context->getInputShape(kSizeTensor);
120     NN_RET_CHECK_EQ(getNumberOfDimensions(sizeShape), 1);
121     NN_RET_CHECK_EQ(getSizeOfDimension(sizeShape, 0), n_dims);
122 
123     const int32_t* beginData = context->getInputBuffer<int32_t>(kBeginTensor);
124     const int32_t* sizeData = context->getInputBuffer<int32_t>(kSizeTensor);
125 
126     Shape outputShape = context->getOutputShape(kOutputTensor);
127     outputShape.dimensions.resize(n_dims);
128     for (int i = 0; i < n_dims; ++i) {
129         const int32_t sliceBegin = beginData[i];
130         int32_t sliceSize = sizeData[i];
131         if (sliceSize == -1) {
132             sliceSize = getSizeOfDimension(inputShape, i) - sliceBegin;
133         }
134         NN_RET_CHECK_LE(beginData[i], getSizeOfDimension(inputShape, i));
135         NN_RET_CHECK_GE(sliceSize, 0);
136         NN_RET_CHECK_LE(sliceBegin + sliceSize, getSizeOfDimension(inputShape, i));
137         outputShape.dimensions[i] = sliceSize;
138     }
139     return context->setOutputShape(kOutputTensor, outputShape);
140 }
141 
execute(IOperationExecutionContext * context)142 bool execute(IOperationExecutionContext* context) {
143     // Bypass execution in the case of zero-sized input.
144     if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
145     switch (context->getInputType(kInputTensor)) {
146         case OperandType::TENSOR_FLOAT16:
147             return evalGeneric(context->getInputBuffer<_Float16>(kInputTensor),
148                                context->getInputShape(kInputTensor),
149                                context->getInputBuffer<int32_t>(kBeginTensor),
150                                context->getInputShape(kBeginTensor),
151                                context->getInputBuffer<int32_t>(kSizeTensor),
152                                context->getInputShape(kSizeTensor),
153                                context->getOutputBuffer<_Float16>(kOutputTensor),
154                                context->getOutputShape(kOutputTensor));
155         case OperandType::TENSOR_FLOAT32:
156             return evalGeneric(context->getInputBuffer<float>(kInputTensor),
157                                context->getInputShape(kInputTensor),
158                                context->getInputBuffer<int32_t>(kBeginTensor),
159                                context->getInputShape(kBeginTensor),
160                                context->getInputBuffer<int32_t>(kSizeTensor),
161                                context->getInputShape(kSizeTensor),
162                                context->getOutputBuffer<float>(kOutputTensor),
163                                context->getOutputShape(kOutputTensor));
164         case OperandType::TENSOR_INT32:
165             return evalGeneric(context->getInputBuffer<int32_t>(kInputTensor),
166                                context->getInputShape(kInputTensor),
167                                context->getInputBuffer<int32_t>(kBeginTensor),
168                                context->getInputShape(kBeginTensor),
169                                context->getInputBuffer<int32_t>(kSizeTensor),
170                                context->getInputShape(kSizeTensor),
171                                context->getOutputBuffer<int32_t>(kOutputTensor),
172                                context->getOutputShape(kOutputTensor));
173         case OperandType::TENSOR_QUANT8_ASYMM:
174             return evalGeneric(context->getInputBuffer<uint8_t>(kInputTensor),
175                                context->getInputShape(kInputTensor),
176                                context->getInputBuffer<int32_t>(kBeginTensor),
177                                context->getInputShape(kBeginTensor),
178                                context->getInputBuffer<int32_t>(kSizeTensor),
179                                context->getInputShape(kSizeTensor),
180                                context->getOutputBuffer<uint8_t>(kOutputTensor),
181                                context->getOutputShape(kOutputTensor));
182         case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
183             return evalGeneric(context->getInputBuffer<int8_t>(kInputTensor),
184                                context->getInputShape(kInputTensor),
185                                context->getInputBuffer<int32_t>(kBeginTensor),
186                                context->getInputShape(kBeginTensor),
187                                context->getInputBuffer<int32_t>(kSizeTensor),
188                                context->getInputShape(kSizeTensor),
189                                context->getOutputBuffer<int8_t>(kOutputTensor),
190                                context->getOutputShape(kOutputTensor));
191         default:
192             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
193     }
194 }
195 #endif  // NN_INCLUDE_CPU_IMPLEMENTATION
196 
197 }  // namespace slice
198 
199 NN_REGISTER_OPERATION(SLICE, slice::kOperationName, slice::validate, slice::prepare, slice::execute,
200                       .allowZeroSizedInput = true);
201 
202 }  // namespace nn
203 }  // namespace android
204