1 /*
2  * Copyright (C) 2017 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 #include "OperationsUtils.h"
18 #define LOG_TAG "Operations"
19 
20 #include <tensorflow/lite/kernels/internal/optimized/legacy_optimized_ops.h>
21 #include <tensorflow/lite/kernels/internal/reference/legacy_reference_ops.h>
22 #include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
23 #include <tensorflow/lite/kernels/internal/types.h>
24 
25 #include <algorithm>
26 #include <iterator>
27 #include <vector>
28 
29 #include "CpuOperationUtils.h"
30 #include "HalInterfaces.h"
31 #include "OperationResolver.h"
32 #include "Tracing.h"
33 
34 namespace android {
35 namespace nn {
36 namespace concatenation {
37 
38 constexpr char kOperationName[] = "CONCATENATION";
39 
40 constexpr uint32_t kNumOutputs = 1;
41 constexpr uint32_t kOutputTensor = 0;
42 
43 namespace {
44 
45 using namespace hal;
46 
47 template <typename T>
concatenation(const std::vector<const T * > & inputDataPtrs,const std::vector<Shape> & inputShapes,int32_t axis,T * outputData,const Shape & outputShape)48 bool concatenation(const std::vector<const T*>& inputDataPtrs,
49                    const std::vector<Shape>& inputShapes, int32_t axis, T* outputData,
50                    const Shape& outputShape) {
51     NNTRACE_TRANS("concatenation");
52     int num_inputs = inputShapes.size();
53     std::vector<tflite::Dims<4>*> inputDimsPtr(num_inputs);
54     std::vector<tflite::Dims<4>> inputDims(num_inputs);
55     for (int i = 0; i < num_inputs; i++) {
56         inputDims[i] = convertShapeToDims(inputShapes[i]);
57         inputDimsPtr[i] = &inputDims[i];
58     }
59     NNTRACE_COMP_SWITCH("optimized_ops::Concatenation");
60     tflite::optimized_ops::Concatenation<tflite::FusedActivationFunctionType::kNone, T>(
61             getNumberOfDimensions(outputShape) - axis - 1, inputDataPtrs.data(),
62             inputDimsPtr.data(), num_inputs, outputData, convertShapeToDims(outputShape));
63 
64     return true;
65 }
66 
67 template <>
concatenation(const std::vector<const uint8_t * > & inputDataPtrs,const std::vector<Shape> & inputShapes,int32_t axis,uint8_t * outputData,const Shape & outputShape)68 bool concatenation<uint8_t>(const std::vector<const uint8_t*>& inputDataPtrs,
69                             const std::vector<Shape>& inputShapes, int32_t axis,
70                             uint8_t* outputData, const Shape& outputShape) {
71     NNTRACE_TRANS("concatenationQuant8");
72     int num_inputs = inputShapes.size();
73     std::vector<float> inputScales(num_inputs);
74     std::vector<int32> inputOffsets(num_inputs);
75     std::vector<tflite::Dims<4>*> inputDimsPtr(num_inputs);
76     std::vector<tflite::Dims<4>> inputDims(num_inputs);
77     for (int i = 0; i < num_inputs; i++) {
78         inputScales[i] = inputShapes[i].scale;
79         inputOffsets[i] = inputShapes[i].offset;
80         inputDims[i] = convertShapeToDims(inputShapes[i]);
81         inputDimsPtr[i] = &inputDims[i];
82     }
83 
84     NNTRACE_COMP_SWITCH("reference_ops::Concatenation");
85     tflite::reference_ops::Concatenation(
86             getNumberOfDimensions(outputShape) - axis - 1, inputDataPtrs.data(),
87             inputDimsPtr.data(), inputOffsets.data(), inputScales.data(), num_inputs, outputData,
88             convertShapeToDims(outputShape), outputShape.offset, outputShape.scale);
89 
90     return true;
91 }
92 
93 template <typename T>
concatenation(IOperationExecutionContext * context)94 inline bool concatenation(IOperationExecutionContext* context) {
95     uint32_t inputCount = context->getNumInputs() - 1;
96     std::vector<const T*> inputDatas;
97     std::vector<Shape> inputShapes;
98     for (uint32_t i = 0; i < inputCount; ++i) {
99         const T* buffer = context->getInputBuffer<T>(i);
100         if (buffer == nullptr) continue;
101         inputDatas.push_back(buffer);
102         inputShapes.push_back(context->getInputShape(i));
103     }
104     return concatenation(inputDatas, inputShapes, context->getInputValue<int32_t>(inputCount),
105                          context->getOutputBuffer<T>(kOutputTensor),
106                          context->getOutputShape(kOutputTensor));
107 }
108 
109 template <>
concatenation(IOperationExecutionContext * context)110 inline bool concatenation<int8_t>(IOperationExecutionContext* context) {
111     uint32_t inputCount = context->getNumInputs() - 1;
112     std::vector<std::vector<uint8_t>> inputs_uint8(inputCount);
113     for (int i = 0; i < inputCount; ++i) {
114         const auto currentSize = getNumberOfElements(context->getInputShape(i));
115         inputs_uint8[i].resize(currentSize);
116         if (currentSize != 0) {
117             convertInt8ToUInt8(context->getInputBuffer<int8_t>(i), &inputs_uint8[i]);
118         }
119     }
120     std::vector<const uint8_t*> inputDatas;
121     std::vector<Shape> inputShapes;
122     for (uint32_t i = 0; i < inputCount; ++i) {
123         inputDatas.push_back(inputs_uint8[i].data());
124         inputShapes.push_back(context->getInputShape(i));
125         inputShapes[i].offset += 128;
126     }
127 
128     std::vector<uint8_t> output_uint8(getNumberOfElements(context->getOutputShape(kOutputTensor)));
129     Shape outputShape(context->getOutputShape(kOutputTensor));
130     outputShape.offset += 128;
131     NN_RET_CHECK(concatenation(inputDatas, inputShapes, context->getInputValue<int32_t>(inputCount),
132                                output_uint8.data(), outputShape));
133 
134     convertUInt8ToInt8(output_uint8, context->getOutputBuffer<int8_t>(kOutputTensor));
135 
136     return true;
137 }
138 
139 }  // namespace
140 
validate(const IOperationValidationContext * context)141 bool validate(const IOperationValidationContext* context) {
142     uint32_t inputCount = context->getNumInputs();
143     NN_RET_CHECK_GE(inputCount, 2);
144     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
145     const OperandType inputType = context->getInputType(0);
146     if (inputType == OperandType::TENSOR_FLOAT32 || inputType == OperandType::TENSOR_QUANT8_ASYMM) {
147         NN_RET_CHECK(validateHalVersion(context, HalVersion::V1_0));
148     } else if (inputType == OperandType::TENSOR_FLOAT16) {
149         NN_RET_CHECK(validateHalVersion(context, HalVersion::V1_2));
150     } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
151         NN_RET_CHECK(validateHalVersion(context, HalVersion::V1_3));
152     } else {
153         NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
154     }
155     std::vector<OperandType> inExpectedTypes(inputCount - 1, inputType);
156     inExpectedTypes.push_back(OperandType::INT32);
157     if (context->getHalVersion() < HalVersion::V1_2 &&
158         inputType == OperandType::TENSOR_QUANT8_ASYMM) {
159         const Shape& output = context->getOutputShape(kOutputTensor);
160         for (uint32_t i = 0; i < inputCount - 1; ++i) {
161             const Shape& input = context->getInputShape(i);
162             NN_RET_CHECK_EQ(input.scale, output.scale);
163             NN_RET_CHECK_EQ(input.offset, output.offset);
164         }
165     }
166     for (uint32_t i = 0; i < inputCount - 1; ++i) {
167         const uint32_t inputRank = getNumberOfDimensions(context->getInputShape(i));
168         if (inputRank != 0) {
169             NN_RET_CHECK_LE(inputRank, 4);
170         }
171     }
172     return validateInputTypes(context, inExpectedTypes) &&
173            validateOutputTypes(context, {inputType});
174 }
175 
prepare(IOperationExecutionContext * context)176 bool prepare(IOperationExecutionContext* context) {
177     uint32_t numInputs = context->getNumInputs();
178     NN_RET_CHECK_GE(numInputs, 2);
179     const Shape& input0 = context->getInputShape(0);
180     uint32_t numDimensions = getNumberOfDimensions(input0);
181     int32_t axis = context->getInputValue<int32_t>(numInputs - 1);
182     NN_RET_CHECK_GE(axis, 0);
183     NN_RET_CHECK_LT(axis, numDimensions);
184     NN_RET_CHECK_LE(numDimensions, 4);
185 
186     uint32_t sumAxis = getSizeOfDimension(input0, axis);
187     for (uint32_t i = 1; i < numInputs - 1; ++i) {
188         const Shape& input = context->getInputShape(i);
189         NN_RET_CHECK_EQ(getNumberOfDimensions(input), numDimensions);
190         NN_RET_CHECK(input.type == input0.type);
191         for (uint32_t d = 0; d < numDimensions; ++d) {
192             if (d == axis) {
193                 sumAxis += getSizeOfDimension(input, axis);
194             } else {
195                 NN_RET_CHECK_EQ(getSizeOfDimension(input0, d), getSizeOfDimension(input, d));
196             }
197         }
198     }
199 
200     Shape output = context->getOutputShape(kOutputTensor);
201     output.type = input0.type;
202     output.dimensions = input0.dimensions;
203     output.dimensions[axis] = sumAxis;
204     return context->setOutputShape(kOutputTensor, output);
205 }
206 
execute(IOperationExecutionContext * context)207 bool execute(IOperationExecutionContext* context) {
208     // Bypass execution in the case of zero-sized input.
209     if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true;
210     switch (context->getInputType(0)) {
211         case OperandType::TENSOR_FLOAT16:
212             return concatenation<_Float16>(context);
213         case OperandType::TENSOR_FLOAT32:
214             return concatenation<float>(context);
215         case OperandType::TENSOR_QUANT8_ASYMM:
216             return concatenation<uint8_t>(context);
217         case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
218             return concatenation<int8_t>(context);
219         default:
220             NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
221     }
222 }
223 
224 }  // namespace concatenation
225 
226 NN_REGISTER_OPERATION(CONCATENATION, concatenation::kOperationName, concatenation::validate,
227                       concatenation::prepare, concatenation::execute, .allowZeroSizedInput = true);
228 
229 }  // namespace nn
230 }  // namespace android
231