blob: 166c44435d75311fc819452a1d9e7e777bdb37c6 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
7#include <armnn/ArmNN.hpp>
8#include <armnn/Tensor.hpp>
telsoa014fcda012018-03-09 14:13:49 +00009
David Beckac42efd2018-09-26 17:41:13 +010010#include <test/TensorHelpers.hpp>
telsoa014fcda012018-03-09 14:13:49 +000011
David Beckac42efd2018-09-26 17:41:13 +010012#include <backends/CpuTensorHandle.hpp>
13#include <backends/WorkloadFactory.hpp>
telsoa014fcda012018-03-09 14:13:49 +000014
David Beckac42efd2018-09-26 17:41:13 +010015#include <backends/test/QuantizeHelper.hpp>
telsoa014fcda012018-03-09 14:13:49 +000016
telsoa014fcda012018-03-09 14:13:49 +000017template<typename T>
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010018LayerTestResult<T, 4> BatchNormTestImpl(armnn::IWorkloadFactory& workloadFactory,
19 const armnn::TensorShape& inputOutputTensorShape,
20 const std::vector<float>& inputValues,
21 const std::vector<float>& expectedOutputValues,
22 float qScale,
23 int32_t qOffset,
24 armnn::DataLayout dataLayout)
telsoa014fcda012018-03-09 14:13:49 +000025{
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010026 armnn::TensorInfo inputTensorInfo(inputOutputTensorShape, armnn::GetDataType<T>());
27 armnn::TensorInfo outputTensorInfo(inputOutputTensorShape, armnn::GetDataType<T>());
telsoa014fcda012018-03-09 14:13:49 +000028
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010029 armnn::DataLayoutIndexed dataLayoutIndexed(dataLayout);
30
31 armnn::TensorInfo tensorInfo({ inputOutputTensorShape[dataLayoutIndexed.GetChannelsIndex()] },
32 armnn::GetDataType<T>());
telsoa014fcda012018-03-09 14:13:49 +000033
34 // Set quantization parameters if the requested type is a quantized type.
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010035 if (armnn::IsQuantizedType<T>())
telsoa014fcda012018-03-09 14:13:49 +000036 {
37 inputTensorInfo.SetQuantizationScale(qScale);
38 inputTensorInfo.SetQuantizationOffset(qOffset);
39 outputTensorInfo.SetQuantizationScale(qScale);
40 outputTensorInfo.SetQuantizationOffset(qOffset);
41 tensorInfo.SetQuantizationScale(qScale);
42 tensorInfo.SetQuantizationOffset(qOffset);
43 }
44
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010045 auto inputTensor = MakeTensor<T, 4>(inputTensorInfo,
46 QuantizedVector<T>(qScale, qOffset, inputValues));
telsoa014fcda012018-03-09 14:13:49 +000047
telsoa01c577f2c2018-08-31 09:22:23 +010048 // These values are per-channel of the input.
telsoa014fcda012018-03-09 14:13:49 +000049 auto mean = MakeTensor<T, 1>(tensorInfo, QuantizedVector<T>(qScale, qOffset, {3, -2}));
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010050 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}));
53
54 LayerTestResult<T, 4> result(outputTensorInfo);
55
56 result.outputExpected = MakeTensor<T, 4>(inputTensorInfo,
57 QuantizedVector<T>(qScale, qOffset, expectedOutputValues));
telsoa014fcda012018-03-09 14:13:49 +000058
59 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
60 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
61
telsoa014fcda012018-03-09 14:13:49 +000062 armnn::ScopedCpuTensorHandle meanTensor(tensorInfo);
63 armnn::ScopedCpuTensorHandle varianceTensor(tensorInfo);
64 armnn::ScopedCpuTensorHandle betaTensor(tensorInfo);
65 armnn::ScopedCpuTensorHandle gammaTensor(tensorInfo);
66
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010067 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;
75
telsoa014fcda012018-03-09 14:13:49 +000076 AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]);
77 AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]);
78 AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]);
79 AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]);
80
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010081 AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
82 AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
telsoa014fcda012018-03-09 14:13:49 +000083
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010084 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchNormalization(descriptor, info);
telsoa014fcda012018-03-09 14:13:49 +000085
86 inputHandle->Allocate();
87 outputHandle->Allocate();
88
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010089 CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0][0]);
telsoa014fcda012018-03-09 14:13:49 +000090
Matteo Martincigh539b44d2018-10-01 09:26:39 +010091 workloadFactory.Finalize();
telsoa014fcda012018-03-09 14:13:49 +000092 workload->Execute();
93
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010094 CopyDataFromITensorHandle(&result.output[0][0][0][0], outputHandle.get());
telsoa014fcda012018-03-09 14:13:49 +000095
Matteo Martincigh8eb675e2018-10-17 14:43:29 +010096 return result;
Matteo Martincigh539b44d2018-10-01 09:26:39 +010097}
Nikhil Rajd1340932018-10-18 14:27:50 +010098
99
100template<typename T>
101LayerTestResult<T,4> BatchNormTestNhwcImpl(armnn::IWorkloadFactory& workloadFactory,
102 float qScale,
103 int32_t qOffset)
104{
105 const unsigned int width = 2;
106 const unsigned int height = 3;
107 const unsigned int channels = 2;
108 const unsigned int num = 1;
109
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>());
113
114 // Set quantization parameters if the requested type is a quantized type.
115 if(armnn::IsQuantizedType<T>())
116 {
117 inputTensorInfo.SetQuantizationScale(qScale);
118 inputTensorInfo.SetQuantizationOffset(qOffset);
119 outputTensorInfo.SetQuantizationScale(qScale);
120 outputTensorInfo.SetQuantizationOffset(qOffset);
121 tensorInfo.SetQuantizationScale(qScale);
122 tensorInfo.SetQuantizationOffset(qOffset);
123 }
124
125 auto input = MakeTensor<T, 4>(inputTensorInfo,
126 QuantizedVector<T>(qScale, qOffset,
127 {
128 1.f, 1.f, 4.f, 1.f,
129 4.f, 4.f, 2.f, 1.f,
130 1.f, -2.f, 6.f, 4.f
131 }));
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);
138
139 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
140 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
141
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);
148
149 AllocateAndCopyDataToITensorHandle(&meanTensor, &mean[0]);
150 AllocateAndCopyDataToITensorHandle(&varianceTensor, &variance[0]);
151 AllocateAndCopyDataToITensorHandle(&betaTensor, &beta[0]);
152 AllocateAndCopyDataToITensorHandle(&gammaTensor, &gamma[0]);
153
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;
162
163 // For each channel:
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,
168 {
169 1.f, 3.f, 4.f, 3.f,
170 4.f, 4.f, 2.f, 3.f,
171 1.f, 2.f, 6.f, 4.f
172 }));
173
174 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateBatchNormalization(data, info);
175
176 inputHandle->Allocate();
177 outputHandle->Allocate();
178
179 CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]);
180
181 workloadFactory.Finalize();
182 workload->Execute();
183
184 CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get());
185
186 return ret;
187}