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