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 | |
| 12 | #include <boost/array.hpp> |
| 13 | #include <boost/test/unit_test.hpp> |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 14 | #include <boost/test/data/test_case.hpp> |
| 15 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 16 | #include <log/log.h> |
| 17 | |
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 |
| 32 | static const boost::array<armnn::Compute, 1> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef }}; |
| 33 | #else |
| 34 | static const boost::array<armnn::Compute, 2> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef, armnn::Compute::GpuAcc }}; |
| 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 |
| 79 | Request request = {}; |
| 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 | |
| 92 | auto execStatus = Execute(preparedModel, request); |
| 93 | |
| 94 | const float * expectedOutput = expectedOutputTensor.GetData(); |
| 95 | for (unsigned int i = 0; i < expectedOutputTensor.GetNumElements(); ++i) |
| 96 | { |
| 97 | BOOST_TEST(outdata[i] == expectedOutput[i]); |
| 98 | } |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 99 | } |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 100 | |
saoste01 | 50db26c | 2018-10-24 12:33:42 +0100 | [diff] [blame] | 101 | } // namespace |
| 102 | |
| 103 | BOOST_DATA_TEST_CASE(Transpose , COMPUTE_DEVICES) |
| 104 | { |
| 105 | int32_t perm[] = {3, 2, 0, 1}; |
| 106 | TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}}; |
| 107 | TestTensor expected{armnn::TensorShape{2, 2, 2, 1},{1, 5, 2, 6, 3, 7, 4, 8}}; |
| 108 | |
| 109 | TransposeTestImpl(input, perm, expected, sample); |
| 110 | } |
| 111 | |
| 112 | BOOST_DATA_TEST_CASE(TransposeNHWCToArmNN , COMPUTE_DEVICES) |
| 113 | { |
| 114 | int32_t perm[] = {0, 2, 3, 1}; |
| 115 | TestTensor input{armnn::TensorShape{1, 2, 2, 3},{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}; |
| 116 | TestTensor expected{armnn::TensorShape{1, 3, 2, 2},{1, 11, 21, 31, 2, 12, 22, 32, 3, 13, 23, 33}}; |
| 117 | |
| 118 | TransposeTestImpl(input, perm, expected, sample); |
| 119 | } |
| 120 | |
| 121 | BOOST_DATA_TEST_CASE(TransposeArmNNToNHWC , COMPUTE_DEVICES) |
| 122 | { |
| 123 | int32_t perm[] = {0, 3, 1, 2}; |
| 124 | TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}}; |
| 125 | TestTensor expected{armnn::TensorShape{1, 2, 2, 2},{1, 5, 2, 6, 3, 7, 4, 8}}; |
| 126 | |
| 127 | TransposeTestImpl(input, perm, expected, sample); |
| 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_SUITE_END() |
| 131 | |