aboutsummaryrefslogtreecommitdiff
path: root/test/1.1/Mean.cpp
blob: c7c5a9b5974a67354f669d79fcf98dd18681c087 (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
249
250
251
252
253
254
255
256
257
258
259
260
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "../DriverTestHelpers.hpp"
#include "../TestTensor.hpp"

#include "../1.1/HalPolicy.hpp"

#include <doctest/doctest.h>

#include <array>

using namespace android::hardware;
using namespace driverTestHelpers;
using namespace armnn_driver;

using HalPolicy = hal_1_1::HalPolicy;
using RequestArgument = V1_0::RequestArgument;

namespace
{

void MeanTestImpl(const TestTensor& input,
                  const hidl_vec<uint32_t>& axisDimensions,
                  const int32_t* axisValues,
                  int32_t keepDims,
                  const TestTensor& expectedOutput,
                  bool fp16Enabled,
                  armnn::Compute computeDevice)
{
    auto driver = std::make_unique<ArmnnDriver>(DriverOptions(computeDevice, fp16Enabled));

    HalPolicy::Model model = {};

    AddInputOperand<HalPolicy>(model, input.GetDimensions());

    AddTensorOperand<HalPolicy>(model,
                                axisDimensions,
                                const_cast<int32_t*>(axisValues),
                                HalPolicy::OperandType::TENSOR_INT32);

    AddIntOperand<HalPolicy>(model, keepDims);

    AddOutputOperand<HalPolicy>(model, expectedOutput.GetDimensions());

    model.operations.resize(1);
    model.operations[0].type               = HalPolicy::OperationType::MEAN;
    model.operations[0].inputs             = hidl_vec<uint32_t>{ 0, 1, 2 };
    model.operations[0].outputs            = hidl_vec<uint32_t>{ 3 };
    model.relaxComputationFloat32toFloat16 = fp16Enabled;

    android::sp<V1_0::IPreparedModel> preparedModel = PrepareModel(model, *driver);

    // The request's memory pools will follow the same order as the inputs
    V1_0::DataLocation inLoc = {};
    inLoc.poolIndex          = 0;
    inLoc.offset             = 0;
    inLoc.length             = input.GetNumElements() * sizeof(float);
    RequestArgument inArg    = {};
    inArg.location           = inLoc;
    inArg.dimensions         = input.GetDimensions();

    // An additional memory pool is needed for the output
    V1_0::DataLocation outLoc = {};
    outLoc.poolIndex          = 1;
    outLoc.offset             = 0;
    outLoc.length             = expectedOutput.GetNumElements() * sizeof(float);
    RequestArgument outArg    = {};
    outArg.location           = outLoc;
    outArg.dimensions         = expectedOutput.GetDimensions();

    // Make the request based on the arguments
    V1_0::Request request = {};
    request.inputs  = hidl_vec<RequestArgument>{ inArg };
    request.outputs = hidl_vec<RequestArgument>{ outArg };

    // Set the input data
    AddPoolAndSetData(input.GetNumElements(), request, input.GetData());

    // Add memory for the output
    android::sp<IMemory> outMemory = AddPoolAndGetData<float>(expectedOutput.GetNumElements(), request);
    const float* outputData = static_cast<const float*>(static_cast<void*>(outMemory->getPointer()));

    if (preparedModel.get() != nullptr)
    {
        V1_0::ErrorStatus execStatus = Execute(preparedModel, request);
        CHECK((int)execStatus == (int)V1_0::ErrorStatus::NONE);
    }

    const float* expectedOutputData = expectedOutput.GetData();
    for (unsigned int i = 0; i < expectedOutput.GetNumElements(); i++)
    {
        CHECK(outputData[i] == expectedOutputData[i]);
    }
}

} // anonymous namespace

TEST_SUITE("MeanTests_CpuRef")
{
    TEST_CASE("MeanNoKeepDimsTest_CpuRef")
    {
        TestTensor input{ armnn::TensorShape{ 4, 3, 2 },
                          { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
                            11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f,
                            20.0f, 21.0f, 22.0f, 23.0f, 24.0f } };
        hidl_vec<uint32_t> axisDimensions = { 2 };
        int32_t axisValues[] = { 0, 1 };
        int32_t keepDims = 0;
        TestTensor expectedOutput{ armnn::TensorShape{ 2 }, { 12.0f, 13.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, false, armnn::Compute::CpuRef);
    }

    TEST_CASE("MeanKeepDimsTest_CpuRef")
    {
        TestTensor input{ armnn::TensorShape{ 1, 1, 3, 2 }, { 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f } };
        hidl_vec<uint32_t> axisDimensions = { 1 };
        int32_t axisValues[] = { 2 };
        int32_t keepDims = 1;
        TestTensor expectedOutput{ armnn::TensorShape{ 1, 1, 1, 2 }, {  2.0f, 2.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, false, armnn::Compute::CpuRef);
    }

    TEST_CASE("MeanFp16NoKeepDimsTest_CpuRef")
    {
        TestTensor input{ armnn::TensorShape{ 4, 3, 2 },
                          { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
                            11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f,
                            20.0f, 21.0f, 22.0f, 23.0f, 24.0f } };
        hidl_vec<uint32_t> axisDimensions = { 2 };
        int32_t axisValues[] = { 0, 1 };
        int32_t keepDims = 0;
        TestTensor expectedOutput{ armnn::TensorShape{ 2 }, { 12.0f, 13.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, true, armnn::Compute::CpuRef);
    }

    TEST_CASE("MeanFp16KeepDimsTest_CpuRef")
    {
        TestTensor input{ armnn::TensorShape{ 1, 1, 3, 2 }, { 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f } };
        hidl_vec<uint32_t> axisDimensions = { 1 };
        int32_t axisValues[] = { 2 };
        int32_t keepDims = 1;
        TestTensor expectedOutput{ armnn::TensorShape{ 1, 1, 1, 2 }, {  2.0f, 2.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, true, armnn::Compute::CpuRef);
    }
}

#ifdef ARMCOMPUTECL_ENABLED
TEST_SUITE("MeanTests_CpuAcc")
{
    TEST_CASE("MeanNoKeepDimsTest_CpuAcc")
    {
        TestTensor input{ armnn::TensorShape{ 4, 3, 2 },
                          { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
                            11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f,
                            20.0f, 21.0f, 22.0f, 23.0f, 24.0f } };
        hidl_vec<uint32_t> axisDimensions = { 2 };
        int32_t axisValues[] = { 0, 1 };
        int32_t keepDims = 0;
        TestTensor expectedOutput{ armnn::TensorShape{ 2 }, { 12.0f, 13.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, false, armnn::Compute::CpuAcc);
    }

    TEST_CASE("MeanKeepDimsTest_CpuAcc")
    {
        TestTensor input{ armnn::TensorShape{ 1, 1, 3, 2 }, { 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f } };
        hidl_vec<uint32_t> axisDimensions = { 1 };
        int32_t axisValues[] = { 2 };
        int32_t keepDims = 1;
        TestTensor expectedOutput{ armnn::TensorShape{ 1, 1, 1, 2 }, {  2.0f, 2.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, false, armnn::Compute::CpuAcc);
    }

    TEST_CASE("MeanFp16NoKeepDimsTest_CpuAcc")
    {
        TestTensor input{ armnn::TensorShape{ 4, 3, 2 },
                          { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
                            11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f,
                            20.0f, 21.0f, 22.0f, 23.0f, 24.0f } };
        hidl_vec<uint32_t> axisDimensions = { 2 };
        int32_t axisValues[] = { 0, 1 };
        int32_t keepDims = 0;
        TestTensor expectedOutput{ armnn::TensorShape{ 2 }, { 12.0f, 13.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, true, armnn::Compute::CpuAcc);
    }

    TEST_CASE("MeanFp16KeepDimsTest_CpuAcc")
    {
        TestTensor input{ armnn::TensorShape{ 1, 1, 3, 2 }, { 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f } };
        hidl_vec<uint32_t> axisDimensions = { 1 };
        int32_t axisValues[] = { 2 };
        int32_t keepDims = 1;
        TestTensor expectedOutput{ armnn::TensorShape{ 1, 1, 1, 2 }, {  2.0f, 2.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, true, armnn::Compute::CpuAcc);
    }
}

TEST_SUITE("MeanTests_GpuAcc")
{
    TEST_CASE("MeanNoKeepDimsTest_GpuAcc")
    {
        TestTensor input{ armnn::TensorShape{ 4, 3, 2 },
                          { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
                            11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f,
                            20.0f, 21.0f, 22.0f, 23.0f, 24.0f } };
        hidl_vec<uint32_t> axisDimensions = { 2 };
        int32_t axisValues[] = { 0, 1 };
        int32_t keepDims = 0;
        TestTensor expectedOutput{ armnn::TensorShape{ 2 }, { 12.0f, 13.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, false, armnn::Compute::GpuAcc);
    }

    TEST_CASE("MeanKeepDimsTest_GpuAcc")
    {
        TestTensor input{ armnn::TensorShape{ 1, 1, 3, 2 }, { 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f } };
        hidl_vec<uint32_t> axisDimensions = { 1 };
        int32_t axisValues[] = { 2 };
        int32_t keepDims = 1;
        TestTensor expectedOutput{ armnn::TensorShape{ 1, 1, 1, 2 }, {  2.0f, 2.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, false, armnn::Compute::GpuAcc);
    }

    TEST_CASE("MeanFp16NoKeepDimsTest_GpuAcc")
    {
        TestTensor input{ armnn::TensorShape{ 4, 3, 2 },
                          { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
                            11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f,
                            20.0f, 21.0f, 22.0f, 23.0f, 24.0f } };
        hidl_vec<uint32_t> axisDimensions = { 2 };
        int32_t axisValues[] = { 0, 1 };
        int32_t keepDims = 0;
        TestTensor expectedOutput{ armnn::TensorShape{ 2 }, { 12.0f, 13.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, true, armnn::Compute::GpuAcc);
    }

    TEST_CASE("MeanFp16KeepDimsTest_GpuAcc")
    {
        TestTensor input{ armnn::TensorShape{ 1, 1, 3, 2 }, { 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f } };
        hidl_vec<uint32_t> axisDimensions = { 1 };
        int32_t axisValues[] = { 2 };
        int32_t keepDims = 1;
        TestTensor expectedOutput{ armnn::TensorShape{ 1, 1, 1, 2 }, {  2.0f, 2.0f } };

        MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, true, armnn::Compute::GpuAcc);
    }
}
#endif