blob: f2c77b3fa80bf1f50b54e6349e455e789bc7ad67 [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
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
89 android::sp<IMemory> outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request);
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 }
saoste0150db26c2018-10-24 12:33:42 +010099}
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100100
saoste0150db26c2018-10-24 12:33:42 +0100101} // namespace
102
103BOOST_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
112BOOST_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
121BOOST_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
130BOOST_AUTO_TEST_SUITE_END()
131