Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [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. |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "InstanceNormalizationTestImpl.hpp" |
| 7 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 8 | #include <QuantizeHelper.hpp> |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 9 | #include <ResolveType.hpp> |
| 10 | |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 11 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 12 | #include <armnn/backends/TensorHandle.hpp> |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 13 | #include <armnn/backends/IBackendInternal.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 14 | #include <armnn/backends/WorkloadFactory.hpp> |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 15 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 16 | #include <armnnTestUtils/DataLayoutUtils.hpp> |
| 17 | #include <armnnTestUtils/TensorCopyUtils.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 18 | #include <armnnTestUtils/WorkloadTestUtils.hpp> |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 19 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 20 | #include <TensorHelpers.hpp> |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 21 | |
| 22 | namespace |
| 23 | { |
| 24 | |
| 25 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 26 | LayerTestResult<T, 4> InstanceNormTestImpl( |
| 27 | armnn::IWorkloadFactory& workloadFactory, |
| 28 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 29 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 30 | const armnn::TensorInfo& inputTensorInfo, |
| 31 | const armnn::TensorInfo& outputTensorInfo, |
| 32 | const std::vector<float>& inputValues, |
| 33 | const std::vector<float>& expectedOutputValues, |
| 34 | armnn::InstanceNormalizationQueueDescriptor descriptor, |
| 35 | float qScale = 0.0f, |
| 36 | int32_t qOffset = 0) |
| 37 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 38 | IgnoreUnused(memoryManager); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 39 | std::vector<T> inputTensor = armnnUtils::QuantizedVector<T>(inputValues, qScale, qOffset); |
| 40 | std::vector<T> expectedOutput = armnnUtils::QuantizedVector<T>(expectedOutputValues, qScale, qOffset); |
| 41 | std::vector<T> actualOutput(outputTensorInfo.GetNumElements()); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 42 | |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 43 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 44 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 45 | |
| 46 | armnn::WorkloadInfo info; |
| 47 | |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 48 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 49 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 50 | |
| 51 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateInstanceNormalization(descriptor, info); |
| 52 | |
| 53 | inputHandle->Allocate(); |
| 54 | outputHandle->Allocate(); |
| 55 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 56 | CopyDataToITensorHandle(inputHandle.get(), inputTensor.data()); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 57 | |
| 58 | workload->Execute(); |
| 59 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 60 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 61 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 62 | return LayerTestResult<T, 4>(actualOutput, |
| 63 | expectedOutput, |
| 64 | outputHandle->GetShape(), |
| 65 | outputTensorInfo.GetShape()); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 69 | LayerTestResult<T, 4> InstanceNormTest( |
| 70 | armnn::IWorkloadFactory& workloadFactory, |
| 71 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 72 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 73 | armnn::DataLayout dataLayout) |
| 74 | { |
| 75 | // BatchSize: 2 |
| 76 | // Height: 2 |
| 77 | // Width: 2 |
| 78 | // Channels: 2 |
| 79 | |
| 80 | const armnn::TensorShape inputOutputShape{ 2, 2, 2, 2 }; |
| 81 | |
| 82 | armnn::TensorInfo inputTensorInfo(inputOutputShape, ArmnnType); |
| 83 | armnn::TensorInfo outputTensorInfo(inputOutputShape, ArmnnType); |
| 84 | |
| 85 | std::vector<float> inputValues |
| 86 | { |
| 87 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 88 | 0.f, 1.f, |
| 89 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 90 | 0.f, 2.f, |
| 91 | |
| 92 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 93 | 0.f, 2.f, |
| 94 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 95 | 0.f, 4.f, |
| 96 | |
| 97 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 98 | 1.f, -1.f, |
| 99 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 100 | -1.f, 2.f, |
| 101 | |
| 102 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 103 | -1.f, -2.f, |
| 104 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 105 | 1.f, 4.f |
| 106 | }; |
| 107 | |
| 108 | std::vector<float> expectedOutputValues |
| 109 | { |
| 110 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 111 | 0.f, -1.1470304f, |
| 112 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 113 | 0.f, -0.22940612f, |
| 114 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 115 | 0.f, -0.22940612f, |
| 116 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 117 | 0.f, 1.6058424f, |
| 118 | |
| 119 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 120 | 0.99995005f, -0.7337929f, |
| 121 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 122 | -0.99995005f, 0.52413774f, |
| 123 | |
| 124 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 125 | -0.99995005f, -1.1531031f, |
| 126 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 127 | 0.99995005f, 1.3627582f |
| 128 | }; |
| 129 | |
| 130 | if (dataLayout == armnn::DataLayout::NCHW) |
| 131 | { |
| 132 | PermuteTensorNhwcToNchw(inputTensorInfo, inputValues); |
| 133 | PermuteTensorNhwcToNchw(outputTensorInfo, expectedOutputValues); |
| 134 | } |
| 135 | |
| 136 | armnn::InstanceNormalizationQueueDescriptor descriptor; |
| 137 | descriptor.m_Parameters.m_Eps = 0.0001f; |
| 138 | descriptor.m_Parameters.m_Beta = 0.0f; |
| 139 | descriptor.m_Parameters.m_Gamma = 1.0f; |
| 140 | descriptor.m_Parameters.m_DataLayout = dataLayout; |
| 141 | |
| 142 | return InstanceNormTestImpl<ArmnnType>( |
| 143 | workloadFactory, |
| 144 | memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 145 | tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 146 | inputTensorInfo, |
| 147 | outputTensorInfo, |
| 148 | inputValues, |
| 149 | expectedOutputValues, |
| 150 | descriptor); |
| 151 | } |
| 152 | |
| 153 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 154 | LayerTestResult<T, 4> InstanceNormTest2( |
| 155 | armnn::IWorkloadFactory& workloadFactory, |
| 156 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 157 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 158 | armnn::DataLayout dataLayout) |
| 159 | { |
| 160 | // BatchSize: 2 |
| 161 | // Height: 2 |
| 162 | // Width: 2 |
| 163 | // Channels: 2 |
| 164 | |
| 165 | const armnn::TensorShape inputOutputShape{ 2, 2, 2, 2 }; |
| 166 | |
| 167 | armnn::TensorInfo inputTensorInfo(inputOutputShape, ArmnnType); |
| 168 | armnn::TensorInfo outputTensorInfo(inputOutputShape, ArmnnType); |
| 169 | |
| 170 | std::vector<float> inputValues |
| 171 | { |
| 172 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 173 | 0.f, 1.f, |
| 174 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 175 | 0.f, 2.f, |
| 176 | |
| 177 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 178 | 0.f, 2.f, |
| 179 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 180 | 0.f, 4.f, |
| 181 | |
| 182 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 183 | 1.f, -1.f, |
| 184 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 185 | -1.f, 2.f, |
| 186 | |
| 187 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 188 | -1.f, -2.f, |
| 189 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 190 | 1.f, 4.f |
| 191 | }; |
| 192 | |
| 193 | std::vector<float> expectedOutputValues |
| 194 | { |
| 195 | // Batch 0, Height 0, Width 0 x Channel (2) |
| 196 | 10.f, 7.7059393f, |
| 197 | // Batch 0, Height 0, Width 1 x Channel (2) |
| 198 | 10.f, 9.541187f, |
| 199 | |
| 200 | // Batch 0, Height 1, Width 0 x Channel (2) |
| 201 | 10.f, 9.541187f, |
| 202 | // Batch 0, Height 1, Width 1 x Channel (2) |
| 203 | 10.f, 13.211685f, |
| 204 | |
| 205 | // Batch 1, Height 0, Width 0 x Channel (2) |
| 206 | 11.9999f, 8.532414f, |
| 207 | // Batch 1, Height 0, Width 1 x Channel (2) |
| 208 | 8.0001f, 11.048275f, |
| 209 | |
| 210 | // Batch 1, Height 1, Width 0 x Channel (2) |
| 211 | 8.0001f, 7.693794f, |
| 212 | // Batch 1, Height 1, Width 1 x Channel (2) |
| 213 | 11.9999f, 12.725516f |
| 214 | }; |
| 215 | |
| 216 | if (dataLayout == armnn::DataLayout::NCHW) |
| 217 | { |
| 218 | PermuteTensorNhwcToNchw(inputTensorInfo, inputValues); |
| 219 | PermuteTensorNhwcToNchw(outputTensorInfo, expectedOutputValues); |
| 220 | } |
| 221 | |
| 222 | armnn::InstanceNormalizationQueueDescriptor descriptor; |
| 223 | descriptor.m_Parameters.m_Eps = 0.0001f; |
| 224 | descriptor.m_Parameters.m_Beta = 10.0f; |
| 225 | descriptor.m_Parameters.m_Gamma = 2.0f; |
| 226 | descriptor.m_Parameters.m_DataLayout = dataLayout; |
| 227 | |
| 228 | return InstanceNormTestImpl<ArmnnType>( |
| 229 | workloadFactory, |
| 230 | memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 231 | tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 232 | inputTensorInfo, |
| 233 | outputTensorInfo, |
| 234 | inputValues, |
| 235 | expectedOutputValues, |
| 236 | descriptor); |
| 237 | } |
| 238 | |
| 239 | } // anonymous namespace |
| 240 | |
| 241 | LayerTestResult<float, 4> InstanceNormFloat32Test( |
| 242 | armnn::IWorkloadFactory& workloadFactory, |
| 243 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 244 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 245 | armnn::DataLayout dataLayout) |
| 246 | { |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 247 | return InstanceNormTest<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, dataLayout); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | LayerTestResult<armnn::Half, 4> InstanceNormFloat16Test( |
| 251 | armnn::IWorkloadFactory& workloadFactory, |
| 252 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 253 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 254 | armnn::DataLayout dataLayout) |
| 255 | { |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 256 | return InstanceNormTest<armnn::DataType::Float16>(workloadFactory, memoryManager, tensorHandleFactory, dataLayout); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | LayerTestResult<float, 4> InstanceNormFloat32Test2( |
| 260 | armnn::IWorkloadFactory& workloadFactory, |
| 261 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 262 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 263 | armnn::DataLayout dataLayout) |
| 264 | { |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 265 | return InstanceNormTest2<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, dataLayout); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | LayerTestResult<armnn::Half, 4> InstanceNormFloat16Test2( |
| 269 | armnn::IWorkloadFactory& workloadFactory, |
| 270 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 271 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 272 | armnn::DataLayout dataLayout) |
| 273 | { |
Finn Williams | c43de6a | 2020-08-27 11:13:25 +0100 | [diff] [blame] | 274 | return InstanceNormTest2<armnn::DataType::Float16>(workloadFactory, memoryManager, tensorHandleFactory, dataLayout); |
Aron Virginas-Tar | 8168f40 | 2019-10-04 13:10:16 +0100 | [diff] [blame] | 275 | } |