aboutsummaryrefslogtreecommitdiff
path: root/src/backends/backendsCommon/test/Convolution2dEndToEndTestImpl.hpp
blob: f53f97ae884632338fe33c0a6387cece5141aac3 (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
128
129
130
131
132
133
134
135
136
137
138
139
//
// Copyright © 2022, 2024 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#pragma once

#include "EndToEndTestImpl.hpp"
#include <armnnUtils/QuantizeHelper.hpp>

#include <ResolveType.hpp>

#include <CommonTestUtils.hpp>
#include <armnnTestUtils/DataLayoutUtils.hpp>

#include <map>
#include <vector>

namespace
{

armnn::INetworkPtr CreateConstConvolution2dNetwork(const armnn::Convolution2dDescriptor& descriptor,
                                                   const armnn::TensorInfo& inputInfo,
                                                   const armnn::TensorInfo& weightsInfo,
                                                   const armnn::TensorInfo& biasInfo,
                                                   const armnn::TensorInfo& outputInfo,
                                                   const armnn::ConstTensor& weights,
                                                   const armnn::ConstTensor& biases,
                                                   bool biasEnabled)
{
    using namespace armnn;

    INetworkPtr network(INetwork::Create());
    IConnectableLayer* input = network->AddInputLayer(0, "input");
    IConnectableLayer* weightsLayer = network->AddConstantLayer(weights, "Weights");
    IConnectableLayer* convolution2d = network->AddConvolution2dLayer(descriptor, "convolution2d");
    IConnectableLayer* output = network->AddOutputLayer(0, "output");

    Connect(input, convolution2d, inputInfo, 0, 0);
    Connect(weightsLayer, convolution2d, weightsInfo, 0, 1);

    if(biasEnabled)
    {
        armnn::IConnectableLayer* biasLayer = network->AddConstantLayer(biases, "Bias");
        Connect(biasLayer, convolution2d, biasInfo, 0, 2);
    }

    Connect(convolution2d, output, outputInfo, 0, 0);

    return network;
}

template<DataType ArmnnIType, DataType ArmnnWType = ArmnnIType, DataType ArmnnBType = ArmnnIType,
        DataType ArmnnOType = ArmnnIType>
void Convolution2dEndToEnd(const std::vector<armnn::BackendId>& backends,
                           armnn::DataLayout dataLayout,
                           bool biasEnabled = true)
{
    using namespace armnn;
    using IT = ResolveType<ArmnnIType>;
    using WT = ResolveType<ArmnnWType>;
    using BT = ResolveType<ArmnnBType>;
    using OT = ResolveType<ArmnnOType>;

    const float   qScale  = 1.0f;
    const int32_t qOffset = IsQuantizedType<IT>() ? 10 : 0; // offset must be zero for non-quantized types

    TensorInfo inputInfo(  { 1, 5, 5, 1 }, ArmnnIType, qScale,          qOffset, true);
    TensorInfo weightsInfo({ 1, 3, 3, 1 }, ArmnnWType, qScale,          qOffset, true);
    TensorInfo biasesInfo( { 1 },          ArmnnBType, qScale * qScale, 0,       true);
    TensorInfo outputInfo( { 1, 3, 3, 1 }, ArmnnOType, qScale,          qOffset);

    std::vector<float> inputData =
            {
                    1, 5, 2, 3, 5,
                    8, 7, 3, 6, 3,
                    3, 3, 9, 1, 9,
                    4, 1, 8, 1, 3,
                    6, 8, 1, 9, 2
            };

    std::vector<float> weightsData =
            {
                    4, 5, 6,
                    0, 0, 0,
                    3, 2, 1
            };

    std::vector<float> biasesData = { 1 };
    float bias = biasEnabled ? biasesData[0] : 0;

    std::vector<float> expectedOutputData =
            {
                    65 + bias, 76 + bias,  91 + bias,
                    107 + bias, 99 + bias,  89 + bias,
                    116 + bias, 98 + bias, 118 + bias
            };

    Convolution2dDescriptor descriptor;
    descriptor.m_PadLeft     = 0;
    descriptor.m_PadRight    = 0;
    descriptor.m_PadTop      = 0;
    descriptor.m_PadBottom   = 0;
    descriptor.m_StrideX     = 1;
    descriptor.m_StrideY     = 1;
    descriptor.m_BiasEnabled = biasEnabled;
    descriptor.m_DataLayout  = dataLayout;

    if (dataLayout == DataLayout::NCHW)
    {
        PermuteTensorNhwcToNchw(inputInfo,   inputData);
        PermuteTensorNhwcToNchw(weightsInfo, weightsData);
        PermuteTensorNhwcToNchw(outputInfo,  expectedOutputData);
    }

    // Convert data
    std::vector<IT> qInputData = armnnUtils::QuantizedVector<IT>(inputData, qScale, qOffset);
    std::vector<WT> qWeightsData = armnnUtils::QuantizedVector<WT>(weightsData, qScale, qOffset);
    std::vector<BT> qBiasesData = armnnUtils::QuantizedVector<BT>(biasesData, qScale * qScale, 0);
    std::vector<OT> qExpectedOutputData = armnnUtils::QuantizedVector<OT>(expectedOutputData, qScale, qOffset);

    ConstTensor weights(weightsInfo, qWeightsData);
    ConstTensor biases(biasesInfo, qBiasesData);

    INetworkPtr network = CreateConstConvolution2dNetwork(descriptor,
                                                          inputInfo,
                                                          weightsInfo,
                                                          biasesInfo,
                                                          outputInfo,
                                                          weights,
                                                          biases,
                                                          biasEnabled);

    EndToEndLayerTestImpl<ArmnnIType, ArmnnOType>(std::move(network),
                                                  {{ 0, qInputData }},
                                                  {{ 0, qExpectedOutputData }},
                                                  backends);
}

} // anonymous namespace