David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 1 | // |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved. |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 3 | // 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) |
Teresa Charlin | ddbda6a | 2024-02-07 22:58:29 +0000 | [diff] [blame] | 13 | #include "layers/GpuFsaActivation.hpp" |
Teresa Charlin | 5bda973 | 2024-02-08 18:46:38 +0000 | [diff] [blame] | 14 | #include "layers/GpuFsaBatchMatMul.hpp" |
Tracy Narine | bc5a5d5 | 2024-02-06 15:22:41 +0000 | [diff] [blame] | 15 | #include "layers/GpuFsaCast.hpp" |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 16 | #include "layers/GpuFsaConvolution2d.hpp" |
Tianle Cheng | fbfa49e | 2024-01-23 11:21:48 +0000 | [diff] [blame] | 17 | #include "layers/GpuFsaDepthwiseConvolution2d.hpp" |
Teresa Charlin | 20dda37 | 2024-02-08 16:23:25 +0000 | [diff] [blame] | 18 | #include "layers/GpuFsaElementwiseBinary.hpp" |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame] | 19 | #include "layers/GpuFsaPooling2d.hpp" |
Declan-ARM | 5e90b83 | 2024-02-07 13:07:31 +0000 | [diff] [blame] | 20 | #include "layers/GpuFsaReshape.hpp" |
Teresa Charlin | 1d6b731 | 2024-02-07 22:02:48 +0000 | [diff] [blame] | 21 | #include "layers/GpuFsaResize.hpp" |
John Mcloughlin | 3375390 | 2024-02-07 15:00:57 +0000 | [diff] [blame] | 22 | #include "layers/GpuFsaSoftmax.hpp" |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace armnn |
| 28 | { |
| 29 | |
| 30 | template<typename ... Args> |
| 31 | bool IsGpuFsaBackendSupported(Optional<std::string&> reasonIfUnsupported, Args... args) |
| 32 | { |
| 33 | IgnoreUnused(reasonIfUnsupported, (args)...); |
| 34 | #if defined(ARMCOMPUTEGPUFSA_ENABLED) |
| 35 | return true; |
| 36 | #else |
| 37 | if (reasonIfUnsupported) |
| 38 | { |
| 39 | reasonIfUnsupported.value() = "The armnn library has been built without CL support"; |
| 40 | } |
| 41 | return false; |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | #if defined(ARMCOMPUTEGPUFSA_ENABLED) |
| 46 | #define FORWARD_GPUFSA_LAYER_SUPPORT_FUNC(expr) (expr) |
| 47 | #else |
| 48 | #define FORWARD_GPUFSA_LAYER_SUPPORT_FUNC(expr) IsGpuFsaBackendSupported(reasonIfUnsupported) |
| 49 | #endif |
| 50 | |
| 51 | #if defined(ARMCOMPUTEGPUFSA_ENABLED) |
| 52 | template<class FuncType, class... Args> |
| 53 | inline bool CheckIsLayerSupported(FuncType&& func, Optional<std::string&> reasonIfUnsupported, Args&&... args) |
| 54 | { |
| 55 | arm_compute::Status aclStatus = func(std::forward<Args>(args)...); |
| 56 | const bool supported = (aclStatus.error_code() == arm_compute::ErrorCode::OK); |
| 57 | if (!supported && reasonIfUnsupported) |
| 58 | { |
| 59 | reasonIfUnsupported.value() = aclStatus.error_description(); |
| 60 | } |
| 61 | return supported; |
| 62 | } |
| 63 | |
| 64 | #define FORWARD_LAYER_VALIDATE_FUNC(func, reasonIfUnsupported, ...) \ |
| 65 | return CheckIsLayerSupported(func, reasonIfUnsupported, __VA_ARGS__); |
| 66 | #else |
| 67 | #define FORWARD_LAYER_VALIDATE_FUNC(func, reasonIfUnsupported, ...) \ |
| 68 | return IsGpuFsaBackendSupported(reasonIfUnsupported, __VA_ARGS__); |
| 69 | #endif |
| 70 | |
| 71 | bool GpuFsaLayerSupport::IsLayerSupported(const LayerType& type, |
| 72 | const std::vector<TensorInfo>& infos, |
| 73 | const BaseDescriptor& descriptor, |
| 74 | const Optional<LstmInputParamsInfo>& lstmParamsInfo, |
| 75 | const Optional<QuantizedLstmInputParamsInfo>& quantizedLstmInputParamsInfo, |
| 76 | Optional<std::string&> reasonIfUnsupported) const |
| 77 | { |
| 78 | IgnoreUnused(lstmParamsInfo); |
| 79 | IgnoreUnused(quantizedLstmInputParamsInfo); |
| 80 | |
Tracy Narine | bc5a5d5 | 2024-02-06 15:22:41 +0000 | [diff] [blame] | 81 | switch (type) |
| 82 | { |
Teresa Charlin | ddbda6a | 2024-02-07 22:58:29 +0000 | [diff] [blame] | 83 | case LayerType::Activation: |
| 84 | { |
| 85 | if (infos.size() != 2) |
| 86 | { |
| 87 | throw InvalidArgumentException("Invalid number of Activation TensorInfos. " |
| 88 | "TensorInfos should be of format: {input, output}."); |
| 89 | } |
| 90 | |
| 91 | auto desc = PolymorphicDowncast<const ActivationDescriptor*>(&descriptor); |
| 92 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaActivationValidate, |
| 93 | reasonIfUnsupported, |
| 94 | infos[0], |
| 95 | *desc); |
| 96 | } |
Teresa Charlin | 5bda973 | 2024-02-08 18:46:38 +0000 | [diff] [blame] | 97 | case LayerType::BatchMatMul: |
| 98 | { |
| 99 | if (infos.size() != 3) |
| 100 | { |
| 101 | throw InvalidArgumentException("Invalid number of BatchMatMul TensorInfos. " |
| 102 | "TensorInfos should be of format: {input0, input1 output}."); |
| 103 | } |
| 104 | |
| 105 | auto desc = PolymorphicDowncast<const BatchMatMulDescriptor*>(&descriptor); |
Teresa Charlin | 5bda973 | 2024-02-08 18:46:38 +0000 | [diff] [blame] | 106 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaBatchMatMulValidate, |
| 107 | reasonIfUnsupported, |
| 108 | infos[0], |
| 109 | infos[1], |
| 110 | *desc); |
| 111 | } |
Tracy Narine | bc5a5d5 | 2024-02-06 15:22:41 +0000 | [diff] [blame] | 112 | case LayerType::Cast: |
| 113 | { |
| 114 | if (infos.size() != 2) |
| 115 | { |
| 116 | throw InvalidArgumentException("Invalid number of cast TensorInfos. " |
| 117 | "TensorInfos should be of format: {input, output}."); |
| 118 | } |
Teresa Charlin | ddbda6a | 2024-02-07 22:58:29 +0000 | [diff] [blame] | 119 | |
Tracy Narine | bc5a5d5 | 2024-02-06 15:22:41 +0000 | [diff] [blame] | 120 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaCastValidate, |
| 121 | reasonIfUnsupported, |
| 122 | infos[0], |
| 123 | infos[1]); |
| 124 | } |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 125 | case LayerType::Convolution2d: |
| 126 | { |
| 127 | if (infos.size() != 4) |
| 128 | { |
| 129 | throw InvalidArgumentException("Invalid number of Convolution2d TensorInfos. " |
| 130 | "TensorInfos should be of format: {input, output, weights, biases}."); |
| 131 | } |
| 132 | |
Tracy Narine | e7d2785 | 2024-01-26 09:13:19 +0000 | [diff] [blame] | 133 | auto desc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor); |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 134 | if (infos[3] == TensorInfo()) |
| 135 | { |
| 136 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaConvolution2dValidate, |
| 137 | reasonIfUnsupported, |
| 138 | infos[0], |
Tracy Narine | e7d2785 | 2024-01-26 09:13:19 +0000 | [diff] [blame] | 139 | *desc, |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 140 | infos[2], |
| 141 | EmptyOptional()); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaConvolution2dValidate, |
| 146 | reasonIfUnsupported, |
| 147 | infos[0], |
Tracy Narine | e7d2785 | 2024-01-26 09:13:19 +0000 | [diff] [blame] | 148 | *desc, |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 149 | infos[2], |
| 150 | infos[3]); |
| 151 | } |
| 152 | } |
Tianle Cheng | fbfa49e | 2024-01-23 11:21:48 +0000 | [diff] [blame] | 153 | case LayerType::DepthwiseConvolution2d: |
| 154 | { |
| 155 | if (infos.size() != 4) |
| 156 | { |
| 157 | throw InvalidArgumentException("Invalid number of DepthwiseConvolution2dDescriptor TensorInfos. " |
| 158 | "TensorInfos should be of format: {input, output, weights, biases}."); |
| 159 | } |
| 160 | |
Tracy Narine | bc5a5d5 | 2024-02-06 15:22:41 +0000 | [diff] [blame] | 161 | auto desc = PolymorphicDowncast<const DepthwiseConvolution2dDescriptor*>(&descriptor); |
Tianle Cheng | fbfa49e | 2024-01-23 11:21:48 +0000 | [diff] [blame] | 162 | if (infos[3] == TensorInfo()) |
| 163 | { |
| 164 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaDepthwiseConvolution2dValidate, |
| 165 | reasonIfUnsupported, |
| 166 | infos[0], |
Tracy Narine | e7d2785 | 2024-01-26 09:13:19 +0000 | [diff] [blame] | 167 | *desc, |
Tianle Cheng | fbfa49e | 2024-01-23 11:21:48 +0000 | [diff] [blame] | 168 | infos[2], |
| 169 | EmptyOptional()); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaDepthwiseConvolution2dValidate, |
| 174 | reasonIfUnsupported, |
| 175 | infos[0], |
Tracy Narine | e7d2785 | 2024-01-26 09:13:19 +0000 | [diff] [blame] | 176 | *desc, |
Tianle Cheng | fbfa49e | 2024-01-23 11:21:48 +0000 | [diff] [blame] | 177 | infos[2], |
| 178 | infos[3]); |
| 179 | } |
| 180 | } |
Tracy Narine | e7d2785 | 2024-01-26 09:13:19 +0000 | [diff] [blame] | 181 | case LayerType::ElementwiseBinary: |
| 182 | { |
| 183 | if (infos.size() != 3) |
| 184 | { |
| 185 | throw InvalidArgumentException("Invalid number of ElementwiseBinary TensorInfos. " |
Teresa Charlin | 20dda37 | 2024-02-08 16:23:25 +0000 | [diff] [blame] | 186 | "TensorInfos should be of format: {input0, input1, output}."); |
Tracy Narine | e7d2785 | 2024-01-26 09:13:19 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Tracy Narine | bc5a5d5 | 2024-02-06 15:22:41 +0000 | [diff] [blame] | 189 | auto desc = PolymorphicDowncast<const ElementwiseBinaryDescriptor*>(&descriptor); |
Teresa Charlin | 20dda37 | 2024-02-08 16:23:25 +0000 | [diff] [blame] | 190 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaElementwiseBinaryValidate, |
| 191 | reasonIfUnsupported, |
| 192 | infos[0], |
| 193 | infos[1], |
| 194 | *desc); |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame] | 195 | } |
| 196 | case LayerType::Pooling2d: |
| 197 | { |
| 198 | if (infos.size() != 2) |
| 199 | { |
| 200 | throw InvalidArgumentException("Invalid number of Pooling2d TensorInfos. " |
| 201 | "TensorInfos should be of format: {input, output}."); |
| 202 | } |
| 203 | |
| 204 | auto desc = PolymorphicDowncast<const Pooling2dDescriptor*>(&descriptor); |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame] | 205 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaPooling2dValidate, |
| 206 | reasonIfUnsupported, |
| 207 | infos[0], |
| 208 | *desc); |
Tracy Narine | e7d2785 | 2024-01-26 09:13:19 +0000 | [diff] [blame] | 209 | } |
Declan-ARM | 5e90b83 | 2024-02-07 13:07:31 +0000 | [diff] [blame] | 210 | case LayerType::Reshape: |
| 211 | { |
| 212 | if (infos.size() != 2) |
| 213 | { |
| 214 | throw InvalidArgumentException("Invalid number of Reshape TensorInfos. " |
| 215 | "TensorInfos should be of format: { input, output }."); |
| 216 | } |
| 217 | |
| 218 | auto desc = PolymorphicDowncast<const ReshapeDescriptor*>(&descriptor); |
| 219 | |
| 220 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaReshapeValidate, |
| 221 | reasonIfUnsupported, |
| 222 | infos[0], |
| 223 | *desc); |
| 224 | } |
Teresa Charlin | 1d6b731 | 2024-02-07 22:02:48 +0000 | [diff] [blame] | 225 | case LayerType::Resize: |
| 226 | { |
| 227 | if (infos.size() != 2) |
| 228 | { |
| 229 | throw InvalidArgumentException("Invalid number of Resize TensorInfos. " |
| 230 | "TensorInfos should be of format: {input, output}."); |
| 231 | } |
| 232 | |
| 233 | auto desc = PolymorphicDowncast<const ResizeDescriptor*>(&descriptor); |
Teresa Charlin | 1d6b731 | 2024-02-07 22:02:48 +0000 | [diff] [blame] | 234 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaResizeValidate, |
| 235 | reasonIfUnsupported, |
| 236 | infos[0], |
| 237 | *desc); |
| 238 | } |
John Mcloughlin | 3375390 | 2024-02-07 15:00:57 +0000 | [diff] [blame] | 239 | case LayerType::Softmax: |
| 240 | { |
| 241 | if (infos.size() != 2) |
| 242 | { |
| 243 | throw InvalidArgumentException("Invalid number of Softmax TensorInfos. " |
| 244 | "TensorInfos should be of format: {input, output}."); |
| 245 | } |
| 246 | |
| 247 | auto desc = PolymorphicDowncast<const SoftmaxDescriptor*>(&descriptor); |
| 248 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaSoftmaxValidate, |
| 249 | reasonIfUnsupported, |
| 250 | infos[0], |
| 251 | infos[1], |
| 252 | *desc); |
| 253 | } |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 254 | case LayerType::Constant: |
| 255 | case LayerType::Input: |
| 256 | case LayerType::Output: |
| 257 | return IsGpuFsaBackendSupported(reasonIfUnsupported, infos[0]); |
| 258 | default: |
| 259 | // Layers not supported in the GpuFsa backend. |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | } // namespace armnn |