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 <backendsCommon/CpuTensorHandle.hpp>
11 #include <backendsCommon/WorkloadFactory.hpp>
12 #include <backendsCommon/test/QuantizeHelper.hpp>
14 #include <test/TensorHelpers.hpp>
17 LayerTestResult<T, 4> BatchNormTestImpl(armnn::IWorkloadFactory& workloadFactory,
18 const armnn::TensorShape& inputOutputTensorShape,
19 const std::vector<float>& inputValues,
20 const std::vector<float>& expectedOutputValues,
23 armnn::DataLayout dataLayout)
25 armnn::TensorInfo inputTensorInfo(inputOutputTensorShape, armnn::GetDataType<T>());
26 armnn::TensorInfo outputTensorInfo(inputOutputTensorShape, armnn::GetDataType<T>());
28 armnn::DataLayoutIndexed dataLayoutIndexed(dataLayout);
30 armnn::TensorInfo tensorInfo({ inputOutputTensorShape[dataLayoutIndexed.GetChannelsIndex()] },
31 armnn::GetDataType<T>());
33 // Set quantization parameters if the requested type is a quantized type.
34 if (armnn::IsQuantizedType<T>())
36 inputTensorInfo.SetQuantizationScale(qScale);
37 inputTensorInfo.SetQuantizationOffset(qOffset);
38 outputTensorInfo.SetQuantizationScale(qScale);
39 outputTensorInfo.SetQuantizationOffset(qOffset);
40 tensorInfo.SetQuantizationScale(qScale);
41 tensorInfo.SetQuantizationOffset(qOffset);
44 auto inputTensor = MakeTensor<T, 4>(inputTensorInfo,
45 QuantizedVector<T>(qScale, qOffset, inputValues));
47 // These values are per-channel of the input.
48 auto mean = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, -2}));
49 auto variance = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {4, 9}));
50 auto beta = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, 2}));
51 auto gamma = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {2, 1}));
53 LayerTestResult<T, 4> result(outputTensorInfo);
55 result.outputExpected = MakeTensor<T, 4>(inputTensorInfo,
56 QuantizedVector<T>(qScale, qOffset, expectedOutputValues));
58 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
59 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
61 armnn::ScopedCpuTensorHandle meanTensor(tensorInfo);
62 armnn::ScopedCpuTensorHandle varianceTensor(tensorInfo);
63 armnn::ScopedCpuTensorHandle betaTensor(tensorInfo);
64 armnn::ScopedCpuTensorHandle gammaTensor(tensorInfo);
66 armnn::BatchNormalizationQueueDescriptor descriptor;
67 descriptor.m_Mean = &meanTensor;
68 descriptor.m_Variance = &varianceTensor;
69 descriptor.m_Beta = &betaTensor;
70 descriptor.m_Gamma = &gammaTensor;
71 descriptor.m_Parameters.m_Eps = 0.0f;
72 descriptor.m_Parameters.m_DataLayout = dataLayout;
73 armnn::WorkloadInfo info;
75 AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]);
76 AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]);
77 AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]);
78 AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]);
80 AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
81 AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
83 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchNormalization(descriptor, info);
85 inputHandle->Allocate();
86 outputHandle->Allocate();
88 CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0][0]);
90 workloadFactory.Finalize();
93 CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get());
100 LayerTestResult<T,4> BatchNormTestNhwcImpl(armnn::IWorkloadFactory& workloadFactory,
104 const unsigned int width = 2;
105 const unsigned int height = 3;
106 const unsigned int channels = 2;
107 const unsigned int num = 1;
109 armnn::TensorInfo inputTensorInfo({num, height, width, channels}, armnn::GetDataType<T>());
110 armnn::TensorInfo outputTensorInfo({num, height, width, channels}, armnn::GetDataType<T>());
111 armnn::TensorInfo tensorInfo({channels}, armnn::GetDataType<T>());
113 // Set quantization parameters if the requested type is a quantized type.
114 if(armnn::IsQuantizedType<T>())
116 inputTensorInfo.SetQuantizationScale(qScale);
117 inputTensorInfo.SetQuantizationOffset(qOffset);
118 outputTensorInfo.SetQuantizationScale(qScale);
119 outputTensorInfo.SetQuantizationOffset(qOffset);
120 tensorInfo.SetQuantizationScale(qScale);
121 tensorInfo.SetQuantizationOffset(qOffset);
124 auto input = MakeTensor<T, 4>(inputTensorInfo,
125 QuantizedVector<T>(qScale, qOffset,
131 // These values are per-channel of the input.
132 auto mean = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, -2}));
133 auto variance = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {4, 9}));
134 auto beta = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, 2}));
135 auto gamma = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {2, 1}));
136 LayerTestResult<T,4> ret(outputTensorInfo);
138 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
139 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
141 armnn::BatchNormalizationQueueDescriptor data;
142 armnn::WorkloadInfo info;
143 armnn::ScopedCpuTensorHandle meanTensor(tensorInfo);
144 armnn::ScopedCpuTensorHandle varianceTensor(tensorInfo);
145 armnn::ScopedCpuTensorHandle betaTensor(tensorInfo);
146 armnn::ScopedCpuTensorHandle gammaTensor(tensorInfo);
148 AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]);
149 AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]);
150 AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]);
151 AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]);
153 AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
154 AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
155 data.m_Mean = &meanTensor;
156 data.m_Variance = &varianceTensor;
157 data.m_Beta = &betaTensor;
158 data.m_Gamma = &gammaTensor;
159 data.m_Parameters.m_Eps = 0.0f;
160 data.m_Parameters.m_DataLayout = armnn::DataLayout::NHWC;
163 // substract mean, divide by standard deviation (with an epsilon to avoid div by 0),
164 // multiply by gamma and add beta
165 ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo,
166 QuantizedVector<T>(qScale, qOffset,
173 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchNormalization(data, info);
175 inputHandle->Allocate();
176 outputHandle->Allocate();
178 CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]);
180 workloadFactory.Finalize();
183 CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get());