• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnnTestUtils/LayerTestResult.hpp>
9 
10 #include <armnn/ArmNN.hpp>
11 
12 #include <ResolveType.hpp>
13 
14 #include <armnn/backends/IBackendInternal.hpp>
15 #include <armnn/backends/Workload.hpp>
16 #include <armnn/backends/WorkloadData.hpp>
17 #include <armnn/backends/WorkloadFactory.hpp>
18 
19 #include <DataTypeUtils.hpp>
20 #include <armnnTestUtils/TensorCopyUtils.hpp>
21 #include <armnnTestUtils/WorkloadTestUtils.hpp>
22 
23 #include <armnnTestUtils/TensorHelpers.hpp>
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>>
ElementwiseUnaryTestHelper(armnn::IWorkloadFactory & workloadFactory,const armnn::IBackendInternal::IMemoryManagerSharedPtr & memoryManager,armnn::UnaryOperation op,const unsigned int shape[NumDims],std::vector<float> values,float quantScale,int quantOffset,const unsigned int outShape[NumDims],std::vector<float> outValues,const armnn::ITensorHandleFactory & tensorHandleFactory,float outQuantScale,int outQuantOffset)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,
45     const armnn::ITensorHandleFactory& tensorHandleFactory,
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 
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());
61 
62     std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo);
63     std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo);
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 
76     CopyDataToITensorHandle(inputHandle.get(), input.data());
77 
78     workload->PostAllocationConfigure();
79     ExecuteWorkload(*workload, memoryManager);
80 
81     CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get());
82 
83     return LayerTestResult<T, NumDims>(actualOutput,
84                                        expectedOutput,
85                                        outputHandle->GetShape(),
86                                        outputTensorInfo.GetShape());
87 
88 }
89 
90 template <std::size_t NumDims,
91           armnn::DataType ArmnnType,
92           typename T = armnn::ResolveType<ArmnnType>>
ElementwiseUnaryTestHelper(armnn::IWorkloadFactory & workloadFactory,const armnn::IBackendInternal::IMemoryManagerSharedPtr & memoryManager,armnn::UnaryOperation op,const unsigned int shape[NumDims],std::vector<float> values,const unsigned int outShape[NumDims],std::vector<float> outValues,const armnn::ITensorHandleFactory & tensorHandleFactory,float quantScale=1.0f,int quantOffset=0)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,
101     const armnn::ITensorHandleFactory& tensorHandleFactory,
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,
115         tensorHandleFactory,
116         quantScale,
117         quantOffset);
118 }