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