From 6b47809e7d6c55d20a05d863ce2f09159f381f85 Mon Sep 17 00:00:00 2001 From: Samuel Yap Date: Wed, 6 Jul 2022 15:36:03 +0100 Subject: IVGCVSW-7109: Add Batch MatMul front end support - Reference * Descriptors added for BatchMatMul * Layer definition added * Input validation added (will likely change when opt. param support comes in) * Ref workload implementation for BatchMatMul added (will also change with opt. param support) * Ref layer tests made for BatchMatMul * CMake and other build files updated Signed-off-by: Samuel Yap Change-Id: Ic885301da543ee0fbe7922b85e7f9658c4efc617 --- src/armnn/layers/BatchMatMulLayer.cpp | 97 +++++++++++++++++++++++++++++++++++ src/armnn/layers/BatchMatMulLayer.hpp | 46 +++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/armnn/layers/BatchMatMulLayer.cpp create mode 100644 src/armnn/layers/BatchMatMulLayer.hpp (limited to 'src/armnn/layers') diff --git a/src/armnn/layers/BatchMatMulLayer.cpp b/src/armnn/layers/BatchMatMulLayer.cpp new file mode 100644 index 0000000000..501de2d091 --- /dev/null +++ b/src/armnn/layers/BatchMatMulLayer.cpp @@ -0,0 +1,97 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// +#include "BatchMatMulLayer.hpp" + +#include +#include "layers/LayerCloneBase.hpp" + +namespace armnn +{ + +BatchMatMulLayer::BatchMatMulLayer(const BatchMatMulDescriptor& param, const char* name) + : LayerWithParameters(2, 1, LayerType::BatchMatMul, param, name) +{} + +std::unique_ptr BatchMatMulLayer::CreateWorkload(const IWorkloadFactory& factory) const +{ + BatchMatMulQueueDescriptor descriptor; + SetAdditionalInfo(descriptor); + + return factory.CreateWorkload(LayerType::BatchMatMul, descriptor, PrepInfoAndDesc(descriptor)); +} + +BatchMatMulLayer* BatchMatMulLayer::Clone(Graph& graph) const +{ + auto layer = CloneBase(graph, m_Param, GetName()); + + return std::move(layer); +} + +std::vector BatchMatMulLayer::InferOutputShapes(const std::vector& inputShapes) const +{ + ARMNN_ASSERT(inputShapes.size() == 2); + + TensorShape inputXShape = inputShapes[0]; + TensorShape inputYShape = inputShapes[1]; + + // Note: Take into account what pre-adjoint or pre-transposing will do to the inferred output shape + + TensorShape& longerInput = inputXShape.GetNumDimensions() >= inputYShape.GetNumDimensions()? + inputXShape:inputYShape; + TensorShape& shorterInput = inputXShape.GetNumDimensions() >= inputYShape.GetNumDimensions()? + inputYShape:inputXShape; + + unsigned int inputNumDimsOffset = longerInput.GetNumDimensions() - shorterInput.GetNumDimensions(); + + unsigned int outputNumDimensions = longerInput.GetNumDimensions(); + + std::vector tensorDimensions(outputNumDimensions, 0); + + auto axesToMul = BatchMatMulDescriptor::GetAxesToMul(m_Param, inputXShape, inputYShape); + const auto& longerAxesToMul = (axesToMul.first.first >= axesToMul.second.first && + axesToMul.first.second >= axesToMul.second.second) ? + axesToMul.first : axesToMul.second; + + for (unsigned int i = 0; i < outputNumDimensions; ++i) + { + if (i == longerAxesToMul.first) + { + tensorDimensions[i] = &shorterInput == &inputXShape ? inputXShape[i - inputNumDimsOffset] : inputXShape[i]; + } + else if(i == longerAxesToMul.second) + { + tensorDimensions[i] = &shorterInput == &inputYShape ? inputYShape[i - inputNumDimsOffset] : inputYShape[i]; + } + else // The other dimensions not to be multiplied (but may be broadcasted) + { + // Does NOT validate whether it's a valid broadcast - that's done in the validate func in WorkloadData.cpp + tensorDimensions[i] = static_cast(i) - static_cast(inputNumDimsOffset) < 0 ? + longerInput[i] : + std::max(longerInput[i], shorterInput[i - inputNumDimsOffset]); + } + } + + auto outputShape = TensorShape(outputNumDimensions, tensorDimensions.data()); + return std::vector({ outputShape }); +} + +void BatchMatMulLayer::ValidateTensorShapesFromInputs() +{ + VerifyLayerConnections(2, CHECK_LOCATION()); + + const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape(); + + VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod); + + auto inferredShapes = InferOutputShapes({ + GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(), + GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape() }); + + ARMNN_ASSERT(inferredShapes.size() == 1); + + ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "BatchMatMulLayer"); +} + +} // namespace armnn \ No newline at end of file diff --git a/src/armnn/layers/BatchMatMulLayer.hpp b/src/armnn/layers/BatchMatMulLayer.hpp new file mode 100644 index 0000000000..8dc79d33c4 --- /dev/null +++ b/src/armnn/layers/BatchMatMulLayer.hpp @@ -0,0 +1,46 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "LayerWithParameters.hpp" + +namespace armnn +{ + +class BatchMatMulLayer : public LayerWithParameters +{ +public: + /// Makes a workload for the BatchMatMul type. + /// @param [in] graph The graph where this layer can be found. + /// @param [in] factory The workload factory which will create the workload. + /// @return A pointer to the created workload, or nullptr if not created. + virtual std::unique_ptr CreateWorkload(const IWorkloadFactory &factory) const override; + + /// Creates a dynamically-allocated copy of this layer. + /// @param [in] graph The graph into which this layer is being cloned. + BatchMatMulLayer* Clone(Graph &graph) const override; + + /// Infers the output shape from the given input shapes. + /// @param [in] inputShapes The vector of input shapes for BatchMatMul. + /// @return A vector of inferred output shape. + std::vector InferOutputShapes(const std::vector& inputShapes) const override; + + /// Check if the input tensor shapes + /// will lead to a valid configuration of @ref BatchMatMulLayer. + /// @param [in] shapeInferenceMethod Indicates if output shape shall be overwritten or just validated. + void ValidateTensorShapesFromInputs() override; + +protected: + /// Constructor to create a BatchMatMulLayer. + /// @param [in] param BatchMatMulDescriptor to configure optional parameters for batch matrix multiplication + /// @param [in] name Optional name for the layer + BatchMatMulLayer(const BatchMatMulDescriptor& param, const char* name); + + /// Default destructor + ~BatchMatMulLayer() = default; +}; + +} \ No newline at end of file -- cgit v1.2.1