telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 7 | #include <ResolveType.hpp> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 8 | #include "WorkloadTestUtils.hpp" |
| 9 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | #include <armnn/ArmNN.hpp> |
| 11 | #include <armnn/Tensor.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 13 | #include <backendsCommon/CpuTensorHandle.hpp> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 14 | #include <backendsCommon/IBackendInternal.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 15 | #include <backendsCommon/WorkloadFactory.hpp> |
| 16 | #include <backendsCommon/test/QuantizeHelper.hpp> |
| 17 | |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 18 | #include <test/TensorHelpers.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 19 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 20 | #include <DataLayoutIndexed.hpp> |
| 21 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 22 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 23 | LayerTestResult<T, 4> BatchNormTestImpl( |
| 24 | armnn::IWorkloadFactory& workloadFactory, |
| 25 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 26 | const armnn::TensorShape& inputOutputTensorShape, |
| 27 | const std::vector<float>& inputValues, |
| 28 | const std::vector<float>& expectedOutputValues, |
| 29 | float qScale, |
| 30 | int32_t qOffset, |
| 31 | armnn::DataLayout dataLayout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | { |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 33 | armnn::TensorInfo inputTensorInfo(inputOutputTensorShape, ArmnnType); |
| 34 | armnn::TensorInfo outputTensorInfo(inputOutputTensorShape, ArmnnType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 36 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(dataLayout); |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 37 | |
| 38 | armnn::TensorInfo tensorInfo({ inputOutputTensorShape[dataLayoutIndexed.GetChannelsIndex()] }, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 39 | ArmnnType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 40 | |
| 41 | // Set quantization parameters if the requested type is a quantized type. |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 42 | if (armnn::IsQuantizedType<T>()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 43 | { |
| 44 | inputTensorInfo.SetQuantizationScale(qScale); |
| 45 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 46 | outputTensorInfo.SetQuantizationScale(qScale); |
| 47 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 48 | tensorInfo.SetQuantizationScale(qScale); |
| 49 | tensorInfo.SetQuantizationOffset(qOffset); |
| 50 | } |
| 51 | |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 52 | auto inputTensor = MakeTensor<T, 4>(inputTensorInfo, |
| 53 | QuantizedVector<T>(qScale, qOffset, inputValues)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 54 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 55 | // These values are per-channel of the input. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | auto mean = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, -2})); |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 57 | auto variance = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {4, 9})); |
| 58 | auto beta = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, 2})); |
| 59 | auto gamma = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {2, 1})); |
| 60 | |
| 61 | LayerTestResult<T, 4> result(outputTensorInfo); |
| 62 | |
| 63 | result.outputExpected = MakeTensor<T, 4>(inputTensorInfo, |
| 64 | QuantizedVector<T>(qScale, qOffset, expectedOutputValues)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | |
| 66 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 67 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 68 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 69 | armnn::ScopedCpuTensorHandle meanTensor(tensorInfo); |
| 70 | armnn::ScopedCpuTensorHandle varianceTensor(tensorInfo); |
| 71 | armnn::ScopedCpuTensorHandle betaTensor(tensorInfo); |
| 72 | armnn::ScopedCpuTensorHandle gammaTensor(tensorInfo); |
| 73 | |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 74 | armnn::BatchNormalizationQueueDescriptor descriptor; |
| 75 | descriptor.m_Mean = &meanTensor; |
| 76 | descriptor.m_Variance = &varianceTensor; |
| 77 | descriptor.m_Beta = &betaTensor; |
| 78 | descriptor.m_Gamma = &gammaTensor; |
| 79 | descriptor.m_Parameters.m_Eps = 0.0f; |
| 80 | descriptor.m_Parameters.m_DataLayout = dataLayout; |
| 81 | armnn::WorkloadInfo info; |
| 82 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 83 | AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]); |
| 84 | AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]); |
| 85 | AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]); |
| 86 | AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]); |
| 87 | |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 88 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 89 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 91 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchNormalization(descriptor, info); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 92 | |
| 93 | inputHandle->Allocate(); |
| 94 | outputHandle->Allocate(); |
| 95 | |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 96 | CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0][0]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | |
| 98 | workload->Execute(); |
| 99 | |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 100 | CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 102 | return result; |
Matteo Martincigh | 539b44d | 2018-10-01 09:26:39 +0100 | [diff] [blame] | 103 | } |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 104 | |
| 105 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 106 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 107 | LayerTestResult<T,4> BatchNormTestNhwcImpl( |
| 108 | armnn::IWorkloadFactory& workloadFactory, |
| 109 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 110 | float qScale, |
| 111 | int32_t qOffset) |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 112 | { |
| 113 | const unsigned int width = 2; |
| 114 | const unsigned int height = 3; |
| 115 | const unsigned int channels = 2; |
| 116 | const unsigned int num = 1; |
| 117 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 118 | armnn::TensorInfo inputTensorInfo({num, height, width, channels}, ArmnnType); |
| 119 | armnn::TensorInfo outputTensorInfo({num, height, width, channels}, ArmnnType); |
| 120 | armnn::TensorInfo tensorInfo({channels}, ArmnnType); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 121 | |
| 122 | // Set quantization parameters if the requested type is a quantized type. |
| 123 | if(armnn::IsQuantizedType<T>()) |
| 124 | { |
| 125 | inputTensorInfo.SetQuantizationScale(qScale); |
| 126 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 127 | outputTensorInfo.SetQuantizationScale(qScale); |
| 128 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 129 | tensorInfo.SetQuantizationScale(qScale); |
| 130 | tensorInfo.SetQuantizationOffset(qOffset); |
| 131 | } |
| 132 | |
| 133 | auto input = MakeTensor<T, 4>(inputTensorInfo, |
| 134 | QuantizedVector<T>(qScale, qOffset, |
| 135 | { |
| 136 | 1.f, 1.f, 4.f, 1.f, |
| 137 | 4.f, 4.f, 2.f, 1.f, |
| 138 | 1.f, -2.f, 6.f, 4.f |
| 139 | })); |
| 140 | // These values are per-channel of the input. |
| 141 | auto mean = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, -2})); |
| 142 | auto variance = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {4, 9})); |
| 143 | auto beta = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, 2})); |
| 144 | auto gamma = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {2, 1})); |
| 145 | LayerTestResult<T,4> ret(outputTensorInfo); |
| 146 | |
| 147 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 148 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 149 | |
| 150 | armnn::BatchNormalizationQueueDescriptor data; |
| 151 | armnn::WorkloadInfo info; |
| 152 | armnn::ScopedCpuTensorHandle meanTensor(tensorInfo); |
| 153 | armnn::ScopedCpuTensorHandle varianceTensor(tensorInfo); |
| 154 | armnn::ScopedCpuTensorHandle betaTensor(tensorInfo); |
| 155 | armnn::ScopedCpuTensorHandle gammaTensor(tensorInfo); |
| 156 | |
| 157 | AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]); |
| 158 | AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]); |
| 159 | AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]); |
| 160 | AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]); |
| 161 | |
| 162 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 163 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 164 | data.m_Mean = &meanTensor; |
| 165 | data.m_Variance = &varianceTensor; |
| 166 | data.m_Beta = &betaTensor; |
| 167 | data.m_Gamma = &gammaTensor; |
| 168 | data.m_Parameters.m_Eps = 0.0f; |
| 169 | data.m_Parameters.m_DataLayout = armnn::DataLayout::NHWC; |
| 170 | |
| 171 | // For each channel: |
| 172 | // substract mean, divide by standard deviation (with an epsilon to avoid div by 0), |
| 173 | // multiply by gamma and add beta |
| 174 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, |
| 175 | QuantizedVector<T>(qScale, qOffset, |
| 176 | { |
| 177 | 1.f, 3.f, 4.f, 3.f, |
| 178 | 4.f, 4.f, 2.f, 3.f, |
| 179 | 1.f, 2.f, 6.f, 4.f |
| 180 | })); |
| 181 | |
| 182 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchNormalization(data, info); |
| 183 | |
| 184 | inputHandle->Allocate(); |
| 185 | outputHandle->Allocate(); |
| 186 | |
| 187 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 188 | |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 189 | workload->Execute(); |
| 190 | |
| 191 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 192 | |
| 193 | return ret; |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 194 | } |