ArmNN
 25.11
Loading...
Searching...
No Matches
ClDepthwiseConvolutionWorkload.cpp
Go to the documentation of this file.
1//
2// Copyright © 2017-2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
7
8#include <ResolveType.hpp>
9#include "ClWorkloadUtils.hpp"
10
11#include <armnn/Exceptions.hpp>
14#include <cl/ClTensorHandle.hpp>
18
19#include <arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h>
20
21namespace armnn
22{
23
24using namespace armcomputetensorutils;
25
26arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo& input,
27 const TensorInfo& output,
28 const DepthwiseConvolution2dDescriptor& descriptor,
29 const TensorInfo& weights,
30 const Optional<TensorInfo>& biases,
31 const ActivationDescriptor* activationDescriptor)
32{
33 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
34 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
35
36 // ArmNN format for weights for depthwise is [1, H, W, C] independently of the input/output layout
37 //
38 // ACL format for weights for depthwise is:
39 // - [1, H, W, C] for [N, H, W, C] input/output layout (matches with ArmNN)
40 // - [1, C, H, W] for [N, C, H, W] input/output layout
41 //
42 // Therefore ArmNN weights have to be permuted when input/output layout is [N, C, H, W] to pass them to ACL.
43 // The PermuteDepthwiseConv2dWeights backend optimization takes care of this, but it has not been performed yet,
44 // so we do the permute here for the TensorInfo weights.
45 unsigned int aclDepthMultiplier;
46 TensorInfo weightsPermuted;
47 std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input,descriptor.m_DataLayout);
48
49 // Convert the weights into the compute library format
50 arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
51 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
52
53 arm_compute::TensorInfo aclBiasesInfo;
54 arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr;
55 if (descriptor.m_BiasEnabled)
56 {
57 if (!biases.has_value())
58 {
59 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
60 "ArmNN ClDepthwiseConv2dWorkload has empty bias value."};
61 }
62 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
63 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
64 optionalAclBiasesInfo = &aclBiasesInfo;
65 }
66
67 const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
68 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
69 descriptor.m_DilationX,
70 descriptor.m_DilationY);
71
72 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
73 activationDescriptor);
74
75 return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo,
76 &aclWeightsInfo,
77 optionalAclBiasesInfo,
78 &aclOutputInfo,
79 aclPadStrideInfo,
80 aclDepthMultiplier,
81 activationInfo,
82 aclDilationInfo);
83
84}
85
88 const WorkloadInfo& info,
89 const arm_compute::CLCompileContext& clCompileContext)
91{
92 m_Data.ValidateInputsOutputs("ClDepthwiseConv2dWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
93
94 arm_compute::ICLTensor& input = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
95 arm_compute::ICLTensor& output = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
96 arm_compute::ICLTensor& weights = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
97 weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
98 arm_compute::ITensorInfo* weightsInfo = weights.info();
99 arm_compute::ITensorInfo* inputInfo = input.info();
100 auto weightsShape = weightsInfo->tensor_shape();
101 auto inputShape = inputInfo->tensor_shape();
102
103 // The PermuteDepthwiseConv2dWeights backend optimization has been performed,
104 // converting weights to have the same data layout as input.
105 unsigned int depthMultiplier =
106 ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape);
107
108 arm_compute::ICLTensor* bias = nullptr;
109 if (m_Data.m_Parameters.m_BiasEnabled)
110 {
111 bias = &PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
112 bias->info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
113 }
114
115 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
116 m_Data.m_Parameters.m_DilationX,
117 m_Data.m_Parameters.m_DilationY);
118
119 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
120 input.info()->set_data_layout(aclDataLayout);
121 weights.info()->set_data_layout(aclDataLayout);
122 output.info()->set_data_layout(aclDataLayout);
123
124 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
125
126 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
127
128 m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>();
129
130 {
131 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClDepthwiseConvolutionWorkload_configure");
132 static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure(
133 clCompileContext,
134 &input,
135 &weights,
136 bias,
137 &output,
138 padStrideInfo,
139 depthMultiplier,
140 activationInfo,
141 aclDilationInfo);
142 }
143
144 // Add details for profiling output
145 WorkloadInfo detailsInfo;
146
147 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
148 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
149
150 // Report Profiling Details
151 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClDepthwiseConvolutionWorkload_Construct",
152 descriptor.m_Parameters,
153 detailsInfo,
154 GetGuid());
155}
156
163
164} // namespace armnn
#define ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
#define CHECK_LOCATION()
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
arm::pipe::ProfilingGuid GetGuid() const final
Definition Workload.hpp:52
ClBaseWorkload(const DepthwiseConvolution2dQueueDescriptor &descriptor, const WorkloadInfo &info)
ClDepthwiseConvolutionWorkload(const DepthwiseConvolution2dQueueDescriptor &descriptor, const WorkloadInfo &info, const arm_compute::CLCompileContext &clCompileContext)
std::unique_ptr< arm_compute::IFunction > m_DepthwiseConvolutionLayer
bool has_value() const noexcept
Definition Optional.hpp:53
bool IsConstant() const
Definition Tensor.cpp:513
Copyright (c) 2021 ARM Limited and Contributors.
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
DestType PolymorphicDowncast(SourceType *value)
Polymorphic downcast for build in pointers only.
arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const DepthwiseConvolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases, const ActivationDescriptor *activationDescriptor)
std::tuple< TensorInfo, unsigned int > Convert1HWOTensorInfoToAcl(const TensorInfo &weightInfo, const TensorInfo &inputInfo, const DataLayout dataLayout)
Weights for depthwise have a datalayout of [1,H,W,O] = [1,H,W,I*M] This function coverts a TensorInfo...
arm_compute::ActivationLayerInfo ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor &actDesc)
An ActivationDescriptor for the ActivationLayer.
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
uint32_t m_DilationY
Dilation factor value for height dimension.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
uint32_t GetNumInputs() const
Get the number of views/inputs.
uint32_t m_DilationX
Dilation factor value for width dimension.
bool m_BiasEnabled
Enable/disable bias.
Depthwise Convolution 2D layer workload data.
Contains information about TensorInfos of a layer.
std::vector< TensorInfo > m_OutputTensorInfos
std::vector< TensorInfo > m_InputTensorInfos