blob: 206f9b98df65830f6736db9c258ae7139ffeaa39 [file] [log] [blame]
saoste0150db26c2018-10-24 12:33:42 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
saoste0150db26c2018-10-24 12:33:42 +01005#include "OperationsUtils.h"
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +01006
7#include "../DriverTestHelpers.hpp"
8#include "../TestTensor.hpp"
9
10#include "../1.1/HalPolicy.hpp"
11
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010012#include <boost/test/unit_test.hpp>
saoste0150db26c2018-10-24 12:33:42 +010013#include <boost/test/data/test_case.hpp>
14
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010015#include <log/log.h>
16
Colm Doneland7fdbe22020-10-30 16:57:43 +000017#include <array>
saoste0150db26c2018-10-24 12:33:42 +010018#include <cmath>
19
20BOOST_AUTO_TEST_SUITE(TransposeTests)
21
22using namespace android::hardware;
23using namespace driverTestHelpers;
24using namespace armnn_driver;
25
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010026using HalPolicy = hal_1_1::HalPolicy;
Sadik Armagan188675f2021-02-12 17:16:42 +000027using RequestArgument = V1_0::RequestArgument;
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010028
saoste0150db26c2018-10-24 12:33:42 +010029namespace
30{
31
Kevin Mayedc5ffa2019-05-22 12:02:53 +010032#ifndef ARMCOMPUTECL_ENABLED
Colm Doneland7fdbe22020-10-30 16:57:43 +000033 static const std::array<armnn::Compute, 1> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef }};
Kevin Mayedc5ffa2019-05-22 12:02:53 +010034#else
Colm Doneland7fdbe22020-10-30 16:57:43 +000035 static const std::array<armnn::Compute, 2> COMPUTE_DEVICES = {{ armnn::Compute::CpuRef, armnn::Compute::GpuAcc }};
Kevin Mayedc5ffa2019-05-22 12:02:53 +010036#endif
saoste0150db26c2018-10-24 12:33:42 +010037
38void TransposeTestImpl(const TestTensor & inputs, int32_t perm[],
39 const TestTensor & expectedOutputTensor, armnn::Compute computeDevice)
40{
41 auto driver = std::make_unique<ArmnnDriver>(DriverOptions(computeDevice));
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010042 HalPolicy::Model model = {};
saoste0150db26c2018-10-24 12:33:42 +010043
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010044 AddInputOperand<HalPolicy>(model,inputs.GetDimensions());
45
46 AddTensorOperand<HalPolicy>(model,
47 hidl_vec<uint32_t>{4},
48 perm,
49 HalPolicy::OperandType::TENSOR_INT32);
50
51 AddOutputOperand<HalPolicy>(model, expectedOutputTensor.GetDimensions());
saoste0150db26c2018-10-24 12:33:42 +010052
53 model.operations.resize(1);
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010054 model.operations[0].type = HalPolicy::OperationType::TRANSPOSE;
saoste0150db26c2018-10-24 12:33:42 +010055 model.operations[0].inputs = hidl_vec<uint32_t>{0, 1};
56 model.operations[0].outputs = hidl_vec<uint32_t>{2};
57
Sadik Armagand6539c52019-05-22 18:00:30 +010058 android::sp<V1_0::IPreparedModel> preparedModel = PrepareModel(model, *driver);
saoste0150db26c2018-10-24 12:33:42 +010059
60 // the request's memory pools will follow the same order as
61 // the inputs
Sadik Armagan188675f2021-02-12 17:16:42 +000062 V1_0::DataLocation inloc = {};
63 inloc.poolIndex = 0;
64 inloc.offset = 0;
65 inloc.length = inputs.GetNumElements() * sizeof(float);
66 RequestArgument input = {};
67 input.location = inloc;
68 input.dimensions = inputs.GetDimensions();
saoste0150db26c2018-10-24 12:33:42 +010069
70 // and an additional memory pool is needed for the output
Sadik Armagan188675f2021-02-12 17:16:42 +000071 V1_0::DataLocation outloc = {};
72 outloc.poolIndex = 1;
73 outloc.offset = 0;
74 outloc.length = expectedOutputTensor.GetNumElements() * sizeof(float);
75 RequestArgument output = {};
76 output.location = outloc;
77 output.dimensions = expectedOutputTensor.GetDimensions();
saoste0150db26c2018-10-24 12:33:42 +010078
79 // make the request based on the arguments
Kevin Mayec1e5b82020-02-26 17:00:39 +000080 V1_0::Request request = {};
saoste0150db26c2018-10-24 12:33:42 +010081 request.inputs = hidl_vec<RequestArgument>{input};
82 request.outputs = hidl_vec<RequestArgument>{output};
83
84 // set the input data
85 AddPoolAndSetData(inputs.GetNumElements(),
86 request,
87 inputs.GetData());
88
89 // add memory for the output
Ellen Norris-Thompson976ad3e2019-08-21 15:21:14 +010090 android::sp<IMemory> outMemory = AddPoolAndGetData<float>(expectedOutputTensor.GetNumElements(), request);
saoste0150db26c2018-10-24 12:33:42 +010091 float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer()));
92
Sadik Armagand4636872020-04-27 10:15:41 +010093 if (preparedModel.get() != nullptr)
94 {
95 auto execStatus = Execute(preparedModel, request);
96 }
saoste0150db26c2018-10-24 12:33:42 +010097
98 const float * expectedOutput = expectedOutputTensor.GetData();
99 for (unsigned int i = 0; i < expectedOutputTensor.GetNumElements(); ++i)
100 {
101 BOOST_TEST(outdata[i] == expectedOutput[i]);
102 }
saoste0150db26c2018-10-24 12:33:42 +0100103}
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100104
saoste0150db26c2018-10-24 12:33:42 +0100105} // namespace
106
107BOOST_DATA_TEST_CASE(Transpose , COMPUTE_DEVICES)
108{
James Conroy1bde8e32020-01-22 16:40:57 +0000109 int32_t perm[] = {2, 3, 1, 0};
saoste0150db26c2018-10-24 12:33:42 +0100110 TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}};
111 TestTensor expected{armnn::TensorShape{2, 2, 2, 1},{1, 5, 2, 6, 3, 7, 4, 8}};
112
113 TransposeTestImpl(input, perm, expected, sample);
114}
115
116BOOST_DATA_TEST_CASE(TransposeNHWCToArmNN , COMPUTE_DEVICES)
117{
James Conroy1bde8e32020-01-22 16:40:57 +0000118 int32_t perm[] = {0, 3, 1, 2};
saoste0150db26c2018-10-24 12:33:42 +0100119 TestTensor input{armnn::TensorShape{1, 2, 2, 3},{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}};
120 TestTensor expected{armnn::TensorShape{1, 3, 2, 2},{1, 11, 21, 31, 2, 12, 22, 32, 3, 13, 23, 33}};
121
122 TransposeTestImpl(input, perm, expected, sample);
123}
124
125BOOST_DATA_TEST_CASE(TransposeArmNNToNHWC , COMPUTE_DEVICES)
126{
James Conroy1bde8e32020-01-22 16:40:57 +0000127 int32_t perm[] = {0, 2, 3, 1};
saoste0150db26c2018-10-24 12:33:42 +0100128 TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}};
129 TestTensor expected{armnn::TensorShape{1, 2, 2, 2},{1, 5, 2, 6, 3, 7, 4, 8}};
130
131 TransposeTestImpl(input, perm, expected, sample);
132}
133
134BOOST_AUTO_TEST_SUITE_END()
135