2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
8 #include <backendsCommon/WorkloadData.hpp>
9 #include <backendsCommon/CpuTensorHandle.hpp>
11 #include <boost/cast.hpp>
12 #include <boost/format.hpp>
13 #include <boost/log/trivial.hpp>
20 void InputSlot::Insert(Layer& layer)
22 BOOST_ASSERT(layer.GetNumOutputSlots() == 1);
24 OutputSlot* const prevSlot = GetConnectedOutputSlot();
26 if (prevSlot != nullptr)
28 // Disconnects parent from this.
29 prevSlot->Disconnect(*this);
31 // Connects inserted layer to parent.
32 BOOST_ASSERT(layer.GetNumInputSlots() == 1);
33 prevSlot->Connect(layer.GetInputSlot(0));
35 // Sets tensor info for inserted layer.
36 const TensorInfo& tensorInfo = prevSlot->GetTensorInfo();
37 layer.GetOutputHandler().SetTensorInfo(tensorInfo);
40 // Connects inserted layer to this.
41 layer.GetOutputSlot(0).Connect(*this);
44 const InputSlot* OutputSlot::GetConnection(unsigned int index) const
46 ValidateConnectionIndex(index);
47 return m_Connections[index];
50 InputSlot* OutputSlot::GetConnection(unsigned int index)
52 ValidateConnectionIndex(index);
53 return m_Connections[index];
56 void OutputSlot::SetTensorInfo(const TensorInfo& tensorInfo)
58 GetOutputHandler().SetTensorInfo(tensorInfo);
61 const TensorInfo& OutputSlot::GetTensorInfo() const
63 return GetOutputHandler().GetTensorInfo();
66 bool OutputSlot::IsTensorInfoSet() const
68 return GetOutputHandler().IsTensorInfoSet();
71 bool OutputSlot::ValidateTensorShape(const TensorShape& shape) const
73 BOOST_ASSERT_MSG(IsTensorInfoSet(), "TensorInfo must be set in order to validate the shape.");
74 return shape == m_OutputHandler.GetTensorInfo().GetShape();
77 int OutputSlot::Connect(InputSlot& destination)
79 destination.SetConnection(this);
80 m_Connections.push_back(&destination);
81 return boost::numeric_cast<int>(m_Connections.size() - 1);
84 void OutputSlot::Disconnect(InputSlot& slot)
86 slot.SetConnection(nullptr);
87 m_Connections.erase(std::remove(m_Connections.begin(), m_Connections.end(), &slot), m_Connections.end());
90 void OutputSlot::DisconnectAll()
92 while (GetNumConnections() > 0)
94 InputSlot& connection = *GetConnection(0);
95 Disconnect(connection);
99 void OutputSlot::MoveAllConnections(OutputSlot& destination)
101 while (GetNumConnections() > 0)
103 InputSlot& connection = *GetConnection(0);
104 Disconnect(connection);
105 destination.Connect(connection);
109 void OutputSlot::ValidateConnectionIndex(unsigned int index) const
111 if (boost::numeric_cast<std::size_t>(index) >= m_Connections.size())
113 throw InvalidArgumentException(
114 boost::str(boost::format("GetConnection: Invalid index %1% provided") % index));
119 LayerGuid GenerateLayerGuid()
121 // Note: Not thread safe.
122 static LayerGuid newGuid=0;
127 Layer::Layer(unsigned int numInputSlots,
128 unsigned int numOutputSlots,
132 : m_OutputHandlers(numOutputSlots)
133 , m_LayerName(name ? name : "")
135 , m_DataLayout(layout)
136 , m_BackendId(UninitializedBackendId())
137 , m_Guid(GenerateLayerGuid())
139 m_InputSlots.reserve(numInputSlots);
140 for (unsigned int i = 0; i < numInputSlots; ++i)
142 m_InputSlots.emplace_back(*this, i);
145 m_OutputSlots.reserve(numOutputSlots);
146 for (unsigned int i = 0; i < numOutputSlots; ++i)
148 m_OutputSlots.emplace_back(*this, m_OutputHandlers[i]);
152 Layer::Layer(unsigned int numInputSlots,
153 unsigned int numOutputSlots,
156 : Layer(numInputSlots, numOutputSlots, type, DataLayout::NCHW, name)
160 void Layer::CollectWorkloadInputs(WorkloadDataCollector& dataCollector, const Graph& graph) const
162 for (auto&& inputSlot : GetInputSlots())
164 // The graph must be well-formed at this point.
165 BOOST_ASSERT(inputSlot.GetConnection());
166 const OutputHandler& outputHandler = inputSlot.GetConnectedOutputSlot()->GetOutputHandler();
167 dataCollector.Push(outputHandler.GetData(), outputHandler.GetTensorInfo());
171 void Layer::CollectWorkloadOutputs(WorkloadDataCollector& dataCollector, const Graph& graph) const
173 for (auto&& outputHandler : m_OutputHandlers)
175 outputHandler.CollectWorkloadOutputs(dataCollector);
179 void Layer::CreateTensorHandles(Graph& graph, const IWorkloadFactory& factory)
181 for (auto&& outputHandler : m_OutputHandlers)
183 outputHandler.CreateTensorHandles(factory);
187 void Layer::ReleaseConstantData()
189 // Now free up the static data.
190 OperateOnConstantTensors([](std::unique_ptr<ScopedCpuTensorHandle>& handle)
192 handle.reset(nullptr);
196 DataType Layer::GetDataType() const
198 if (GetNumInputSlots() > 0) // Ignore the input layer.
200 return GetInputSlot(0).GetConnection()->GetTensorInfo().GetDataType();
202 return GetOutputSlot(0).GetTensorInfo().GetDataType();
205 void Layer::ResetPriority() const
211 LayerPriority Layer::GetPriority() const
213 constexpr LayerPriority inputPrio = std::numeric_limits<LayerPriority>::lowest();
214 constexpr LayerPriority outputPrio = std::numeric_limits<LayerPriority>::max();
216 if (GetType() == LayerType::Input)
218 m_Priority = inputPrio;
220 else if (GetType() == LayerType::Output)
222 m_Priority = outputPrio;
224 else if (m_Priority == 0)
228 throw GraphValidationException("Graph has circular dependencies: cannot walk");
231 auto maxPrio = [](const LayerPriority prio, const InputSlot& slot) -> LayerPriority
233 const Layer& input = slot.GetConnectedOutputSlot()->GetOwningLayer();
234 return std::max(prio, input.GetPriority());
238 LayerPriority parentPrio = std::accumulate(GetInputSlots().cbegin(), GetInputSlots().cend(), 0U, maxPrio);
241 if (parentPrio >= outputPrio)
243 throw GraphValidationException("Graph has too many edges");
246 m_Priority = parentPrio + 1U;
252 void Layer::VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation& location) const
254 BOOST_ASSERT(GetNumInputSlots() == expectedConnections);
256 for (unsigned int i=0; i<expectedConnections; ++i)
258 if (GetInputSlot(i).GetConnection() == nullptr)
260 throw LayerValidationException(
263 "Input connection #%1% must be connected "
264 "for %2% layer %3% %4%")
266 % GetLayerTypeAsCString(this->GetType())
268 % location.AsString()));
270 if(! GetInputSlot(i).GetConnection()->IsTensorInfoSet())
272 throw LayerValidationException(
275 "TensorInfo of Input connection #%1% must be set on connected OutputSlot for "
278 % GetLayerTypeAsCString(this->GetType())
280 % location.AsString()));
285 std::vector<TensorShape> Layer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
287 BOOST_ASSERT(GetNumInputSlots() != 0);
288 BOOST_ASSERT(GetNumOutputSlots() != 0);
290 // By default we return what we got, meaning the output shape(s) are the same as the input(s).
291 // This only works if the number of inputs and outputs are the same. Since we are in the Layer
292 // base class, this means the implementation needs to be overridden in the specific layers for
293 // the other cases. So the missing implementation justifies the UnimplementedException.
295 if (GetNumInputSlots() != GetNumOutputSlots())
297 throw UnimplementedException(
300 "Default implementation for InferOutputShapes can only be used for "
301 "layers with the same number of input and output slots. This doesn't "
302 "hold for %1% layer %2% (#inputs=%3% #outputs=%4%) %5%")
303 % GetLayerTypeAsCString(this->GetType())
306 % GetNumOutputSlots()
307 % CHECK_LOCATION().AsString()));