blob: c7137d7ac8d7b0d04767d21b5e2c2bcf265fc48f [file] [log] [blame]
David Monahanbd738082023-12-08 12:50:02 +00001//
2// Copyright © 2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "GpuFsaConvolution2d.hpp"
7
8#include <armnn/Types.hpp>
9
10#include <aclCommon/ArmComputeTensorUtils.hpp>
11
12#include <arm_compute/core/ITensorInfo.h>
13#include <arm_compute/core/TensorInfo.h>
14#include <arm_compute/core/TensorShape.h>
15#include <arm_compute/core/CL/CLKernelLibrary.h>
16#include <arm_compute/core/CL/CLCompileContext.h>
17
18#include <arm_compute/dynamic_fusion/runtime/gpu/cl/ClWorkloadRuntime.h>
19#include <arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h>
20#include <src/dynamic_fusion/sketch/gpu/GpuWorkloadContextImpl.h>
21#include <arm_compute/dynamic_fusion/sketch/gpu/operators/GpuConv2d.h>
22#include <arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h>
23
24#include <vector>
25#include <iostream>
26
27namespace armnn
28{
29
30using namespace armcomputetensorutils;
31
32arm_compute::Status GpuFsaConvolution2dValidate(const TensorInfo& input,
33 const Convolution2dDescriptor& descriptor,
34 const TensorInfo& weights,
35 const Optional<TensorInfo>& biases)
36{
37 // Create a new workload sketch, for validation purposes
38 auto compileCtx = arm_compute::CLKernelLibrary::get().get_compile_context();
39 auto workloadContext = GpuWorkloadContext(&compileCtx);
40 GpuWorkloadSketch sketch{ &workloadContext };
41
42 // Build and create tensor infos using the sketch
43 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
44 arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
45 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
46
47 auto inputInfo = workloadContext.create_tensor_info(aclInputInfo);
48 auto weightInfo = workloadContext.create_tensor_info(aclWeightsInfo);
49
50 // Only create the bias tensor info if enabled, otherwise pass nullptr to validate_op
51 arm_compute::TensorInfo aclBiasInfo;
52 arm_compute::TensorInfo biasSketchInfo;
53 arm_compute::TensorInfo* biasSketchInfoPtr = nullptr;
54
55 if (descriptor.m_BiasEnabled)
56 {
57 if(!biases.has_value())
58 {
59 throw InvalidArgumentException("GpuFsaConvolution2d::ValidateOp: No biases set when biases are enabled");
60 }
61 aclBiasInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
62 aclBiasInfo.set_are_values_constant(biases.value().IsConstant());
63
64 biasSketchInfo = workloadContext.create_tensor_info(aclBiasInfo);
65 biasSketchInfoPtr = &biasSketchInfo;
66 }
67
68 // Set Conv2d attributes using descriptor
69 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
70 descriptor.m_DilationY);
71 const arm_compute::Padding2D aclPadInfo = BuildArmComputePaddingInfo(descriptor);
72 const arm_compute::Size2D aclStrideInfo = BuildArmComputeSize2D(descriptor.m_StrideX, descriptor.m_StrideY);
73
74 Conv2dAttributes conv2DAttributes{};
75 conv2DAttributes.dilation(aclDilationInfo);
76 conv2DAttributes.pad(aclPadInfo);
77 conv2DAttributes.stride(aclStrideInfo);
78
79 // Validate operator, check status and update reasonIfUnsupported
80 arm_compute::Status aclStatus = GpuConv2d::validate_op(sketch,
81 &inputInfo,
82 &weightInfo,
83 biasSketchInfoPtr,
84 conv2DAttributes);
85
86 return aclStatus;
87}
88
89void GpuFsaConvolution2dCreateOp(GpuFsaPreCompiledBlob* blob,
90 const TensorInfo& input,
91 const Convolution2dDescriptor& descriptor,
92 const TensorInfo& weights,
93 const Optional<TensorInfo>& biases)
94{
95/*
96 * Creating an Op for the GpuFds backend requires us to create and maintain quite a bit of data, which is then stored
97 * in a GpuFsaPreCompiledBlob for execution later. Specifically we need:
98 * GpuWorkloadContext, this contains the TensorInfos and is unique to the Graph being executed
99 * Sketch, this is similar to a subgraph and can contain one or more operations. Multiple ops can be "fused" together
100 * using a single sketch.
101 * The TensorInfoIds, these are the ids of the TensorInfos used when creating the sketch. They refer to the TensorInfos
102 * stored within the GpuWorkloadContext and are used to fetch them later when executing the sketch.
103 */
104 using namespace arm_compute::experimental::dynamic_fusion;
105 GpuWorkloadSketch* sketch = blob->sketch.get();
106 GpuWorkloadContext* workloadContext = blob->workloadContext.get();
107 std::vector<int32_t> inputIds = {};
108 std::vector<int32_t> outputIds = {};
109
110 // Build and create tensor infos using the sketch
111 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
112 arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout);
113 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
114 auto inputInfo = workloadContext->create_tensor_info(aclInputInfo);
115 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
116 inputIds.emplace_back(inputInfo.id());
117
118 auto weightInfo = workloadContext->create_tensor_info(aclWeightsInfo);
119 inputIds.emplace_back(weightInfo.id());
120
121 // Only create the bias tensor info if enabled, otherwise pass nullptr to validate_op
122 arm_compute::TensorInfo aclBiasInfo;
123 arm_compute::TensorInfo biasSketchInfo;
124 arm_compute::ITensorInfo* biasSketchInfoPtr = nullptr;
125
126 if (descriptor.m_BiasEnabled)
127 {
128 if(!biases.has_value())
129 {
130 throw InvalidArgumentException("GpuFsaConvolution2d::CreateOp: No biases set when biases are enabled");
131 }
132 aclBiasInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
133 aclBiasInfo.set_are_values_constant(biases.value().IsConstant());
134
135 biasSketchInfo = workloadContext->create_tensor_info(aclBiasInfo);
136 inputIds.emplace_back(biasSketchInfo.id());
137 biasSketchInfoPtr = workloadContext->implementation().get_tensor_info(biasSketchInfo.id());
138 }
139
140 // Set Conv2d attributes using descriptor
141 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX,
142 descriptor.m_DilationY);
143 const arm_compute::Padding2D aclPadInfo = BuildArmComputePaddingInfo(descriptor);
144 const arm_compute::Size2D aclStrideInfo = BuildArmComputeSize2D(descriptor.m_StrideX, descriptor.m_StrideY);
145
146 Conv2dAttributes conv2DAttributes{};
147 conv2DAttributes.dilation(aclDilationInfo);
148 conv2DAttributes.pad(aclPadInfo);
149 conv2DAttributes.stride(aclStrideInfo);
150
151 // Validate operator, check status and update reasonIfUnsupported
152 arm_compute::Status aclStatus =
153 GpuConv2d::validate_op(*sketch,
154 workloadContext->implementation().get_tensor_info(inputInfo.id()),
155 workloadContext->implementation().get_tensor_info(weightInfo.id()),
156 biasSketchInfoPtr,
157 conv2DAttributes);
158
159 const bool supported = (aclStatus.error_code() == arm_compute::ErrorCode::OK);
160 if (!supported)
161 {
162 throw BackendCapabilityException("\"GpuFsa\" backend failed during Convolution2D operation validation");
163 }
164
165 arm_compute::ITensorInfo* convOutInfo =
166 GpuConv2d::create_op(*sketch,
167 workloadContext->implementation().get_tensor_info(inputInfo.id()),
168 workloadContext->implementation().get_tensor_info(weightInfo.id()),
169 biasSketchInfoPtr,
170 conv2DAttributes);
171
172 arm_compute::TensorInfo outputDstInfo = workloadContext->create_tensor_info();
173 outputIds.emplace_back(outputDstInfo.id());
174
175 GpuOutput::create_op(*sketch, convOutInfo, workloadContext->implementation().get_tensor_info(outputDstInfo.id()));
176 blob->inputIds = std::make_unique<std::vector<int32_t>>(inputIds);
177 blob->outputIds = std::make_unique<std::vector<int32_t>>(outputIds);
178}
179
180} // namespace armnn