blob: 540cdd7bc8778017da6541c98f6ba1f54810b1ae [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
Sadik Armagan9150bff2021-05-26 15:40:53 +010010#include <doctest/doctest.h>
surmeh0149b9e102018-05-17 14:11:25 +010011#include <log/log.h>
12
telsoa01ce3e84a2018-08-31 09:31:35 +010013#include <OperationsUtils.h>
surmeh0149b9e102018-05-17 14:11:25 +010014
telsoa01ce3e84a2018-08-31 09:31:35 +010015using namespace android::hardware;
surmeh0149b9e102018-05-17 14:11:25 +010016using namespace driverTestHelpers;
telsoa01ce3e84a2018-08-31 09:31:35 +010017using namespace armnn_driver;
surmeh0149b9e102018-05-17 14:11:25 +010018
Sadik Armagan188675f2021-02-12 17:16:42 +000019using RequestArgument = V1_0::RequestArgument;
20
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 { \
Sadik Armagan9150bff2021-05-26 15:40:53 +010026 CHECK_MESSAGE((result == fp16Expectation || result == fp32Expectation), result << \
Kevin Mayedc5ffa2019-05-22 12:02:53 +010027 " does not match either " << fp16Expectation << "[fp16] or " << fp32Expectation << "[fp32]"); \
28 } else \
29 { \
Sadik Armagan9150bff2021-05-26 15:40:53 +010030 CHECK(result == fp32Expectation); \
Kevin Mayedc5ffa2019-05-22 12:02:53 +010031 }
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};
Sadik Armagan9150bff2021-05-26 15:40:53 +010056 float biasValue[] = {0.f};
surmeh0149b9e102018-05-17 14:11:25 +010057
Sadik Armagan9150bff2021-05-26 15:40:53 +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
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010062 AddIntOperand<HalPolicy>(model, 2); // stride x
63 AddIntOperand<HalPolicy>(model, 2); // stride y
64 AddIntOperand<HalPolicy>(model, 0); // no activation
Sadik Armagan9150bff2021-05-26 15:40:53 +010065 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;
Sadik Armagan9150bff2021-05-26 15:40:53 +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};
surmeh0149b9e102018-05-17 14:11:25 +010072
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
Sadik Armagan188675f2021-02-12 17:16:42 +000078 V1_0::DataLocation inloc = {};
Sadik Armagan9150bff2021-05-26 15:40:53 +010079 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 > {};
surmeh0149b9e102018-05-17 14:11:25 +010085
Sadik Armagan188675f2021-02-12 17:16:42 +000086 V1_0::DataLocation outloc = {};
Sadik Armagan9150bff2021-05-26 15:40:53 +010087 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 > {};
surmeh0149b9e102018-05-17 14:11:25 +010093
Kevin Mayec1e5b82020-02-26 17:00:39 +000094 V1_0::Request request = {};
Sadik Armagan9150bff2021-05-26 15:40:53 +010095 request.inputs = hidl_vec < RequestArgument > {input};
96 request.outputs = hidl_vec < RequestArgument > {output};
surmeh0149b9e102018-05-17 14:11:25 +010097
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
Sadik Armagand4636872020-04-27 10:15:41 +0100107 if (preparedModel.get() != nullptr)
108 {
109 Execute(preparedModel, request);
110 }
surmeh0149b9e102018-05-17 14:11:25 +0100111
112 // check the result
Nikhil Raj77605822018-09-03 11:25:56 +0100113 switch (paddingScheme)
surmeh0149b9e102018-05-17 14:11:25 +0100114 {
Sadik Armagan9150bff2021-05-26 15:40:53 +0100115 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 CHECK(outdata[1] == 0.f);
121 break;
122 default:
123 CHECK(false);
124 break;
surmeh0149b9e102018-05-17 14:11:25 +0100125 }
126}
127
Nikhil Raj77605822018-09-03 11:25:56 +0100128} // namespace driverTestHelpers