/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "Operations" #include "Concatenation.h" #include #include #include #include "OperationResolver.h" #include "OperationsExecutionUtils.h" #include "Tracing.h" #include "nnapi/Validation.h" #ifdef NN_INCLUDE_CPU_IMPLEMENTATION #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-parameter" #pragma clang diagnostic ignored "-Wsign-compare" #pragma clang diagnostic ignored "-Winvalid-partial-specialization" #include #include #include #include #pragma clang diagnostic pop #include "CpuOperationUtils.h" #endif // NN_INCLUDE_CPU_IMPLEMENTATION namespace android { namespace nn { namespace concatenation { #ifdef NN_INCLUDE_CPU_IMPLEMENTATION namespace { template bool concatenation(const std::vector& inputDataPtrs, const std::vector& inputShapes, int32_t axis, T* outputData, const Shape& outputShape) { NNTRACE_TRANS("concatenation"); int num_inputs = inputShapes.size(); std::vector*> inputDimsPtr(num_inputs); std::vector> inputDims(num_inputs); for (int i = 0; i < num_inputs; i++) { inputDims[i] = convertShapeToDims(inputShapes[i]); inputDimsPtr[i] = &inputDims[i]; } NNTRACE_COMP_SWITCH("optimized_ops::Concatenation"); tflite::optimized_ops::Concatenation( getNumberOfDimensions(outputShape) - axis - 1, inputDataPtrs.data(), inputDimsPtr.data(), num_inputs, outputData, convertShapeToDims(outputShape)); return true; } template <> bool concatenation(const std::vector& inputDataPtrs, const std::vector& inputShapes, int32_t axis, uint8_t* outputData, const Shape& outputShape) { NNTRACE_TRANS("concatenationQuant8"); int num_inputs = inputShapes.size(); std::vector inputScales(num_inputs); std::vector inputOffsets(num_inputs); std::vector*> inputDimsPtr(num_inputs); std::vector> inputDims(num_inputs); for (int i = 0; i < num_inputs; i++) { inputScales[i] = inputShapes[i].scale; inputOffsets[i] = inputShapes[i].offset; inputDims[i] = convertShapeToDims(inputShapes[i]); inputDimsPtr[i] = &inputDims[i]; } NNTRACE_COMP_SWITCH("reference_ops::Concatenation"); tflite::reference_ops::Concatenation( getNumberOfDimensions(outputShape) - axis - 1, inputDataPtrs.data(), inputDimsPtr.data(), inputOffsets.data(), inputScales.data(), num_inputs, outputData, convertShapeToDims(outputShape), outputShape.offset, outputShape.scale); return true; } template inline bool concatenation(IOperationExecutionContext* context) { uint32_t inputCount = context->getNumInputs() - 1; std::vector inputDatas; std::vector inputShapes; for (uint32_t i = 0; i < inputCount; ++i) { const T* buffer = context->getInputBuffer(i); if (buffer == nullptr) continue; inputDatas.push_back(buffer); inputShapes.push_back(context->getInputShape(i)); } return concatenation(inputDatas, inputShapes, context->getInputValue(inputCount), context->getOutputBuffer(kOutputTensor), context->getOutputShape(kOutputTensor)); } template <> inline bool concatenation(IOperationExecutionContext* context) { uint32_t inputCount = context->getNumInputs() - 1; std::vector> inputs_uint8(inputCount); for (uint32_t i = 0; i < inputCount; ++i) { const auto currentSize = getNumberOfElements(context->getInputShape(i)); inputs_uint8[i].resize(currentSize); if (currentSize != 0) { convertInt8ToUInt8(context->getInputBuffer(i), &inputs_uint8[i]); } } std::vector inputDatas; std::vector inputShapes; for (uint32_t i = 0; i < inputCount; ++i) { inputDatas.push_back(inputs_uint8[i].data()); inputShapes.push_back(context->getInputShape(i)); inputShapes[i].offset += 128; } std::vector output_uint8(getNumberOfElements(context->getOutputShape(kOutputTensor))); Shape outputShape(context->getOutputShape(kOutputTensor)); outputShape.offset += 128; NN_RET_CHECK(concatenation(inputDatas, inputShapes, context->getInputValue(inputCount), output_uint8.data(), outputShape)); convertUInt8ToInt8(output_uint8, context->getOutputBuffer(kOutputTensor)); return true; } } // namespace bool prepare(IOperationExecutionContext* context) { uint32_t numInputs = context->getNumInputs(); NN_RET_CHECK_GE(numInputs, 2u); const Shape& input0 = context->getInputShape(0); uint32_t numDimensions = getNumberOfDimensions(input0); int32_t axis = context->getInputValue(numInputs - 1); NN_RET_CHECK_GE(axis, 0); NN_RET_CHECK_LT(static_cast(axis), numDimensions); NN_RET_CHECK_LE(numDimensions, 4u); uint32_t sumAxis = getSizeOfDimension(input0, axis); for (uint32_t i = 1; i < numInputs - 1; ++i) { const Shape& input = context->getInputShape(i); NN_RET_CHECK_EQ(getNumberOfDimensions(input), numDimensions); NN_RET_CHECK(input.type == input0.type); for (uint32_t d = 0; d < numDimensions; ++d) { if (d == static_cast(axis)) { sumAxis += getSizeOfDimension(input, axis); } else { NN_RET_CHECK_EQ(getSizeOfDimension(input0, d), getSizeOfDimension(input, d)); } } } Shape output = context->getOutputShape(kOutputTensor); output.type = input0.type; output.dimensions = input0.dimensions; output.dimensions[axis] = sumAxis; return context->setOutputShape(kOutputTensor, output); } bool execute(IOperationExecutionContext* context) { // Bypass execution in the case of zero-sized input. if (getNumberOfElements(context->getOutputShape(kOutputTensor)) == 0) return true; switch (context->getInputType(0)) { case OperandType::TENSOR_FLOAT16: return concatenation<_Float16>(context); case OperandType::TENSOR_FLOAT32: return concatenation(context); case OperandType::TENSOR_QUANT8_ASYMM: return concatenation(context); case OperandType::TENSOR_QUANT8_ASYMM_SIGNED: return concatenation(context); default: NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName; } } #endif // NN_INCLUDE_CPU_IMPLEMENTATION } // namespace concatenation NN_REGISTER_OPERATION_DEFAULT_VALIDATION(CONCATENATION, concatenation::prepare, concatenation::execute, .allowZeroSizedInput = true); } // namespace nn } // namespace android