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);
43 if (factory.SupportsSubTensors())
45 std::queue<MergerLayer*> m_MergerLayers;
47 m_MergerLayers.push(this);
48 while (!m_MergerLayers.empty())
50 MergerLayer* currentLayer = m_MergerLayers.front();
51 ITensorHandle* parentTensor = currentLayer->GetOutputHandler(0).GetData();
55 const unsigned int numInputSlots = currentLayer->GetNumInputSlots();
56 for (unsigned int i = 0; i < numInputSlots; ++i)
58 OutputSlot* slot = currentLayer->GetInputSlot(i).GetConnectedOutputSlot();
59 OutputHandler& outputHandler = slot->GetOutputHandler();
60 outputHandler.SetData(factory.CreateSubTensorHandle(*parentTensor,
61 outputHandler.GetTensorInfo().GetShape(),
62 currentLayer->m_Param.GetViewOrigin(i)));
64 Layer& inputLayer = slot->GetOwningLayer();
65 if (inputLayer.GetType() == LayerType::Merger)
67 m_MergerLayers.push(boost::polymorphic_downcast<MergerLayer*>(&inputLayer));
74 MergerLayer* MergerLayer::Clone(Graph& graph) const
76 return CloneBase<MergerLayer>(graph, m_Param, GetName());
79 std::vector<TensorShape> MergerLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
81 BOOST_ASSERT(inputShapes.size() == m_Param.GetNumViews());
83 unsigned int numDims = m_Param.GetNumDimensions();
84 for (unsigned int i=0; i< inputShapes.size(); i++)
86 auto& inputShape = inputShapes[i];
88 ConditionalThrowIfNotEqual<LayerValidationException>(
89 "MergerLayer: Num Dimensions must match all inputs.",
91 inputShape.GetNumDimensions());
94 // Finds the bounding box (extents) of all the views.
95 std::vector<unsigned int> extentMin(numDims);
96 std::vector<unsigned int> extentMax(numDims);
97 for (unsigned int i = 0; i < inputShapes.size(); i++)
99 const uint32_t* origin = m_Param.GetViewOrigin(i);
100 const armnn::TensorShape& shape = inputShapes[i];
101 for (unsigned int d = 0; d < numDims; d++)
103 extentMin[d] = std::min(extentMin[d], origin[d]);
104 extentMax[d] = std::max(extentMax[d], origin[d] + shape[d]);
108 // Checks that the bounding box starts at the origin.
109 if (!std::all_of(extentMin.begin(), extentMin.end(), [](unsigned int s) { return s == 0; }))
111 throw LayerValidationException("MergerLayer: there is no view that starts at the origin");
114 // Checks that there are no overlaps of views (this would lead to undefined output at those locations).
115 // Checks each pair of views against each other
116 // (and doesn't bother to check against self, or check the same pair both ways round).
117 for (unsigned int a = 0; a < inputShapes.size(); a++)
119 const uint32_t* aOrigin = m_Param.GetViewOrigin(a);
120 const armnn::TensorShape& aShape = inputShapes[a];
121 for (unsigned int b = 0; b < a; b++)
123 const uint32_t* bOrigin = m_Param.GetViewOrigin(b);
124 const armnn::TensorShape& bShape = inputShapes[b];
126 bool allAxesOverlap = true;
127 for (unsigned int d = 0; d < numDims && allAxesOverlap; d++)
129 unsigned int a1 = aOrigin[d];
130 unsigned int a2 = aOrigin[d] + aShape[d];
132 unsigned int b1 = bOrigin[d];
133 unsigned int b2 = bOrigin[d] + bShape[d];
135 if (a2 <= b1 || b2 <= a1)
137 allAxesOverlap = false;
142 throw LayerValidationException("MergerLayer: Some views overlap.");
147 // Checks that there are no "holes", i.e. regions of the output which is not covered by a view.
148 // Because we already checked that there are no overlaps, this can be done simply by checking that
149 // the total 'volume' of the views is the same as the output.
150 unsigned int totalViewsVolume = 0;
151 for (unsigned int i = 0; i < inputShapes.size(); i++)
153 totalViewsVolume += inputShapes[i].GetNumElements();
155 unsigned int outputVolume = 1;
156 for (unsigned int d = 0; d < numDims; d++)
158 outputVolume *= (extentMax[d] - extentMin[d]);
161 ConditionalThrowIfNotEqual<LayerValidationException>(
162 "MergerLayer: there are some gaps between views",
166 return std::vector<TensorShape>({ TensorShape({numDims, extentMax.data()}) });
169 void MergerLayer::ValidateTensorShapesFromInputs()
171 // Validates Merger layer.
172 ConditionalThrowIfNotEqual<LayerValidationException>(
173 "MergerLayer: Num Inputs must match num views.",
174 m_Param.GetNumViews(),
177 VerifyLayerConnections(m_Param.GetNumViews(), CHECK_LOCATION());
179 std::vector<TensorShape> inputShapes;
180 for (uint i = 0; i < GetNumInputSlots(); ++i)
182 inputShapes.push_back(GetInputSlot(i).GetConnection()->GetTensorInfo().GetShape());
185 auto inferredShapes = InferOutputShapes(inputShapes);
187 BOOST_ASSERT(inferredShapes.size() == 1);
189 ConditionalThrowIfNotEqual<LayerValidationException>(
190 "MergerLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
191 GetOutputSlot(0).GetTensorInfo().GetShape(),
195 } // namespace armnn armnn