josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 1 | // |
Teresa Charlin | fbf0e5b | 2020-08-17 01:01:06 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 8 | #include <armnnTestUtils/LayerTestResult.hpp> |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 9 | |
| 10 | #include <armnn/ArmNN.hpp> |
| 11 | |
| 12 | #include <ResolveType.hpp> |
| 13 | |
| 14 | #include <armnn/backends/IBackendInternal.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 15 | #include <armnn/backends/Workload.hpp> |
| 16 | #include <armnn/backends/WorkloadData.hpp> |
| 17 | #include <armnn/backends/WorkloadFactory.hpp> |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 18 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 19 | #include <DataTypeUtils.hpp> |
| 20 | #include <armnnTestUtils/TensorCopyUtils.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 21 | #include <armnnTestUtils/WorkloadTestUtils.hpp> |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 22 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 23 | #include <TensorHelpers.hpp> |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 24 | |
| 25 | #include <memory> |
| 26 | |
| 27 | std::unique_ptr<armnn::IWorkload> CreateWorkload( |
| 28 | const armnn::IWorkloadFactory& workloadFactory, |
| 29 | const armnn::WorkloadInfo& info, |
| 30 | const armnn::ElementwiseUnaryQueueDescriptor& descriptor); |
| 31 | |
| 32 | template <std::size_t NumDims, |
| 33 | armnn::DataType ArmnnType, |
| 34 | typename T = armnn::ResolveType<ArmnnType>> |
| 35 | LayerTestResult<T, NumDims> ElementwiseUnaryTestHelper( |
| 36 | armnn::IWorkloadFactory & workloadFactory, |
| 37 | const armnn::IBackendInternal::IMemoryManagerSharedPtr & memoryManager, |
| 38 | armnn::UnaryOperation op, |
| 39 | const unsigned int shape[NumDims], |
| 40 | std::vector<float> values, |
| 41 | float quantScale, |
| 42 | int quantOffset, |
| 43 | const unsigned int outShape[NumDims], |
| 44 | std::vector<float> outValues, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 45 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 46 | float outQuantScale, |
| 47 | int outQuantOffset) |
| 48 | { |
| 49 | armnn::TensorInfo inputTensorInfo{NumDims, shape, ArmnnType}; |
| 50 | armnn::TensorInfo outputTensorInfo{NumDims, outShape, ArmnnType}; |
| 51 | |
| 52 | inputTensorInfo.SetQuantizationScale(quantScale); |
| 53 | inputTensorInfo.SetQuantizationOffset(quantOffset); |
| 54 | |
| 55 | outputTensorInfo.SetQuantizationScale(outQuantScale); |
| 56 | outputTensorInfo.SetQuantizationOffset(outQuantOffset); |
| 57 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 58 | std::vector<T> input = ConvertToDataType<ArmnnType>(values, inputTensorInfo); |
| 59 | std::vector<T> expectedOutput = ConvertToDataType<ArmnnType>(outValues, inputTensorInfo); |
| 60 | std::vector<T> actualOutput(outputTensorInfo.GetNumElements()); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 61 | |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 62 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 63 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 64 | |
| 65 | armnn::ElementwiseUnaryDescriptor desc(op); |
| 66 | armnn::ElementwiseUnaryQueueDescriptor qDesc; |
| 67 | qDesc.m_Parameters = desc; |
| 68 | armnn::WorkloadInfo info; |
| 69 | AddInputToWorkload(qDesc, info, inputTensorInfo, inputHandle.get()); |
| 70 | AddOutputToWorkload(qDesc, info, outputTensorInfo, outputHandle.get()); |
| 71 | auto workload = CreateWorkload(workloadFactory, info, qDesc); |
| 72 | |
| 73 | inputHandle->Allocate(); |
| 74 | outputHandle->Allocate(); |
| 75 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 76 | CopyDataToITensorHandle(inputHandle.get(), input.data()); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 77 | |
| 78 | workload->PostAllocationConfigure(); |
| 79 | ExecuteWorkload(*workload, memoryManager); |
| 80 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 81 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 82 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 83 | return LayerTestResult<T, NumDims>(actualOutput, |
| 84 | expectedOutput, |
| 85 | outputHandle->GetShape(), |
| 86 | outputTensorInfo.GetShape()); |
| 87 | |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | template <std::size_t NumDims, |
| 91 | armnn::DataType ArmnnType, |
| 92 | typename T = armnn::ResolveType<ArmnnType>> |
| 93 | LayerTestResult<T, NumDims> ElementwiseUnaryTestHelper( |
| 94 | armnn::IWorkloadFactory & workloadFactory, |
| 95 | const armnn::IBackendInternal::IMemoryManagerSharedPtr & memoryManager, |
| 96 | armnn::UnaryOperation op, |
| 97 | const unsigned int shape[NumDims], |
| 98 | std::vector<float> values, |
| 99 | const unsigned int outShape[NumDims], |
| 100 | std::vector<float> outValues, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 101 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 102 | float quantScale = 1.0f, |
| 103 | int quantOffset = 0) |
| 104 | { |
| 105 | return ElementwiseUnaryTestHelper<NumDims, ArmnnType>( |
| 106 | workloadFactory, |
| 107 | memoryManager, |
| 108 | op, |
| 109 | shape, |
| 110 | values, |
| 111 | quantScale, |
| 112 | quantOffset, |
| 113 | outShape, |
| 114 | outValues, |
Keith Davis | 33a626f | 2020-08-27 15:38:12 +0100 | [diff] [blame] | 115 | tensorHandleFactory, |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 116 | quantScale, |
| 117 | quantOffset); |
| 118 | } |