• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "DriverTestHelpers.hpp"
9 
10 #include <boost/test/unit_test.hpp>
11 #include <log/log.h>
12 
13 #include <OperationsUtils.h>
14 
15 BOOST_AUTO_TEST_SUITE(Convolution2DTests)
16 
17 using namespace android::hardware;
18 using namespace driverTestHelpers;
19 using namespace armnn_driver;
20 
21 namespace driverTestHelpers
22 {
23 #define ARMNN_ANDROID_FP16_TEST(result, fp16Expectation, fp32Expectation, fp16Enabled) \
24    if (fp16Enabled) \
25    { \
26        BOOST_TEST((result == fp16Expectation || result == fp32Expectation), result << \
27        " does not match either " << fp16Expectation << "[fp16] or " << fp32Expectation << "[fp32]"); \
28    } else \
29    { \
30       BOOST_TEST(result == fp32Expectation); \
31    }
32 
33 void SetModelFp16Flag(V1_0::Model& model, bool fp16Enabled);
34 
35 void SetModelFp16Flag(V1_1::Model& model, bool fp16Enabled);
36 
37 template<typename HalPolicy>
PaddingTestImpl(android::nn::PaddingScheme paddingScheme,bool fp16Enabled=false)38 void PaddingTestImpl(android::nn::PaddingScheme paddingScheme, bool fp16Enabled = false)
39 {
40     using HalModel         = typename HalPolicy::Model;
41     using HalOperationType = typename HalPolicy::OperationType;
42 
43     armnn::Compute computeDevice = armnn::Compute::GpuAcc;
44 
45 #ifndef ARMCOMPUTECL_ENABLED
46     computeDevice = armnn::Compute::CpuRef;
47 #endif
48 
49     auto driver = std::make_unique<ArmnnDriver>(DriverOptions(computeDevice, fp16Enabled));
50     HalModel model = {};
51 
52     uint32_t outSize = paddingScheme == android::nn::kPaddingSame ? 2 : 1;
53 
54     // add operands
55     float weightValue[] = {1.f, -1.f, 0.f, 1.f};
56     float biasValue[]   = {0.f};
57 
58     AddInputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 2, 3, 1});
59     AddTensorOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 2, 2, 1}, weightValue);
60     AddTensorOperand<HalPolicy>(model, hidl_vec<uint32_t>{1}, biasValue);
61     AddIntOperand<HalPolicy>(model, (int32_t)paddingScheme); // padding
62     AddIntOperand<HalPolicy>(model, 2); // stride x
63     AddIntOperand<HalPolicy>(model, 2); // stride y
64     AddIntOperand<HalPolicy>(model, 0); // no activation
65     AddOutputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 1, outSize, 1});
66 
67     // make the convolution operation
68     model.operations.resize(1);
69     model.operations[0].type = HalOperationType::CONV_2D;
70     model.operations[0].inputs  = hidl_vec<uint32_t>{0, 1, 2, 3, 4, 5, 6};
71     model.operations[0].outputs = hidl_vec<uint32_t>{7};
72 
73     // make the prepared model
74     SetModelFp16Flag(model, fp16Enabled);
75     android::sp<V1_0::IPreparedModel> preparedModel = PrepareModel(model, *driver);
76 
77     // construct the request
78     DataLocation inloc    = {};
79     inloc.poolIndex       = 0;
80     inloc.offset          = 0;
81     inloc.length          = 6 * sizeof(float);
82     RequestArgument input = {};
83     input.location        = inloc;
84     input.dimensions      = hidl_vec<uint32_t>{};
85 
86     DataLocation outloc    = {};
87     outloc.poolIndex       = 1;
88     outloc.offset          = 0;
89     outloc.length          = outSize * sizeof(float);
90     RequestArgument output = {};
91     output.location        = outloc;
92     output.dimensions      = hidl_vec<uint32_t>{};
93 
94     V1_0::Request request = {};
95     request.inputs  = hidl_vec<RequestArgument>{input};
96     request.outputs = hidl_vec<RequestArgument>{output};
97 
98     // set the input data (matching source test)
99     float indata[] = {1024.25f, 1.f, 0.f, 3.f, -1, -1024.25f};
100     AddPoolAndSetData(6, request, indata);
101 
102     // add memory for the output
103     android::sp<IMemory> outMemory = AddPoolAndGetData<float>(outSize, request);
104     float* outdata = reinterpret_cast<float*>(static_cast<void*>(outMemory->getPointer()));
105 
106     // run the execution
107     if (preparedModel.get() != nullptr)
108     {
109         Execute(preparedModel, request);
110     }
111 
112     // check the result
113     switch (paddingScheme)
114     {
115     case android::nn::kPaddingValid:
116         ARMNN_ANDROID_FP16_TEST(outdata[0], 1022.f, 1022.25f, fp16Enabled)
117         break;
118     case android::nn::kPaddingSame:
119         ARMNN_ANDROID_FP16_TEST(outdata[0], 1022.f, 1022.25f, fp16Enabled)
120         BOOST_TEST(outdata[1] == 0.f);
121         break;
122     default:
123         BOOST_TEST(false);
124         break;
125     }
126 }
127 
128 } // namespace driverTestHelpers
129 
130 BOOST_AUTO_TEST_SUITE_END()
131