2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include "SplitterLayer.hpp"
7 #include "LayerCloneBase.hpp"
9 #include <armnn/TypesUtils.hpp>
10 #include <backendsCommon/WorkloadData.hpp>
11 #include <backendsCommon/WorkloadFactory.hpp>
16 SplitterLayer::SplitterLayer(const ViewsDescriptor& param, const char* name)
17 : LayerWithParameters(1, param.GetNumViews(), LayerType::Splitter, param, name)
21 std::unique_ptr<IWorkload> SplitterLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const
23 SplitterQueueDescriptor descriptor;
25 // Copies the window origins to the descriptor.
26 for (unsigned int i = 0; i < m_Param.GetNumViews(); ++i)
28 descriptor.m_ViewOrigins.emplace_back(
29 std::vector<unsigned int>(m_Param.GetViewOrigin(i), m_Param.GetViewOrigin(i) + m_Param.GetNumDimensions()));
32 return factory.CreateSplitter(descriptor, PrepInfoAndDesc(descriptor, graph));
35 void SplitterLayer::CreateTensorHandles(Graph& graph, const IWorkloadFactory& factory)
37 //If sub tensors are supported than all the "splitter" need to do is to
38 //set the outputs to be appropriate sub tensors of the input.
39 if (factory.SupportsSubTensors())
41 const OutputHandler& outputHandler = GetInputSlots()[0].GetConnectedOutputSlot()->GetOutputHandler();
43 ITensorHandle* inputData = outputHandler.GetData();
44 //Creates the outputs as subtensors of the input.
45 for (unsigned int i = 0; i < m_Param.GetNumViews(); ++i)
47 m_OutputHandlers[i].SetData(factory.CreateSubTensorHandle(*inputData,
48 m_OutputHandlers[i].GetTensorInfo().GetShape(),
49 m_Param.GetViewOrigin(i)));
54 for (unsigned int i = 0; i < m_Param.GetNumViews(); ++i)
56 m_OutputHandlers[i].CreateTensorHandles(factory);
61 SplitterLayer* SplitterLayer::Clone(Graph& graph) const
63 return CloneBase<SplitterLayer>(graph, m_Param, GetName());
66 std::vector<TensorShape> SplitterLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
68 BOOST_ASSERT(inputShapes.size() == m_Param.GetNumViews());
69 std::vector<TensorShape> outShapes;
70 //Output shapes must match View shapes.
71 for (unsigned int viewIdx = 0; viewIdx < m_Param.GetNumViews(); viewIdx++)
73 const uint32_t* sizes = m_Param.GetViewSizes(viewIdx);
74 outShapes.push_back(TensorShape(m_Param.GetNumDimensions(), sizes));
79 void SplitterLayer::ValidateTensorShapesFromInputs()
81 std::vector<TensorShape> views;
82 for (unsigned int viewIdx = 0; viewIdx < m_Param.GetNumViews(); viewIdx++)
84 const uint32_t* sizes = m_Param.GetViewSizes(viewIdx);
85 views.push_back(TensorShape(m_Param.GetNumDimensions(), sizes));
88 auto inferredShapes = InferOutputShapes(views);
90 BOOST_ASSERT(inferredShapes.size() == m_Param.GetNumViews());
92 for (unsigned int viewIdx = 0; viewIdx < m_Param.GetNumViews(); viewIdx++)
94 ConditionalThrowIfNotEqual<LayerValidationException>(
95 "SplitterLayer: View sizes must match output tensor shapes.",
96 GetOutputSlot(viewIdx).GetTensorInfo().GetShape(),
97 inferredShapes[viewIdx]);