IVGCVSW-2049 + IVGCVSW-2051 Create the CL Mean Float workload and add
[platform/upstream/armnn.git] / src / backends / cl / workloads / ClMeanWorkload.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "ClMeanWorkload.hpp"
7
8 #include <backends/cl/ClTensorHandle.hpp>
9 #include <backends/aclCommon/ArmComputeTensorUtils.hpp>
10
11 #include "ClWorkloadUtils.hpp"
12
13 namespace
14 {
15
16 void ConvertArmnnAxesToAclCoordinates(size_t inputDimensions,
17                                       unsigned int originalInputRank,
18                                       const std::vector<unsigned int>& armnnAxes,
19                                       arm_compute::Coordinates& outAclCoords)
20 {
21     if (armnnAxes.empty())
22     {
23         // If no reduction axes were provided, then the input must be reduced along all dimensions.
24         // Since arm_compute::CLReduceMean does not accept an empty vector as the reduction dimensions, we then
25         // manually create a vector including all the input dimensions (in reversed order) as:
26         //
27         // { inputDimensions - 1, inputDimensions - 2, ..., 1, 0 }
28         //
29         outAclCoords.set_num_dimensions(inputDimensions);
30         std::generate(outAclCoords.begin(), outAclCoords.end(), [d = inputDimensions - 1] () mutable { return d--; });
31     }
32     else
33     {
34         // Create a vector of reduction dimensions (in reversed order) with the given reduction axes.
35         //
36         // Adjust the given reduction axes according to the original rank of the input tensor (before ACL applied any
37         // dimension correction).
38         // For example, if the input tensor originally had 4 dimensions, and one of the reduction axes was 2, then the
39         // new value for that reduction axis should be 1.
40         //
41         // Example:
42         // ArmNN input shape = { 1, 1, 3, 2 } -> ACL input shape = { 2, 3 }
43         // ArmNN reduction axis = { 2 }       -> ACL reduction axis = { 1 }
44         // ArmNN reduction axis = { 3 }       -> ACL reduction axis = { 0 }
45         //
46         // The transformation: ACL reduction axis index = original rank - ArmNN reduction axis index - 1
47         //
48         outAclCoords.set_num_dimensions(armnnAxes.size());
49         std::transform(armnnAxes.begin(), armnnAxes.end(),
50                        outAclCoords.begin(),
51                        [originalInputRank](unsigned int i){ return originalInputRank - i - 1; });
52     }
53 }
54
55 } // anonymous namespace
56
57 namespace armnn
58 {
59 using namespace armcomputetensorutils;
60
61 arm_compute::Status ClMeanValidate(const TensorInfo& input,
62                                    const TensorInfo& output,
63                                    const MeanDescriptor& desc)
64 {
65     const arm_compute::TensorInfo aclInputInfo  = armcomputetensorutils::BuildArmComputeTensorInfo(input);
66     const arm_compute::TensorInfo aclOutputInfo = armcomputetensorutils::BuildArmComputeTensorInfo(output);
67
68     arm_compute::Coordinates coords;
69     ConvertArmnnAxesToAclCoordinates(aclInputInfo.num_dimensions(),
70                                      input.GetNumDimensions(),
71                                      desc.m_Axis,
72                                      coords);
73
74     return arm_compute::CLReduceMean::validate(&aclInputInfo, coords, desc.m_KeepDims, &aclOutputInfo);
75 }
76
77 ClMeanWorkload::ClMeanWorkload(const MeanQueueDescriptor& descriptor, const WorkloadInfo& info)
78     : BaseWorkload<MeanQueueDescriptor>(descriptor, info)
79 {
80     m_Data.ValidateInputsOutputs("ClMeanWorkload", 1, 1);
81
82     arm_compute::ICLTensor& input  = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
83     arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
84
85     arm_compute::Coordinates coords;
86     ConvertArmnnAxesToAclCoordinates(input.info()->num_dimensions(),
87                                      info.m_InputTensorInfos[0].GetNumDimensions(),
88                                      m_Data.m_Parameters.m_Axis,
89                                      coords);
90
91     m_Layer.configure(&input, coords, m_Data.m_Parameters.m_KeepDims, &output);
92 }
93
94 void ClMeanWorkload::Execute() const
95 {
96     ARMNN_SCOPED_PROFILING_EVENT_CL("ClMeanWorkload_Execute");
97     m_Layer.run();
98 }
99
100 } //namespace armnn