From 50db26cbd83e7753c1581cc8c29b8e575da66ade Mon Sep 17 00:00:00 2001 From: saoste01 Date: Wed, 24 Oct 2018 12:33:42 +0100 Subject: IVGCVSW-2062: Add unit test for TRANSPOSE Change-Id: I12e462c9a0a3613f0158f73929c566f53d1b3c76 --- test/1.1/Transpose.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ test/Android.mk | 1 + 2 files changed, 116 insertions(+) create mode 100644 test/1.1/Transpose.cpp diff --git a/test/1.1/Transpose.cpp b/test/1.1/Transpose.cpp new file mode 100644 index 00000000..a0521436 --- /dev/null +++ b/test/1.1/Transpose.cpp @@ -0,0 +1,115 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// +#include "../DriverTestHelpers.hpp" +#include +#include +#include +#include "../TestTensor.hpp" +#include "OperationsUtils.h" +#include + +#include + +BOOST_AUTO_TEST_SUITE(TransposeTests) + +using namespace android::hardware; +using namespace driverTestHelpers; +using namespace armnn_driver; + +namespace +{ + +static const boost::array COMPUTE_DEVICES = {{ armnn::Compute::CpuRef, armnn::Compute::GpuAcc }}; + +void TransposeTestImpl(const TestTensor & inputs, int32_t perm[], + const TestTensor & expectedOutputTensor, armnn::Compute computeDevice) +{ + auto driver = std::make_unique(DriverOptions(computeDevice)); + V1_1::Model model = {}; + + AddInputOperand(model,inputs.GetDimensions()); + AddTensorOperand(model, hidl_vec{4}, perm, OperandType::TENSOR_INT32); + AddOutputOperand(model, expectedOutputTensor.GetDimensions()); + + model.operations.resize(1); + model.operations[0].type = V1_1::OperationType::TRANSPOSE; + model.operations[0].inputs = hidl_vec{0, 1}; + model.operations[0].outputs = hidl_vec{2}; + + android::sp preparedModel = PrepareModel(model, *driver); + + // the request's memory pools will follow the same order as + // the inputs + DataLocation inloc = {}; + inloc.poolIndex = 0; + inloc.offset = 0; + inloc.length = inputs.GetNumElements() * sizeof(float); + RequestArgument input = {}; + input.location = inloc; + input.dimensions = inputs.GetDimensions(); + + // and an additional memory pool is needed for the output + DataLocation outloc = {}; + outloc.poolIndex = 1; + outloc.offset = 0; + outloc.length = expectedOutputTensor.GetNumElements() * sizeof(float); + RequestArgument output = {}; + output.location = outloc; + output.dimensions = expectedOutputTensor.GetDimensions(); + + // make the request based on the arguments + Request request = {}; + request.inputs = hidl_vec{input}; + request.outputs = hidl_vec{output}; + + // set the input data + AddPoolAndSetData(inputs.GetNumElements(), + request, + inputs.GetData()); + + // add memory for the output + android::sp outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request); + float* outdata = static_cast(static_cast(outMemory->getPointer())); + + auto execStatus = Execute(preparedModel, request); + + const float * expectedOutput = expectedOutputTensor.GetData(); + for (unsigned int i = 0; i < expectedOutputTensor.GetNumElements(); ++i) + { + BOOST_TEST(outdata[i] == expectedOutput[i]); + } + +} +} // namespace + +BOOST_DATA_TEST_CASE(Transpose , COMPUTE_DEVICES) +{ + int32_t perm[] = {3, 2, 0, 1}; + TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}}; + TestTensor expected{armnn::TensorShape{2, 2, 2, 1},{1, 5, 2, 6, 3, 7, 4, 8}}; + + TransposeTestImpl(input, perm, expected, sample); +} + +BOOST_DATA_TEST_CASE(TransposeNHWCToArmNN , COMPUTE_DEVICES) +{ + int32_t perm[] = {0, 2, 3, 1}; + TestTensor input{armnn::TensorShape{1, 2, 2, 3},{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}; + TestTensor expected{armnn::TensorShape{1, 3, 2, 2},{1, 11, 21, 31, 2, 12, 22, 32, 3, 13, 23, 33}}; + + TransposeTestImpl(input, perm, expected, sample); +} + +BOOST_DATA_TEST_CASE(TransposeArmNNToNHWC , COMPUTE_DEVICES) +{ + int32_t perm[] = {0, 3, 1, 2}; + TestTensor input{armnn::TensorShape{1, 2, 2, 2},{1, 2, 3, 4, 5, 6, 7, 8}}; + TestTensor expected{armnn::TensorShape{1, 2, 2, 2},{1, 5, 2, 6, 3, 7, 4, 8}}; + + TransposeTestImpl(input, perm, expected, sample); +} + +BOOST_AUTO_TEST_SUITE_END() + diff --git a/test/Android.mk b/test/Android.mk index 0e9824fd..3a58618b 100644 --- a/test/Android.mk +++ b/test/Android.mk @@ -121,6 +121,7 @@ LOCAL_CFLAGS := \ LOCAL_SRC_FILES := \ 1.0/Convolution2D.cpp \ 1.1/Convolution2D.cpp \ + 1.1/Transpose.cpp \ Tests.cpp \ UtilsTests.cpp \ Concurrent.cpp \ -- cgit v1.2.1