blob: 26fb03f55d1cd22f4b109a8219658f77e3ae905b [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
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00007#include <Graph.hpp>
8
9#include <backendsCommon/WorkloadFactory.hpp>
telsoa014fcda012018-03-09 14:13:49 +000010
11#include <boost/core/ignore_unused.hpp>
12
13namespace
14{
15armnn::Graph dummyGraph;
16
telsoa01c577f2c2018-08-31 09:22:23 +010017// Make a dummy TensorInfo object.
telsoa014fcda012018-03-09 14:13:49 +000018template<armnn::DataType DataType>
19armnn::TensorInfo MakeDummyTensorInfo()
20{
21 return armnn::TensorInfo({2,2,2,2}, DataType);
22}
23
24
25// Make a dummy WorkloadInfo using a dummy TensorInfo.
26template<armnn::DataType DataType>
27armnn::WorkloadInfo MakeDummyWorkloadInfo(unsigned int numInputs, unsigned int numOutputs)
28{
29 armnn::WorkloadInfo info;
30 for (unsigned int i=0; i < numInputs; i++)
31 {
32 info.m_InputTensorInfos.push_back(MakeDummyTensorInfo<DataType>());
33 }
34 for (unsigned int o=0; o < numOutputs; o++)
35 {
36 info.m_OutputTensorInfos.push_back(MakeDummyTensorInfo<DataType>());
37 }
38 return info;
39}
40
telsoa01c577f2c2018-08-31 09:22:23 +010041// Template class to create a dummy layer (2 parameters).
telsoa014fcda012018-03-09 14:13:49 +000042template<typename LayerType, typename DescType = typename LayerType::DescriptorType>
43struct DummyLayer
44{
45 DummyLayer()
46 {
47 m_Layer = dummyGraph.AddLayer<LayerType>(DescType(), "");
48 }
49 ~DummyLayer()
50 {
51 dummyGraph.EraseLayer(m_Layer);
52 }
53 LayerType* m_Layer;
54};
55
telsoa01c577f2c2018-08-31 09:22:23 +010056// Template class to create a dummy layer (1 parameter).
telsoa014fcda012018-03-09 14:13:49 +000057template<typename LayerType>
58struct DummyLayer<LayerType, void>
59{
60 DummyLayer()
61 {
62 m_Layer = dummyGraph.AddLayer<LayerType>("");
63 }
64 ~DummyLayer()
65 {
66 dummyGraph.EraseLayer(m_Layer);
67 }
68 LayerType* m_Layer;
69};
70
71template<>
telsoa01c577f2c2018-08-31 09:22:23 +010072struct DummyLayer<armnn::BatchNormalizationLayer>
73{
74 DummyLayer()
75 {
76 m_Layer = dummyGraph.AddLayer<armnn::BatchNormalizationLayer>(armnn::BatchNormalizationDescriptor(), "");
77 m_Layer->m_Mean = std::make_unique<armnn::ScopedCpuTensorHandle>(
78 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
79 m_Layer->m_Variance = std::make_unique<armnn::ScopedCpuTensorHandle>(
80 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
81 m_Layer->m_Beta = std::make_unique<armnn::ScopedCpuTensorHandle>(
82 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
83 m_Layer->m_Gamma = std::make_unique<armnn::ScopedCpuTensorHandle>(
84 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
85 }
86 ~DummyLayer()
87 {
88 dummyGraph.EraseLayer(m_Layer);
89 }
90 armnn::BatchNormalizationLayer* m_Layer;
91
92};
93
94template<>
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +000095struct DummyLayer<armnn::BatchToSpaceNdLayer>
96{
97 DummyLayer()
98 {
99 m_Layer = dummyGraph.AddLayer<armnn::BatchToSpaceNdLayer>(armnn::BatchToSpaceNdDescriptor(), "");
100 }
101 ~DummyLayer()
102 {
103 dummyGraph.EraseLayer(m_Layer);
104 }
105 armnn::BatchToSpaceNdLayer* m_Layer;
106};
107
108template<>
telsoa014fcda012018-03-09 14:13:49 +0000109struct DummyLayer<armnn::ConstantLayer, void>
110{
111 DummyLayer()
112 {
telsoa01c577f2c2018-08-31 09:22:23 +0100113 m_Layer = dummyGraph.AddLayer<armnn::ConstantLayer>("");
telsoa014fcda012018-03-09 14:13:49 +0000114 }
115 ~DummyLayer()
116 {
117 dummyGraph.EraseLayer(m_Layer);
118 }
119 armnn::ConstantLayer* m_Layer;
120};
121
122template<>
123struct DummyLayer<armnn::InputLayer, armnn::LayerBindingId>
124{
125 DummyLayer()
126 {
127 m_Layer = dummyGraph.AddLayer<armnn::InputLayer>(armnn::LayerBindingId(), "");
128
129 }
130 ~DummyLayer()
131 {
132 dummyGraph.EraseLayer(m_Layer);
133 }
134 armnn::InputLayer* m_Layer;
135};
136
137template<>
138struct DummyLayer<armnn::MergerLayer>
139{
140 DummyLayer()
141 {
142 armnn::OriginsDescriptor desc(2);
143 m_Layer = dummyGraph.AddLayer<armnn::MergerLayer>(desc, "");
144
145 }
146 ~DummyLayer()
147 {
148 dummyGraph.EraseLayer(m_Layer);
149 }
150 armnn::MergerLayer* m_Layer;
151};
152
153template<>
154struct DummyLayer<armnn::OutputLayer, armnn::LayerBindingId>
155{
156 DummyLayer()
157 {
158 m_Layer = dummyGraph.AddLayer<armnn::OutputLayer>(armnn::LayerBindingId(), "");
159
160 }
161 ~DummyLayer()
162 {
163 dummyGraph.EraseLayer(m_Layer);
164 }
165 armnn::OutputLayer* m_Layer;
166};
167
168template<>
169struct DummyLayer<armnn::SplitterLayer>
170{
171 DummyLayer()
172 {
173 armnn::ViewsDescriptor desc(1);
174 m_Layer = dummyGraph.AddLayer<armnn::SplitterLayer>(desc, "");
175
176 }
177 ~DummyLayer()
178 {
179 dummyGraph.EraseLayer(m_Layer);
180 }
181 armnn::SplitterLayer* m_Layer;
182};
183
184template <typename ConvolutionLayerType>
185struct DummyConvolutionLayer
186{
187 DummyConvolutionLayer()
188 {
189 typename ConvolutionLayerType::DescriptorType desc;
190 m_Layer = dummyGraph.AddLayer<ConvolutionLayerType>(desc, "");
191 m_Layer->m_Weight = std::make_unique<armnn::ScopedCpuTensorHandle>(
192 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
193 m_Layer->m_Bias = std::make_unique<armnn::ScopedCpuTensorHandle>(
194 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
195 }
196 ~DummyConvolutionLayer()
197 {
198 dummyGraph.EraseLayer(m_Layer);
199 }
200 ConvolutionLayerType* m_Layer;
201};
202
203template<>
204struct DummyLayer<armnn::Convolution2dLayer>
205 : public DummyConvolutionLayer<armnn::Convolution2dLayer>
206{
207};
208
209template<>
210struct DummyLayer<armnn::DepthwiseConvolution2dLayer>
211 : public DummyConvolutionLayer<armnn::DepthwiseConvolution2dLayer>
212{
213};
214
telsoa01c577f2c2018-08-31 09:22:23 +0100215template <typename LstmLayerType>
216struct DummyLstmLayer
217{
218 DummyLstmLayer()
219 {
220 typename LstmLayerType::DescriptorType desc;
221 desc.m_CifgEnabled = false;
222
223 m_Layer = dummyGraph.AddLayer<LstmLayerType>(armnn::LstmDescriptor(), "");
224 m_Layer->m_BasicParameters.m_InputToForgetWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
225 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
226 m_Layer->m_BasicParameters.m_InputToCellWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
227 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
228 m_Layer->m_BasicParameters.m_InputToOutputWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
229 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
230 m_Layer->m_BasicParameters.m_RecurrentToForgetWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
231 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
232 m_Layer->m_BasicParameters.m_RecurrentToCellWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
233 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
234 m_Layer->m_BasicParameters.m_RecurrentToOutputWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
235 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
236 m_Layer->m_BasicParameters.m_ForgetGateBias = std::make_unique<armnn::ScopedCpuTensorHandle>(
237 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
238 m_Layer->m_BasicParameters.m_CellBias = std::make_unique<armnn::ScopedCpuTensorHandle>(
239 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
240 m_Layer->m_BasicParameters.m_OutputGateBias = std::make_unique<armnn::ScopedCpuTensorHandle>(
241 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
242
243 m_Layer->m_CifgParameters.m_InputToInputWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
244 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
245 m_Layer->m_CifgParameters.m_RecurrentToInputWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
246 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
247 m_Layer->m_CifgParameters.m_CellToInputWeights = std::make_unique<armnn::ScopedCpuTensorHandle>(
248 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
249 m_Layer->m_CifgParameters.m_InputGateBias = std::make_unique<armnn::ScopedCpuTensorHandle>(
250 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
251 }
252 ~DummyLstmLayer()
253 {
254 dummyGraph.EraseLayer(m_Layer);
255 }
256 armnn::LstmLayer* m_Layer;
257};
258
259template<>
260struct DummyLayer<armnn::LstmLayer>
261 : public DummyLstmLayer<armnn::LstmLayer>
262{
263};
264
265template<>
266struct DummyLayer<armnn::FullyConnectedLayer>
267{
268 DummyLayer()
269 {
270 armnn::FullyConnectedLayer::DescriptorType desc;
271 m_Layer = dummyGraph.AddLayer<armnn::FullyConnectedLayer>(desc, "");
272 m_Layer->m_Weight = std::make_unique<armnn::ScopedCpuTensorHandle>(
273 armnn::TensorInfo(armnn::TensorShape({1,1,1,1}), armnn::DataType::Float32));
274 }
275 ~DummyLayer()
276 {
277 dummyGraph.EraseLayer(m_Layer);
278 }
279 armnn::FullyConnectedLayer* m_Layer;
280};
281
telsoa014fcda012018-03-09 14:13:49 +0000282// Tag for giving LayerType entries a unique strong type each.
283template<armnn::LayerType>
284struct Tag{};
285
286#define DECLARE_LAYER_POLICY_CUSTOM_PARAM(name, descType) \
287template<armnn::DataType DataType> \
288struct LayerTypePolicy<armnn::LayerType::name, DataType> \
289{ \
290 using Type = armnn::name##Layer; \
291 using Desc = descType; \
292 using QueueDesc = armnn::name##QueueDescriptor; \
293 constexpr static const char* NameStr = #name; \
294 \
295 static std::unique_ptr<armnn::IWorkload> MakeDummyWorkload(armnn::IWorkloadFactory *factory, \
296 unsigned int nIn, unsigned int nOut) \
297 { \
298 QueueDesc desc; \
299 armnn::WorkloadInfo info = MakeDummyWorkloadInfo<DataType>(nIn, nOut); \
300 return factory->Create##name(desc, info); \
301 } \
302};
303
telsoa01c577f2c2018-08-31 09:22:23 +0100304// Define a layer policy specialization for use with the IsLayerSupported tests.
telsoa014fcda012018-03-09 14:13:49 +0000305// Use this version for layers whose constructor takes 1 parameter(name).
306#define DECLARE_LAYER_POLICY_1_PARAM(name) DECLARE_LAYER_POLICY_CUSTOM_PARAM(name, void)
307
telsoa01c577f2c2018-08-31 09:22:23 +0100308// Define a layer policy specialization for use with the IsLayerSupported tests.
telsoa014fcda012018-03-09 14:13:49 +0000309// Use this version for layers whose constructor takes 2 parameters(descriptor and name).
310#define DECLARE_LAYER_POLICY_2_PARAM(name) DECLARE_LAYER_POLICY_CUSTOM_PARAM(name, armnn::name##Descriptor)
311
telsoa01c577f2c2018-08-31 09:22:23 +0100312// Layer policy template.
telsoa014fcda012018-03-09 14:13:49 +0000313template<armnn::LayerType Type, armnn::DataType DataType>
314struct LayerTypePolicy;
315
316// Every entry in the armnn::LayerType enum must be accounted for below.
317DECLARE_LAYER_POLICY_2_PARAM(Activation)
318
319DECLARE_LAYER_POLICY_1_PARAM(Addition)
320
321DECLARE_LAYER_POLICY_2_PARAM(BatchNormalization)
322
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +0000323DECLARE_LAYER_POLICY_2_PARAM(BatchToSpaceNd)
324
telsoa014fcda012018-03-09 14:13:49 +0000325DECLARE_LAYER_POLICY_1_PARAM(Constant)
326
telsoa01c577f2c2018-08-31 09:22:23 +0100327DECLARE_LAYER_POLICY_1_PARAM(ConvertFp16ToFp32)
328
329DECLARE_LAYER_POLICY_1_PARAM(ConvertFp32ToFp16)
330
telsoa014fcda012018-03-09 14:13:49 +0000331DECLARE_LAYER_POLICY_2_PARAM(Convolution2d)
332
333DECLARE_LAYER_POLICY_1_PARAM(MemCopy)
334
Nattapat Chaimanowong964e9552019-03-26 11:03:26 +0000335DECLARE_LAYER_POLICY_1_PARAM(Debug)
Nattapat Chaimanowonga9a1cf12018-12-03 16:06:49 +0000336
telsoa014fcda012018-03-09 14:13:49 +0000337DECLARE_LAYER_POLICY_2_PARAM(DepthwiseConvolution2d)
338
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +0000339DECLARE_LAYER_POLICY_1_PARAM(Dequantize)
340
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +0000341DECLARE_LAYER_POLICY_2_PARAM(DetectionPostProcess)
342
FrancisMurtagh20995952018-12-17 12:11:36 +0000343DECLARE_LAYER_POLICY_1_PARAM(Equal)
344
telsoa014fcda012018-03-09 14:13:49 +0000345DECLARE_LAYER_POLICY_2_PARAM(FakeQuantization)
346
347DECLARE_LAYER_POLICY_1_PARAM(Floor)
348
349DECLARE_LAYER_POLICY_2_PARAM(FullyConnected)
350
narpra01b89b05f2019-01-16 09:53:09 +0000351DECLARE_LAYER_POLICY_1_PARAM(Gather)
352
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000353DECLARE_LAYER_POLICY_1_PARAM(Greater)
354
telsoa014fcda012018-03-09 14:13:49 +0000355DECLARE_LAYER_POLICY_CUSTOM_PARAM(Input, armnn::LayerBindingId)
356
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100357DECLARE_LAYER_POLICY_2_PARAM(L2Normalization)
telsoa014fcda012018-03-09 14:13:49 +0000358
telsoa01c577f2c2018-08-31 09:22:23 +0100359DECLARE_LAYER_POLICY_2_PARAM(Lstm)
360
Nattapat Chaimanowong5a4304a2018-11-28 10:44:37 +0000361DECLARE_LAYER_POLICY_1_PARAM(Maximum)
362
narpra0132b90462018-09-13 11:07:48 +0100363DECLARE_LAYER_POLICY_2_PARAM(Mean)
364
telsoa014fcda012018-03-09 14:13:49 +0000365DECLARE_LAYER_POLICY_2_PARAM(Merger)
366
kevmay0190539692018-11-29 08:40:19 +0000367DECLARE_LAYER_POLICY_1_PARAM(Minimum)
368
telsoa014fcda012018-03-09 14:13:49 +0000369DECLARE_LAYER_POLICY_1_PARAM(Multiplication)
370
371DECLARE_LAYER_POLICY_2_PARAM(Normalization)
372
373DECLARE_LAYER_POLICY_CUSTOM_PARAM(Output, armnn::LayerBindingId)
374
Mohamed Nour Abouelseoud5662c202018-09-24 13:30:09 +0100375DECLARE_LAYER_POLICY_2_PARAM(Pad)
376
Derek Lambertia9cca6a2019-03-25 15:41:58 +0000377DECLARE_LAYER_POLICY_1_PARAM(Quantize)
378
telsoa014fcda012018-03-09 14:13:49 +0000379DECLARE_LAYER_POLICY_2_PARAM(Permute)
380
381DECLARE_LAYER_POLICY_2_PARAM(Pooling2d)
382
Matteo Martincigh49124022019-01-11 13:25:59 +0000383DECLARE_LAYER_POLICY_2_PARAM(PreCompiled)
384
Francis Murtaghe7a86a42018-08-29 12:42:10 +0100385DECLARE_LAYER_POLICY_1_PARAM(Division)
386
telsoa014fcda012018-03-09 14:13:49 +0000387DECLARE_LAYER_POLICY_2_PARAM(ResizeBilinear)
388
telsoa01c577f2c2018-08-31 09:22:23 +0100389DECLARE_LAYER_POLICY_2_PARAM(Reshape)
390
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +0000391DECLARE_LAYER_POLICY_1_PARAM(Rsqrt)
392
telsoa014fcda012018-03-09 14:13:49 +0000393DECLARE_LAYER_POLICY_2_PARAM(Softmax)
394
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000395DECLARE_LAYER_POLICY_2_PARAM(SpaceToBatchNd)
396
telsoa014fcda012018-03-09 14:13:49 +0000397DECLARE_LAYER_POLICY_2_PARAM(Splitter)
398
Conor Kennedy430b5d82018-11-14 15:28:28 +0000399DECLARE_LAYER_POLICY_2_PARAM(StridedSlice)
400
David Beckc2044fe2018-09-05 15:00:38 +0100401DECLARE_LAYER_POLICY_1_PARAM(Subtraction)
telsoa014fcda012018-03-09 14:13:49 +0000402
403
404// Generic implementation to get the number of input slots for a given layer type;
405template<armnn::LayerType Type>
406unsigned int GetNumInputs(const armnn::Layer& layer)
407{
408 return layer.GetNumInputSlots();
409}
410
411// Generic implementation to get the number of output slots for a given layer type;
412template<armnn::LayerType Type>
413unsigned int GetNumOutputs(const armnn::Layer& layer)
414{
415 return layer.GetNumOutputSlots();
416}
417
418template<>
419unsigned int GetNumInputs<armnn::LayerType::Merger>(const armnn::Layer& layer)
420{
421 boost::ignore_unused(layer);
422 return 2;
423}
424
telsoa01c577f2c2018-08-31 09:22:23 +0100425// Tests that the IsLayerSupported() function returns the correct value.
426// We determined the correct value by *trying* to create the relevant workload and seeing if it matches what we expect.
telsoa014fcda012018-03-09 14:13:49 +0000427// Returns true if expectations are met, otherwise returns false.
428template<typename FactoryType, armnn::DataType DataType, armnn::LayerType Type>
429bool IsLayerSupportedTest(FactoryType *factory, Tag<Type>)
430{
431 using LayerPolicy = LayerTypePolicy<Type, DataType>;
432 using LayerType = typename LayerPolicy::Type;
433 using LayerDesc = typename LayerPolicy::Desc;
434 DummyLayer<LayerType, LayerDesc> layer;
435
436 unsigned int numIn = GetNumInputs<Type>(*layer.m_Layer);
437 unsigned int numOut = GetNumOutputs<Type>(*layer.m_Layer);
438
telsoa01c577f2c2018-08-31 09:22:23 +0100439 // Make another dummy layer just to make IsLayerSupported have valid inputs.
telsoa014fcda012018-03-09 14:13:49 +0000440 DummyLayer<armnn::ConstantLayer, void> previousLayer;
telsoa01c577f2c2018-08-31 09:22:23 +0100441 // Set output of the previous layer to a dummy tensor.
telsoa014fcda012018-03-09 14:13:49 +0000442 armnn::TensorInfo output = MakeDummyTensorInfo<DataType>();
443 previousLayer.m_Layer->GetOutputSlot(0).SetTensorInfo(output);
telsoa01c577f2c2018-08-31 09:22:23 +0100444 // Connect all outputs of the previous layer to inputs of tested layer.
telsoa014fcda012018-03-09 14:13:49 +0000445 for (unsigned int i = 0; i < numIn; i++)
446 {
447 armnn::IOutputSlot& previousLayerOutputSlot = previousLayer.m_Layer->GetOutputSlot(0);
448 armnn::IInputSlot& layerInputSlot = layer.m_Layer->GetInputSlot(i);
449 previousLayerOutputSlot.Connect(layerInputSlot);
450 }
telsoa01c577f2c2018-08-31 09:22:23 +0100451 // Set outputs of tested layer to a dummy tensor.
telsoa014fcda012018-03-09 14:13:49 +0000452 for (unsigned int i = 0; i < numOut; i++)
453 {
454 layer.m_Layer->GetOutputSlot(0).SetTensorInfo(output);
455 }
456
457 std::string layerName = LayerPolicy::NameStr;
458 std::string reasonIfUnsupported;
459 if (FactoryType::IsLayerSupported(*layer.m_Layer, DataType, reasonIfUnsupported))
460 {
461 std::string errorMsg = " layer expected support but found none.";
462 try
463 {
464 bool retVal = LayerPolicy::MakeDummyWorkload(factory, numIn, numOut).get() != nullptr;
Matteo Martincighfbebcbd2018-10-16 09:45:08 +0100465 BOOST_CHECK_MESSAGE(retVal, layerName << errorMsg);
telsoa014fcda012018-03-09 14:13:49 +0000466 return retVal;
467 }
telsoa01c577f2c2018-08-31 09:22:23 +0100468 catch(const armnn::InvalidArgumentException& e)
telsoa014fcda012018-03-09 14:13:49 +0000469 {
470 boost::ignore_unused(e);
471 // This is ok since we throw InvalidArgumentException when creating the dummy workload.
472 return true;
473 }
474 catch(const std::exception& e)
475 {
476 errorMsg = e.what();
477 BOOST_TEST_ERROR(layerName << ": " << errorMsg);
478 return false;
479 }
telsoa01c577f2c2018-08-31 09:22:23 +0100480 catch(...)
telsoa014fcda012018-03-09 14:13:49 +0000481 {
482 errorMsg = "Unexpected error while testing support for ";
483 BOOST_TEST_ERROR(errorMsg << layerName);
484 return false;
485 }
486 }
487 else
488 {
489 std::string errorMsg = "layer expected no support (giving reason: " + reasonIfUnsupported + ") but found some.";
490 try
491 {
492 bool retVal = LayerPolicy::MakeDummyWorkload(factory, numIn, numOut).get() == nullptr;
493 BOOST_CHECK_MESSAGE(retVal, layerName << errorMsg);
494 return retVal;
495 }
496 // These two exceptions are ok: For workloads that are partially supported, attempting to instantiate them
497 // using parameters that make IsLayerSupported() return false should throw an
telsoa01c577f2c2018-08-31 09:22:23 +0100498 // InvalidArgumentException or UnimplementedException.
telsoa014fcda012018-03-09 14:13:49 +0000499 catch(const armnn::InvalidArgumentException& e)
500 {
501 boost::ignore_unused(e);
502 return true;
503 }
telsoa01c577f2c2018-08-31 09:22:23 +0100504 catch(const armnn::UnimplementedException& e)
telsoa014fcda012018-03-09 14:13:49 +0000505 {
506 boost::ignore_unused(e);
507 return true;
508 }
509 catch(const std::exception& e)
510 {
511 errorMsg = e.what();
512 BOOST_TEST_ERROR(layerName << ": " << errorMsg);
513 return false;
514 }
telsoa01c577f2c2018-08-31 09:22:23 +0100515 catch(...)
telsoa014fcda012018-03-09 14:13:49 +0000516 {
517 errorMsg = "Unexpected error while testing support for ";
518 BOOST_TEST_ERROR(errorMsg << layerName);
519 return false;
520 }
521 }
522}
523
telsoa01c577f2c2018-08-31 09:22:23 +0100524// Helper function to compute the next type in the LayerType enum.
telsoa014fcda012018-03-09 14:13:49 +0000525constexpr armnn::LayerType NextType(armnn::LayerType type)
526{
527 return static_cast<armnn::LayerType>(static_cast<int>(type)+1);
528}
529
telsoa01c577f2c2018-08-31 09:22:23 +0100530// Termination function for determining the end of the LayerType enumeration.
telsoa014fcda012018-03-09 14:13:49 +0000531template<typename FactoryType, armnn::DataType DataType, armnn::LayerType Type>
532bool IsLayerSupportedTestsImpl(FactoryType *factory, Tag<armnn::LayerType::LastLayer>)
533{
534 return IsLayerSupportedTest<FactoryType, DataType, Type>(factory, Tag<Type>());
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000535}
telsoa014fcda012018-03-09 14:13:49 +0000536
telsoa01c577f2c2018-08-31 09:22:23 +0100537// Recursive function to test and enter in the LayerType enum and then iterate on the next entry.
telsoa014fcda012018-03-09 14:13:49 +0000538template<typename FactoryType, armnn::DataType DataType, armnn::LayerType Type>
539bool IsLayerSupportedTestsImpl(FactoryType *factory, Tag<Type>)
540{
541 bool v = IsLayerSupportedTest<FactoryType, DataType, Type>(factory, Tag<Type>());
542
543 return v &&
544 IsLayerSupportedTestsImpl<FactoryType, DataType, NextType(Type)>
545 (factory, Tag<NextType(Type)>());
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000546}
telsoa014fcda012018-03-09 14:13:49 +0000547
548// Helper function to pass through to the test framework.
549template<typename FactoryType, armnn::DataType DataType>
550bool IsLayerSupportedTests(FactoryType *factory)
551{
552 return IsLayerSupportedTestsImpl<FactoryType, DataType>(factory, Tag<armnn::LayerType::FirstLayer>());
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000553}
telsoa014fcda012018-03-09 14:13:49 +0000554
555template<armnn::LayerType Type>
556bool TestLayerTypeMatches()
557{
558 using LayerPolicy = LayerTypePolicy<Type, armnn::DataType::Float32>;
559 using LayerType = typename LayerPolicy::Type;
560 using LayerDesc = typename LayerPolicy::Desc;
561 DummyLayer<LayerType, LayerDesc> layer;
562
563 std::stringstream ss;
564 ss << LayerPolicy::NameStr << " layer type mismatches expected layer type value.";
565 bool v = Type == layer.m_Layer->GetType();
566 BOOST_CHECK_MESSAGE(v, ss.str());
567 return v;
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000568}
telsoa014fcda012018-03-09 14:13:49 +0000569
570template<armnn::LayerType Type>
571bool LayerTypeMatchesTestImpl(Tag<armnn::LayerType::LastLayer>)
572{
573 return TestLayerTypeMatches<Type>();
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000574}
telsoa014fcda012018-03-09 14:13:49 +0000575
576template<armnn::LayerType Type>
577bool LayerTypeMatchesTestImpl(Tag<Type>)
578{
579 return TestLayerTypeMatches<Type>() &&
580 LayerTypeMatchesTestImpl<NextType(Type)>(Tag<NextType(Type)>());
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000581}
telsoa014fcda012018-03-09 14:13:49 +0000582
telsoa01c577f2c2018-08-31 09:22:23 +0100583template<typename FactoryType, typename LayerType, armnn::DataType InputDataType , armnn::DataType OutputDataType>
584bool IsConvertLayerSupportedTests(std::string& reasonIfUnsupported)
585{
586 armnn::Graph graph;
587 LayerType* const layer = graph.AddLayer<LayerType>("LayerName");
588
589 armnn::Layer* const input = graph.AddLayer<armnn::InputLayer>(0, "input");
590 armnn::Layer* const output = graph.AddLayer<armnn::OutputLayer>(0, "output");
591
592 armnn::TensorInfo inputTensorInfo({1, 3, 2, 3}, InputDataType);
593 armnn::TensorInfo outputTensorInfo({1, 3, 2, 3}, OutputDataType);
594
595 input->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
596 input->GetOutputHandler(0).SetTensorInfo(inputTensorInfo);
597 layer->GetOutputSlot(0).Connect(output->GetInputSlot(0));
598 layer->GetOutputHandler(0).SetTensorInfo(outputTensorInfo);
599
600 bool result = FactoryType::IsLayerSupported(*layer, InputDataType, reasonIfUnsupported);
601
602 return result;
Matteo Martincigh59a950c2018-12-13 12:48:25 +0000603}
telsoa01c577f2c2018-08-31 09:22:23 +0100604
Matthew Bentham1f0ff352019-01-02 13:26:31 +0000605template<typename FactoryType, armnn::DataType InputDataType , armnn::DataType OutputDataType>
606bool IsMeanLayerSupportedTests(std::string& reasonIfUnsupported)
607{
608 armnn::Graph graph;
609 static const std::vector<unsigned> axes = {1, 0};
610 armnn::MeanDescriptor desc(axes, false);
611
612 armnn::Layer* const layer = graph.AddLayer<armnn::MeanLayer>(desc, "LayerName");
613
614 armnn::Layer* const input = graph.AddLayer<armnn::InputLayer>(0, "input");
615 armnn::Layer* const output = graph.AddLayer<armnn::OutputLayer>(0, "output");
616
617 armnn::TensorInfo inputTensorInfo({4, 3, 2}, InputDataType);
618 armnn::TensorInfo outputTensorInfo({2}, OutputDataType);
619
620 input->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
621 input->GetOutputHandler(0).SetTensorInfo(inputTensorInfo);
622 layer->GetOutputSlot(0).Connect(output->GetInputSlot(0));
623 layer->GetOutputHandler(0).SetTensorInfo(outputTensorInfo);
624
625 bool result = FactoryType::IsLayerSupported(*layer, InputDataType, reasonIfUnsupported);
626
627 return result;
628}
629
630
telsoa014fcda012018-03-09 14:13:49 +0000631} //namespace