telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 7 | #include "WorkloadTestUtils.hpp" |
Nina Drozd | d41b259 | 2018-11-19 13:03:36 +0000 | [diff] [blame] | 8 | #include "TensorUtils.hpp" |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 9 | #include <ResolveType.hpp> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 10 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 11 | #include <Permute.hpp> |
| 12 | #include <DataLayoutIndexed.hpp> |
| 13 | |
| 14 | #include <test/TensorHelpers.hpp> |
| 15 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | #include <armnn/ArmNN.hpp> |
| 17 | #include <armnn/Tensor.hpp> |
| 18 | #include <armnn/TypesUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 19 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 20 | #include <backendsCommon/CpuTensorHandle.hpp> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 21 | #include <backendsCommon/IBackendInternal.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 22 | #include <backendsCommon/WorkloadFactory.hpp> |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 23 | #include <backendsCommon/test/QuantizeHelper.hpp> |
| 24 | |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 25 | #include <boost/numeric/conversion/cast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 26 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 27 | #include <string> |
| 28 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | // Mapping from input type to bias type for fully connected layers. |
| 30 | // float => float, uint8_t => int32_t |
| 31 | template<typename T> |
| 32 | struct FullyConnectedBiasTypeForInputType; |
| 33 | |
| 34 | template<> |
| 35 | struct FullyConnectedBiasTypeForInputType<float> |
| 36 | { |
| 37 | using Type = float; |
| 38 | }; |
| 39 | |
| 40 | template<> |
| 41 | struct FullyConnectedBiasTypeForInputType<uint8_t> |
| 42 | { |
| 43 | using Type = int32_t; |
| 44 | }; |
| 45 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 46 | // Modifies a std::vector in-place using a specified bias. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | template<typename T, typename B> |
| 48 | void ApplyBias(std::vector<T>& v, float vScale, int32_t vOffset, |
| 49 | const std::vector<B>& bias, float bScale, int32_t bOffset, uint32_t w, uint32_t h) |
| 50 | { |
| 51 | BOOST_ASSERT_MSG((armnn::IsQuantizedType<T>() && vScale != 0.0f) || (!armnn::IsQuantizedType<T>()), |
| 52 | "Invalid type and parameter combination."); |
| 53 | BOOST_ASSERT_MSG((armnn::IsQuantizedType<B>() && bScale != 0.0f) || (!armnn::IsQuantizedType<B>()), |
| 54 | "Invalid type and parameter combination."); |
| 55 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | // Note we need to dequantize and re-quantize the image value and the bias. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 57 | for (uint32_t i = 0; i < bias.size(); ++i) |
| 58 | { |
| 59 | float dBias = SelectiveDequantize(bias[i], bScale, bOffset); |
| 60 | for (uint32_t y = 0; y < h; ++y) |
| 61 | { |
| 62 | for (uint32_t x = 0; x < w; ++x) |
| 63 | { |
| 64 | uint32_t offset = (i * h + y) * w + x; |
| 65 | BOOST_ASSERT(offset < v.size()); |
| 66 | T& outRef = v[offset]; |
| 67 | float dOutput = SelectiveDequantize(outRef, vScale, vOffset); |
| 68 | outRef = SelectiveQuantize<T>(dOutput + dBias, vScale, vOffset); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 74 | template<armnn::DataType ArmnnType, armnn::DataType ArmnnBType, |
| 75 | typename T = armnn::ResolveType<ArmnnType>, typename B = armnn::ResolveType<ArmnnBType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 76 | LayerTestResult<T, 4> SimpleConvolution2dTestImpl( |
| 77 | armnn::IWorkloadFactory& workloadFactory, |
| 78 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 79 | const boost::multi_array<T, 4>& originalInput, |
| 80 | const boost::multi_array<T, 4>& originalKernel, |
| 81 | const boost::multi_array<B, 1>& bias, |
| 82 | const boost::multi_array<T, 4>& originalOutputExpected, |
| 83 | float qScale, |
| 84 | int32_t qOffset, |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 85 | const armnn::DataLayout layout = armnn::DataLayout::NCHW, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 86 | uint32_t padLeft = 0, |
| 87 | uint32_t padTop = 0, |
| 88 | uint32_t padRight = 0, |
Mike Kelly | 7332ed8 | 2018-12-20 17:03:06 +0000 | [diff] [blame] | 89 | uint32_t padBottom = 0, |
| 90 | uint32_t strideX = 1, |
| 91 | uint32_t strideY = 1) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 92 | { |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 93 | unsigned int inputHeight = boost::numeric_cast<unsigned int>(originalInput.shape()[2]); |
| 94 | unsigned int inputWidth = boost::numeric_cast<unsigned int>(originalInput.shape()[3]); |
| 95 | unsigned int inputChannels = boost::numeric_cast<unsigned int>(originalInput.shape()[1]); |
| 96 | unsigned int inputNum = boost::numeric_cast<unsigned int>(originalInput.shape()[0]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 98 | unsigned int outputHeight = boost::numeric_cast<unsigned int>(originalOutputExpected.shape()[2]); |
| 99 | unsigned int outputWidth = boost::numeric_cast<unsigned int>(originalOutputExpected.shape()[3]); |
| 100 | unsigned int outputChannels = boost::numeric_cast<unsigned int>(originalOutputExpected.shape()[1]); |
| 101 | unsigned int outputNum = boost::numeric_cast<unsigned int>(originalOutputExpected.shape()[0]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 102 | |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 103 | unsigned int kernelHeight = boost::numeric_cast<unsigned int>(originalKernel.shape()[2]); |
| 104 | unsigned int kernelWidth = boost::numeric_cast<unsigned int>(originalKernel.shape()[3]); |
| 105 | unsigned int kernelChannels = boost::numeric_cast<unsigned int>(originalKernel.shape()[1]); |
| 106 | unsigned int kernelDepthMul = boost::numeric_cast<unsigned int>(originalKernel.shape()[0]); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 107 | |
| 108 | bool biasEnabled = bias.size() > 0; |
| 109 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | // This function currently assumes 1 batch of input/output (and duplicates this into 2 batches). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | BOOST_ASSERT(inputNum == 1); |
| 112 | BOOST_ASSERT(outputNum == 1); |
| 113 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 114 | // If a bias is used, its size must equal the number of output channels. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | BOOST_ASSERT(!biasEnabled || bias.size() == outputChannels); |
| 116 | |
| 117 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 118 | // Note these tensors will use two (identical) batches. |
Nina Drozd | d41b259 | 2018-11-19 13:03:36 +0000 | [diff] [blame] | 119 | armnn::TensorInfo inputTensorInfo = |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 120 | armnnUtils::GetTensorInfo(2*inputNum, inputChannels, inputHeight, inputWidth, layout, ArmnnType); |
Nina Drozd | d41b259 | 2018-11-19 13:03:36 +0000 | [diff] [blame] | 121 | armnn::TensorInfo outputTensorInfo = |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 122 | armnnUtils::GetTensorInfo(2*outputNum, outputChannels, outputHeight, outputWidth, layout, ArmnnType); |
Nina Drozd | d41b259 | 2018-11-19 13:03:36 +0000 | [diff] [blame] | 123 | armnn::TensorInfo kernelDesc = |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 124 | armnnUtils::GetTensorInfo(kernelDepthMul, kernelChannels, kernelHeight, kernelWidth, layout, ArmnnType); |
| 125 | armnn::TensorInfo biasDesc({static_cast<unsigned int>(bias.size())}, ArmnnBType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 126 | |
| 127 | // Set quantization parameters if the requested type is a quantized type. |
| 128 | if(armnn::IsQuantizedType<T>()) |
| 129 | { |
| 130 | inputTensorInfo.SetQuantizationScale(qScale); |
| 131 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 132 | outputTensorInfo.SetQuantizationScale(qScale); |
| 133 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 134 | kernelDesc.SetQuantizationScale(qScale); |
| 135 | kernelDesc.SetQuantizationOffset(qOffset); |
| 136 | biasDesc.SetQuantizationScale(qScale*qScale); |
| 137 | biasDesc.SetQuantizationOffset(0); |
| 138 | } |
| 139 | |
| 140 | LayerTestResult<T, 4> ret(outputTensorInfo); |
| 141 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | // Construct input data - two batches of the same input image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 143 | std::vector<T> inputImage; |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 144 | inputImage.assign(originalInput.data(), originalInput.data() + 1*inputChannels*inputHeight*inputWidth); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 145 | std::vector<T> inputData; |
| 146 | inputData.insert(inputData.end(), inputImage.begin(), inputImage.end()); |
| 147 | inputData.insert(inputData.end(), inputImage.begin(), inputImage.end()); |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 148 | |
| 149 | // at this point if we require it permute the input data |
| 150 | const armnn::PermutationVector NCHWToNHWC = { 0, 3, 1, 2 }; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 151 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 152 | { |
| 153 | std::vector<T> tmp(inputData.size()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 154 | armnnUtils::Permute(inputTensorInfo.GetShape(), NCHWToNHWC, inputData.data(), tmp.data(), sizeof(T)); |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 155 | inputData = tmp; |
| 156 | } |
| 157 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 158 | auto batchedInput = MakeTensor<T, 4>(inputTensorInfo, inputData); |
| 159 | |
| 160 | std::vector<T> outputImage; |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 161 | outputImage.assign(originalOutputExpected.data(), |
| 162 | originalOutputExpected.data() + outputChannels*outputHeight*outputWidth); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 163 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 164 | // Apply bias to output image if it is enabled. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 165 | if(biasEnabled) |
| 166 | { |
| 167 | std::vector<T> biasV; |
| 168 | biasV.assign(bias.data(), bias.data() + outputChannels); |
| 169 | ApplyBias(outputImage, outputTensorInfo.GetQuantizationScale(), outputTensorInfo.GetQuantizationOffset(), |
| 170 | biasV, biasDesc.GetQuantizationScale(), biasDesc.GetQuantizationOffset(), |
| 171 | outputWidth, outputHeight); |
| 172 | } |
| 173 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 174 | // Construct expected output data - two identical images. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 175 | std::vector<T> outputData; |
| 176 | outputData.insert(outputData.end(), outputImage.begin(), outputImage.end()); |
| 177 | outputData.insert(outputData.end(), outputImage.begin(), outputImage.end()); |
| 178 | |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 179 | // at this point if we require it permute the expected output |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 180 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 181 | { |
| 182 | std::vector<T> tmp(outputData.size()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 183 | armnnUtils::Permute(outputTensorInfo.GetShape(), NCHWToNHWC, outputData.data(), tmp.data(), sizeof(T)); |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 184 | outputData = tmp; |
| 185 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 186 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputData); |
| 187 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 188 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 189 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 190 | |
| 191 | armnn::Convolution2dQueueDescriptor data; |
| 192 | armnn::WorkloadInfo info; |
| 193 | armnn::ScopedCpuTensorHandle weightsTensor(kernelDesc); |
| 194 | armnn::ScopedCpuTensorHandle biasTensor(biasDesc); |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 195 | // Permute the kernel if necessary |
| 196 | boost::multi_array<T, 4> kernel = boost::multi_array<T, 4>(originalKernel); |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 197 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 198 | { |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 199 | armnnUtils::Permute(kernelDesc.GetShape(), NCHWToNHWC, originalKernel.data(), kernel.data(), sizeof(T)); |
jimfly01 | 0a088a6 | 2018-10-25 17:05:05 +0100 | [diff] [blame] | 200 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 201 | AllocateAndCopyDataToITensorHandle(&weightsTensor, &kernel[0][0][0][0]); |
| 202 | |
| 203 | if(biasEnabled) |
| 204 | { |
| 205 | AllocateAndCopyDataToITensorHandle(&biasTensor, &bias[0]); |
| 206 | } |
| 207 | |
| 208 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 209 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 210 | |
| 211 | data.m_Weight = &weightsTensor; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 212 | data.m_Bias = &biasTensor; // Still set this whether or not bias is enabled - can be a source of bugs. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 213 | data.m_Parameters.m_StrideX = strideX; |
| 214 | data.m_Parameters.m_StrideY = strideY; |
| 215 | data.m_Parameters.m_PadLeft = padLeft; |
| 216 | data.m_Parameters.m_PadRight = padRight; |
| 217 | data.m_Parameters.m_PadTop = padTop; |
| 218 | data.m_Parameters.m_PadBottom = padBottom; |
| 219 | data.m_Parameters.m_BiasEnabled = biasEnabled; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 220 | data.m_Parameters.m_DataLayout = layout; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 221 | |
| 222 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateConvolution2d(data, info); |
| 223 | inputHandle->Allocate(); |
| 224 | outputHandle->Allocate(); |
| 225 | |
| 226 | CopyDataToITensorHandle(inputHandle.get(), &batchedInput[0][0][0][0]); |
| 227 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 228 | ExecuteWorkload(*workload, memoryManager); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 229 | |
| 230 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 235 | template<armnn::DataType ArmnnType, armnn::DataType ArmnnBType, |
| 236 | typename T = armnn::ResolveType<ArmnnType>, typename B = armnn::ResolveType<ArmnnBType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 237 | LayerTestResult<T, 4> SimpleConvolution2dNhwcTestImpl( |
| 238 | armnn::IWorkloadFactory& workloadFactory, |
| 239 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 240 | const boost::multi_array<T, 4>& input, |
| 241 | const boost::multi_array<T, 4>& kernel, |
| 242 | const boost::multi_array<B, 1>& bias, |
| 243 | const boost::multi_array<T, 4>& outputExpected, |
Mike Kelly | 7332ed8 | 2018-12-20 17:03:06 +0000 | [diff] [blame] | 244 | const armnn::DataLayout dataLayout, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 245 | float qScale, |
| 246 | int32_t qOffset, |
| 247 | uint32_t padLeft = 1, |
| 248 | uint32_t padTop = 1, |
| 249 | uint32_t padRight = 1, |
| 250 | uint32_t padBottom = 1, |
| 251 | uint32_t strideX = 1, |
| 252 | uint32_t strideY = 1) |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 253 | { |
| 254 | unsigned int inputNum = boost::numeric_cast<unsigned int>(input.shape()[0]); |
| 255 | unsigned int inputChannels = boost::numeric_cast<unsigned int>(input.shape()[3]); |
| 256 | unsigned int inputHeight = boost::numeric_cast<unsigned int>(input.shape()[1]); |
| 257 | unsigned int inputWidth = boost::numeric_cast<unsigned int>(input.shape()[2]); |
| 258 | |
| 259 | unsigned int kernelChanMul = boost::numeric_cast<unsigned int>(kernel.shape()[0]); |
| 260 | unsigned int kernelChannels = boost::numeric_cast<unsigned int>(kernel.shape()[3]); |
| 261 | unsigned int kernelHeight = boost::numeric_cast<unsigned int>(kernel.shape()[1]); |
| 262 | unsigned int kernelWidth = boost::numeric_cast<unsigned int>(kernel.shape()[2]); |
| 263 | |
| 264 | unsigned int outputNum = boost::numeric_cast<unsigned int>(outputExpected.shape()[0]); |
| 265 | unsigned int outputChannels = boost::numeric_cast<unsigned int>(outputExpected.shape()[3]); |
| 266 | unsigned int outputHeight = boost::numeric_cast<unsigned int>(outputExpected.shape()[1]); |
| 267 | unsigned int outputWidth = boost::numeric_cast<unsigned int>(outputExpected.shape()[2]); |
| 268 | |
| 269 | bool biasEnabled = bias.size() > 0; |
| 270 | |
| 271 | // Creates the tensors. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 272 | armnn::TensorInfo inputTensorInfo({inputNum, inputHeight, inputWidth, inputChannels}, ArmnnType); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 273 | armnn::TensorInfo outputTensorInfo({outputNum, outputHeight, outputWidth, outputChannels}, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 274 | ArmnnType); |
| 275 | armnn::TensorInfo kernelDesc({kernelChanMul, kernelHeight, kernelWidth, kernelChannels}, ArmnnType); |
| 276 | armnn::TensorInfo biasDesc({static_cast<unsigned int>(bias.size())}, ArmnnBType); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 277 | |
| 278 | // Construct the input data. |
| 279 | std::vector<T> inputData; |
| 280 | inputData.assign(input.data(), input.data() + inputHeight*inputWidth*inputChannels); |
| 281 | auto batchedInput = MakeTensor<T, 4>(inputTensorInfo, inputData); |
| 282 | |
| 283 | // Construct the output data, with bias applied, as appropriate. |
| 284 | std::vector<T> outputData; |
| 285 | outputData.assign(outputExpected.data(), outputExpected.data() + outputHeight*outputWidth*outputChannels); |
| 286 | |
| 287 | LayerTestResult<T, 4> ret(outputTensorInfo); |
| 288 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputData); |
| 289 | |
| 290 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 291 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 292 | |
| 293 | armnn::ScopedCpuTensorHandle weightsTensor(kernelDesc); |
| 294 | AllocateAndCopyDataToITensorHandle(&weightsTensor, &kernel[0][0][0][0]); |
| 295 | |
| 296 | armnn::ScopedCpuTensorHandle biasTensor(biasDesc); |
| 297 | |
| 298 | armnn::Convolution2dQueueDescriptor data; |
| 299 | |
| 300 | data.m_Weight = &weightsTensor; |
| 301 | data.m_Bias = &biasTensor; // Still set this whether or not bias is enabled - can be a source of bugs. |
| 302 | data.m_Parameters.m_StrideX = strideX; |
| 303 | data.m_Parameters.m_StrideY = strideY; |
| 304 | data.m_Parameters.m_PadLeft = padLeft; |
| 305 | data.m_Parameters.m_PadRight = padRight; |
| 306 | data.m_Parameters.m_PadTop = padTop; |
| 307 | data.m_Parameters.m_PadBottom = padBottom; |
| 308 | data.m_Parameters.m_BiasEnabled = biasEnabled; |
| 309 | data.m_Parameters.m_DataLayout = dataLayout; |
| 310 | |
| 311 | armnn::WorkloadInfo info; |
| 312 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 313 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 314 | |
| 315 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateConvolution2d(data, info); |
| 316 | inputHandle->Allocate(); |
| 317 | outputHandle->Allocate(); |
| 318 | |
| 319 | CopyDataToITensorHandle(inputHandle.get(), &batchedInput[0][0][0][0]); |
| 320 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 321 | ExecuteWorkload(*workload, memoryManager); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 322 | |
| 323 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 324 | |
| 325 | return ret; |
| 326 | } |
| 327 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 328 | template<armnn::DataType ArmnnType, armnn::DataType ArmnnBType, |
| 329 | typename T = armnn::ResolveType<ArmnnType>, typename B = armnn::ResolveType<ArmnnBType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 330 | LayerTestResult<T, 4> DepthwiseConvolution2dAsymmetricTestImpl( |
| 331 | armnn::IWorkloadFactory& workloadFactory, |
| 332 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 333 | const boost::multi_array<T, 4>& input, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 334 | const boost::multi_array<T, 4>& kernel, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 335 | const boost::multi_array<B, 1>& bias, |
| 336 | const boost::multi_array<T, 4>& outputExpected, |
| 337 | float qScale, |
| 338 | int32_t qOffset, |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 339 | const armnn::DataLayout layout, |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 340 | uint32_t padLeft = 0, |
| 341 | uint32_t padTop = 0, |
| 342 | uint32_t padRight = 0, |
| 343 | uint32_t padBottom = 0, |
| 344 | uint32_t strideX = 1, |
| 345 | uint32_t strideY = 1) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 346 | { |
| 347 | unsigned int inputNum = boost::numeric_cast<unsigned int>(input.shape()[0]); |
| 348 | unsigned int inputChannels = boost::numeric_cast<unsigned int>(input.shape()[1]); |
| 349 | unsigned int inputHeight = boost::numeric_cast<unsigned int>(input.shape()[2]); |
| 350 | unsigned int inputWidth = boost::numeric_cast<unsigned int>(input.shape()[3]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 351 | unsigned int kernelChanMul = boost::numeric_cast<unsigned int>(kernel.shape()[0]); |
| 352 | unsigned int kernelChannels = boost::numeric_cast<unsigned int>(kernel.shape()[1]); |
| 353 | unsigned int kernelHeight = boost::numeric_cast<unsigned int>(kernel.shape()[2]); |
| 354 | unsigned int kernelWidth = boost::numeric_cast<unsigned int>(kernel.shape()[3]); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 355 | unsigned int outputNum = boost::numeric_cast<unsigned int>(outputExpected.shape()[0]); |
| 356 | unsigned int outputChannels = boost::numeric_cast<unsigned int>(outputExpected.shape()[1]); |
| 357 | unsigned int outputHeight = boost::numeric_cast<unsigned int>(outputExpected.shape()[2]); |
| 358 | unsigned int outputWidth = boost::numeric_cast<unsigned int>(outputExpected.shape()[3]); |
| 359 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 360 | // If a bias is used, its size must equal the number of output channels. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 361 | bool biasEnabled = bias.size() > 0; |
| 362 | BOOST_ASSERT(!biasEnabled || bias.size() == outputChannels); |
| 363 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 364 | // Creates the tensors. |
Nina Drozd | d41b259 | 2018-11-19 13:03:36 +0000 | [diff] [blame] | 365 | armnn::TensorInfo inputTensorInfo = |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 366 | armnnUtils::GetTensorInfo(inputNum, inputChannels, inputHeight, inputWidth, layout, ArmnnType); |
Nina Drozd | d41b259 | 2018-11-19 13:03:36 +0000 | [diff] [blame] | 367 | armnn::TensorInfo outputTensorInfo = |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 368 | armnnUtils::GetTensorInfo(outputNum, outputChannels, outputHeight, outputWidth, layout, ArmnnType); |
| 369 | armnn::TensorInfo kernelDesc({kernelChanMul, kernelChannels, kernelHeight, kernelWidth}, ArmnnType); |
| 370 | armnn::TensorInfo biasDesc({static_cast<unsigned int>(bias.size())}, ArmnnBType); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 371 | |
| 372 | // Set quantization parameters if the requested type is a quantized type. |
| 373 | if (armnn::IsQuantizedType<T>()) |
| 374 | { |
| 375 | inputTensorInfo.SetQuantizationScale(qScale); |
| 376 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 377 | outputTensorInfo.SetQuantizationScale(qScale); |
| 378 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 379 | kernelDesc.SetQuantizationScale(qScale); |
| 380 | kernelDesc.SetQuantizationOffset(qOffset); |
| 381 | biasDesc.SetQuantizationScale(qScale*qScale); |
| 382 | biasDesc.SetQuantizationOffset(0); |
| 383 | } |
| 384 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 385 | // Construct the input data. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 386 | std::vector<T> inputData; |
| 387 | inputData.assign(input.data(), input.data() + inputChannels*inputHeight*inputWidth); |
jimfly01 | 382a91d | 2018-10-26 15:55:50 +0100 | [diff] [blame] | 388 | |
| 389 | // At this point if we require it permute the input data |
| 390 | const armnn::PermutationVector NCHWToNHWC = { 0, 3, 1, 2 }; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 391 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | 382a91d | 2018-10-26 15:55:50 +0100 | [diff] [blame] | 392 | { |
| 393 | std::vector<T> tmp(inputData.size()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 394 | armnnUtils::Permute(inputTensorInfo.GetShape(), NCHWToNHWC, inputData.data(), tmp.data(), sizeof(T)); |
jimfly01 | 382a91d | 2018-10-26 15:55:50 +0100 | [diff] [blame] | 395 | inputData = tmp; |
| 396 | } |
| 397 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 398 | auto batchedInput = MakeTensor<T, 4>(inputTensorInfo, inputData); |
| 399 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 400 | // Construct the output data, with bias applied, as appropriate. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 401 | std::vector<T> outputData; |
| 402 | outputData.assign(outputExpected.data(), outputExpected.data() + outputChannels*outputHeight*outputWidth); |
| 403 | if (biasEnabled) |
| 404 | { |
| 405 | std::vector<T> biasV; |
| 406 | biasV.assign(bias.data(), bias.data() + outputChannels); |
| 407 | ApplyBias(outputData, outputTensorInfo.GetQuantizationScale(), outputTensorInfo.GetQuantizationOffset(), |
| 408 | biasV, biasDesc.GetQuantizationScale(), biasDesc.GetQuantizationOffset(), |
| 409 | outputWidth, outputHeight); |
| 410 | } |
| 411 | |
| 412 | LayerTestResult<T, 4> ret(outputTensorInfo); |
jimfly01 | 382a91d | 2018-10-26 15:55:50 +0100 | [diff] [blame] | 413 | |
| 414 | // At this point if we require it permute the expected output |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 415 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | 382a91d | 2018-10-26 15:55:50 +0100 | [diff] [blame] | 416 | { |
| 417 | std::vector<T> tmp(outputData.size()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 418 | armnnUtils::Permute(outputTensorInfo.GetShape(), NCHWToNHWC, outputData.data(), tmp.data(), sizeof(T)); |
jimfly01 | 382a91d | 2018-10-26 15:55:50 +0100 | [diff] [blame] | 419 | outputData = tmp; |
| 420 | } |
| 421 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 422 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputData); |
| 423 | |
| 424 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 425 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 426 | |
| 427 | armnn::ScopedCpuTensorHandle weightsTensor(kernelDesc); |
jimfly01 | 382a91d | 2018-10-26 15:55:50 +0100 | [diff] [blame] | 428 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 429 | AllocateAndCopyDataToITensorHandle(&weightsTensor, &kernel[0][0][0][0]); |
| 430 | |
| 431 | armnn::ScopedCpuTensorHandle biasTensor(biasDesc); |
| 432 | if (biasEnabled) |
| 433 | { |
| 434 | AllocateAndCopyDataToITensorHandle(&biasTensor, &bias[0]); |
| 435 | } |
| 436 | |
| 437 | armnn::DepthwiseConvolution2dQueueDescriptor data; |
| 438 | data.m_Weight = &weightsTensor; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 439 | data.m_Bias = &biasTensor; // Still set this whether or not bias is enabled - it can be a source of bugs. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 440 | data.m_Parameters.m_StrideX = strideX; |
| 441 | data.m_Parameters.m_StrideY = strideY; |
| 442 | data.m_Parameters.m_PadLeft = padLeft; |
| 443 | data.m_Parameters.m_PadRight = padRight; |
| 444 | data.m_Parameters.m_PadTop = padTop; |
| 445 | data.m_Parameters.m_PadBottom = padBottom; |
| 446 | data.m_Parameters.m_BiasEnabled = biasEnabled; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 447 | data.m_Parameters.m_DataLayout = layout; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 448 | |
| 449 | armnn::WorkloadInfo info; |
| 450 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 451 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 452 | |
| 453 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateDepthwiseConvolution2d(data, info); |
| 454 | inputHandle->Allocate(); |
| 455 | outputHandle->Allocate(); |
| 456 | |
| 457 | CopyDataToITensorHandle(inputHandle.get(), &batchedInput[0][0][0][0]); |
| 458 | |
Ruomei Yan | 495852f | 2019-05-23 11:37:33 +0100 | [diff] [blame] | 459 | ExecuteWorkload(*workload, memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 460 | |
| 461 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 462 | |
| 463 | return ret; |
| 464 | } |
| 465 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 466 | template<armnn::DataType ArmnnType, armnn::DataType ArmnnBType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 467 | LayerTestResult<T, 4> DepthwiseConvolution2dDepthMul1TestImpl( |
| 468 | armnn::IWorkloadFactory& workloadFactory, |
| 469 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 470 | float qScale, |
| 471 | int32_t qOffset, |
| 472 | bool biasEnabled, |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 473 | const armnn::DataLayout layout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 474 | { |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 475 | using B = armnn::ResolveType<ArmnnBType>; |
| 476 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 477 | unsigned int inputHeight = 3; |
| 478 | unsigned int inputWidth = 3; |
| 479 | unsigned int inputChannels = 2; |
| 480 | unsigned int inputNum = 1; |
| 481 | |
| 482 | unsigned int kernelHeight = 3; |
| 483 | unsigned int kernelWidth = 3; |
| 484 | unsigned int kernelChannels = inputChannels; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 485 | unsigned int kernelDepthMultiplier = 1; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 486 | |
| 487 | unsigned int outputHeight = 1; |
| 488 | unsigned int outputWidth = 1; |
| 489 | unsigned int outputChannels = kernelChannels; |
| 490 | unsigned int outputNum = inputNum; |
| 491 | |
Nina Drozd | d41b259 | 2018-11-19 13:03:36 +0000 | [diff] [blame] | 492 | armnn::TensorInfo inputTensorInfo = |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 493 | armnnUtils::GetTensorInfo(inputNum, inputChannels, inputHeight, inputWidth, layout, ArmnnType); |
Nina Drozd | d41b259 | 2018-11-19 13:03:36 +0000 | [diff] [blame] | 494 | armnn::TensorInfo outputTensorInfo = |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 495 | armnnUtils::GetTensorInfo(outputNum, outputChannels, outputHeight, outputWidth, layout, ArmnnType); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 496 | armnn::TensorInfo kernelDesc({kernelDepthMultiplier, kernelChannels, kernelHeight, kernelWidth}, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 497 | ArmnnType); |
| 498 | armnn::TensorInfo biasDesc({ outputChannels }, ArmnnBType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 499 | |
| 500 | // Set quantization parameters if the requested type is a quantized type. |
| 501 | if(armnn::IsQuantizedType<T>()) |
| 502 | { |
| 503 | inputTensorInfo.SetQuantizationScale(qScale); |
| 504 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 505 | outputTensorInfo.SetQuantizationScale(qScale); |
| 506 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 507 | kernelDesc.SetQuantizationScale(qScale); |
| 508 | kernelDesc.SetQuantizationOffset(qOffset); |
| 509 | biasDesc.SetQuantizationScale(qScale*qScale); |
| 510 | biasDesc.SetQuantizationOffset(0); |
| 511 | } |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 512 | std::vector<T> inputData = std::vector<T>( |
| 513 | QuantizedVector<T>(inputTensorInfo.GetQuantizationScale(), inputTensorInfo.GetQuantizationOffset(), { |
| 514 | 1.f, 2.f, 1.f, |
| 515 | 2.f, 1.f, 2.f, |
| 516 | 1.f, 2.f, 1.f, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 517 | |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 518 | 1.f, 2.f, 1.f, |
| 519 | 2.f, 1.f, 2.f, |
| 520 | 1.f, 2.f, 1.f, |
| 521 | })); |
| 522 | // at this point if we require it permute the input data |
| 523 | const armnn::PermutationVector NCHWToNHWC = { 0, 3, 1, 2 }; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 524 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 525 | { |
| 526 | std::vector<T> tmp(inputData.size()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 527 | armnnUtils::Permute(inputTensorInfo.GetShape(), NCHWToNHWC, inputData.data(), tmp.data(), sizeof(T)); |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 528 | inputData = tmp; |
| 529 | } |
| 530 | auto input = MakeTensor<T, 4>(inputTensorInfo, inputData); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 531 | |
| 532 | std::vector<B> biasV(QuantizedVector<B>(biasDesc.GetQuantizationScale(), biasDesc.GetQuantizationOffset(), |
| 533 | {0, 2})); |
| 534 | auto bias = MakeTensor<B, 1>(biasDesc, biasV); |
| 535 | |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 536 | std::vector<T> kernelData = std::vector<T>( |
| 537 | QuantizedVector<T>(kernelDesc.GetQuantizationScale(), kernelDesc.GetQuantizationOffset(), { |
| 538 | 1.f, 0.f, 1.f, |
| 539 | 0.f, 0.f, 0.f, |
| 540 | -1.f, 0.f, -1.f, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 541 | |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 542 | 1.f, 0.f, 1.f, |
| 543 | 0.f, 0.f, 0.f, |
| 544 | -1.f, 0.f, -1.f, |
| 545 | })); |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 546 | auto kernel = MakeTensor<T, 4>(kernelDesc, kernelData); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 547 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 548 | // Manually calculated. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 549 | std::vector<T> outputImage( |
| 550 | QuantizedVector<T>(outputTensorInfo.GetQuantizationScale(), |
| 551 | outputTensorInfo.GetQuantizationOffset(), |
| 552 | {0.f, 0.f}) |
| 553 | ); |
| 554 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 555 | // Optionally apply bias to output image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 556 | if(biasEnabled) |
| 557 | { |
| 558 | ApplyBias(outputImage, outputTensorInfo.GetQuantizationScale(), outputTensorInfo.GetQuantizationOffset(), |
| 559 | biasV, biasDesc.GetQuantizationScale(), biasDesc.GetQuantizationOffset(), |
| 560 | outputWidth, outputHeight); |
| 561 | } |
| 562 | |
| 563 | LayerTestResult<T, 4> ret(outputTensorInfo); |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 564 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 565 | { |
| 566 | std::vector<T> tmp(outputImage.size()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 567 | armnnUtils::Permute(outputTensorInfo.GetShape(), NCHWToNHWC, outputImage.data(), tmp.data(), sizeof(T)); |
jimfly01 | b9c8963 | 2018-10-26 16:50:13 +0100 | [diff] [blame] | 568 | outputImage = tmp; |
| 569 | } |
| 570 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 571 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputImage); |
| 572 | |
| 573 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 574 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 575 | |
| 576 | armnn::DepthwiseConvolution2dQueueDescriptor data; |
| 577 | armnn::WorkloadInfo info; |
| 578 | armnn::ScopedCpuTensorHandle weightsTensor(kernelDesc); |
| 579 | armnn::ScopedCpuTensorHandle biasTensor(biasDesc); |
| 580 | |
| 581 | AllocateAndCopyDataToITensorHandle(&weightsTensor, &kernel[0][0][0][0]); |
| 582 | AllocateAndCopyDataToITensorHandle(&biasTensor, &bias[0]); |
| 583 | |
| 584 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 585 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 586 | |
| 587 | data.m_Weight = &weightsTensor; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 588 | data.m_Bias = &biasTensor; // Still set this whether or not bias is enabled. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 589 | data.m_Parameters.m_StrideX = 1; |
| 590 | data.m_Parameters.m_StrideY = 1; |
| 591 | data.m_Parameters.m_PadLeft = 0; |
| 592 | data.m_Parameters.m_PadRight = 0; |
| 593 | data.m_Parameters.m_PadTop = 0; |
| 594 | data.m_Parameters.m_PadBottom = 0; |
| 595 | data.m_Parameters.m_BiasEnabled = biasEnabled; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 596 | data.m_Parameters.m_DataLayout = layout; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 597 | |
| 598 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateDepthwiseConvolution2d(data, info); |
| 599 | inputHandle->Allocate(); |
| 600 | outputHandle->Allocate(); |
| 601 | |
| 602 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 603 | |
Ruomei Yan | 495852f | 2019-05-23 11:37:33 +0100 | [diff] [blame] | 604 | ExecuteWorkload(*workload, memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 605 | |
| 606 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 607 | |
| 608 | return ret; |
| 609 | } |
| 610 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 611 | template<armnn::DataType ArmnnType, armnn::DataType ArmnnBType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 612 | LayerTestResult<T, 4> DepthwiseConvolution2dTestImpl( |
| 613 | armnn::IWorkloadFactory& workloadFactory, |
| 614 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 615 | float qScale, |
| 616 | int32_t qOffset, |
| 617 | bool biasEnabled, |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 618 | const armnn::DataLayout layout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 619 | { |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 620 | using B = armnn::ResolveType<ArmnnBType>; |
| 621 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 622 | unsigned int depthMultiplier = 2; |
| 623 | |
| 624 | unsigned int inputHeight = 8; |
| 625 | unsigned int inputWidth = 16; |
| 626 | unsigned int inputChannels = 2; |
| 627 | unsigned int inputBatchSize = 1; |
| 628 | |
| 629 | unsigned int kernelHeight = 5; |
| 630 | unsigned int kernelWidth = 3; |
| 631 | |
| 632 | unsigned int outputHeight = inputHeight - kernelHeight + 1 + 2; |
| 633 | unsigned int outputWidth = (inputWidth - kernelWidth + 1)/2; |
| 634 | unsigned int outputChannels = inputChannels * depthMultiplier; |
| 635 | unsigned int outputBatchSize = inputBatchSize; |
| 636 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 637 | armnn::TensorInfo inputTensorInfo = armnnUtils::GetTensorInfo( |
| 638 | inputBatchSize, inputChannels, inputHeight, inputWidth, layout, ArmnnType); |
| 639 | armnn::TensorInfo outputTensorInfo = armnnUtils::GetTensorInfo( |
| 640 | outputBatchSize, outputChannels, outputHeight, outputWidth, layout, ArmnnType); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 641 | armnn::TensorInfo kernelDesc({depthMultiplier, inputChannels, kernelHeight, kernelWidth}, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 642 | ArmnnType); |
| 643 | armnn::TensorInfo biasDesc({outputChannels}, ArmnnBType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 644 | |
| 645 | // Set quantization parameters if the requested type is a quantized type. |
| 646 | if(armnn::IsQuantizedType<T>()) |
| 647 | { |
| 648 | inputTensorInfo.SetQuantizationScale(qScale); |
| 649 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 650 | outputTensorInfo.SetQuantizationScale(qScale); |
| 651 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 652 | kernelDesc.SetQuantizationScale(qScale); |
| 653 | kernelDesc.SetQuantizationOffset(qOffset); |
| 654 | biasDesc.SetQuantizationScale(qScale*qScale); |
| 655 | biasDesc.SetQuantizationOffset(0); |
| 656 | } |
| 657 | |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 658 | // NOTE: originalInputData is in NCHW format |
| 659 | std::vector<T> originalInputData = std::vector<T>( |
| 660 | QuantizedVector<T>(inputTensorInfo.GetQuantizationScale(), inputTensorInfo.GetQuantizationOffset(), { |
| 661 | 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, |
| 662 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 663 | 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, |
| 664 | 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, |
| 665 | 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, |
| 666 | 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, |
| 667 | 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, |
| 668 | 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, |
| 669 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 670 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 671 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 672 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 673 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 674 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 675 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 676 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 677 | })); |
| 678 | std::vector<T> inputData = originalInputData; |
| 679 | // at this point if we require it permute the input data |
| 680 | const armnn::PermutationVector NCHWToNHWC = { 0, 3, 1, 2 }; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 681 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 682 | { |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 683 | armnnUtils::Permute(inputTensorInfo.GetShape(), NCHWToNHWC, |
| 684 | originalInputData.data(), inputData.data(), sizeof(T)); |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 685 | } |
| 686 | auto input = MakeTensor<T, 4>(inputTensorInfo, inputData); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 687 | |
| 688 | std::vector<B> biasV(QuantizedVector<B>(biasDesc.GetQuantizationScale(), biasDesc.GetQuantizationOffset(), |
| 689 | {0, 2, 1, -1})); |
| 690 | auto bias = MakeTensor<B, 1>(biasDesc, biasV); |
| 691 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 692 | std::vector<T> kernelData = std::vector<T>( |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 693 | QuantizedVector<T>(kernelDesc.GetQuantizationScale(), kernelDesc.GetQuantizationOffset(), { |
| 694 | 1, 1, 1, |
| 695 | 1, -1, 1, |
| 696 | 1, 1, 1, |
| 697 | 1, 1, 1, |
| 698 | 1, 1, 1, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 699 | |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 700 | 2, 2, 2, |
| 701 | 2, 2, 2, |
| 702 | 2, 2, 2, |
| 703 | 2, 2, 2, |
| 704 | 2, 2, 2, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 705 | |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 706 | 0, 0, 0, |
| 707 | 0, -1, 0, |
| 708 | 0, 0, 0, |
| 709 | 0, 0, 0, |
| 710 | 0, 0, 0, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 711 | |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 712 | 0, 0, 0, |
| 713 | 0, 0, 0, |
| 714 | 0, 1, 0, |
| 715 | 0, 0, 0, |
| 716 | 0, 0, 0 |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 717 | |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 718 | })); |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 719 | auto kernel = MakeTensor<T, 4>(kernelDesc, kernelData); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 720 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 721 | // Manually calculated. |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 722 | std::vector<T> originalOutputImage = std::vector<T>( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 723 | QuantizedVector<T>(outputTensorInfo.GetQuantizationScale(), outputTensorInfo.GetQuantizationOffset(), { |
| 724 | 3.5f, 3.5f, 3.5f, 3.5f, 3.5f, 3.5f, 3.5f, |
| 725 | 6.0f, 6.0f, 6.0f, 6.0f, 6.0f, 6.0f, 6.0f, |
| 726 | 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, |
| 727 | 6.5f, 6.5f, 6.5f, 6.5f, 6.5f, 6.5f, 6.5f, |
| 728 | 6.5f, 6.5f, 6.5f, 6.5f, 6.5f, 6.5f, 6.5f, |
| 729 | 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, 5.0f, |
| 730 | |
| 731 | -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, |
| 732 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 733 | -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, |
| 734 | -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, |
| 735 | -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, |
| 736 | -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, |
| 737 | |
| 738 | 8.0f, 8.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 739 | 10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 740 | 10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 741 | 10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 742 | 10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 743 | 8.0f, 8.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 744 | |
| 745 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 746 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 747 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 748 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 749 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 750 | 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f |
| 751 | })); |
| 752 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 753 | // Optionally apply bias to output image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 754 | if(biasEnabled) |
| 755 | { |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 756 | ApplyBias(originalOutputImage, |
| 757 | outputTensorInfo.GetQuantizationScale(), |
| 758 | outputTensorInfo.GetQuantizationOffset(), |
| 759 | biasV, |
| 760 | biasDesc.GetQuantizationScale(), |
| 761 | biasDesc.GetQuantizationOffset(), |
| 762 | outputWidth, |
| 763 | outputHeight); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | LayerTestResult<T, 4> ret(outputTensorInfo); |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 767 | std::vector<T> outputImage = originalOutputImage; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 768 | if (layout == armnn::DataLayout::NHWC) |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 769 | { |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 770 | armnnUtils::Permute(outputTensorInfo.GetShape(), NCHWToNHWC, |
| 771 | originalOutputImage.data(), outputImage.data(), sizeof(T)); |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 772 | } |
| 773 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 774 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputImage); |
| 775 | |
| 776 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 777 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 778 | |
| 779 | armnn::DepthwiseConvolution2dQueueDescriptor data; |
| 780 | armnn::WorkloadInfo info; |
| 781 | armnn::ScopedCpuTensorHandle weightsTensor(kernelDesc); |
| 782 | armnn::ScopedCpuTensorHandle biasTensor(biasDesc); |
| 783 | |
| 784 | AllocateAndCopyDataToITensorHandle(&weightsTensor, &kernel[0][0][0][0]); |
| 785 | AllocateAndCopyDataToITensorHandle(&biasTensor, &bias[0]); |
| 786 | |
| 787 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 788 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 789 | |
| 790 | data.m_Weight = &weightsTensor; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 791 | data.m_Bias = &biasTensor; // Still set this whether or not bias is enabled. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 792 | data.m_Parameters.m_StrideX = 2; |
| 793 | data.m_Parameters.m_StrideY = 1; |
| 794 | data.m_Parameters.m_PadLeft = 0; |
| 795 | data.m_Parameters.m_PadRight = 0; |
| 796 | data.m_Parameters.m_PadTop = 1; |
| 797 | data.m_Parameters.m_PadBottom = 1; |
| 798 | data.m_Parameters.m_BiasEnabled = biasEnabled; |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 799 | data.m_Parameters.m_DataLayout = layout; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 800 | |
| 801 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateDepthwiseConvolution2d(data, info); |
| 802 | inputHandle->Allocate(); |
| 803 | outputHandle->Allocate(); |
| 804 | |
| 805 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 806 | |
Ruomei Yan | 495852f | 2019-05-23 11:37:33 +0100 | [diff] [blame] | 807 | ExecuteWorkload(*workload, memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 808 | |
| 809 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 810 | |
| 811 | return ret; |
| 812 | } |
| 813 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 814 | template<armnn::DataType ArmnnType, armnn::DataType ArmnnBType, |
| 815 | typename T = armnn::ResolveType<ArmnnType>, typename B = armnn::ResolveType<ArmnnBType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 816 | LayerTestResult<T, 4> DepthwiseConvolution2dNhwcTestImpl( |
| 817 | armnn::IWorkloadFactory& workloadFactory, |
| 818 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 819 | const boost::multi_array<T, 4>& input, |
| 820 | const boost::multi_array<T, 4>& kernel, |
| 821 | const boost::multi_array<B, 1>& bias, |
| 822 | const boost::multi_array<T, 4>& outputExpected, |
| 823 | float qScale, |
| 824 | int32_t qOffset, |
| 825 | uint32_t padLeft = 0, |
| 826 | uint32_t padTop = 0, |
| 827 | uint32_t padRight = 0, |
| 828 | uint32_t padBottom = 0, |
| 829 | uint32_t strideX = 1, |
Bruno Goncalves | 22972f0 | 2019-04-26 21:03:24 -0300 | [diff] [blame] | 830 | uint32_t strideY = 1, |
| 831 | uint32_t dilationX = 1, |
| 832 | uint32_t dilationY = 1) |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 833 | { |
| 834 | unsigned int inputNum = boost::numeric_cast<unsigned int>(input.shape()[0]); |
| 835 | unsigned int inputChannels = boost::numeric_cast<unsigned int>(input.shape()[3]); |
| 836 | unsigned int inputHeight = boost::numeric_cast<unsigned int>(input.shape()[1]); |
| 837 | unsigned int inputWidth = boost::numeric_cast<unsigned int>(input.shape()[2]); |
| 838 | |
| 839 | unsigned int kernelChanMul = boost::numeric_cast<unsigned int>(kernel.shape()[0]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 840 | unsigned int kernelChannels = boost::numeric_cast<unsigned int>(kernel.shape()[1]); |
| 841 | unsigned int kernelHeight = boost::numeric_cast<unsigned int>(kernel.shape()[2]); |
| 842 | unsigned int kernelWidth = boost::numeric_cast<unsigned int>(kernel.shape()[3]); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 843 | |
| 844 | unsigned int outputNum = boost::numeric_cast<unsigned int>(outputExpected.shape()[0]); |
| 845 | unsigned int outputChannels = boost::numeric_cast<unsigned int>(outputExpected.shape()[3]); |
| 846 | unsigned int outputHeight = boost::numeric_cast<unsigned int>(outputExpected.shape()[1]); |
| 847 | unsigned int outputWidth = boost::numeric_cast<unsigned int>(outputExpected.shape()[2]); |
| 848 | |
| 849 | // Creates the tensors. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 850 | armnn::TensorInfo inputTensorInfo({inputNum, inputHeight, inputWidth, inputChannels}, ArmnnType); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 851 | armnn::TensorInfo outputTensorInfo({outputNum, outputHeight, outputWidth, outputChannels}, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 852 | ArmnnType); |
| 853 | armnn::TensorInfo kernelDesc({kernelChanMul, kernelChannels, kernelHeight, kernelWidth}, ArmnnType); |
| 854 | armnn::TensorInfo biasDesc({static_cast<unsigned int>(bias.size())}, ArmnnBType); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 855 | |
| 856 | // Set quantization parameters if the requested type is a quantized type. |
| 857 | if (armnn::IsQuantizedType<T>()) |
| 858 | { |
| 859 | inputTensorInfo.SetQuantizationScale(qScale); |
| 860 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 861 | outputTensorInfo.SetQuantizationScale(qScale); |
| 862 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 863 | kernelDesc.SetQuantizationScale(qScale); |
| 864 | kernelDesc.SetQuantizationOffset(qOffset); |
| 865 | biasDesc.SetQuantizationScale(qScale*qScale); |
| 866 | biasDesc.SetQuantizationOffset(0); |
| 867 | } |
| 868 | |
| 869 | // Construct the input data. |
| 870 | std::vector<T> inputData; |
| 871 | inputData.assign(input.data(), input.data() + inputHeight*inputWidth*inputChannels); |
| 872 | auto batchedInput = MakeTensor<T, 4>(inputTensorInfo, inputData); |
| 873 | |
| 874 | // Construct the output data, with bias applied, as appropriate. |
| 875 | std::vector<T> outputData; |
| 876 | outputData.assign(outputExpected.data(), outputExpected.data() + outputHeight*outputWidth*outputChannels); |
| 877 | |
| 878 | LayerTestResult<T, 4> ret(outputTensorInfo); |
| 879 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, outputData); |
| 880 | |
| 881 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 882 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 883 | |
| 884 | armnn::ScopedCpuTensorHandle weightsTensor(kernelDesc); |
| 885 | AllocateAndCopyDataToITensorHandle(&weightsTensor, &kernel[0][0][0][0]); |
| 886 | |
| 887 | armnn::ScopedCpuTensorHandle biasTensor(biasDesc); |
| 888 | |
| 889 | armnn::DepthwiseConvolution2dQueueDescriptor data; |
| 890 | data.m_Weight = &weightsTensor; |
| 891 | data.m_Bias = &biasTensor; // Still set this whether or not bias is enabled - it can be a source of bugs. |
| 892 | data.m_Parameters.m_StrideX = strideX; |
| 893 | data.m_Parameters.m_StrideY = strideY; |
| 894 | data.m_Parameters.m_PadLeft = padLeft; |
| 895 | data.m_Parameters.m_PadRight = padRight; |
| 896 | data.m_Parameters.m_PadTop = padTop; |
| 897 | data.m_Parameters.m_PadBottom = padBottom; |
| 898 | data.m_Parameters.m_DataLayout = armnn::DataLayout::NHWC; |
Bruno Goncalves | 22972f0 | 2019-04-26 21:03:24 -0300 | [diff] [blame] | 899 | data.m_Parameters.m_DilationX = dilationX; |
| 900 | data.m_Parameters.m_DilationY = dilationY; |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 901 | |
| 902 | armnn::WorkloadInfo info; |
| 903 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 904 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 905 | |
| 906 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateDepthwiseConvolution2d(data, info); |
| 907 | |
| 908 | inputHandle->Allocate(); |
| 909 | outputHandle->Allocate(); |
| 910 | |
| 911 | CopyDataToITensorHandle(inputHandle.get(), &batchedInput[0][0][0][0]); |
| 912 | |
Ruomei Yan | 495852f | 2019-05-23 11:37:33 +0100 | [diff] [blame] | 913 | ExecuteWorkload(*workload, memoryManager); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 914 | |
| 915 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 916 | |
| 917 | return ret; |
| 918 | } |
| 919 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 920 | template<armnn::DataType ArmnnType, armnn::DataType ArmnnBType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 921 | LayerTestResult<T,4> Convolution1dTestImpl( |
| 922 | armnn::IWorkloadFactory& workloadFactory, |
| 923 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 924 | float qScale, |
| 925 | int32_t qOffset, |
| 926 | bool biasEnabled) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 927 | { |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 928 | using B = armnn::ResolveType<ArmnnBType>; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 929 | // Until we have a specialist 1D convolution layer, we can fake one using |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 930 | // 2D convolution with the final dimension set to 1. |
| 931 | // I don't anticipate this being particularly slow, given that convolution is implemented |
| 932 | // as a matrix multiplication, at which point dimension doesn't matter. |
| 933 | |
| 934 | unsigned int batchSize = 1; |
| 935 | unsigned int inputChannels = 2; |
| 936 | unsigned int outputChannels = 3; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 937 | unsigned int inputSize = 5; // The 1D size (could view as 'width' or 'height'). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 938 | unsigned int kernelSize = 3; |
| 939 | unsigned int padSize = 2; |
| 940 | unsigned int stride = 1; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 941 | unsigned int outputSize = 7; // (inputSize + 2 * padSize - kernelSize + 1) / stride. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 942 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 943 | armnn::TensorInfo inputInfo({batchSize, inputChannels, inputSize, 1}, ArmnnType); |
| 944 | armnn::TensorInfo outputInfo({batchSize, outputChannels, outputSize, 1}, ArmnnType); |
| 945 | armnn::TensorInfo kernelInfo({outputChannels, inputChannels, kernelSize, 1}, ArmnnType); |
| 946 | armnn::TensorInfo biasInfo({outputChannels}, ArmnnBType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 947 | |
| 948 | // Set quantization parameters if the requested type is a quantized type. |
| 949 | if(armnn::IsQuantizedType<T>()) |
| 950 | { |
| 951 | inputInfo.SetQuantizationScale(qScale); |
| 952 | inputInfo.SetQuantizationOffset(qOffset); |
| 953 | outputInfo.SetQuantizationScale(qScale); |
| 954 | outputInfo.SetQuantizationOffset(qOffset); |
| 955 | kernelInfo.SetQuantizationScale(qScale); |
| 956 | kernelInfo.SetQuantizationOffset(qOffset); |
| 957 | biasInfo.SetQuantizationScale(inputInfo.GetQuantizationScale()*kernelInfo.GetQuantizationScale()); |
| 958 | biasInfo.SetQuantizationOffset(0); |
| 959 | } |
| 960 | |
| 961 | std::vector<T> inputData( |
| 962 | QuantizedVector<T>(inputInfo.GetQuantizationScale(), inputInfo.GetQuantizationOffset(), { |
| 963 | 5.0f, -2.0f, 2.5f, 0.0f, 1.0f, |
| 964 | -3.0f, 3.2f, 5.0f, 2.0f, 3.0f, |
| 965 | })); |
| 966 | |
| 967 | std::vector<T> kernelData( |
| 968 | QuantizedVector<T>(kernelInfo.GetQuantizationScale(), kernelInfo.GetQuantizationOffset(), { |
| 969 | 1.0f, 0.0f, 0.0f, |
| 970 | 0.0f, 2.0f, -1.5f, |
| 971 | |
| 972 | 0.0f, 0.0f, 0.0f, |
| 973 | 0.2f, 0.2f, 0.2f, |
| 974 | |
| 975 | 0.5f, 0.0f, 0.5f, |
| 976 | 0.0f, -1.0f, 0.0f |
| 977 | })); |
| 978 | |
| 979 | std::vector<B> biasData( |
| 980 | QuantizedVector<B>(biasInfo.GetQuantizationScale(), biasInfo.GetQuantizationOffset(), { |
| 981 | 1.0f, 0.0f, 0.0f |
| 982 | })); |
| 983 | |
| 984 | std::vector<T> outputData( |
| 985 | QuantizedVector<T>(outputInfo.GetQuantizationScale(), outputInfo.GetQuantizationOffset(), { |
| 986 | 4.5f, -10.8f, 5.0f + 6.4f - 7.5f, -2.0f + 10.0f -3.0f, 2.5f + 4.0f - 4.5f, 6.0f, 1.0f, |
| 987 | -0.6f, -0.6f + 0.64f, -0.6f + 0.64f + 1.0f, 0.64f + 1.0f + 0.4f, 1.0f + 0.4f + 0.6f, 0.4f + 0.6f, 0.6f, |
| 988 | 2.5f, -1.0f + 3.0f, 1.25f - 3.2f + 2.5f, -1.0f - 5.0f, 1.25f + 0.5f - 2.0f, -3.0f, 0.5f |
| 989 | })); |
| 990 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 991 | // Optionally apply bias to output image. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 992 | if(biasEnabled) |
| 993 | { |
| 994 | ApplyBias(outputData, outputInfo.GetQuantizationScale(), outputInfo.GetQuantizationOffset(), |
| 995 | biasData, biasInfo.GetQuantizationScale(), biasInfo.GetQuantizationOffset(), |
| 996 | 1, outputSize); |
| 997 | } |
| 998 | |
| 999 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputInfo); |
| 1000 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputInfo); |
| 1001 | |
| 1002 | armnn::Convolution2dQueueDescriptor data; |
| 1003 | armnn::WorkloadInfo info; |
| 1004 | armnn::ScopedCpuTensorHandle weightsTensor(kernelInfo); |
| 1005 | armnn::ScopedCpuTensorHandle biasTensor(biasInfo); |
| 1006 | |
| 1007 | AllocateAndCopyDataToITensorHandle(&weightsTensor, kernelData.data()); |
| 1008 | AllocateAndCopyDataToITensorHandle(&biasTensor, biasData.data()); |
| 1009 | |
| 1010 | AddInputToWorkload(data, info, inputInfo, inputHandle.get()); |
| 1011 | AddOutputToWorkload(data, info, outputInfo, outputHandle.get()); |
| 1012 | |
| 1013 | data.m_Weight = &weightsTensor; |
| 1014 | data.m_Bias = &biasTensor; |
| 1015 | data.m_Parameters.m_StrideX = 1; |
| 1016 | data.m_Parameters.m_StrideY = stride; |
| 1017 | data.m_Parameters.m_PadLeft = 0; |
| 1018 | data.m_Parameters.m_PadRight = 0; |
| 1019 | data.m_Parameters.m_PadTop = padSize; |
| 1020 | data.m_Parameters.m_PadBottom = padSize; |
| 1021 | data.m_Parameters.m_BiasEnabled = biasEnabled; |
| 1022 | |
| 1023 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateConvolution2d(data, info); |
| 1024 | inputHandle->Allocate(); |
| 1025 | outputHandle->Allocate(); |
| 1026 | |
| 1027 | CopyDataToITensorHandle(inputHandle.get(), inputData.data()); |
| 1028 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1029 | ExecuteWorkload(*workload, memoryManager); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1030 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1031 | // Output |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1032 | LayerTestResult<T,4> ret(outputInfo); |
| 1033 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 1034 | ret.outputExpected = MakeTensor<T, 4>(outputInfo, outputData); |
| 1035 | return ret; |
| 1036 | } |
| 1037 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 1038 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1039 | LayerTestResult<T,4> CompareConvolution2dTestImpl( |
| 1040 | armnn::IWorkloadFactory& workloadFactory, |
| 1041 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1042 | armnn::IWorkloadFactory& refWorkloadFactory) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1043 | { |
| 1044 | unsigned int inputHeight = 8; |
| 1045 | unsigned int inputWidth = 16; |
| 1046 | unsigned int inputChannels = 3; |
| 1047 | unsigned int inputNum = 5; |
| 1048 | |
| 1049 | unsigned int kernelHeight = 3; |
| 1050 | unsigned int kernelWidth = 3; |
| 1051 | |
| 1052 | unsigned int strideX = 2; |
| 1053 | unsigned int strideY = 3; |
| 1054 | unsigned int padX = 1; |
| 1055 | unsigned int padY = 1; |
| 1056 | |
| 1057 | unsigned int outputNum = inputNum; |
| 1058 | unsigned int outputChannels = 2; |
| 1059 | unsigned int outputHeight = (inputHeight + 2 * padY - kernelHeight + strideY) / strideY; |
| 1060 | unsigned int outputWidth = (inputWidth + 2 * padX - kernelWidth + strideX) / strideX; |
| 1061 | |
| 1062 | armnn::TensorInfo inputTensorInfo; |
| 1063 | armnn::TensorInfo outputTensorInfo; |
| 1064 | armnn::TensorInfo kernelDesc; |
| 1065 | armnn::TensorInfo biasDesc; |
| 1066 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1067 | unsigned int inputShape[] = {inputNum, inputChannels, inputHeight, inputWidth}; |
| 1068 | unsigned int outputShape[] = {outputNum, outputChannels, outputHeight, outputWidth}; |
| 1069 | unsigned int kernelShape[] = {outputChannels, inputChannels, kernelHeight, kernelWidth}; |
| 1070 | unsigned int biasShape[] = {outputChannels}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1071 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 1072 | inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType); |
| 1073 | outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType); |
| 1074 | kernelDesc = armnn::TensorInfo(4, kernelShape, ArmnnType); |
| 1075 | biasDesc = armnn::TensorInfo(1, biasShape, ArmnnType); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1076 | |
| 1077 | LayerTestResult<T,4> ret(outputTensorInfo); |
| 1078 | |
| 1079 | auto input = MakeRandomTensor<T, 4>(inputTensorInfo, 124908); |
| 1080 | auto kernel = MakeRandomTensor<T, 4>(kernelDesc, 891234); |
| 1081 | auto bias = MakeRandomTensor<T, 1>(biasDesc, 1028); |
| 1082 | |
| 1083 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 1084 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 1085 | |
| 1086 | armnn::Convolution2dQueueDescriptor data; |
| 1087 | armnn::WorkloadInfo info; |
| 1088 | armnn::ScopedCpuTensorHandle weightsTensor(kernelDesc); |
| 1089 | armnn::ScopedCpuTensorHandle biasTensor(biasDesc); |
| 1090 | |
| 1091 | AllocateAndCopyDataToITensorHandle(&weightsTensor, &kernel[0][0][0][0]); |
| 1092 | AllocateAndCopyDataToITensorHandle(&biasTensor, &bias[0]); |
| 1093 | |
| 1094 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 1095 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 1096 | data.m_Weight = &weightsTensor; |
| 1097 | data.m_Bias = &biasTensor; |
| 1098 | data.m_Parameters.m_StrideX = strideX; |
| 1099 | data.m_Parameters.m_StrideY = strideY; |
| 1100 | data.m_Parameters.m_PadLeft = padX; |
| 1101 | data.m_Parameters.m_PadRight = padX; |
| 1102 | data.m_Parameters.m_PadTop = padY; |
| 1103 | data.m_Parameters.m_PadBottom = padY; |
| 1104 | data.m_Parameters.m_BiasEnabled = true; |
| 1105 | |
| 1106 | std::unique_ptr<armnn::ITensorHandle> outputHandleRef = refWorkloadFactory.CreateTensorHandle(outputTensorInfo); |
| 1107 | std::unique_ptr<armnn::ITensorHandle> inputHandleRef = refWorkloadFactory.CreateTensorHandle(inputTensorInfo); |
| 1108 | |
| 1109 | armnn::Convolution2dQueueDescriptor refData = data; |
| 1110 | armnn::WorkloadInfo refInfo = info; |
| 1111 | SetWorkloadInput(refData, refInfo, 0, inputTensorInfo, inputHandleRef.get()); |
| 1112 | SetWorkloadOutput(refData, refInfo, 0, outputTensorInfo, outputHandleRef.get()); |
| 1113 | |
| 1114 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateConvolution2d(data, info); |
| 1115 | std::unique_ptr<armnn::IWorkload> workloadRef = refWorkloadFactory.CreateConvolution2d(refData, refInfo); |
| 1116 | |
| 1117 | outputHandleRef->Allocate(); |
| 1118 | inputHandleRef->Allocate(); |
| 1119 | |
| 1120 | inputHandle->Allocate(); |
| 1121 | outputHandle->Allocate(); |
| 1122 | |
| 1123 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 1124 | CopyDataToITensorHandle(inputHandleRef.get(), &input[0][0][0][0]); |
| 1125 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1126 | ExecuteWorkload(*workload, memoryManager); |
Aron Virginas-Tar | 6057895 | 2018-10-31 11:04:01 +0000 | [diff] [blame] | 1127 | |
Mike Kelly | 9b39832 | 2019-05-22 17:21:49 +0100 | [diff] [blame] | 1128 | workloadRef->PostAllocationConfigure(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1129 | workloadRef->Execute(); |
| 1130 | |
| 1131 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 1132 | CopyDataFromITensorHandle(&ret.outputExpected[0][0][0][0], outputHandleRef.get()); |
| 1133 | |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 1137 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1138 | LayerTestResult<T, 4> CompareDepthwiseConvolution2dTestImpl( |
| 1139 | armnn::IWorkloadFactory& workloadFactory, |
| 1140 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 1141 | armnn::IWorkloadFactory& refWorkloadFactory, |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 1142 | const armnnUtils::DataLayoutIndexed& layout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1143 | { |
| 1144 | unsigned int inputHeight = 8; |
| 1145 | unsigned int inputWidth = 16; |
| 1146 | unsigned int inputChannels = 3; |
| 1147 | unsigned int inputNum = 5; |
| 1148 | |
| 1149 | unsigned int kernelHeight = 3; |
| 1150 | unsigned int kernelWidth = 3; |
| 1151 | unsigned int channelMultiplier = 1; |
| 1152 | |
| 1153 | unsigned int strideX = 2; |
| 1154 | unsigned int strideY = 3; |
| 1155 | unsigned int padX = 1; |
| 1156 | unsigned int padY = 1; |
| 1157 | |
| 1158 | unsigned int outputNum = inputNum; |
| 1159 | unsigned int outputChannels = inputChannels * channelMultiplier; |
| 1160 | unsigned int outputHeight = (inputHeight + 2 * padY - kernelHeight + strideY) / strideY; |
| 1161 | unsigned int outputWidth = (inputWidth + 2 * padX - kernelWidth + strideX) / strideX; |
| 1162 | |
| 1163 | armnn::TensorInfo inputTensorInfo; |
| 1164 | armnn::TensorInfo outputTensorInfo; |
| 1165 | armnn::TensorInfo kernelDesc; |
| 1166 | armnn::TensorInfo biasDesc; |
| 1167 | |
jimfly01 | 7af00da | 2018-10-31 14:43:53 +0000 | [diff] [blame] | 1168 | |
| 1169 | std::vector<unsigned int> inputShape; |
| 1170 | std::vector<unsigned int> outputShape; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1171 | std::vector<unsigned int> kernelShape{ channelMultiplier, inputChannels, kernelHeight, kernelWidth }; |
| 1172 | std::vector<unsigned int> biasShape{ outputChannels }; |
jimfly01 | 7af00da | 2018-10-31 14:43:53 +0000 | [diff] [blame] | 1173 | switch (layout.GetDataLayout()) |
| 1174 | { |
| 1175 | case armnn::DataLayout::NCHW: |
| 1176 | inputShape = { inputNum, inputChannels, inputHeight, inputWidth }; |
| 1177 | outputShape = { outputNum, outputChannels, outputHeight, outputWidth }; |
jimfly01 | 7af00da | 2018-10-31 14:43:53 +0000 | [diff] [blame] | 1178 | break; |
| 1179 | case armnn::DataLayout ::NHWC: |
| 1180 | inputShape = { inputNum, inputHeight, inputWidth, inputChannels }; |
| 1181 | outputShape = { outputNum, outputHeight, outputWidth, outputChannels }; |
jimfly01 | 7af00da | 2018-10-31 14:43:53 +0000 | [diff] [blame] | 1182 | break; |
| 1183 | default: |
| 1184 | throw armnn::InvalidArgumentException("unknown data layout [" |
| 1185 | + std::to_string(static_cast<int>(layout.GetDataLayout())) + "]"); |
| 1186 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1187 | |
| 1188 | float inputsQScale = armnn::IsQuantizedType<T>() ? 1.0f : 0; |
| 1189 | float outputQScale = armnn::IsQuantizedType<T>() ? 2.0f : 0; |
| 1190 | int32_t qOffset = 0; |
| 1191 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 1192 | inputTensorInfo = armnn::TensorInfo(4, inputShape.data(), ArmnnType, inputsQScale, qOffset); |
| 1193 | outputTensorInfo = armnn::TensorInfo(4, outputShape.data(), ArmnnType, outputQScale, qOffset); |
| 1194 | kernelDesc = armnn::TensorInfo(4, kernelShape.data(), ArmnnType, inputsQScale, qOffset); |
jimfly01 | 7af00da | 2018-10-31 14:43:53 +0000 | [diff] [blame] | 1195 | biasDesc = armnn::TensorInfo( |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 1196 | 1, biasShape.data(), armnn::GetBiasDataType(ArmnnType), inputsQScale, qOffset); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1197 | |
| 1198 | LayerTestResult<T, 4> ret(outputTensorInfo); |
| 1199 | |
| 1200 | auto input = MakeRandomTensor<T, 4>(inputTensorInfo, 124908, 0.0f, 255.0f); |
| 1201 | auto kernel = MakeRandomTensor<T, 4>(kernelDesc, 891234, 0.0f, 255.0f); |
jimfly01 | d84216a | 2018-10-26 12:56:21 +0100 | [diff] [blame] | 1202 | auto bias = MakeRandomTensor<typename FullyConnectedBiasTypeForInputType<T>::Type, 1>( |
| 1203 | biasDesc, 1028, 0.0f, 255.0f); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1204 | |
| 1205 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 1206 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 1207 | |
| 1208 | armnn::DepthwiseConvolution2dQueueDescriptor data; |
| 1209 | armnn::WorkloadInfo info; |
| 1210 | armnn::ScopedCpuTensorHandle weightsTensor(kernelDesc); |
| 1211 | armnn::ScopedCpuTensorHandle biasTensor(biasDesc); |
| 1212 | |
| 1213 | AllocateAndCopyDataToITensorHandle(&weightsTensor, &kernel[0][0][0][0]); |
| 1214 | AllocateAndCopyDataToITensorHandle(&biasTensor, &bias[0]); |
| 1215 | |
| 1216 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 1217 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 1218 | data.m_Weight = &weightsTensor; |
| 1219 | data.m_Bias = &biasTensor; |
| 1220 | data.m_Parameters.m_StrideX = strideX; |
| 1221 | data.m_Parameters.m_StrideY = strideY; |
| 1222 | data.m_Parameters.m_PadLeft = padX; |
| 1223 | data.m_Parameters.m_PadRight = padX; |
| 1224 | data.m_Parameters.m_PadTop = padY; |
| 1225 | data.m_Parameters.m_PadBottom = padY; |
| 1226 | data.m_Parameters.m_BiasEnabled = true; |
jimfly01 | 7af00da | 2018-10-31 14:43:53 +0000 | [diff] [blame] | 1227 | data.m_Parameters.m_DataLayout = layout.GetDataLayout(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1228 | |
| 1229 | std::unique_ptr<armnn::ITensorHandle> outputHandleRef = refWorkloadFactory.CreateTensorHandle(outputTensorInfo); |
| 1230 | std::unique_ptr<armnn::ITensorHandle> inputHandleRef = refWorkloadFactory.CreateTensorHandle(inputTensorInfo); |
| 1231 | |
| 1232 | armnn::DepthwiseConvolution2dQueueDescriptor refData = data; |
| 1233 | armnn::WorkloadInfo refInfo = info; |
| 1234 | SetWorkloadInput(refData, refInfo, 0, inputTensorInfo, inputHandleRef.get()); |
| 1235 | SetWorkloadOutput(refData, refInfo, 0, outputTensorInfo, outputHandleRef.get()); |
| 1236 | |
| 1237 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateDepthwiseConvolution2d(data, info); |
| 1238 | std::unique_ptr<armnn::IWorkload> workloadRef = refWorkloadFactory.CreateDepthwiseConvolution2d(refData, refInfo); |
| 1239 | |
| 1240 | outputHandleRef->Allocate(); |
| 1241 | inputHandleRef->Allocate(); |
| 1242 | |
| 1243 | inputHandle->Allocate(); |
| 1244 | outputHandle->Allocate(); |
| 1245 | |
| 1246 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 1247 | CopyDataToITensorHandle(inputHandleRef.get(), &input[0][0][0][0]); |
| 1248 | |
Ruomei Yan | 495852f | 2019-05-23 11:37:33 +0100 | [diff] [blame] | 1249 | ExecuteWorkload(*workload, memoryManager); |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 1250 | |
Ruomei Yan | 495852f | 2019-05-23 11:37:33 +0100 | [diff] [blame] | 1251 | workloadRef->PostAllocationConfigure(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1252 | workloadRef->Execute(); |
| 1253 | |
| 1254 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 1255 | CopyDataFromITensorHandle(&ret.outputExpected[0][0][0][0], outputHandleRef.get()); |
| 1256 | |
| 1257 | return ret; |
| 1258 | } |