surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // See LICENSE file in the project root for full license information. |
| 4 | // |
| 5 | #include "DriverTestHelpers.hpp" |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | #include <log/log.h> |
| 8 | |
| 9 | #include "OperationsUtils.h" |
| 10 | |
| 11 | BOOST_AUTO_TEST_SUITE(Convolution2DTests) |
| 12 | |
| 13 | using ArmnnDriver = armnn_driver::ArmnnDriver; |
| 14 | using DriverOptions = armnn_driver::DriverOptions; |
| 15 | using namespace driverTestHelpers; |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | void PaddingTestImpl(android::nn::PaddingScheme paddingScheme) |
| 21 | { |
| 22 | auto driver = std::make_unique<ArmnnDriver>(DriverOptions(armnn::Compute::CpuRef)); |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame^] | 23 | V1_0::Model model = {}; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 24 | |
| 25 | uint32_t outSize = paddingScheme == android::nn::kPaddingSame ? 2 : 1; |
| 26 | |
| 27 | // add operands |
| 28 | float weightValue[] = {1, -1, 0, 1}; |
| 29 | float biasValue[] = {0}; |
| 30 | |
| 31 | AddInputOperand(model, hidl_vec<uint32_t>{1, 2, 3, 1}); |
| 32 | AddTensorOperand(model, hidl_vec<uint32_t>{1, 2, 2, 1}, weightValue); |
| 33 | AddTensorOperand(model, hidl_vec<uint32_t>{1}, biasValue); |
| 34 | AddIntOperand(model, (int32_t)paddingScheme); // padding |
| 35 | AddIntOperand(model, 2); // stride x |
| 36 | AddIntOperand(model, 2); // stride y |
| 37 | AddIntOperand(model, 0); // no activation |
| 38 | AddOutputOperand(model, hidl_vec<uint32_t>{1, 1, outSize, 1}); |
| 39 | |
| 40 | // make the convolution operation |
| 41 | model.operations.resize(1); |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame^] | 42 | model.operations[0].type = V1_0::OperationType::CONV_2D; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 43 | model.operations[0].inputs = hidl_vec<uint32_t>{0, 1, 2, 3, 4, 5, 6}; |
| 44 | model.operations[0].outputs = hidl_vec<uint32_t>{7}; |
| 45 | |
| 46 | // make the prepared model |
| 47 | android::sp<IPreparedModel> preparedModel = PrepareModel(model, *driver); |
| 48 | |
| 49 | // construct the request |
| 50 | DataLocation inloc = {}; |
| 51 | inloc.poolIndex = 0; |
| 52 | inloc.offset = 0; |
| 53 | inloc.length = 6 * sizeof(float); |
| 54 | RequestArgument input = {}; |
| 55 | input.location = inloc; |
| 56 | input.dimensions = hidl_vec<uint32_t>{}; |
| 57 | |
| 58 | DataLocation outloc = {}; |
| 59 | outloc.poolIndex = 1; |
| 60 | outloc.offset = 0; |
| 61 | outloc.length = outSize * sizeof(float); |
| 62 | RequestArgument output = {}; |
| 63 | output.location = outloc; |
| 64 | output.dimensions = hidl_vec<uint32_t>{}; |
| 65 | |
| 66 | Request request = {}; |
| 67 | request.inputs = hidl_vec<RequestArgument>{input}; |
| 68 | request.outputs = hidl_vec<RequestArgument>{output}; |
| 69 | |
| 70 | |
| 71 | // set the input data (matching source test) |
| 72 | float indata[] = {4, 1, 0, 3, -1, 2}; |
| 73 | AddPoolAndSetData(6, request, indata); |
| 74 | |
| 75 | // add memory for the output |
| 76 | android::sp<IMemory> outMemory = AddPoolAndGetData(outSize, request); |
| 77 | float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer())); |
| 78 | |
| 79 | // run the execution |
| 80 | Execute(preparedModel, request); |
| 81 | |
| 82 | // check the result |
| 83 | if (paddingScheme == android::nn::kPaddingValid) |
| 84 | { |
| 85 | BOOST_TEST(outdata[0] == 2); |
| 86 | } |
| 87 | else if (paddingScheme == android::nn::kPaddingSame) |
| 88 | { |
| 89 | BOOST_TEST(outdata[0] == 2); |
| 90 | BOOST_TEST(outdata[1] == 0); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | BOOST_TEST(false); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | } // namespace <anonymous> |
| 99 | |
| 100 | BOOST_AUTO_TEST_CASE(ConvValidPadding) |
| 101 | { |
| 102 | PaddingTestImpl(android::nn::kPaddingValid); |
| 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(ConvSamePadding) |
| 106 | { |
| 107 | PaddingTestImpl(android::nn::kPaddingSame); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_SUITE_END() |