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