blob: 38216f106bb6020f8d735e336e5ef084a7301f82 [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{
Kevin Mayedc5ffa2019-05-22 12:02:53 +010023#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 }
surmeh0149b9e102018-05-17 14:11:25 +010032
Nikhil Raj77605822018-09-03 11:25:56 +010033void SetModelFp16Flag(V1_0::Model& model, bool fp16Enabled);
34
Nikhil Raj77605822018-09-03 11:25:56 +010035void SetModelFp16Flag(V1_1::Model& model, bool fp16Enabled);
Nikhil Raj77605822018-09-03 11:25:56 +010036
37template<typename HalPolicy>
38void PaddingTestImpl(android::nn::PaddingScheme paddingScheme, bool fp16Enabled = false)
surmeh0149b9e102018-05-17 14:11:25 +010039{
Nikhil Raj77605822018-09-03 11:25:56 +010040 using HalModel = typename HalPolicy::Model;
41 using HalOperationType = typename HalPolicy::OperationType;
42
Kevin Mayedc5ffa2019-05-22 12:02:53 +010043 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));
Nikhil Raj77605822018-09-03 11:25:56 +010050 HalModel model = {};
surmeh0149b9e102018-05-17 14:11:25 +010051
52 uint32_t outSize = paddingScheme == android::nn::kPaddingSame ? 2 : 1;
53
54 // add operands
Nikhil Raj77605822018-09-03 11:25:56 +010055 float weightValue[] = {1.f, -1.f, 0.f, 1.f};
56 float biasValue[] = {0.f};
surmeh0149b9e102018-05-17 14:11:25 +010057
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010058 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});
surmeh0149b9e102018-05-17 14:11:25 +010066
67 // make the convolution operation
68 model.operations.resize(1);
Nikhil Raj77605822018-09-03 11:25:56 +010069 model.operations[0].type = HalOperationType::CONV_2D;
surmeh0149b9e102018-05-17 14:11:25 +010070 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
Nikhil Raj77605822018-09-03 11:25:56 +010074 SetModelFp16Flag(model, fp16Enabled);
Sadik Armagane6e54a82019-05-08 10:18:05 +010075 android::sp<V1_0::IPreparedModel> preparedModel = PrepareModel(model, *driver);
surmeh0149b9e102018-05-17 14:11:25 +010076
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
Kevin Mayec1e5b82020-02-26 17:00:39 +000094 V1_0::Request request = {};
surmeh0149b9e102018-05-17 14:11:25 +010095 request.inputs = hidl_vec<RequestArgument>{input};
96 request.outputs = hidl_vec<RequestArgument>{output};
97
surmeh0149b9e102018-05-17 14:11:25 +010098 // set the input data (matching source test)
Nikhil Raj77605822018-09-03 11:25:56 +010099 float indata[] = {1024.25f, 1.f, 0.f, 3.f, -1, -1024.25f};
surmeh0149b9e102018-05-17 14:11:25 +0100100 AddPoolAndSetData(6, request, indata);
101
102 // add memory for the output
Ellen Norris-Thompson976ad3e2019-08-21 15:21:14 +0100103 android::sp<IMemory> outMemory = AddPoolAndGetData<float>(outSize, request);
Nikhil Raj77605822018-09-03 11:25:56 +0100104 float* outdata = reinterpret_cast<float*>(static_cast<void*>(outMemory->getPointer()));
surmeh0149b9e102018-05-17 14:11:25 +0100105
106 // run the execution
107 Execute(preparedModel, request);
108
109 // check the result
Nikhil Raj77605822018-09-03 11:25:56 +0100110 switch (paddingScheme)
surmeh0149b9e102018-05-17 14:11:25 +0100111 {
Nikhil Raj77605822018-09-03 11:25:56 +0100112 case android::nn::kPaddingValid:
Kevin Mayedc5ffa2019-05-22 12:02:53 +0100113 ARMNN_ANDROID_FP16_TEST(outdata[0], 1022.f, 1022.25f, fp16Enabled)
Nikhil Raj77605822018-09-03 11:25:56 +0100114 break;
115 case android::nn::kPaddingSame:
Kevin Mayedc5ffa2019-05-22 12:02:53 +0100116 ARMNN_ANDROID_FP16_TEST(outdata[0], 1022.f, 1022.25f, fp16Enabled)
117 BOOST_TEST(outdata[1] == 0.f);
Nikhil Raj77605822018-09-03 11:25:56 +0100118 break;
119 default:
surmeh0149b9e102018-05-17 14:11:25 +0100120 BOOST_TEST(false);
Nikhil Raj77605822018-09-03 11:25:56 +0100121 break;
surmeh0149b9e102018-05-17 14:11:25 +0100122 }
123}
124
Nikhil Raj77605822018-09-03 11:25:56 +0100125} // namespace driverTestHelpers
surmeh0149b9e102018-05-17 14:11:25 +0100126
127BOOST_AUTO_TEST_SUITE_END()