David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2024 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "GpuFsaConvolution2d.hpp" |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame^] | 7 | #include "UtilsGpuFsa.hpp" |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 8 | |
| 9 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 10 | |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 11 | #include <arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h> |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame^] | 12 | #include <arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h> |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 13 | #include <arm_compute/dynamic_fusion/sketch/gpu/operators/GpuConv2d.h> |
| 14 | #include <arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h> |
| 15 | |
| 16 | #include <vector> |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 17 | |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame^] | 18 | using namespace arm_compute::experimental::dynamic_fusion; |
| 19 | using namespace armnn::armcomputetensorutils; |
| 20 | |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 21 | namespace armnn |
| 22 | { |
| 23 | |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 24 | arm_compute::Status GpuFsaConvolution2dValidate(const TensorInfo& input, |
| 25 | const Convolution2dDescriptor& descriptor, |
| 26 | const TensorInfo& weights, |
| 27 | const Optional<TensorInfo>& biases) |
| 28 | { |
| 29 | // Create a new workload sketch, for validation purposes |
| 30 | auto compileCtx = arm_compute::CLKernelLibrary::get().get_compile_context(); |
| 31 | auto workloadContext = GpuWorkloadContext(&compileCtx); |
| 32 | GpuWorkloadSketch sketch{ &workloadContext }; |
| 33 | |
| 34 | // Build and create tensor infos using the sketch |
| 35 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 36 | arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout); |
| 37 | aclWeightsInfo.set_are_values_constant(weights.IsConstant()); |
| 38 | |
| 39 | auto inputInfo = workloadContext.create_tensor_info(aclInputInfo); |
| 40 | auto weightInfo = workloadContext.create_tensor_info(aclWeightsInfo); |
| 41 | |
| 42 | // Only create the bias tensor info if enabled, otherwise pass nullptr to validate_op |
| 43 | arm_compute::TensorInfo aclBiasInfo; |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 44 | arm_compute::ITensorInfo* biasSketchInfoPtr = nullptr; |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 45 | |
| 46 | if (descriptor.m_BiasEnabled) |
| 47 | { |
| 48 | if(!biases.has_value()) |
| 49 | { |
| 50 | throw InvalidArgumentException("GpuFsaConvolution2d::ValidateOp: No biases set when biases are enabled"); |
| 51 | } |
| 52 | aclBiasInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
| 53 | aclBiasInfo.set_are_values_constant(biases.value().IsConstant()); |
| 54 | |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 55 | biasSketchInfoPtr = workloadContext.create_tensor_info(aclBiasInfo); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame^] | 58 | Conv2dAttributes conv2dAttributes = CreateConv2dAttributes(descriptor); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 59 | |
| 60 | // Validate operator, check status and update reasonIfUnsupported |
| 61 | arm_compute::Status aclStatus = GpuConv2d::validate_op(sketch, |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 62 | inputInfo, |
| 63 | weightInfo, |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 64 | biasSketchInfoPtr, |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame^] | 65 | conv2dAttributes); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 66 | |
| 67 | return aclStatus; |
| 68 | } |
| 69 | |
| 70 | void GpuFsaConvolution2dCreateOp(GpuFsaPreCompiledBlob* blob, |
| 71 | const TensorInfo& input, |
| 72 | const Convolution2dDescriptor& descriptor, |
| 73 | const TensorInfo& weights, |
| 74 | const Optional<TensorInfo>& biases) |
| 75 | { |
| 76 | /* |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 77 | * Creating an Op for the GpuFsa backend requires us to create and maintain quite a bit of data, which is then stored |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 78 | * in a GpuFsaPreCompiledBlob for execution later. Specifically we need: |
| 79 | * GpuWorkloadContext, this contains the TensorInfos and is unique to the Graph being executed |
| 80 | * Sketch, this is similar to a subgraph and can contain one or more operations. Multiple ops can be "fused" together |
| 81 | * using a single sketch. |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 82 | * The inputTensorinfos / outputTensorInfos, these are pointers to the TensorInfos used when creating the sketch. |
| 83 | * They refer to the TensorInfos stored within the GpuWorkloadContext and are needed when executing the sketch |
| 84 | * as the TensorInfos used when creating the Tensors must match those used to create the Sketch. Otherwise the runtime |
| 85 | * doesn't know which Tensors to use. |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 86 | */ |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 87 | GpuWorkloadSketch* sketch = blob->sketch.get(); |
| 88 | GpuWorkloadContext* workloadContext = blob->workloadContext.get(); |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 89 | std::vector<arm_compute::ITensorInfo*> inputTensorInfos = {}; |
| 90 | std::vector<arm_compute::ITensorInfo*> outputTensorInfos = {}; |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 91 | |
| 92 | // Build and create tensor infos using the sketch |
| 93 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 94 | arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout); |
| 95 | aclWeightsInfo.set_are_values_constant(weights.IsConstant()); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 96 | |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 97 | inputTensorInfos.emplace_back(workloadContext->create_tensor_info(aclInputInfo)); |
| 98 | inputTensorInfos.emplace_back(workloadContext->create_tensor_info(aclWeightsInfo)); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 99 | |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 100 | // Only create the bias tensor info if enabled, otherwise pass nullptr to validate_op / create_op |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 101 | arm_compute::TensorInfo aclBiasInfo; |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 102 | arm_compute::ITensorInfo* biasSketchInfoPtr = nullptr; |
| 103 | |
| 104 | if (descriptor.m_BiasEnabled) |
| 105 | { |
| 106 | if(!biases.has_value()) |
| 107 | { |
| 108 | throw InvalidArgumentException("GpuFsaConvolution2d::CreateOp: No biases set when biases are enabled"); |
| 109 | } |
| 110 | aclBiasInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
| 111 | aclBiasInfo.set_are_values_constant(biases.value().IsConstant()); |
| 112 | |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 113 | inputTensorInfos.emplace_back(workloadContext->create_tensor_info(aclBiasInfo)); |
| 114 | biasSketchInfoPtr = inputTensorInfos[2]; |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame^] | 117 | Conv2dAttributes conv2dAttributes = CreateConv2dAttributes(descriptor); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 118 | |
| 119 | // Validate operator, check status and update reasonIfUnsupported |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 120 | arm_compute::Status aclStatus = GpuConv2d::validate_op(*sketch, |
| 121 | inputTensorInfos[0], |
| 122 | inputTensorInfos[1], |
| 123 | biasSketchInfoPtr, |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame^] | 124 | conv2dAttributes); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 125 | |
| 126 | const bool supported = (aclStatus.error_code() == arm_compute::ErrorCode::OK); |
| 127 | if (!supported) |
| 128 | { |
| 129 | throw BackendCapabilityException("\"GpuFsa\" backend failed during Convolution2D operation validation"); |
| 130 | } |
| 131 | |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 132 | // Create the Op within the Sketch using the TensorInfos we have stored |
| 133 | arm_compute::ITensorInfo* convOutInfo = GpuConv2d::create_op(*sketch, |
| 134 | inputTensorInfos[0], |
| 135 | inputTensorInfos[1], |
| 136 | biasSketchInfoPtr, |
Teresa Charlin | a52bca2 | 2024-02-01 17:36:48 +0000 | [diff] [blame^] | 137 | conv2dAttributes); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 138 | |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 139 | // Create the Output |
| 140 | outputTensorInfos.emplace_back(workloadContext->create_tensor_info()); |
| 141 | GpuOutput::create_op(*sketch, convOutInfo, outputTensorInfos[0]); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 142 | |
Orlaith Monahan | e1ac869 | 2024-01-23 13:52:30 +0000 | [diff] [blame] | 143 | // Store the TensorInfos within the blob as unique_ptrs to be used later |
| 144 | blob->inputTensorInfos = std::make_unique<std::vector<arm_compute::ITensorInfo*>>(inputTensorInfos); |
| 145 | blob->outputTensorInfos = std::make_unique<std::vector<arm_compute::ITensorInfo*>>(outputTensorInfos); |
David Monahan | bd73808 | 2023-12-08 12:50:02 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace armnn |