blob: 18c9ac8f5bfc3a2f8f0f920881ae8257d4eb62ef [file] [log] [blame]
David Monahan8a570462023-11-22 13:24:25 +00001//
David Monahanbd738082023-12-08 12:50:02 +00002// Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved.
David Monahan8a570462023-11-22 13:24:25 +00003// SPDX-License-Identifier: MIT
4//
5
6#include "GpuFsaLayerSupport.hpp"
7
8#include <armnn/Types.hpp>
9#include <armnn/utility/IgnoreUnused.hpp>
10#include <armnn/utility/PolymorphicDowncast.hpp>
11
12#if defined(ARMCOMPUTEGPUFSA_ENABLED)
David Monahanbd738082023-12-08 12:50:02 +000013#include "layers/GpuFsaConvolution2d.hpp"
Tianle Chengfbfa49e2024-01-23 11:21:48 +000014#include "layers/GpuFsaDepthwiseConvolution2d.hpp"
David Monahan8a570462023-11-22 13:24:25 +000015#endif
16
17#include <vector>
18
19namespace armnn
20{
21
22template<typename ... Args>
23bool IsGpuFsaBackendSupported(Optional<std::string&> reasonIfUnsupported, Args... args)
24{
25 IgnoreUnused(reasonIfUnsupported, (args)...);
26#if defined(ARMCOMPUTEGPUFSA_ENABLED)
27 return true;
28#else
29 if (reasonIfUnsupported)
30 {
31 reasonIfUnsupported.value() = "The armnn library has been built without CL support";
32 }
33 return false;
34#endif
35}
36
37#if defined(ARMCOMPUTEGPUFSA_ENABLED)
38#define FORWARD_GPUFSA_LAYER_SUPPORT_FUNC(expr) (expr)
39#else
40#define FORWARD_GPUFSA_LAYER_SUPPORT_FUNC(expr) IsGpuFsaBackendSupported(reasonIfUnsupported)
41#endif
42
43#if defined(ARMCOMPUTEGPUFSA_ENABLED)
44template<class FuncType, class... Args>
45inline bool CheckIsLayerSupported(FuncType&& func, Optional<std::string&> reasonIfUnsupported, Args&&... args)
46{
47 arm_compute::Status aclStatus = func(std::forward<Args>(args)...);
48 const bool supported = (aclStatus.error_code() == arm_compute::ErrorCode::OK);
49 if (!supported && reasonIfUnsupported)
50 {
51 reasonIfUnsupported.value() = aclStatus.error_description();
52 }
53 return supported;
54}
55
56#define FORWARD_LAYER_VALIDATE_FUNC(func, reasonIfUnsupported, ...) \
57 return CheckIsLayerSupported(func, reasonIfUnsupported, __VA_ARGS__);
58#else
59#define FORWARD_LAYER_VALIDATE_FUNC(func, reasonIfUnsupported, ...) \
60 return IsGpuFsaBackendSupported(reasonIfUnsupported, __VA_ARGS__);
61#endif
62
63bool GpuFsaLayerSupport::IsLayerSupported(const LayerType& type,
64 const std::vector<TensorInfo>& infos,
65 const BaseDescriptor& descriptor,
66 const Optional<LstmInputParamsInfo>& lstmParamsInfo,
67 const Optional<QuantizedLstmInputParamsInfo>& quantizedLstmInputParamsInfo,
68 Optional<std::string&> reasonIfUnsupported) const
69{
70 IgnoreUnused(lstmParamsInfo);
71 IgnoreUnused(quantizedLstmInputParamsInfo);
72
73 switch (type) {
74 case LayerType::Convolution2d:
75 {
76 if (infos.size() != 4)
77 {
78 throw InvalidArgumentException("Invalid number of Convolution2d TensorInfos. "
79 "TensorInfos should be of format: {input, output, weights, biases}.");
80 }
81
82 auto desc = *(PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor));
83 if (infos[3] == TensorInfo())
84 {
85 FORWARD_LAYER_VALIDATE_FUNC(GpuFsaConvolution2dValidate,
86 reasonIfUnsupported,
87 infos[0],
88 desc,
89 infos[2],
90 EmptyOptional());
91 }
92 else
93 {
94 FORWARD_LAYER_VALIDATE_FUNC(GpuFsaConvolution2dValidate,
95 reasonIfUnsupported,
96 infos[0],
97 desc,
98 infos[2],
99 infos[3]);
100 }
101 }
Tianle Chengfbfa49e2024-01-23 11:21:48 +0000102 case LayerType::DepthwiseConvolution2d:
103 {
104 if (infos.size() != 4)
105 {
106 throw InvalidArgumentException("Invalid number of DepthwiseConvolution2dDescriptor TensorInfos. "
107 "TensorInfos should be of format: {input, output, weights, biases}.");
108 }
109
110 auto desc = *(PolymorphicDowncast<const DepthwiseConvolution2dDescriptor*>(&descriptor));
111 if (infos[3] == TensorInfo())
112 {
113 FORWARD_LAYER_VALIDATE_FUNC(GpuFsaDepthwiseConvolution2dValidate,
114 reasonIfUnsupported,
115 infos[0],
116 desc,
117 infos[2],
118 EmptyOptional());
119 }
120 else
121 {
122 FORWARD_LAYER_VALIDATE_FUNC(GpuFsaDepthwiseConvolution2dValidate,
123 reasonIfUnsupported,
124 infos[0],
125 desc,
126 infos[2],
127 infos[3]);
128 }
129 }
David Monahan8a570462023-11-22 13:24:25 +0000130 case LayerType::Constant:
131 case LayerType::Input:
132 case LayerType::Output:
133 return IsGpuFsaBackendSupported(reasonIfUnsupported, infos[0]);
134 default:
135 // Layers not supported in the GpuFsa backend.
136 return false;
137 }
138}
139
140} // namespace armnn