blob: 7a36130acb75decfc42bd1f254eafe5e209b724d [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
12#include <boost/array.hpp>
13#include <boost/test/unit_test.hpp>
saoste0150db26c2018-10-24 12:33:42 +010014#include <boost/test/data/test_case.hpp>
15
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010016#include <log/log.h>
17
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;
27
saoste0150db26c2018-10-24 12:33:42 +010028namespace
29{
30
Kevin Mayedc5ffa2019-05-22 12:02:53 +010031#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
saoste0150db26c2018-10-24 12:33:42 +010036
37void 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-Tar44cfd842019-06-14 15:45:03 +010041 HalPolicy::Model model = {};
saoste0150db26c2018-10-24 12:33:42 +010042
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010043 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());
saoste0150db26c2018-10-24 12:33:42 +010051
52 model.operations.resize(1);
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010053 model.operations[0].type = HalPolicy::OperationType::TRANSPOSE;
saoste0150db26c2018-10-24 12:33:42 +010054 model.operations[0].inputs = hidl_vec<uint32_t>{0, 1};
55 model.operations[0].outputs = hidl_vec<uint32_t>{2};
56
Sadik Armagand6539c52019-05-22 18:00:30 +010057 android::sp<V1_0::IPreparedModel> preparedModel = PrepareModel(model, *driver);
saoste0150db26c2018-10-24 12:33:42 +010058
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 Mayec1e5b82020-02-26 17:00:39 +000079 V1_0::Request request = {};
saoste0150db26c2018-10-24 12:33:42 +010080 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-Thompson976ad3e2019-08-21 15:21:14 +010089 android::sp<IMemory> outMemory = AddPoolAndGetData<float>(expectedOutputTensor.GetNumElements(), request);
saoste0150db26c2018-10-24 12:33:42 +010090 float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer()));
91
Sadik Armagand4636872020-04-27 10:15:41 +010092 if (preparedModel.get() != nullptr)
93 {
94 auto execStatus = Execute(preparedModel, request);
95 }
saoste0150db26c2018-10-24 12:33:42 +010096
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 }
saoste0150db26c2018-10-24 12:33:42 +0100102}
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100103
saoste0150db26c2018-10-24 12:33:42 +0100104} // namespace
105
106BOOST_DATA_TEST_CASE(Transpose , COMPUTE_DEVICES)
107{
James Conroy1bde8e32020-01-22 16:40:57 +0000108 int32_t perm[] = {2, 3, 1, 0};
saoste0150db26c2018-10-24 12:33:42 +0100109 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
115BOOST_DATA_TEST_CASE(TransposeNHWCToArmNN , COMPUTE_DEVICES)
116{
James Conroy1bde8e32020-01-22 16:40:57 +0000117 int32_t perm[] = {0, 3, 1, 2};
saoste0150db26c2018-10-24 12:33:42 +0100118 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
124BOOST_DATA_TEST_CASE(TransposeArmNNToNHWC , COMPUTE_DEVICES)
125{
James Conroy1bde8e32020-01-22 16:40:57 +0000126 int32_t perm[] = {0, 2, 3, 1};
saoste0150db26c2018-10-24 12:33:42 +0100127 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
133BOOST_AUTO_TEST_SUITE_END()
134