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 | // |
| 5 | #include "../DriverTestHelpers.hpp" |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | #include <boost/array.hpp> |
| 8 | #include <log/log.h> |
| 9 | #include "../TestTensor.hpp" |
| 10 | #include "OperationsUtils.h" |
| 11 | #include <boost/test/data/test_case.hpp> |
| 12 | |
| 13 | #include <cmath> |
| 14 | |
| 15 | BOOST_AUTO_TEST_SUITE(TransposeTests) |
| 16 | |
| 17 | using namespace android::hardware; |
| 18 | using namespace driverTestHelpers; |
| 19 | using namespace armnn_driver; |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | static const boost::array<armnn::Compute, 2> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef, armnn::Compute::GpuAcc }}; |
| 25 | |
| 26 | void TransposeTestImpl(const TestTensor & inputs, int32_t perm[], |
| 27 | const TestTensor & expectedOutputTensor, armnn::Compute computeDevice) |
| 28 | { |
| 29 | auto driver = std::make_unique<ArmnnDriver>(DriverOptions(computeDevice)); |
| 30 | V1_1::Model model = {}; |
| 31 | |
| 32 | AddInputOperand(model,inputs.GetDimensions()); |
| 33 | AddTensorOperand(model, hidl_vec<uint32_t>{4}, perm, OperandType::TENSOR_INT32); |
| 34 | AddOutputOperand(model, expectedOutputTensor.GetDimensions()); |
| 35 | |
| 36 | model.operations.resize(1); |
| 37 | model.operations[0].type = V1_1::OperationType::TRANSPOSE; |
| 38 | model.operations[0].inputs = hidl_vec<uint32_t>{0, 1}; |
| 39 | model.operations[0].outputs = hidl_vec<uint32_t>{2}; |
| 40 | |
| 41 | android::sp<IPreparedModel> preparedModel = PrepareModel(model, *driver); |
| 42 | |
| 43 | // the request's memory pools will follow the same order as |
| 44 | // the inputs |
| 45 | DataLocation inloc = {}; |
| 46 | inloc.poolIndex = 0; |
| 47 | inloc.offset = 0; |
| 48 | inloc.length = inputs.GetNumElements() * sizeof(float); |
| 49 | RequestArgument input = {}; |
| 50 | input.location = inloc; |
| 51 | input.dimensions = inputs.GetDimensions(); |
| 52 | |
| 53 | // and an additional memory pool is needed for the output |
| 54 | DataLocation outloc = {}; |
| 55 | outloc.poolIndex = 1; |
| 56 | outloc.offset = 0; |
| 57 | outloc.length = expectedOutputTensor.GetNumElements() * sizeof(float); |
| 58 | RequestArgument output = {}; |
| 59 | output.location = outloc; |
| 60 | output.dimensions = expectedOutputTensor.GetDimensions(); |
| 61 | |
| 62 | // make the request based on the arguments |
| 63 | Request request = {}; |
| 64 | request.inputs = hidl_vec<RequestArgument>{input}; |
| 65 | request.outputs = hidl_vec<RequestArgument>{output}; |
| 66 | |
| 67 | // set the input data |
| 68 | AddPoolAndSetData(inputs.GetNumElements(), |
| 69 | request, |
| 70 | inputs.GetData()); |
| 71 | |
| 72 | // add memory for the output |
| 73 | android::sp<IMemory> outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request); |
| 74 | float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer())); |
| 75 | |
| 76 | auto execStatus = Execute(preparedModel, request); |
| 77 | |
| 78 | const float * expectedOutput = expectedOutputTensor.GetData(); |
| 79 | for (unsigned int i = 0; i < expectedOutputTensor.GetNumElements(); ++i) |
| 80 | { |
| 81 | BOOST_TEST(outdata[i] == expectedOutput[i]); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | } // namespace |
| 86 | |
| 87 | BOOST_DATA_TEST_CASE(Transpose , COMPUTE_DEVICES) |
| 88 | { |
| 89 | int32_t perm[] = {3, 2, 0, 1}; |
| 90 | TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}}; |
| 91 | TestTensor expected{armnn::TensorShape{2, 2, 2, 1},{1, 5, 2, 6, 3, 7, 4, 8}}; |
| 92 | |
| 93 | TransposeTestImpl(input, perm, expected, sample); |
| 94 | } |
| 95 | |
| 96 | BOOST_DATA_TEST_CASE(TransposeNHWCToArmNN , COMPUTE_DEVICES) |
| 97 | { |
| 98 | int32_t perm[] = {0, 2, 3, 1}; |
| 99 | TestTensor input{armnn::TensorShape{1, 2, 2, 3},{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}; |
| 100 | TestTensor expected{armnn::TensorShape{1, 3, 2, 2},{1, 11, 21, 31, 2, 12, 22, 32, 3, 13, 23, 33}}; |
| 101 | |
| 102 | TransposeTestImpl(input, perm, expected, sample); |
| 103 | } |
| 104 | |
| 105 | BOOST_DATA_TEST_CASE(TransposeArmNNToNHWC , COMPUTE_DEVICES) |
| 106 | { |
| 107 | int32_t perm[] = {0, 3, 1, 2}; |
| 108 | TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}}; |
| 109 | TestTensor expected{armnn::TensorShape{1, 2, 2, 2},{1, 5, 2, 6, 3, 7, 4, 8}}; |
| 110 | |
| 111 | TransposeTestImpl(input, perm, expected, sample); |
| 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_SUITE_END() |
| 115 | |