aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/backends/NeonLayerSupport.cpp
blob: 382b15e2778dd639ac3e5e9639f052b817b45d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#include "NeonLayerSupport.hpp"

#include "LayerSupportCommon.hpp"
#include "InternalTypes.hpp"

#include <armnn/Descriptors.hpp>
#include <armnn/Types.hpp>
#include <armnn/Tensor.hpp>

#include <boost/core/ignore_unused.hpp>

#ifdef ARMCOMPUTENEON_ENABLED
#include "NeonWorkloads/NeonPooling2dBaseWorkload.hpp"
#include "NeonWorkloads/NeonPermuteWorkload.hpp"
#endif

using namespace boost;

namespace armnn
{
bool IsNeonActivationUint8Supported(std::string* reasonIfUnsupported, const ActivationDescriptor& parameters)
{
    if (parameters.m_Function != ActivationFunction::BoundedReLu)
    {
        if (reasonIfUnsupported)
        {
            *reasonIfUnsupported = "Unsupported activation function, only BoundedReLu is supported)";
        }

        return false;
    }

    return true;
}

bool IsNeonDirectConvolutionPreferred(const TensorInfo& weightInfo, const Convolution2dDescriptor& desc)
{
    // See arm_compute::NEDirectConvolutionLayer documentation for the supported cases,
    // and complement with NEDirectConvolutionLayerKernel::configure() implementation

    // Only 1x1 is using direct convolution. Performance results and details are in:
    //    https://jira.arm.com/browse/IVGCVSW-1003
    // Measurements were taken as of clframework: f105ab972135bcd21304883eff040d7e587099bc

    const bool dataTypeSupported = (weightInfo.GetDataType() == armnn::DataType::Float32);

    // Strides: 1|2|3
    const bool strideSupported = (desc.m_StrideX == 1 || desc.m_StrideX == 2 || desc.m_StrideX == 3) &&
                                 (desc.m_StrideY == 1 || desc.m_StrideY == 2 || desc.m_StrideY == 3);

    auto paddingLargerThan = [](const Convolution2dDescriptor& desc, unsigned int value)
    {
        return desc.m_PadLeft > value || desc.m_PadRight > value || desc.m_PadTop > value || desc.m_PadBottom > value;
    };

    // Supported sizes and padding
    const bool sizeAndPaddingSupported =
        // Pad > 0 not supported for 1x1 weights
        (weightInfo.GetShape()[2] == 1 && weightInfo.GetShape()[3] == 1 && !paddingLargerThan(desc, 0u));

    const bool preferDirectConvolution = dataTypeSupported &&
                                         strideSupported &&
                                         sizeAndPaddingSupported &&
                                         // NEDirectConvolutionLayerKernel doesn't support NULL bias
                                         desc.m_BiasEnabled;
    return preferDirectConvolution;
}

bool IsNeonNormalizationDescParamsSupported(std::string* reasonIfUnsupported, const NormalizationDescriptor& parameters)
{
    if (parameters.m_NormMethodType != NormalizationAlgorithmMethod::LocalBrightness)
    {
        if (reasonIfUnsupported)
        {
            *reasonIfUnsupported = "Unsupported normalisation method type, only LocalBrightness is supported";
        }
        return false;
    }
    if (parameters.m_NormSize % 2 == 0)
    {
        if (reasonIfUnsupported)
        {
            *reasonIfUnsupported = "Normalization size must be an odd number.";
        }
        return false;
    }

    return true;
}

bool IsNeonBackendSupported(std::string* reasonIfUnsupported)
{
#if ARMCOMPUTENEON_ENABLED
    return true;
#else
    if (reasonIfUnsupported != nullptr)
    {
        *reasonIfUnsupported = "The armnn library has been built without NEON support";
    }
    return false;
#endif
}

template<typename Float32Func, typename Uint8Func, typename ... Params>
bool IsSupportedForDataTypeNeon(std::string* reasonIfUnsupported,
                                DataType dataType,
                                Float32Func floatFuncPtr,
                                Uint8Func uint8FuncPtr,
                                Params&&... params)
{
    return IsNeonBackendSupported(reasonIfUnsupported) &&
        IsSupportedForDataTypeGeneric(reasonIfUnsupported,
                                         dataType,
                                         floatFuncPtr,
                                         uint8FuncPtr,
                                         std::forward<Params>(params)...);
}

#if ARMCOMPUTENEON_ENABLED
template<class FuncType, class... Args>
inline bool IsWorkloadSupported(FuncType& func, std::string* reasonIfUnsupported, Args&&... args)
{
    arm_compute::Status aclStatus = func(std::forward<Args>(args)...);
    const bool supported = (aclStatus.error_code() == arm_compute::ErrorCode::OK);
    if (!supported && reasonIfUnsupported)
    {
        *reasonIfUnsupported = aclStatus.error_description();
    }
    return supported;
}

#define FORWARD_WORKLOAD_VALIDATE_FUNC(func, reasonIfUnsupported, ...) \
    return IsWorkloadSupported(func, reasonIfUnsupported, __VA_ARGS__);
#else
#define FORWARD_WORKLOAD_VALIDATE_FUNC(func, reasonIfUnsupported, ...) \
    return IsNeonBackendSupported(reasonIfUnsupported);
#endif

bool IsActivationSupportedNeon(const TensorInfo& input,
                               const ActivationDescriptor& descriptor,
                               std::string* reasonIfUnsupported)
{
    ignore_unused(descriptor);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<const ActivationDescriptor&>,
                                      &IsNeonActivationUint8Supported,
                                      descriptor);
}

bool IsNeonDepthwiseConvolution2dDescParamsSupported(std::string* reasonIfUnsupported,
                                                     const DepthwiseConvolution2dDescriptor& parameters,
                                                     const TensorInfo& weights)
{
    ignore_unused(weights);

    if (parameters.m_StrideX < 1 || parameters.m_StrideX > 3)
    {
        if (reasonIfUnsupported)
        {
            *reasonIfUnsupported = "m_StrideX can only be 1, 2 or 3";
        }
        return false;
    }

    // weights.GetShape()[0] = channel multiplier
    if (weights.GetShape()[0] != 1)
    {
        if (reasonIfUnsupported)
        {
            *reasonIfUnsupported = "Channel multiplier only supports the value 1 in the NEON backend";
        }
        return false;
    }

    if (parameters.m_PadLeft != parameters.m_PadRight || parameters.m_PadTop != parameters.m_PadBottom)
    {
        if (reasonIfUnsupported)
        {
            *reasonIfUnsupported = "Asymmetric padding for depthwise convolution currently not supported "
                "in Neon backend";
        }
        return false;
    }

    return true;
}

bool IsAdditionSupportedNeon(const TensorInfo& input0,
                             const TensorInfo& input1,
                             const TensorInfo& output,
                             std::string* reasonIfUnsupported)
{
    ignore_unused(input1);
    ignore_unused(output);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input0.GetDataType(),
                                      &TrueFunc<>,
                                      &FalseFuncU8<>);
}

bool IsBatchNormalizationSupportedNeon(const TensorInfo& input,
                                       const BatchNormalizationDescriptor& descriptor,
                                       std::string* reasonIfUnsupported)
{
    ignore_unused(descriptor);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &FalseFuncU8<>);
}

bool IsConstantSupportedNeon(const TensorInfo& output,
                             std::string* reasonIfUnsupported)
{
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      output.GetDataType(),
                                      &TrueFunc<>,
                                      &TrueFunc<>);
}

bool IsConvolution2dSupportedNeon(const TensorInfo& input,
                                  const Convolution2dDescriptor& descriptor,
                                  const TensorInfo& weights,
                                  std::string* reasonIfUnsupported)
{
    ignore_unused(descriptor);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &FalseFuncU8<>);
}

bool IsDepthwiseConvolutionSupportedNeon(const TensorInfo& input,
                                         const DepthwiseConvolution2dDescriptor& descriptor,
                                         const TensorInfo& weights,
                                         std::string* reasonIfUnsupported)
{
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &IsNeonDepthwiseConvolution2dDescParamsSupported,
                                      &IsNeonDepthwiseConvolution2dDescParamsSupported,
                                      descriptor,
                                      weights);
}

bool IsFullyConnectedSupportedNeon(const TensorInfo& input,
                                   const FullyConnectedDescriptor& descriptor,
                                   std::string* reasonIfUnsupported)
{
    ignore_unused(descriptor);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &FalseFuncU8<>);
}

bool IsInputSupportedNeon(const TensorInfo& input,
                          std::string* reasonIfUnsupported)
{
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &TrueFunc<>);
}

bool IsL2NormalizationSupportedNeon(const TensorInfo& input,
                                    std::string* reasonIfUnsupported)
{
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &FalseFunc<>);
}

bool IsMergerSupportedNeon(const std::vector<const TensorInfo*> inputs,
                           const OriginsDescriptor& descriptor,
                           std::string* reasonIfUnsupported)
{
    ignore_unused(descriptor);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      inputs[0]->GetDataType(),
                                      &TrueFunc<>,
                                      &TrueFunc<>);
}

bool IsMultiplicationSupportedNeon(const TensorInfo& input0,
                                   const TensorInfo& input1,
                                   std::string* reasonIfUnsupported)
{
    ignore_unused(input1);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input0.GetDataType(),
                                      &TrueFunc<>,
                                      &FalseFuncU8<>);
}

bool IsNormalizationSupportedNeon(const TensorInfo& input,
                                  const TensorInfo& output,
                                  const NormalizationDescriptor& descriptor,
                                  std::string* reasonIfUnsupported)
{
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &IsNeonNormalizationDescParamsSupported,
                                      &FalseFuncU8<const NormalizationDescriptor&>,
                                      descriptor);
}

bool IsOutputSupportedNeon(const TensorInfo& output,
                           std::string* reasonIfUnsupported)
{
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      output.GetDataType(),
                                      &TrueFunc<>,
                                      &TrueFunc<>);
}

bool IsPermuteSupportedNeon(const TensorInfo& input,
                            const TensorInfo& output,
                            const PermuteDescriptor& descriptor,
                            std::string* reasonIfUnsupported)
{
    FORWARD_WORKLOAD_VALIDATE_FUNC(NeonPermuteWorkloadValidate, reasonIfUnsupported, input, output, descriptor);
}

bool IsPooling2dSupportedNeon(const TensorInfo& input,
                              const TensorInfo& output,
                              const Pooling2dDescriptor& descriptor,
                              std::string* reasonIfUnsupported)
{
    FORWARD_WORKLOAD_VALIDATE_FUNC(NeonPooling2dWorkloadValidate, reasonIfUnsupported, input, output, descriptor);
}

bool IsResizeBilinearSupportedNeon(const TensorInfo& input,
                                   std::string* reasonIfUnsupported)
{
    ignore_unused(input);
    return false;
}

bool IsSoftmaxSupportedNeon(const TensorInfo& input,
                            const SoftmaxDescriptor& descriptor,
                            std::string* reasonIfUnsupported)
{
    ignore_unused(descriptor);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &TrueFunc<>);
}

bool IsSplitterSupportedNeon(const TensorInfo& input,
                             const ViewsDescriptor& descriptor,
                             std::string* reasonIfUnsupported)
{
    ignore_unused(descriptor);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &TrueFunc<>);
}

bool IsFakeQuantizationSupportedNeon(const TensorInfo& input,
                                     const FakeQuantizationDescriptor& descriptor,
                                     std::string* reasonIfUnsupported)
{
    ignore_unused(input);
    ignore_unused(descriptor);
    return false;
}

bool IsReshapeSupportedNeon(const TensorInfo& input,
                            std::string* reasonIfUnsupported)
{
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &TrueFunc<>);
}

bool IsFloorSupportedNeon(const TensorInfo& input,
                          const TensorInfo& output,
                          std::string* reasonIfUnsupported)
{
    ignore_unused(output);
    return IsSupportedForDataTypeNeon(reasonIfUnsupported,
                                      input.GetDataType(),
                                      &TrueFunc<>,
                                      &FalseFuncU8<>);
}

}