2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
7 #include <armnn/ArmNN.hpp>
8 #include <armnn/Tensor.hpp>
10 #include <test/TensorHelpers.hpp>
12 #include <backends/CpuTensorHandle.hpp>
13 #include <backends/WorkloadFactory.hpp>
15 #include <backends/test/QuantizeHelper.hpp>
18 LayerTestResult<T, 4> BatchNormTestImpl(armnn::IWorkloadFactory& workloadFactory,
19 const armnn::TensorShape& inputOutputTensorShape,
20 const std::vector<float>& inputValues,
21 const std::vector<float>& expectedOutputValues,
24 armnn::DataLayout dataLayout)
26 armnn::TensorInfo inputTensorInfo(inputOutputTensorShape, armnn::GetDataType<T>());
27 armnn::TensorInfo outputTensorInfo(inputOutputTensorShape, armnn::GetDataType<T>());
29 armnn::DataLayoutIndexed dataLayoutIndexed(dataLayout);
31 armnn::TensorInfo tensorInfo({ inputOutputTensorShape[dataLayoutIndexed.GetChannelsIndex()] },
32 armnn::GetDataType<T>());
34 // Set quantization parameters if the requested type is a quantized type.
35 if (armnn::IsQuantizedType<T>())
37 inputTensorInfo.SetQuantizationScale(qScale);
38 inputTensorInfo.SetQuantizationOffset(qOffset);
39 outputTensorInfo.SetQuantizationScale(qScale);
40 outputTensorInfo.SetQuantizationOffset(qOffset);
41 tensorInfo.SetQuantizationScale(qScale);
42 tensorInfo.SetQuantizationOffset(qOffset);
45 auto inputTensor = MakeTensor<T, 4>(inputTensorInfo,
46 QuantizedVector<T>(qScale, qOffset, inputValues));
48 // These values are per-channel of the input.
49 auto mean = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, -2}));
50 auto variance = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {4, 9}));
51 auto beta = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, 2}));
52 auto gamma = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {2, 1}));
54 LayerTestResult<T, 4> result(outputTensorInfo);
56 result.outputExpected = MakeTensor<T, 4>(inputTensorInfo,
57 QuantizedVector<T>(qScale, qOffset, expectedOutputValues));
59 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
60 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
62 armnn::ScopedCpuTensorHandle meanTensor(tensorInfo);
63 armnn::ScopedCpuTensorHandle varianceTensor(tensorInfo);
64 armnn::ScopedCpuTensorHandle betaTensor(tensorInfo);
65 armnn::ScopedCpuTensorHandle gammaTensor(tensorInfo);
67 armnn::BatchNormalizationQueueDescriptor descriptor;
68 descriptor.m_Mean = &meanTensor;
69 descriptor.m_Variance = &varianceTensor;
70 descriptor.m_Beta = &betaTensor;
71 descriptor.m_Gamma = &gammaTensor;
72 descriptor.m_Parameters.m_Eps = 0.0f;
73 descriptor.m_Parameters.m_DataLayout = dataLayout;
74 armnn::WorkloadInfo info;
76 AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]);
77 AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]);
78 AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]);
79 AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]);
81 AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
82 AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
84 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchNormalization(descriptor, info);
86 inputHandle->Allocate();
87 outputHandle->Allocate();
89 CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0][0]);
91 workloadFactory.Finalize();
94 CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get());
101 LayerTestResult<T,4> BatchNormTestNhwcImpl(armnn::IWorkloadFactory& workloadFactory,
105 const unsigned int width = 2;
106 const unsigned int height = 3;
107 const unsigned int channels = 2;
108 const unsigned int num = 1;
110 armnn::TensorInfo inputTensorInfo({num, height, width, channels}, armnn::GetDataType<T>());
111 armnn::TensorInfo outputTensorInfo({num, height, width, channels}, armnn::GetDataType<T>());
112 armnn::TensorInfo tensorInfo({channels}, armnn::GetDataType<T>());
114 // Set quantization parameters if the requested type is a quantized type.
115 if(armnn::IsQuantizedType<T>())
117 inputTensorInfo.SetQuantizationScale(qScale);
118 inputTensorInfo.SetQuantizationOffset(qOffset);
119 outputTensorInfo.SetQuantizationScale(qScale);
120 outputTensorInfo.SetQuantizationOffset(qOffset);
121 tensorInfo.SetQuantizationScale(qScale);
122 tensorInfo.SetQuantizationOffset(qOffset);
125 auto input = MakeTensor<T, 4>(inputTensorInfo,
126 QuantizedVector<T>(qScale, qOffset,
132 // These values are per-channel of the input.
133 auto mean = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, -2}));
134 auto variance = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {4, 9}));
135 auto beta = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, 2}));
136 auto gamma = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {2, 1}));
137 LayerTestResult<T,4> ret(outputTensorInfo);
139 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
140 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
142 armnn::BatchNormalizationQueueDescriptor data;
143 armnn::WorkloadInfo info;
144 armnn::ScopedCpuTensorHandle meanTensor(tensorInfo);
145 armnn::ScopedCpuTensorHandle varianceTensor(tensorInfo);
146 armnn::ScopedCpuTensorHandle betaTensor(tensorInfo);
147 armnn::ScopedCpuTensorHandle gammaTensor(tensorInfo);
149 AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]);
150 AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]);
151 AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]);
152 AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]);
154 AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
155 AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
156 data.m_Mean = &meanTensor;
157 data.m_Variance = &varianceTensor;
158 data.m_Beta = &betaTensor;
159 data.m_Gamma = &gammaTensor;
160 data.m_Parameters.m_Eps = 0.0f;
161 data.m_Parameters.m_DataLayout = armnn::DataLayout::NHWC;
164 // substract mean, divide by standard deviation (with an epsilon to avoid div by 0),
165 // multiply by gamma and add beta
166 ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo,
167 QuantizedVector<T>(qScale, qOffset,
174 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchNormalization(data, info);
176 inputHandle->Allocate();
177 outputHandle->Allocate();
179 CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]);
181 workloadFactory.Finalize();
184 CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get());