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) |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame^] | 13 | #include "layers/GpuFsaConvolution2d.hpp" |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace armnn |
| 19 | { |
| 20 | |
| 21 | template<typename ... Args> |
| 22 | bool IsGpuFsaBackendSupported(Optional<std::string&> reasonIfUnsupported, Args... args) |
| 23 | { |
| 24 | IgnoreUnused(reasonIfUnsupported, (args)...); |
| 25 | #if defined(ARMCOMPUTEGPUFSA_ENABLED) |
| 26 | return true; |
| 27 | #else |
| 28 | if (reasonIfUnsupported) |
| 29 | { |
| 30 | reasonIfUnsupported.value() = "The armnn library has been built without CL support"; |
| 31 | } |
| 32 | return false; |
| 33 | #endif |
| 34 | } |
| 35 | |
| 36 | #if defined(ARMCOMPUTEGPUFSA_ENABLED) |
| 37 | #define FORWARD_GPUFSA_LAYER_SUPPORT_FUNC(expr) (expr) |
| 38 | #else |
| 39 | #define FORWARD_GPUFSA_LAYER_SUPPORT_FUNC(expr) IsGpuFsaBackendSupported(reasonIfUnsupported) |
| 40 | #endif |
| 41 | |
| 42 | #if defined(ARMCOMPUTEGPUFSA_ENABLED) |
| 43 | template<class FuncType, class... Args> |
| 44 | inline bool CheckIsLayerSupported(FuncType&& func, Optional<std::string&> reasonIfUnsupported, Args&&... args) |
| 45 | { |
| 46 | arm_compute::Status aclStatus = func(std::forward<Args>(args)...); |
| 47 | const bool supported = (aclStatus.error_code() == arm_compute::ErrorCode::OK); |
| 48 | if (!supported && reasonIfUnsupported) |
| 49 | { |
| 50 | reasonIfUnsupported.value() = aclStatus.error_description(); |
| 51 | } |
| 52 | return supported; |
| 53 | } |
| 54 | |
| 55 | #define FORWARD_LAYER_VALIDATE_FUNC(func, reasonIfUnsupported, ...) \ |
| 56 | return CheckIsLayerSupported(func, reasonIfUnsupported, __VA_ARGS__); |
| 57 | #else |
| 58 | #define FORWARD_LAYER_VALIDATE_FUNC(func, reasonIfUnsupported, ...) \ |
| 59 | return IsGpuFsaBackendSupported(reasonIfUnsupported, __VA_ARGS__); |
| 60 | #endif |
| 61 | |
| 62 | bool GpuFsaLayerSupport::IsLayerSupported(const LayerType& type, |
| 63 | const std::vector<TensorInfo>& infos, |
| 64 | const BaseDescriptor& descriptor, |
| 65 | const Optional<LstmInputParamsInfo>& lstmParamsInfo, |
| 66 | const Optional<QuantizedLstmInputParamsInfo>& quantizedLstmInputParamsInfo, |
| 67 | Optional<std::string&> reasonIfUnsupported) const |
| 68 | { |
| 69 | IgnoreUnused(lstmParamsInfo); |
| 70 | IgnoreUnused(quantizedLstmInputParamsInfo); |
| 71 | |
| 72 | switch (type) { |
| 73 | case LayerType::Convolution2d: |
| 74 | { |
| 75 | if (infos.size() != 4) |
| 76 | { |
| 77 | throw InvalidArgumentException("Invalid number of Convolution2d TensorInfos. " |
| 78 | "TensorInfos should be of format: {input, output, weights, biases}."); |
| 79 | } |
| 80 | |
| 81 | auto desc = *(PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor)); |
| 82 | if (infos[3] == TensorInfo()) |
| 83 | { |
| 84 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaConvolution2dValidate, |
| 85 | reasonIfUnsupported, |
| 86 | infos[0], |
| 87 | desc, |
| 88 | infos[2], |
| 89 | EmptyOptional()); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | FORWARD_LAYER_VALIDATE_FUNC(GpuFsaConvolution2dValidate, |
| 94 | reasonIfUnsupported, |
| 95 | infos[0], |
| 96 | desc, |
| 97 | infos[2], |
| 98 | infos[3]); |
| 99 | } |
| 100 | } |
| 101 | case LayerType::Constant: |
| 102 | case LayerType::Input: |
| 103 | case LayerType::Output: |
| 104 | return IsGpuFsaBackendSupported(reasonIfUnsupported, infos[0]); |
| 105 | default: |
| 106 | // Layers not supported in the GpuFsa backend. |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | } // namespace armnn |