Nattapat Chaimanowong | a0beb3b | 2019-04-01 17:04:53 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include "WorkloadTestUtils.hpp" |
| 8 | |
| 9 | #include <test/TensorHelpers.hpp> |
| 10 | |
| 11 | #include <armnn/ArmNN.hpp> |
| 12 | #include <armnn/Tensor.hpp> |
| 13 | #include <armnn/TypesUtils.hpp> |
| 14 | |
| 15 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 16 | #include <backendsCommon/IBackendInternal.hpp> |
| 17 | #include <backendsCommon/WorkloadFactory.hpp> |
| 18 | |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | template<typename T, std::size_t Dim> |
| 24 | LayerTestResult<T, Dim> QuantizeTestImpl( |
| 25 | armnn::IWorkloadFactory& workloadFactory, |
| 26 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 27 | const armnn::TensorInfo& inputTensorInfo, |
| 28 | const armnn::TensorInfo& outputTensorInfo, |
| 29 | const std::vector<float>& inputData, |
| 30 | const std::vector<T>& expectedOutputData, |
| 31 | armnn::QuantizeQueueDescriptor descriptor) |
| 32 | { |
| 33 | boost::multi_array<float, Dim> input = MakeTensor<float, Dim>(inputTensorInfo, inputData); |
| 34 | |
| 35 | LayerTestResult<T, Dim> ret(outputTensorInfo); |
| 36 | ret.outputExpected = MakeTensor<T, Dim>(outputTensorInfo, expectedOutputData); |
| 37 | |
| 38 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 39 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 40 | |
| 41 | armnn::WorkloadInfo info; |
| 42 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 43 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 44 | |
| 45 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateQuantize(descriptor, info); |
| 46 | |
| 47 | inputHandle->Allocate(); |
| 48 | outputHandle->Allocate(); |
| 49 | |
| 50 | CopyDataToITensorHandle(inputHandle.get(), input.data()); |
| 51 | |
| 52 | ExecuteWorkload(*workload, memoryManager); |
| 53 | |
| 54 | CopyDataFromITensorHandle(ret.output.data(), outputHandle.get()); |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | template <armnn::DataType ArmnnOutputType, typename T = armnn::ResolveType<ArmnnOutputType>> |
| 60 | LayerTestResult<T, 4> QuantizeSimpleTest( |
| 61 | armnn::IWorkloadFactory& workloadFactory, |
| 62 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 63 | { |
| 64 | armnn::QuantizeQueueDescriptor desc; |
| 65 | |
| 66 | const armnn::TensorInfo inputTensorInfo({1, 2, 2, 3}, armnn::DataType::Float32); |
| 67 | const armnn::TensorInfo outputTensorInfo({1, 2, 2, 3}, ArmnnOutputType, 0.5f, 1); |
| 68 | |
| 69 | std::vector<float> inputData = std::vector<float>( |
| 70 | { |
| 71 | 1.0f, 2.0f, 3.0f, |
| 72 | 4.0f, 5.0f, 6.0f, |
| 73 | 7.0f, 8.0f, 9.0f, |
| 74 | 10.0f, 11.0f, 12.0f, |
| 75 | }); |
| 76 | |
| 77 | std::vector<T> expectedOutputData = std::vector<T>( |
| 78 | { |
| 79 | 3, 5, 7, |
| 80 | 9, 11, 13, |
| 81 | 15, 17, 19, |
| 82 | 21, 23, 25, |
| 83 | }); |
| 84 | |
| 85 | return QuantizeTestImpl<T, 4>(workloadFactory, |
| 86 | memoryManager, |
| 87 | inputTensorInfo, |
| 88 | outputTensorInfo, |
| 89 | inputData, |
| 90 | expectedOutputData, |
| 91 | desc); |
| 92 | } |
| 93 | |
| 94 | template <armnn::DataType ArmnnOutputType, typename T = armnn::ResolveType<ArmnnOutputType>> |
| 95 | LayerTestResult<T, 4> QuantizeClampTest( |
| 96 | armnn::IWorkloadFactory& workloadFactory, |
| 97 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 98 | { |
| 99 | armnn::QuantizeQueueDescriptor desc; |
| 100 | |
| 101 | const armnn::TensorInfo inputTensorInfo({1, 1, 2, 1}, armnn::DataType::Float32); |
| 102 | const armnn::TensorInfo outputTensorInfo({1, 1, 2, 1}, ArmnnOutputType, 0.0001f, 0); |
| 103 | |
| 104 | const T max = std::numeric_limits<T>::max(); |
| 105 | const T min = std::numeric_limits<T>::lowest(); |
| 106 | |
| 107 | std::vector<float> inputData = std::vector<float>( |
| 108 | { |
| 109 | -100.0f, 100.0f |
| 110 | }); |
| 111 | |
| 112 | std::vector<T> expectedOutputData = std::vector<T>( |
| 113 | { |
| 114 | min, max |
| 115 | }); |
| 116 | |
| 117 | return QuantizeTestImpl<T, 4>(workloadFactory, |
| 118 | memoryManager, |
| 119 | inputTensorInfo, |
| 120 | outputTensorInfo, |
| 121 | inputData, |
| 122 | expectedOutputData, |
| 123 | desc); |
| 124 | } |
| 125 | |
| 126 | } // anonymous namespace |