aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/layers/BatchToSpaceNdLayer.cpp
blob: 9accf28137ba865f0f3cbff0a874ecd056db875b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "BatchToSpaceNdLayer.hpp"

#include "LayerCloneBase.hpp"
#include "LayerWithParameters.hpp"
#include "BatchToSpaceNdLayer.hpp"

#include <armnn/TypesUtils.hpp>

#include <backendsCommon/CpuTensorHandle.hpp>
#include <backendsCommon/WorkloadData.hpp>
#include <backendsCommon/WorkloadFactory.hpp>

#include <DataLayoutIndexed.hpp>

using namespace armnnUtils;

namespace armnn
{

BatchToSpaceNdLayer::BatchToSpaceNdLayer(const armnn::BatchToSpaceNdDescriptor& param, const char* name)
    : LayerWithParameters(1, 1, LayerType::BatchToSpaceNd, param, name)
{
}

std::unique_ptr<IWorkload> BatchToSpaceNdLayer::CreateWorkload(const Graph& graph,
                                                               const IWorkloadFactory& factory) const
{
    BatchToSpaceNdQueueDescriptor descriptor;

    return factory.CreateBatchToSpaceNd(descriptor, PrepInfoAndDesc(descriptor, graph));
}

BatchToSpaceNdLayer* BatchToSpaceNdLayer::Clone(Graph& graph) const
{
    auto layer = CloneBase<BatchToSpaceNdLayer>(graph, m_Param, GetName());
    return std::move(layer);
}

void BatchToSpaceNdLayer::ValidateTensorShapesFromInputs()
{
    VerifyLayerConnections(1, CHECK_LOCATION());

    auto inferredShapes = InferOutputShapes({GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape()});

    BOOST_ASSERT(inferredShapes.size() == 1);

    ConditionalThrowIfNotEqual<LayerValidationException>(
        "BatchToSpaceLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
        GetOutputSlot(0).GetTensorInfo().GetShape(),inferredShapes[0]);
}

std::vector<TensorShape> BatchToSpaceNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
{
    const DataLayoutIndexed dataLayout = m_Param.m_DataLayout;
    const TensorShape& inputShape = inputShapes[0];
    unsigned int inBatchSize = inputShape[0];
    unsigned int channelSize = inputShape[dataLayout.GetChannelsIndex()];

    std::vector<unsigned int> theBlockShape = m_Param.m_BlockShape;

    unsigned int overallSize = inBatchSize * inputShape[dataLayout.GetHeightIndex()]
                               * inputShape[dataLayout.GetWidthIndex()];

    std::vector<std::pair<unsigned int, unsigned int>> crops = m_Param.m_Crops;

    std::pair<unsigned int, unsigned int> yCrops = crops[0];
    std::pair<unsigned int, unsigned int> xCrops = crops[1];

    unsigned int inputHeight = inputShape[dataLayout.GetHeightIndex()];
    unsigned int outputHeight;

    unsigned int yCropsTotal = yCrops.first + yCrops.second;

    BOOST_ASSERT_MSG(yCropsTotal <= inputHeight,
                     "BatchToSpaceLayer: Overall height crop should be less than or equal to the input height.");

    unsigned int croppedHeight = inputHeight - yCropsTotal;

    if (theBlockShape.at(0) > 0)
    {
        outputHeight = theBlockShape.at(0) * croppedHeight;
    }
    else
    {
        outputHeight = croppedHeight;
    }

    unsigned int outputWidth;
    unsigned int inputWidth = inputShape[dataLayout.GetWidthIndex()];

    unsigned int xCropsTotal = xCrops.first + xCrops.second;

    BOOST_ASSERT_MSG(xCropsTotal <= inputWidth,
                     "BatchToSpaceLayer: Overall width crop should be less than or equal to the input width.");
    unsigned int croppedWidth = inputWidth - xCropsTotal;

    if (theBlockShape.at(1) > 0)
    {
        outputWidth = theBlockShape.at(1) * croppedWidth;
    }
    else
    {
        outputWidth = croppedWidth;
    }

    unsigned int outputBatchSize = overallSize / (outputHeight * outputWidth);

    if (dataLayout == DataLayout::NHWC)
    {
        return std::vector<TensorShape>({ TensorShape({ outputBatchSize, outputHeight, outputWidth, channelSize }) });
    }
    else
    {
        return std::vector<TensorShape>({ TensorShape({ outputBatchSize, channelSize, outputHeight, outputWidth }) });
    }
}

void BatchToSpaceNdLayer::Accept(ILayerVisitor& visitor) const
{
    visitor.VisitBatchToSpaceNdLayer(this, GetParameters(), GetName());
}

} // namespace armnn