2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "SpaceToBatchNdLayer.hpp"
8 #include "LayerCloneBase.hpp"
10 #include <armnn/TypesUtils.hpp>
12 #include <backendsCommon/WorkloadData.hpp>
13 #include <backendsCommon/WorkloadFactory.hpp>
20 SpaceToBatchNdLayer::SpaceToBatchNdLayer(const SpaceToBatchNdDescriptor param, const char* name)
21 : LayerWithParameters(1, 1, LayerType::SpaceToBatchNd, param, name)
24 std::unique_ptr<IWorkload> SpaceToBatchNdLayer::CreateWorkload(const Graph& graph,
25 const IWorkloadFactory& factory) const
27 SpaceToBatchNdQueueDescriptor descriptor;
28 descriptor.m_Parameters.m_BlockShape = m_Param.m_BlockShape;
29 descriptor.m_Parameters.m_PadList = m_Param.m_PadList;
31 return factory.CreateSpaceToBatchNd(descriptor, PrepInfoAndDesc(descriptor, graph));
34 SpaceToBatchNdLayer* SpaceToBatchNdLayer::Clone(Graph& graph) const
36 return CloneBase<SpaceToBatchNdLayer>(graph, m_Param, GetName());
39 std::vector<TensorShape> SpaceToBatchNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
41 BOOST_ASSERT(inputShapes.size() == 1);
43 TensorShape inputShape = inputShapes[0];
44 TensorShape outputShape(inputShape);
46 outputShape[0] = inputShape[0] * std::accumulate(m_Param.m_BlockShape.begin(),
47 m_Param.m_BlockShape.end(),
51 unsigned int heightIndex = m_Param.m_DataLayout.GetHeightIndex();
52 unsigned int widthIndex = m_Param.m_DataLayout.GetWidthIndex();
54 std::pair<unsigned int, unsigned int> heightPad = m_Param.m_PadList[0];
55 std::pair<unsigned int, unsigned int> widthPad = m_Param.m_PadList[1];
57 outputShape[heightIndex] =
58 (inputShape[heightIndex] + heightPad.first + heightPad.second) / m_Param.m_BlockShape[0];
59 outputShape[widthIndex] =
60 (inputShape[widthIndex] + widthPad.first + widthPad.second) / m_Param.m_BlockShape[1];
62 return std::vector<TensorShape>({ outputShape });
65 void SpaceToBatchNdLayer::ValidateTensorShapesFromInputs()
67 VerifyLayerConnections(1, CHECK_LOCATION());
69 std::vector<TensorShape> inferredShapes = InferOutputShapes({
70 GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape() });
72 BOOST_ASSERT(inferredShapes.size() == 1);
74 ConditionalThrowIfNotEqual<LayerValidationException>(
75 "SpaceToBatchNdLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
76 GetOutputSlot(0).GetTensorInfo().GetShape(),