Samuel Yap | 4b7a34d | 2022-07-06 15:36:03 +0100 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #include "BatchMatMulLayer.hpp" |
| 6 | |
| 7 | #include <armnn/backends/WorkloadFactory.hpp> |
| 8 | #include "layers/LayerCloneBase.hpp" |
| 9 | |
| 10 | namespace armnn |
| 11 | { |
| 12 | |
| 13 | BatchMatMulLayer::BatchMatMulLayer(const BatchMatMulDescriptor& param, const char* name) |
| 14 | : LayerWithParameters(2, 1, LayerType::BatchMatMul, param, name) |
| 15 | {} |
| 16 | |
| 17 | std::unique_ptr<IWorkload> BatchMatMulLayer::CreateWorkload(const IWorkloadFactory& factory) const |
| 18 | { |
| 19 | BatchMatMulQueueDescriptor descriptor; |
| 20 | SetAdditionalInfo(descriptor); |
| 21 | |
| 22 | return factory.CreateWorkload(LayerType::BatchMatMul, descriptor, PrepInfoAndDesc(descriptor)); |
| 23 | } |
| 24 | |
| 25 | BatchMatMulLayer* BatchMatMulLayer::Clone(Graph& graph) const |
| 26 | { |
| 27 | auto layer = CloneBase<BatchMatMulLayer>(graph, m_Param, GetName()); |
| 28 | |
| 29 | return std::move(layer); |
| 30 | } |
| 31 | |
| 32 | std::vector<TensorShape> BatchMatMulLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const |
| 33 | { |
| 34 | ARMNN_ASSERT(inputShapes.size() == 2); |
| 35 | |
| 36 | TensorShape inputXShape = inputShapes[0]; |
| 37 | TensorShape inputYShape = inputShapes[1]; |
| 38 | |
| 39 | // Note: Take into account what pre-adjoint or pre-transposing will do to the inferred output shape |
| 40 | |
| 41 | TensorShape& longerInput = inputXShape.GetNumDimensions() >= inputYShape.GetNumDimensions()? |
| 42 | inputXShape:inputYShape; |
| 43 | TensorShape& shorterInput = inputXShape.GetNumDimensions() >= inputYShape.GetNumDimensions()? |
| 44 | inputYShape:inputXShape; |
| 45 | |
| 46 | unsigned int inputNumDimsOffset = longerInput.GetNumDimensions() - shorterInput.GetNumDimensions(); |
| 47 | |
| 48 | unsigned int outputNumDimensions = longerInput.GetNumDimensions(); |
| 49 | |
| 50 | std::vector<unsigned int> tensorDimensions(outputNumDimensions, 0); |
| 51 | |
| 52 | auto axesToMul = BatchMatMulDescriptor::GetAxesToMul(m_Param, inputXShape, inputYShape); |
| 53 | const auto& longerAxesToMul = (axesToMul.first.first >= axesToMul.second.first && |
| 54 | axesToMul.first.second >= axesToMul.second.second) ? |
| 55 | axesToMul.first : axesToMul.second; |
| 56 | |
| 57 | for (unsigned int i = 0; i < outputNumDimensions; ++i) |
| 58 | { |
| 59 | if (i == longerAxesToMul.first) |
| 60 | { |
| 61 | tensorDimensions[i] = &shorterInput == &inputXShape ? inputXShape[i - inputNumDimsOffset] : inputXShape[i]; |
| 62 | } |
| 63 | else if(i == longerAxesToMul.second) |
| 64 | { |
| 65 | tensorDimensions[i] = &shorterInput == &inputYShape ? inputYShape[i - inputNumDimsOffset] : inputYShape[i]; |
| 66 | } |
| 67 | else // The other dimensions not to be multiplied (but may be broadcasted) |
| 68 | { |
| 69 | // Does NOT validate whether it's a valid broadcast - that's done in the validate func in WorkloadData.cpp |
| 70 | tensorDimensions[i] = static_cast<int>(i) - static_cast<int>(inputNumDimsOffset) < 0 ? |
| 71 | longerInput[i] : |
| 72 | std::max(longerInput[i], shorterInput[i - inputNumDimsOffset]); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | auto outputShape = TensorShape(outputNumDimensions, tensorDimensions.data()); |
| 77 | return std::vector<TensorShape>({ outputShape }); |
| 78 | } |
| 79 | |
| 80 | void BatchMatMulLayer::ValidateTensorShapesFromInputs() |
| 81 | { |
| 82 | VerifyLayerConnections(2, CHECK_LOCATION()); |
| 83 | |
| 84 | const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape(); |
| 85 | |
| 86 | VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod); |
| 87 | |
| 88 | auto inferredShapes = InferOutputShapes({ |
| 89 | GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(), |
| 90 | GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape() }); |
| 91 | |
| 92 | ARMNN_ASSERT(inferredShapes.size() == 1); |
| 93 | |
| 94 | ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "BatchMatMulLayer"); |
| 95 | } |
| 96 | |
| 97 | } // namespace armnn |