2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include "MergerLayer.hpp"
6 #include "LayerCloneBase.hpp"
8 #include <armnn/TypesUtils.hpp>
9 #include <backendsCommon/WorkloadData.hpp>
10 #include <backendsCommon/WorkloadFactory.hpp>
17 MergerLayer::MergerLayer(const OriginsDescriptor& param, const char* name)
18 : LayerWithParameters(param.GetNumViews(), 1, LayerType::Merger, param, name)
22 std::unique_ptr<IWorkload> MergerLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const
24 MergerQueueDescriptor descriptor;
26 // Copies the view origins to the descriptor.
27 descriptor.m_ViewOrigins.reserve(m_Param.GetNumViews());
28 for (unsigned int i = 0; i < m_Param.GetNumViews(); ++i)
30 descriptor.m_ViewOrigins.emplace_back(
31 std::vector<unsigned int>(m_Param.GetViewOrigin(i), m_Param.GetViewOrigin(i) + m_Param.GetNumDimensions()));
34 return factory.CreateMerger(descriptor, PrepInfoAndDesc(descriptor, graph));
37 void MergerLayer::CreateTensorHandles(Graph& graph, const IWorkloadFactory& factory)
39 //If sub tensors are supported than the merger
40 //just needs to make sure that the outputs of the prev layer
41 //are made subtensors of the output of the merger layer.
42 m_OutputHandlers[0].CreateTensorHandles(factory);
44 signed long innerAxis = m_Param.GetNumDimensions() - m_Param.GetConcatAxis();
46 if (factory.SupportsSubTensors() && innerAxis != 1)
48 std::queue<MergerLayer*> m_MergerLayers;
50 m_MergerLayers.push(this);
51 while (!m_MergerLayers.empty())
53 MergerLayer* currentLayer = m_MergerLayers.front();
54 ITensorHandle* parentTensor = currentLayer->GetOutputHandler(0).GetData();
58 const unsigned int numInputSlots = currentLayer->GetNumInputSlots();
59 for (unsigned int i = 0; i < numInputSlots; ++i)
61 OutputSlot* slot = currentLayer->GetInputSlot(i).GetConnectedOutputSlot();
62 OutputHandler& outputHandler = slot->GetOutputHandler();
63 outputHandler.SetData(factory.CreateSubTensorHandle(*parentTensor,
64 outputHandler.GetTensorInfo().GetShape(),
65 currentLayer->m_Param.GetViewOrigin(i)));
67 Layer& inputLayer = slot->GetOwningLayer();
68 if (inputLayer.GetType() == LayerType::Merger)
70 m_MergerLayers.push(boost::polymorphic_downcast<MergerLayer*>(&inputLayer));
77 MergerLayer* MergerLayer::Clone(Graph& graph) const
79 return CloneBase<MergerLayer>(graph, m_Param, GetName());
82 std::vector<TensorShape> MergerLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
84 BOOST_ASSERT(inputShapes.size() == m_Param.GetNumViews());
86 unsigned int numDims = m_Param.GetNumDimensions();
87 for (unsigned int i=0; i< inputShapes.size(); i++)
89 auto& inputShape = inputShapes[i];
91 ConditionalThrowIfNotEqual<LayerValidationException>(
92 "MergerLayer: Num Dimensions must match all inputs.",
94 inputShape.GetNumDimensions());
97 // Finds the bounding box (extents) of all the views.
98 std::vector<unsigned int> extentMin(numDims);
99 std::vector<unsigned int> extentMax(numDims);
100 for (unsigned int i = 0; i < inputShapes.size(); i++)
102 const uint32_t* origin = m_Param.GetViewOrigin(i);
103 const armnn::TensorShape& shape = inputShapes[i];
104 for (unsigned int d = 0; d < numDims; d++)
106 extentMin[d] = std::min(extentMin[d], origin[d]);
107 extentMax[d] = std::max(extentMax[d], origin[d] + shape[d]);
111 // Checks that the bounding box starts at the origin.
112 if (!std::all_of(extentMin.begin(), extentMin.end(), [](unsigned int s) { return s == 0; }))
114 throw LayerValidationException("MergerLayer: there is no view that starts at the origin");
117 // Checks that there are no overlaps of views (this would lead to undefined output at those locations).
118 // Checks each pair of views against each other
119 // (and doesn't bother to check against self, or check the same pair both ways round).
120 for (unsigned int a = 0; a < inputShapes.size(); a++)
122 const uint32_t* aOrigin = m_Param.GetViewOrigin(a);
123 const armnn::TensorShape& aShape = inputShapes[a];
124 for (unsigned int b = 0; b < a; b++)
126 const uint32_t* bOrigin = m_Param.GetViewOrigin(b);
127 const armnn::TensorShape& bShape = inputShapes[b];
129 bool allAxesOverlap = true;
130 for (unsigned int d = 0; d < numDims && allAxesOverlap; d++)
132 unsigned int a1 = aOrigin[d];
133 unsigned int a2 = aOrigin[d] + aShape[d];
135 unsigned int b1 = bOrigin[d];
136 unsigned int b2 = bOrigin[d] + bShape[d];
138 if (a2 <= b1 || b2 <= a1)
140 allAxesOverlap = false;
145 throw LayerValidationException("MergerLayer: Some views overlap.");
150 // Checks that there are no "holes", i.e. regions of the output which is not covered by a view.
151 // Because we already checked that there are no overlaps, this can be done simply by checking that
152 // the total 'volume' of the views is the same as the output.
153 unsigned int totalViewsVolume = 0;
154 for (unsigned int i = 0; i < inputShapes.size(); i++)
156 totalViewsVolume += inputShapes[i].GetNumElements();
158 unsigned int outputVolume = 1;
159 for (unsigned int d = 0; d < numDims; d++)
161 outputVolume *= (extentMax[d] - extentMin[d]);
164 ConditionalThrowIfNotEqual<LayerValidationException>(
165 "MergerLayer: there are some gaps between views",
169 return std::vector<TensorShape>({ TensorShape({numDims, extentMax.data()}) });
172 void MergerLayer::ValidateTensorShapesFromInputs()
174 // Validates Merger layer.
175 ConditionalThrowIfNotEqual<LayerValidationException>(
176 "MergerLayer: Num Inputs must match num views.",
177 m_Param.GetNumViews(),
180 VerifyLayerConnections(m_Param.GetNumViews(), CHECK_LOCATION());
182 std::vector<TensorShape> inputShapes;
183 for (uint i = 0; i < GetNumInputSlots(); ++i)
185 inputShapes.push_back(GetInputSlot(i).GetConnection()->GetTensorInfo().GetShape());
188 auto inferredShapes = InferOutputShapes(inputShapes);
190 BOOST_ASSERT(inferredShapes.size() == 1);
192 ConditionalThrowIfNotEqual<LayerValidationException>(
193 "MergerLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
194 GetOutputSlot(0).GetTensorInfo().GetShape(),
198 } // namespace armnn armnn