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