Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "../DriverTestHelpers.hpp" |
| 7 | #include "../TestTensor.hpp" |
| 8 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 9 | #include "../1.1/HalPolicy.hpp" |
| 10 | |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 11 | #include <boost/array.hpp> |
| 12 | #include <boost/test/data/test_case.hpp> |
| 13 | |
| 14 | BOOST_AUTO_TEST_SUITE(MeanTests) |
| 15 | |
| 16 | using namespace android::hardware; |
| 17 | using namespace driverTestHelpers; |
| 18 | using namespace armnn_driver; |
| 19 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 20 | using HalPolicy = hal_1_1::HalPolicy; |
| 21 | |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 22 | namespace |
| 23 | { |
| 24 | |
Kevin May | edc5ffa | 2019-05-22 12:02:53 +0100 | [diff] [blame] | 25 | #ifndef ARMCOMPUTECL_ENABLED |
| 26 | static const boost::array<armnn::Compute, 1> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef }}; |
| 27 | #else |
| 28 | static const boost::array<armnn::Compute, 2> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef, armnn::Compute::GpuAcc }}; |
| 29 | #endif |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 30 | |
| 31 | void MeanTestImpl(const TestTensor& input, |
| 32 | const hidl_vec<uint32_t>& axisDimensions, |
| 33 | const int32_t* axisValues, |
| 34 | int32_t keepDims, |
| 35 | const TestTensor& expectedOutput, |
| 36 | bool fp16Enabled, |
| 37 | armnn::Compute computeDevice) |
| 38 | { |
| 39 | auto driver = std::make_unique<ArmnnDriver>(DriverOptions(computeDevice, fp16Enabled)); |
| 40 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 41 | HalPolicy::Model model = {}; |
| 42 | |
| 43 | AddInputOperand<HalPolicy>(model, input.GetDimensions()); |
| 44 | |
| 45 | AddTensorOperand<HalPolicy>(model, |
| 46 | axisDimensions, |
| 47 | const_cast<int32_t*>(axisValues), |
| 48 | HalPolicy::OperandType::TENSOR_INT32); |
| 49 | |
| 50 | AddIntOperand<HalPolicy>(model, keepDims); |
| 51 | |
| 52 | AddOutputOperand<HalPolicy>(model, expectedOutput.GetDimensions()); |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 53 | |
| 54 | model.operations.resize(1); |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 55 | model.operations[0].type = HalPolicy::OperationType::MEAN; |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 56 | model.operations[0].inputs = hidl_vec<uint32_t>{ 0, 1, 2 }; |
| 57 | model.operations[0].outputs = hidl_vec<uint32_t>{ 3 }; |
| 58 | model.relaxComputationFloat32toFloat16 = fp16Enabled; |
| 59 | |
Sadik Armagan | d6539c5 | 2019-05-22 18:00:30 +0100 | [diff] [blame] | 60 | android::sp<V1_0::IPreparedModel> preparedModel = PrepareModel(model, *driver); |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 61 | |
| 62 | // The request's memory pools will follow the same order as the inputs |
| 63 | DataLocation inLoc = {}; |
| 64 | inLoc.poolIndex = 0; |
| 65 | inLoc.offset = 0; |
| 66 | inLoc.length = input.GetNumElements() * sizeof(float); |
| 67 | RequestArgument inArg = {}; |
| 68 | inArg.location = inLoc; |
| 69 | inArg.dimensions = input.GetDimensions(); |
| 70 | |
| 71 | // An additional memory pool is needed for the output |
| 72 | DataLocation outLoc = {}; |
| 73 | outLoc.poolIndex = 1; |
| 74 | outLoc.offset = 0; |
| 75 | outLoc.length = expectedOutput.GetNumElements() * sizeof(float); |
| 76 | RequestArgument outArg = {}; |
| 77 | outArg.location = outLoc; |
| 78 | outArg.dimensions = expectedOutput.GetDimensions(); |
| 79 | |
| 80 | // Make the request based on the arguments |
| 81 | Request request = {}; |
| 82 | request.inputs = hidl_vec<RequestArgument>{ inArg }; |
| 83 | request.outputs = hidl_vec<RequestArgument>{ outArg }; |
| 84 | |
| 85 | // Set the input data |
| 86 | AddPoolAndSetData(input.GetNumElements(), request, input.GetData()); |
| 87 | |
| 88 | // Add memory for the output |
| 89 | android::sp<IMemory> outMemory = AddPoolAndGetData(expectedOutput.GetNumElements(), request); |
| 90 | const float* outputData = static_cast<const float*>(static_cast<void*>(outMemory->getPointer())); |
| 91 | |
| 92 | ErrorStatus execStatus = Execute(preparedModel, request); |
| 93 | BOOST_TEST(execStatus == ErrorStatus::NONE); |
| 94 | |
| 95 | const float* expectedOutputData = expectedOutput.GetData(); |
| 96 | for (unsigned int i = 0; i < expectedOutput.GetNumElements(); i++) |
| 97 | { |
| 98 | BOOST_TEST(outputData[i] == expectedOutputData[i]); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | } // anonymous namespace |
| 103 | |
| 104 | BOOST_DATA_TEST_CASE(MeanNoKeepDimsTest, COMPUTE_DEVICES) |
| 105 | { |
| 106 | TestTensor input{ armnn::TensorShape{ 4, 3, 2 }, { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
| 107 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, |
| 108 | 20.0f, 21.0f, 22.0f, 23.0f, 24.0f } }; |
| 109 | hidl_vec<uint32_t> axisDimensions = { 2 }; |
| 110 | int32_t axisValues[] = { 0, 1 }; |
| 111 | int32_t keepDims = 0; |
| 112 | TestTensor expectedOutput{ armnn::TensorShape{ 2 }, { 12.0f, 13.0f } }; |
| 113 | |
| 114 | MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, false, sample); |
| 115 | } |
| 116 | |
| 117 | BOOST_DATA_TEST_CASE(MeanKeepDimsTest, COMPUTE_DEVICES) |
| 118 | { |
| 119 | TestTensor input{ armnn::TensorShape{ 1, 1, 3, 2 }, { 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f } }; |
| 120 | hidl_vec<uint32_t> axisDimensions = { 1 }; |
| 121 | int32_t axisValues[] = { 2 }; |
| 122 | int32_t keepDims = 1; |
| 123 | TestTensor expectedOutput{ armnn::TensorShape{ 1, 1, 1, 2 }, { 2.0f, 2.0f } }; |
| 124 | |
| 125 | MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, false, sample); |
| 126 | } |
| 127 | |
| 128 | BOOST_DATA_TEST_CASE(MeanFp16NoKeepDimsTest, COMPUTE_DEVICES) |
| 129 | { |
| 130 | TestTensor input{ armnn::TensorShape{ 4, 3, 2 }, { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
| 131 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, |
| 132 | 20.0f, 21.0f, 22.0f, 23.0f, 24.0f } }; |
| 133 | hidl_vec<uint32_t> axisDimensions = { 2 }; |
| 134 | int32_t axisValues[] = { 0, 1 }; |
| 135 | int32_t keepDims = 0; |
| 136 | TestTensor expectedOutput{ armnn::TensorShape{ 2 }, { 12.0f, 13.0f } }; |
| 137 | |
| 138 | MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, true, sample); |
| 139 | } |
| 140 | |
| 141 | BOOST_DATA_TEST_CASE(MeanFp16KeepDimsTest, COMPUTE_DEVICES) |
| 142 | { |
| 143 | TestTensor input{ armnn::TensorShape{ 1, 1, 3, 2 }, { 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f } }; |
| 144 | hidl_vec<uint32_t> axisDimensions = { 1 }; |
| 145 | int32_t axisValues[] = { 2 }; |
| 146 | int32_t keepDims = 1; |
| 147 | TestTensor expectedOutput{ armnn::TensorShape{ 1, 1, 1, 2 }, { 2.0f, 2.0f } }; |
| 148 | |
| 149 | MeanTestImpl(input, axisDimensions, axisValues, keepDims, expectedOutput, true, sample); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_SUITE_END() |