saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 5 | #include "OperationsUtils.h" |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 6 | |
| 7 | #include "../DriverTestHelpers.hpp" |
| 8 | #include "../TestTensor.hpp" |
| 9 | |
| 10 | #include "../1.1/HalPolicy.hpp" |
| 11 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 12 | #include <boost/test/unit_test.hpp> |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 13 | #include <boost/test/data/test_case.hpp> |
| 14 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 15 | #include <log/log.h> |
| 16 | |
Colm Donelan | d7fdbe2 | 2020-10-30 16:57:43 +0000 | [diff] [blame] | 17 | #include <array> |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 18 | #include <cmath> |
| 19 | |
| 20 | BOOST_AUTO_TEST_SUITE(TransposeTests) |
| 21 | |
| 22 | using namespace android::hardware; |
| 23 | using namespace driverTestHelpers; |
| 24 | using namespace armnn_driver; |
| 25 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 26 | using HalPolicy = hal_1_1::HalPolicy; |
| 27 | |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 28 | namespace |
| 29 | { |
| 30 | |
Kevin May | edc5ffa | 2019-05-22 12:02:53 +0100 | [diff] [blame] | 31 | #ifndef ARMCOMPUTECL_ENABLED |
Colm Donelan | d7fdbe2 | 2020-10-30 16:57:43 +0000 | [diff] [blame] | 32 | static const std::array<armnn::Compute, 1> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef }}; |
Kevin May | edc5ffa | 2019-05-22 12:02:53 +0100 | [diff] [blame] | 33 | #else |
Colm Donelan | d7fdbe2 | 2020-10-30 16:57:43 +0000 | [diff] [blame] | 34 | static const std::array<armnn::Compute, 2> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef, armnn::Compute::GpuAcc }}; |
Kevin May | edc5ffa | 2019-05-22 12:02:53 +0100 | [diff] [blame] | 35 | #endif |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 36 | |
| 37 | void TransposeTestImpl(const TestTensor & inputs, int32_t perm[], |
| 38 | const TestTensor & expectedOutputTensor, armnn::Compute computeDevice) |
| 39 | { |
| 40 | auto driver = std::make_unique<ArmnnDriver>(DriverOptions(computeDevice)); |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 41 | HalPolicy::Model model = {}; |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 42 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 43 | AddInputOperand<HalPolicy>(model,inputs.GetDimensions()); |
| 44 | |
| 45 | AddTensorOperand<HalPolicy>(model, |
| 46 | hidl_vec<uint32_t>{4}, |
| 47 | perm, |
| 48 | HalPolicy::OperandType::TENSOR_INT32); |
| 49 | |
| 50 | AddOutputOperand<HalPolicy>(model, expectedOutputTensor.GetDimensions()); |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 51 | |
| 52 | model.operations.resize(1); |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 53 | model.operations[0].type = HalPolicy::OperationType::TRANSPOSE; |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 54 | model.operations[0].inputs = hidl_vec<uint32_t>{0, 1}; |
| 55 | model.operations[0].outputs = hidl_vec<uint32_t>{2}; |
| 56 | |
Sadik Armagan | d6539c5 | 2019-05-22 18:00:30 +0100 | [diff] [blame] | 57 | android::sp<V1_0::IPreparedModel> preparedModel = PrepareModel(model, *driver); |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 58 | |
| 59 | // the request's memory pools will follow the same order as |
| 60 | // the inputs |
| 61 | DataLocation inloc = {}; |
| 62 | inloc.poolIndex = 0; |
| 63 | inloc.offset = 0; |
| 64 | inloc.length = inputs.GetNumElements() * sizeof(float); |
| 65 | RequestArgument input = {}; |
| 66 | input.location = inloc; |
| 67 | input.dimensions = inputs.GetDimensions(); |
| 68 | |
| 69 | // and an additional memory pool is needed for the output |
| 70 | DataLocation outloc = {}; |
| 71 | outloc.poolIndex = 1; |
| 72 | outloc.offset = 0; |
| 73 | outloc.length = expectedOutputTensor.GetNumElements() * sizeof(float); |
| 74 | RequestArgument output = {}; |
| 75 | output.location = outloc; |
| 76 | output.dimensions = expectedOutputTensor.GetDimensions(); |
| 77 | |
| 78 | // make the request based on the arguments |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 79 | V1_0::Request request = {}; |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 80 | request.inputs = hidl_vec<RequestArgument>{input}; |
| 81 | request.outputs = hidl_vec<RequestArgument>{output}; |
| 82 | |
| 83 | // set the input data |
| 84 | AddPoolAndSetData(inputs.GetNumElements(), |
| 85 | request, |
| 86 | inputs.GetData()); |
| 87 | |
| 88 | // add memory for the output |
Ellen Norris-Thompson | 976ad3e | 2019-08-21 15:21:14 +0100 | [diff] [blame] | 89 | android::sp<IMemory> outMemory = AddPoolAndGetData<float>(expectedOutputTensor.GetNumElements(), request); |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 90 | float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer())); |
| 91 | |
Sadik Armagan | d463687 | 2020-04-27 10:15:41 +0100 | [diff] [blame] | 92 | if (preparedModel.get() != nullptr) |
| 93 | { |
| 94 | auto execStatus = Execute(preparedModel, request); |
| 95 | } |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 96 | |
| 97 | const float * expectedOutput = expectedOutputTensor.GetData(); |
| 98 | for (unsigned int i = 0; i < expectedOutputTensor.GetNumElements(); ++i) |
| 99 | { |
| 100 | BOOST_TEST(outdata[i] == expectedOutput[i]); |
| 101 | } |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 102 | } |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 103 | |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 104 | } // namespace |
| 105 | |
| 106 | BOOST_DATA_TEST_CASE(Transpose , COMPUTE_DEVICES) |
| 107 | { |
James Conroy | 1bde8e3 | 2020-01-22 16:40:57 +0000 | [diff] [blame] | 108 | int32_t perm[] = {2, 3, 1, 0}; |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 109 | TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}}; |
| 110 | TestTensor expected{armnn::TensorShape{2, 2, 2, 1},{1, 5, 2, 6, 3, 7, 4, 8}}; |
| 111 | |
| 112 | TransposeTestImpl(input, perm, expected, sample); |
| 113 | } |
| 114 | |
| 115 | BOOST_DATA_TEST_CASE(TransposeNHWCToArmNN , COMPUTE_DEVICES) |
| 116 | { |
James Conroy | 1bde8e3 | 2020-01-22 16:40:57 +0000 | [diff] [blame] | 117 | int32_t perm[] = {0, 3, 1, 2}; |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 118 | TestTensor input{armnn::TensorShape{1, 2, 2, 3},{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}; |
| 119 | TestTensor expected{armnn::TensorShape{1, 3, 2, 2},{1, 11, 21, 31, 2, 12, 22, 32, 3, 13, 23, 33}}; |
| 120 | |
| 121 | TransposeTestImpl(input, perm, expected, sample); |
| 122 | } |
| 123 | |
| 124 | BOOST_DATA_TEST_CASE(TransposeArmNNToNHWC , COMPUTE_DEVICES) |
| 125 | { |
James Conroy | 1bde8e3 | 2020-01-22 16:40:57 +0000 | [diff] [blame] | 126 | int32_t perm[] = {0, 2, 3, 1}; |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 127 | TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}}; |
| 128 | TestTensor expected{armnn::TensorShape{1, 2, 2, 2},{1, 5, 2, 6, 3, 7, 4, 8}}; |
| 129 | |
| 130 | TransposeTestImpl(input, perm, expected, sample); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() |
| 134 | |