blob: ff417d96af06c683762575d6b0029a47b8cccdf5 [file] [log] [blame]
surmeh0149b9e102018-05-17 14:11:25 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
surmeh0149b9e102018-05-17 14:11:25 +01004//
Nikhil Raj77605822018-09-03 11:25:56 +01005
6#pragma once
7
surmeh0149b9e102018-05-17 14:11:25 +01008#include "DriverTestHelpers.hpp"
Nikhil Raj77605822018-09-03 11:25:56 +01009
surmeh0149b9e102018-05-17 14:11:25 +010010#include <boost/test/unit_test.hpp>
11#include <log/log.h>
12
telsoa01ce3e84a2018-08-31 09:31:35 +010013#include <OperationsUtils.h>
surmeh0149b9e102018-05-17 14:11:25 +010014
15BOOST_AUTO_TEST_SUITE(Convolution2DTests)
16
telsoa01ce3e84a2018-08-31 09:31:35 +010017using namespace android::hardware;
surmeh0149b9e102018-05-17 14:11:25 +010018using namespace driverTestHelpers;
telsoa01ce3e84a2018-08-31 09:31:35 +010019using namespace armnn_driver;
surmeh0149b9e102018-05-17 14:11:25 +010020
Nikhil Raj77605822018-09-03 11:25:56 +010021namespace driverTestHelpers
surmeh0149b9e102018-05-17 14:11:25 +010022{
23
Nikhil Raj77605822018-09-03 11:25:56 +010024void SetModelFp16Flag(V1_0::Model& model, bool fp16Enabled);
25
26#ifdef ARMNN_ANDROID_NN_V1_1
27void SetModelFp16Flag(V1_1::Model& model, bool fp16Enabled);
28#endif
29
30template<typename HalPolicy>
31void PaddingTestImpl(android::nn::PaddingScheme paddingScheme, bool fp16Enabled = false)
surmeh0149b9e102018-05-17 14:11:25 +010032{
Nikhil Raj77605822018-09-03 11:25:56 +010033 using HalModel = typename HalPolicy::Model;
34 using HalOperationType = typename HalPolicy::OperationType;
35
36 auto driver = std::make_unique<ArmnnDriver>(DriverOptions(armnn::Compute::GpuAcc, fp16Enabled));
37 HalModel model = {};
surmeh0149b9e102018-05-17 14:11:25 +010038
39 uint32_t outSize = paddingScheme == android::nn::kPaddingSame ? 2 : 1;
40
41 // add operands
Nikhil Raj77605822018-09-03 11:25:56 +010042 float weightValue[] = {1.f, -1.f, 0.f, 1.f};
43 float biasValue[] = {0.f};
surmeh0149b9e102018-05-17 14:11:25 +010044
45 AddInputOperand(model, hidl_vec<uint32_t>{1, 2, 3, 1});
46 AddTensorOperand(model, hidl_vec<uint32_t>{1, 2, 2, 1}, weightValue);
47 AddTensorOperand(model, hidl_vec<uint32_t>{1}, biasValue);
48 AddIntOperand(model, (int32_t)paddingScheme); // padding
49 AddIntOperand(model, 2); // stride x
50 AddIntOperand(model, 2); // stride y
51 AddIntOperand(model, 0); // no activation
52 AddOutputOperand(model, hidl_vec<uint32_t>{1, 1, outSize, 1});
53
54 // make the convolution operation
55 model.operations.resize(1);
Nikhil Raj77605822018-09-03 11:25:56 +010056 model.operations[0].type = HalOperationType::CONV_2D;
surmeh0149b9e102018-05-17 14:11:25 +010057 model.operations[0].inputs = hidl_vec<uint32_t>{0, 1, 2, 3, 4, 5, 6};
58 model.operations[0].outputs = hidl_vec<uint32_t>{7};
59
60 // make the prepared model
Nikhil Raj77605822018-09-03 11:25:56 +010061 SetModelFp16Flag(model, fp16Enabled);
surmeh0149b9e102018-05-17 14:11:25 +010062 android::sp<IPreparedModel> preparedModel = PrepareModel(model, *driver);
63
64 // construct the request
65 DataLocation inloc = {};
66 inloc.poolIndex = 0;
67 inloc.offset = 0;
68 inloc.length = 6 * sizeof(float);
69 RequestArgument input = {};
70 input.location = inloc;
71 input.dimensions = hidl_vec<uint32_t>{};
72
73 DataLocation outloc = {};
74 outloc.poolIndex = 1;
75 outloc.offset = 0;
76 outloc.length = outSize * sizeof(float);
77 RequestArgument output = {};
78 output.location = outloc;
79 output.dimensions = hidl_vec<uint32_t>{};
80
81 Request request = {};
82 request.inputs = hidl_vec<RequestArgument>{input};
83 request.outputs = hidl_vec<RequestArgument>{output};
84
surmeh0149b9e102018-05-17 14:11:25 +010085 // set the input data (matching source test)
Nikhil Raj77605822018-09-03 11:25:56 +010086 float indata[] = {1024.25f, 1.f, 0.f, 3.f, -1, -1024.25f};
surmeh0149b9e102018-05-17 14:11:25 +010087 AddPoolAndSetData(6, request, indata);
88
89 // add memory for the output
90 android::sp<IMemory> outMemory = AddPoolAndGetData(outSize, request);
Nikhil Raj77605822018-09-03 11:25:56 +010091 float* outdata = reinterpret_cast<float*>(static_cast<void*>(outMemory->getPointer()));
surmeh0149b9e102018-05-17 14:11:25 +010092
93 // run the execution
94 Execute(preparedModel, request);
95
96 // check the result
Nikhil Raj77605822018-09-03 11:25:56 +010097 switch (paddingScheme)
surmeh0149b9e102018-05-17 14:11:25 +010098 {
Nikhil Raj77605822018-09-03 11:25:56 +010099 case android::nn::kPaddingValid:
100 if (fp16Enabled)
101 {
102 BOOST_TEST(outdata[0] == 1022.f);
103 }
104 else
105 {
106 BOOST_TEST(outdata[0] == 1022.25f);
107 }
108 break;
109 case android::nn::kPaddingSame:
110 if (fp16Enabled)
111 {
112 BOOST_TEST(outdata[0] == 1022.f);
113 BOOST_TEST(outdata[1] == 0.f);
114 }
115 else
116 {
117 BOOST_TEST(outdata[0] == 1022.25f);
118 BOOST_TEST(outdata[1] == 0.f);
119 }
120 break;
121 default:
surmeh0149b9e102018-05-17 14:11:25 +0100122 BOOST_TEST(false);
Nikhil Raj77605822018-09-03 11:25:56 +0100123 break;
surmeh0149b9e102018-05-17 14:11:25 +0100124 }
125}
126
Nikhil Raj77605822018-09-03 11:25:56 +0100127} // namespace driverTestHelpers
surmeh0149b9e102018-05-17 14:11:25 +0100128
129BOOST_AUTO_TEST_SUITE_END()