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