ArmNN
 22.08
BatchMatMulLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "BatchMatMulLayer.hpp"
6 
9 
10 namespace armnn
11 {
12 
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 
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 
81 {
83 
84  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
85 
87 
88  auto inferredShapes = InferOutputShapes({
91 
92  ARMNN_ASSERT(inferredShapes.size() == 1);
93 
94  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "BatchMatMulLayer");
95 }
96 
97 } // namespace armnn
BatchMatMulDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
Infers the output shape from the given input shapes.
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:491
Copyright (c) 2021 ARM Limited and Contributors.
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:206
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:422
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:378
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:324
BatchMatMulLayer(const BatchMatMulDescriptor &param, const char *name)
Constructor to create a BatchMatMulLayer.
static std::pair< std::pair< unsigned int, unsigned int >, std::pair< unsigned int, unsigned int > > GetAxesToMul(const BatchMatMulDescriptor &desc, const TensorShape &tensorXShape, const TensorShape &tensorYShape)
Static helper to get the two axes (for each input) for multiplication.
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
A BatchMatMulDescriptor for the BatchMatMul operator.
void ValidateTensorShapesFromInputs() override
Check if the input tensor shapes will lead to a valid configuration of BatchMatMulLayer.
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:274
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the BatchMatMul type.
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:326
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:319
BatchMatMulLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:423
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:468