aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/test/RefPerAxisIteratorTests.cpp
blob: 06d2703b4ec76b100fe9ba4d028cfac688fc6563 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <reference/workloads/Decoders.hpp>

#include <fmt/format.h>

#include <doctest/doctest.h>

#include <chrono>

template<typename T>
void CompareVector(std::vector<T> vec1, std::vector<T> vec2)
{
    CHECK(vec1.size() == vec2.size());

    bool mismatch = false;
    for (uint i = 0; i < vec1.size(); ++i)
    {
        if (vec1[i] != vec2[i])
        {
            MESSAGE(fmt::format("Vector value mismatch: index={}  {} != {}",
                                i,
                                vec1[i],
                                vec2[i]));

            mismatch = true;
        }
    }

    if (mismatch)
    {
        FAIL("Error in CompareVector. Vectors don't match.");
    }
}

using namespace armnn;

// Basically a per axis decoder but without any decoding/quantization
class MockPerAxisIterator : public PerAxisIterator<const int8_t, Decoder<int8_t>>
{
public:
    MockPerAxisIterator(const int8_t* data, const armnn::TensorShape& tensorShape, const unsigned int axis)
            : PerAxisIterator(data, tensorShape, axis), m_NumElements(tensorShape.GetNumElements())
    {}

    int8_t Get() const override
    {
        return *m_Iterator;
    }

    virtual std::vector<float> DecodeTensor(const TensorShape &tensorShape,
                                            bool isDepthwise = false) override
    {
        IgnoreUnused(tensorShape, isDepthwise);
        return std::vector<float>{};
    };

    // Iterates over data using operator[] and returns vector
    std::vector<int8_t> Loop()
    {
        std::vector<int8_t> vec;
        for (uint32_t i = 0; i < m_NumElements; ++i)
        {
            this->operator[](i);
            vec.emplace_back(Get());
        }
        return vec;
    }

    unsigned int GetAxisIndex()
    {
        return m_AxisIndex;
    }
    unsigned int m_NumElements;
};

TEST_SUITE("RefPerAxisIterator")
{
// Test Loop (Equivalent to DecodeTensor) and Axis = 0
TEST_CASE("PerAxisIteratorTest1")
{
    std::vector<int8_t> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8);

    // test axis=0
    std::vector<int8_t> expOutput = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 0);
    std::vector<int8_t> output = iterator.Loop();
    CompareVector(output, expOutput);

    // Set iterator to index and check if the axis index is correct
    iterator[5];
    CHECK(iterator.GetAxisIndex() == 1u);

    iterator[1];
    CHECK(iterator.GetAxisIndex() == 0u);

    iterator[10];
    CHECK(iterator.GetAxisIndex() == 2u);
}

// Test Axis = 1
TEST_CASE("PerAxisIteratorTest2")
{
    std::vector<int8_t> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8);

    // test axis=1
    std::vector<int8_t> expOutput = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 1);
    std::vector<int8_t> output = iterator.Loop();
    CompareVector(output, expOutput);

    // Set iterator to index and check if the axis index is correct
    iterator[5];
    CHECK(iterator.GetAxisIndex() == 0u);

    iterator[1];
    CHECK(iterator.GetAxisIndex() == 0u);

    iterator[10];
    CHECK(iterator.GetAxisIndex() == 0u);
}

// Test Axis = 2
TEST_CASE("PerAxisIteratorTest3")
{
    std::vector<int8_t> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8);

    // test axis=2
    std::vector<int8_t> expOutput = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 2);
    std::vector<int8_t> output = iterator.Loop();
    CompareVector(output, expOutput);

    // Set iterator to index and check if the axis index is correct
    iterator[5];
    CHECK(iterator.GetAxisIndex() == 0u);

    iterator[1];
    CHECK(iterator.GetAxisIndex() == 0u);

    iterator[10];
    CHECK(iterator.GetAxisIndex() == 1u);
}

// Test Axis = 3
TEST_CASE("PerAxisIteratorTest4")
{
    std::vector<int8_t> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8);

    // test axis=3
    std::vector<int8_t> expOutput = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 3);
    std::vector<int8_t> output = iterator.Loop();
    CompareVector(output, expOutput);

    // Set iterator to index and check if the axis index is correct
    iterator[5];
    CHECK(iterator.GetAxisIndex() == 1u);

    iterator[1];
    CHECK(iterator.GetAxisIndex() == 1u);

    iterator[10];
    CHECK(iterator.GetAxisIndex() == 0u);
}

// Test Axis = 1. Different tensor shape
TEST_CASE("PerAxisIteratorTest5")
{
    using namespace armnn;
    std::vector<int8_t> input =
    {
         0,  1,  2,  3,
         4,  5,  6,  7,
         8,  9, 10, 11,
        12, 13, 14, 15
    };

    std::vector<int8_t> expOutput =
    {
         0,  1,  2,  3,
         4,  5,  6,  7,
         8,  9, 10, 11,
        12, 13, 14, 15
    };

    TensorInfo tensorInfo ({2,2,2,2},DataType::QSymmS8);
    auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 1);
    std::vector<int8_t> output = iterator.Loop();
    CompareVector(output, expOutput);

    // Set iterator to index and check if the axis index is correct
    iterator[5];
    CHECK(iterator.GetAxisIndex() == 1u);

    iterator[1];
    CHECK(iterator.GetAxisIndex() == 0u);

    iterator[10];
    CHECK(iterator.GetAxisIndex() == 0u);
}

// Test the increment and decrement operator
TEST_CASE("PerAxisIteratorTest7")
{
    using namespace armnn;
    std::vector<int8_t> input =
    {
        0, 1,  2,  3,
        4, 5,  6,  7,
        8, 9, 10, 11
    };

    std::vector<int8_t> expOutput =
    {
        0, 1,  2,  3,
        4, 5,  6,  7,
        8, 9, 10, 11
    };

    TensorInfo tensorInfo ({3,1,2,2},DataType::QSymmS8);
    auto iterator = MockPerAxisIterator(input.data(), tensorInfo.GetShape(), 2);

    iterator += 3;
    CHECK(iterator.Get() == expOutput[3]);
    CHECK(iterator.GetAxisIndex() == 1u);

    iterator += 3;
    CHECK(iterator.Get() == expOutput[6]);
    CHECK(iterator.GetAxisIndex() == 1u);

    iterator -= 2;
    CHECK(iterator.Get() == expOutput[4]);
    CHECK(iterator.GetAxisIndex() == 0u);

    iterator -= 1;
    CHECK(iterator.Get() == expOutput[3]);
    CHECK(iterator.GetAxisIndex() == 1u);
}

}