ArmNN
 23.05
TransposeConvolution2dLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017,2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include "LayerCloneBase.hpp"
8 
10 
13 
14 using namespace armnnUtils;
15 
16 namespace armnn
17 {
18 
20  const char* name)
22 {
23 }
24 
25 std::unique_ptr<IWorkload> TransposeConvolution2dLayer::CreateWorkload(const IWorkloadFactory& factory) const
26 {
27  ARMNN_ASSERT_MSG(m_Weight != nullptr, "TransposeConvolution2dLayer: Weights data should not be null.");
28 
30  descriptor.m_Weight = m_Weight.get();
31 
33  {
34  ARMNN_ASSERT_MSG(m_Bias != nullptr, "TransposeConvolution2dLayer: Bias data should not be null.");
35  descriptor.m_Bias = m_Bias.get();
36  }
37 
38  SetAdditionalInfo(descriptor);
39 
40  return factory.CreateWorkload(LayerType::TransposeConvolution2d, descriptor, PrepInfoAndDesc(descriptor));
41 }
42 
44 {
45  auto layer = CloneBase<TransposeConvolution2dLayer>(graph, m_Param, GetName());
46 
47  layer->m_Weight = m_Weight ? m_Weight : nullptr;
48 
49  if (layer->m_Param.m_BiasEnabled)
50  {
51  layer->m_Bias = m_Bias ? m_Bias : nullptr;
52  }
53 
54  return std::move(layer);
55 }
56 
58  const std::vector<TensorShape>& inputShapes) const
59 {
60  ARMNN_ASSERT(inputShapes.size() == 2);
61  const TensorShape& inputShape = inputShapes[0];
62  const TensorShape& kernelShape = inputShapes[1];
63 
64  ARMNN_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Transpose convolutions will always have 4D input");
65 
66  DataLayoutIndexed dataLayoutIndex(m_Param.m_DataLayout);
67 
68  const unsigned int batches = inputShape[0];
69 
70  const unsigned int wInput = inputShape[dataLayoutIndex.GetWidthIndex()];
71  const unsigned int hInput = inputShape[dataLayoutIndex.GetHeightIndex()];
72 
73  const unsigned int wKernel = kernelShape[dataLayoutIndex.GetWidthIndex()];
74  const unsigned int hKernel = kernelShape[dataLayoutIndex.GetHeightIndex()];
75 
76  unsigned int wPadding = m_Param.m_PadLeft + m_Param.m_PadRight;
77  unsigned int hPadding = m_Param.m_PadTop + m_Param.m_PadBottom;
78 
79  unsigned int wOutput = (wInput - 1) * m_Param.m_StrideX + wKernel - wPadding;
80  unsigned int hOutput = (hInput - 1) * m_Param.m_StrideY + hKernel - hPadding;
81  unsigned int cOutput = kernelShape[0];
82 
84  TensorShape( { batches, hOutput, wOutput, cOutput } ) :
85  TensorShape( { batches, cOutput, hOutput, wOutput });
86 
87  return std::vector<TensorShape>({ tensorShape });
88 }
89 
91 {
93 
94  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
95 
97 
98  ARMNN_ASSERT_MSG(m_Weight != nullptr, "TransposeConvolution2dLayer: Weight data cannot be null.");
99 
100  std::vector<TensorShape> expectedOutputShape;
101  std::vector<TensorShape> outputShapeGivenAsInput;
102 
103  expectedOutputShape = InferOutputShapes({GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
104  m_Weight->GetTensorInfo().GetShape() });
105 
106  ARMNN_ASSERT(expectedOutputShape.size() == 1);
107 
108  // If output_shape was specified then use it rather than calculate an inferred output shape.
110  {
111  TensorShape shapeAsTensorShape(static_cast<unsigned int>(m_Param.m_OutputShape.size()),
112  m_Param.m_OutputShape.data());
113  outputShapeGivenAsInput.push_back(shapeAsTensorShape);
114 
115  ARMNN_ASSERT(outputShapeGivenAsInput.size() == 1);
116  ARMNN_ASSERT_MSG(expectedOutputShape == outputShapeGivenAsInput,
117  "TransposeConvolution2dLayer: output calculated by InferOutputShapes and "
118  "the output given as an input parameter to the layer are not matching");
119  }
120 
121  ValidateAndCopyShape(outputShape, expectedOutputShape[0], m_ShapeInferenceMethod, "TransposeConvolution2dLayer");
122 }
123 
125 {
126  // For API stability DO NOT ALTER order and add new members to the end of vector
127  return {m_Weight, m_Bias};
128 }
129 
131 {
132  ManagedConstTensorHandle managedWeight(m_Weight);
133  std::vector<armnn::ConstTensor> constTensors { { managedWeight.GetTensorInfo(), managedWeight.Map() } };
134 
135  ManagedConstTensorHandle managedBias(m_Bias);
136  if (GetParameters().m_BiasEnabled)
137  {
138  constTensors.emplace_back(ConstTensor(managedBias.GetTensorInfo(), managedBias.Map()));
139  }
140 
141  strategy.ExecuteStrategy(this, GetParameters(), constTensors, GetName());
142 }
143 
144 } // namespace armnn
armnn::TransposeConvolution2dDescriptor::m_OutputShapeEnabled
bool m_OutputShapeEnabled
Output shape if it has been specified.
Definition: Descriptors.hpp:1452
armnn::TransposeConvolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:1442
armnn::TransposeConvolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:1436
armnn::TransposeConvolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:1440
DataLayoutIndexed.hpp
armnn::LayerType::TransposeConvolution2d
@ TransposeConvolution2d
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::Layer::VerifyLayerConnections
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:378
armnn::InputSlot::GetConnection
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:206
armnn::TransposeConvolution2dLayer::TransposeConvolution2dLayer
TransposeConvolution2dLayer(const TransposeConvolution2dDescriptor &param, const char *name)
Constructor to create a TransposeConvolution2dLayer.
Definition: TransposeConvolution2dLayer.cpp:19
armnn::TransposeConvolution2dDescriptor::m_OutputShape
std::vector< unsigned int > m_OutputShape
Definition: Descriptors.hpp:1453
armnn::ManagedConstTensorHandle
Definition: TensorHandle.hpp:187
armnn::ConstTensor
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
armnn::TransposeConvolution2dLayer::m_Bias
std::shared_ptr< ConstTensorHandle > m_Bias
A unique pointer to store bias values.
Definition: TransposeConvolution2dLayer.hpp:21
armnn::TransposeConvolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:1448
armnn::LayerWithParameters
Definition: LayerWithParameters.hpp:14
armnnUtils::DataLayoutIndexed::GetWidthIndex
unsigned int GetWidthIndex() const
Definition: DataLayoutIndexed.hpp:25
armnn::ManagedConstTensorHandle::Map
const void * Map(bool blocking=true)
RAII Managed resource Unmaps MemoryArea once out of scope.
Definition: TensorHandle.hpp:196
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:491
TensorHandle.hpp
armnn::IWorkloadFactory
Definition: WorkloadFactory.hpp:22
WorkloadFactory.hpp
armnn::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:274
armnn::Layer::m_ShapeInferenceMethod
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:427
armnn::Layer::ValidateAndCopyShape
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:422
armnn::ManagedConstTensorHandle::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: TensorHandle.hpp:239
armnn::TransposeConvolution2dLayer::GetConstantTensorsByRef
ImmutableConstantTensors GetConstantTensorsByRef() const override
Retrieve the handles to the constant values stored by the layer.
Definition: TransposeConvolution2dLayer.cpp:124
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::IStrategy
Definition: IStrategy.hpp:16
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
armnn::TransposeConvolution2dLayer
This layer represents a 2D transpose convolution operation.
Definition: TransposeConvolution2dLayer.hpp:15
armnnUtils
Definition: CompatibleTypes.hpp:10
armnn::TransposeConvolution2dLayer::m_Weight
std::shared_ptr< ConstTensorHandle > m_Weight
A unique pointer to store weight values.
Definition: TransposeConvolution2dLayer.hpp:19
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:479
armnn::IOutputSlot::GetTensorInfo
virtual const TensorInfo & GetTensorInfo() const =0
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:326
armnn::TransposeConvolution2dQueueDescriptor
Definition: WorkloadData.hpp:539
armnn::LayerWithParameters< TransposeConvolution2dDescriptor >::PrepInfoAndDesc
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *Layer::CreateWorkload.
Definition: LayerWithParameters.hpp:44
armnn::TransposeConvolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:1438
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
armnn::DataLayout::NHWC
@ NHWC
ARMNN_ASSERT_MSG
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
LayerCloneBase.hpp
armnn::TransposeConvolution2dLayer::InferOutputShapes
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
Infers the output shapes from given input shapes and layer properties.
Definition: TransposeConvolution2dLayer.cpp:57
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:324
armnn::TransposeConvolution2dLayer::ExecuteStrategy
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
Definition: TransposeConvolution2dLayer.cpp:130
armnn::IStrategy::ExecuteStrategy
virtual void ExecuteStrategy(const IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
armnn::TransposeConvolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1450
armnn::TensorShape::GetNumDimensions
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
armnn::TransposeConvolution2dDescriptor
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
Definition: Descriptors.hpp:1407
armnn::Graph
Definition: Graph.hpp:30
armnn::TransposeConvolution2dLayer::Clone
TransposeConvolution2dLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: TransposeConvolution2dLayer.cpp:43
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnnUtils::DataLayoutIndexed
Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout.
Definition: DataLayoutIndexed.hpp:17
armnn::IWorkloadFactory::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const
Definition: WorkloadFactory.cpp:1590
armnnUtils::DataLayoutIndexed::GetHeightIndex
unsigned int GetHeightIndex() const
Definition: DataLayoutIndexed.hpp:24
armnn::TransposeConvolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:1444
armnn::TransposeConvolution2dQueueDescriptor::m_Bias
const ConstTensorHandle * m_Bias
Definition: WorkloadData.hpp:547
armnn::Layer::GetName
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:319
armnn::TransposeConvolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:1446
armnn::LayerWithParameters< TransposeConvolution2dDescriptor >::GetParameters
const TransposeConvolution2dDescriptor & GetParameters() const override
Definition: LayerWithParameters.hpp:19
armnn::LayerWithParameters< TransposeConvolution2dDescriptor >::m_Param
TransposeConvolution2dDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
Definition: LayerWithParameters.hpp:52
armnn::IConnectableLayer::ImmutableConstantTensors
std::vector< std::reference_wrapper< const std::shared_ptr< ConstTensorHandle > >> ImmutableConstantTensors
Definition: INetwork.hpp:129
armnn::TransposeConvolution2dLayer::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the TransposeConvolution2d type.
Definition: TransposeConvolution2dLayer.cpp:25
armnn::TransposeConvolution2dQueueDescriptor::m_Weight
const ConstTensorHandle * m_Weight
Definition: WorkloadData.hpp:546
armnn::TransposeConvolution2dLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of TransposeConvolution2dLayer.
Definition: TransposeConvolution2dLayer.cpp:90
TransposeConvolution2dLayer.hpp