blob: e9409634ed1f26407502037f6774bd48c8ccecd0 [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"
Teresa Charlina52bca22024-02-01 17:36:48 +00007#include "UtilsGpuFsa.hpp"
David Monahanbd738082023-12-08 12:50:02 +00008
9#include <aclCommon/ArmComputeTensorUtils.hpp>
10
David Monahanbd738082023-12-08 12:50:02 +000011#include <arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h>
Teresa Charlina52bca22024-02-01 17:36:48 +000012#include <arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h>
David Monahanbd738082023-12-08 12:50:02 +000013#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 Monahanbd738082023-12-08 12:50:02 +000017
Teresa Charlina52bca22024-02-01 17:36:48 +000018using namespace arm_compute::experimental::dynamic_fusion;
19using namespace armnn::armcomputetensorutils;
20
David Monahanbd738082023-12-08 12:50:02 +000021namespace armnn
22{
23
David Monahanbd738082023-12-08 12:50:02 +000024arm_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 Monahane1ac8692024-01-23 13:52:30 +000044 arm_compute::ITensorInfo* biasSketchInfoPtr = nullptr;
David Monahanbd738082023-12-08 12:50:02 +000045
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 Monahane1ac8692024-01-23 13:52:30 +000055 biasSketchInfoPtr = workloadContext.create_tensor_info(aclBiasInfo);
David Monahanbd738082023-12-08 12:50:02 +000056 }
57
Teresa Charlina52bca22024-02-01 17:36:48 +000058 Conv2dAttributes conv2dAttributes = CreateConv2dAttributes(descriptor);
David Monahanbd738082023-12-08 12:50:02 +000059
60 // Validate operator, check status and update reasonIfUnsupported
61 arm_compute::Status aclStatus = GpuConv2d::validate_op(sketch,
Orlaith Monahane1ac8692024-01-23 13:52:30 +000062 inputInfo,
63 weightInfo,
David Monahanbd738082023-12-08 12:50:02 +000064 biasSketchInfoPtr,
Teresa Charlina52bca22024-02-01 17:36:48 +000065 conv2dAttributes);
David Monahanbd738082023-12-08 12:50:02 +000066
67 return aclStatus;
68}
69
70void GpuFsaConvolution2dCreateOp(GpuFsaPreCompiledBlob* blob,
71 const TensorInfo& input,
72 const Convolution2dDescriptor& descriptor,
73 const TensorInfo& weights,
74 const Optional<TensorInfo>& biases)
75{
76/*
Orlaith Monahane1ac8692024-01-23 13:52:30 +000077 * Creating an Op for the GpuFsa backend requires us to create and maintain quite a bit of data, which is then stored
David Monahanbd738082023-12-08 12:50:02 +000078 * 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 Monahane1ac8692024-01-23 13:52:30 +000082 * 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 Monahanbd738082023-12-08 12:50:02 +000086 */
David Monahanbd738082023-12-08 12:50:02 +000087 GpuWorkloadSketch* sketch = blob->sketch.get();
88 GpuWorkloadContext* workloadContext = blob->workloadContext.get();
Orlaith Monahane1ac8692024-01-23 13:52:30 +000089 std::vector<arm_compute::ITensorInfo*> inputTensorInfos = {};
90 std::vector<arm_compute::ITensorInfo*> outputTensorInfos = {};
David Monahanbd738082023-12-08 12:50:02 +000091
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 Monahanbd738082023-12-08 12:50:02 +000096
Orlaith Monahane1ac8692024-01-23 13:52:30 +000097 inputTensorInfos.emplace_back(workloadContext->create_tensor_info(aclInputInfo));
98 inputTensorInfos.emplace_back(workloadContext->create_tensor_info(aclWeightsInfo));
David Monahanbd738082023-12-08 12:50:02 +000099
Orlaith Monahane1ac8692024-01-23 13:52:30 +0000100 // Only create the bias tensor info if enabled, otherwise pass nullptr to validate_op / create_op
David Monahanbd738082023-12-08 12:50:02 +0000101 arm_compute::TensorInfo aclBiasInfo;
David Monahanbd738082023-12-08 12:50:02 +0000102 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 Monahane1ac8692024-01-23 13:52:30 +0000113 inputTensorInfos.emplace_back(workloadContext->create_tensor_info(aclBiasInfo));
114 biasSketchInfoPtr = inputTensorInfos[2];
David Monahanbd738082023-12-08 12:50:02 +0000115 }
116
Teresa Charlina52bca22024-02-01 17:36:48 +0000117 Conv2dAttributes conv2dAttributes = CreateConv2dAttributes(descriptor);
David Monahanbd738082023-12-08 12:50:02 +0000118
119 // Validate operator, check status and update reasonIfUnsupported
Orlaith Monahane1ac8692024-01-23 13:52:30 +0000120 arm_compute::Status aclStatus = GpuConv2d::validate_op(*sketch,
121 inputTensorInfos[0],
122 inputTensorInfos[1],
123 biasSketchInfoPtr,
Teresa Charlina52bca22024-02-01 17:36:48 +0000124 conv2dAttributes);
David Monahanbd738082023-12-08 12:50:02 +0000125
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 Monahane1ac8692024-01-23 13:52:30 +0000132 // 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 Charlina52bca22024-02-01 17:36:48 +0000137 conv2dAttributes);
David Monahanbd738082023-12-08 12:50:02 +0000138
Orlaith Monahane1ac8692024-01-23 13:52:30 +0000139 // Create the Output
140 outputTensorInfos.emplace_back(workloadContext->create_tensor_info());
141 GpuOutput::create_op(*sketch, convOutInfo, outputTensorInfos[0]);
David Monahanbd738082023-12-08 12:50:02 +0000142
Orlaith Monahane1ac8692024-01-23 13:52:30 +0000143 // 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 Monahanbd738082023-12-08 12:50:02 +0000146}
147
148} // namespace armnn