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