ArmNN
 21.11
InstanceNormalizationTestImpl.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include <QuantizeHelper.hpp>
9 #include <ResolveType.hpp>
10 
11 
15 
19 
20 #include <test/TensorHelpers.hpp>
21 
22 namespace
23 {
24 
25 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
26 LayerTestResult<T, 4> InstanceNormTestImpl(
27  armnn::IWorkloadFactory& workloadFactory,
29  const armnn::ITensorHandleFactory& tensorHandleFactory,
30  const armnn::TensorInfo& inputTensorInfo,
31  const armnn::TensorInfo& outputTensorInfo,
32  const std::vector<float>& inputValues,
33  const std::vector<float>& expectedOutputValues,
35  float qScale = 0.0f,
36  int32_t qOffset = 0)
37 {
38  IgnoreUnused(memoryManager);
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());
42 
43  std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo);
44  std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo);
45 
47 
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 
56  CopyDataToITensorHandle(inputHandle.get(), inputTensor.data());
57 
58  workload->Execute();
59 
60  CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get());
61 
62  return LayerTestResult<T, 4>(actualOutput,
63  expectedOutput,
64  outputHandle->GetShape(),
65  outputTensorInfo.GetShape());
66 }
67 
68 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
69 LayerTestResult<T, 4> InstanceNormTest(
70  armnn::IWorkloadFactory& workloadFactory,
72  const armnn::ITensorHandleFactory& tensorHandleFactory,
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 
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,
145  tensorHandleFactory,
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,
157  const armnn::ITensorHandleFactory& tensorHandleFactory,
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 
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,
231  tensorHandleFactory,
232  inputTensorInfo,
233  outputTensorInfo,
234  inputValues,
235  expectedOutputValues,
236  descriptor);
237 }
238 
239 } // anonymous namespace
240 
242  armnn::IWorkloadFactory& workloadFactory,
244  const armnn::ITensorHandleFactory& tensorHandleFactory,
245  armnn::DataLayout dataLayout)
246 {
247  return InstanceNormTest<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, dataLayout);
248 }
249 
251  armnn::IWorkloadFactory& workloadFactory,
253  const armnn::ITensorHandleFactory& tensorHandleFactory,
254  armnn::DataLayout dataLayout)
255 {
256  return InstanceNormTest<armnn::DataType::Float16>(workloadFactory, memoryManager, tensorHandleFactory, dataLayout);
257 }
258 
260  armnn::IWorkloadFactory& workloadFactory,
262  const armnn::ITensorHandleFactory& tensorHandleFactory,
263  armnn::DataLayout dataLayout)
264 {
265  return InstanceNormTest2<armnn::DataType::Float32>(workloadFactory, memoryManager, tensorHandleFactory, dataLayout);
266 }
267 
269  armnn::IWorkloadFactory& workloadFactory,
271  const armnn::ITensorHandleFactory& tensorHandleFactory,
272  armnn::DataLayout dataLayout)
273 {
274  return InstanceNormTest2<armnn::DataType::Float16>(workloadFactory, memoryManager, tensorHandleFactory, dataLayout);
275 }
DataLayout
Definition: Types.hpp:49
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
virtual std::unique_ptr< IWorkload > CreateInstanceNormalization(const InstanceNormalizationQueueDescriptor &descriptor, const WorkloadInfo &info) const
float m_Gamma
Gamma, the scale scalar value applied for the normalized tensor. Defaults to 1.0. ...
LayerTestResult< float, 4 > InstanceNormFloat32Test2(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory, armnn::DataLayout dataLayout)
void IgnoreUnused(Ts &&...)
LayerTestResult< armnn::Half, 4 > InstanceNormFloat16Test2(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory, armnn::DataLayout dataLayout)
float m_Eps
Epsilon, small scalar value added to variance to avoid dividing by zero. Defaults to 1e-12f...
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
LayerTestResult< armnn::Half, 4 > InstanceNormFloat16Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory, armnn::DataLayout dataLayout)
void CopyDataFromITensorHandle(void *memory, const armnn::ITensorHandle *tensorHandle)
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
float m_Beta
Beta, the offset scalar value applied for the normalized tensor. Defaults to 1.0. ...
LayerTestResult< float, 4 > InstanceNormFloat32Test(armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory, armnn::DataLayout dataLayout)
Contains information about TensorInfos of a layer.
virtual std::unique_ptr< ITensorHandle > CreateTensorHandle(const TensorInfo &tensorInfo) const =0
unsigned int GetNumElements() const
Definition: Tensor.hpp:196
void CopyDataToITensorHandle(armnn::ITensorHandle *tensorHandle, const void *memory)
void PermuteTensorNhwcToNchw(armnn::TensorInfo &tensorInfo, std::vector< T > &tensorData)