blob: 0e10b37de2ee96ac7498f1606c7d654cc0534662 [file] [log] [blame]
Teresa Charlin1222dbd2021-09-02 13:58:52 +01001//
Colm Donelanb4ef1632024-02-01 15:00:43 +00002// Copyright © 2021-2024 Arm Ltd and Contributors. All rights reserved.
Teresa Charlin1222dbd2021-09-02 13:58:52 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "ClChannelShuffleWorkload.hpp"
7#include "ClWorkloadUtils.hpp"
8
9#include <armnn/utility/PolymorphicDowncast.hpp>
10
11#include <aclCommon/ArmComputeTensorUtils.hpp>
12
13#include <cl/ClTensorHandle.hpp>
14
15using namespace armnn::armcomputetensorutils;
16
17namespace armnn
18{
19
20arm_compute::Status ClChannelShuffleValidate(const TensorInfo& input,
21 const TensorInfo& output,
22 const ChannelShuffleDescriptor& descriptor)
23{
24 arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
25 arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
26
27 // In Arm NN and in NNAPI, channel shuffle implementation is datalayout agnostic and it has axis as a parameter.
28 // The channel shuffle Implementation for Neon is dependent on datalayout and does not have axis as a parameter,
29 // it only supports channel shuffle for 4D tensors in dimension C (1 or 3).
30 arm_compute::DataLayout aclDataLayout;
31 if (input.GetNumDimensions() == 4)
32 {
33 switch (descriptor.m_Axis)
34 {
35 case 1:
36 aclDataLayout = ConvertDataLayout(armnn::DataLayout::NCHW);
37 break;
38 case 3:
39 aclDataLayout = ConvertDataLayout(armnn::DataLayout::NHWC);
40 break;
41 default:
42 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported axis"};
43 }
44 aclInputInfo.set_data_layout(aclDataLayout);
45 aclOutputInfo.set_data_layout(aclDataLayout);
46 return arm_compute::CLChannelShuffleLayer::validate(&aclInputInfo, &aclOutputInfo, descriptor.m_NumGroups);
47 }
48 else
49 {
50 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR, "Unsupported number of dimensions"};
51 }
52}
53
54ClChannelShuffleWorkload::ClChannelShuffleWorkload(const ChannelShuffleQueueDescriptor& descriptor,
55 const WorkloadInfo& info,
56 const arm_compute::CLCompileContext& clCompileContext)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000057 : ClBaseWorkload<ChannelShuffleQueueDescriptor>(descriptor, info)
Teresa Charlin1222dbd2021-09-02 13:58:52 +010058{
59 // Report Profiling Details
60 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClChannelShufflenWorkload_Construct",
61 descriptor.m_Parameters,
62 info,
63 this->GetGuid());
64
65 m_Data.ValidateInputsOutputs("ClChannelShuffleWorkload", 1, 1);
66
67 arm_compute::ICLTensor& input = PolymorphicDowncast<ClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
68 arm_compute::ICLTensor& output = PolymorphicDowncast<ClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
69
70 // In Arm NN and in NNAPI, channel shuffle implementation is datalayout agnostic and it has axis as a parameter.
71 // The channel shuffle Implementation for Neon is dependent on datalayout and does not have axis as a parameter,
72 // it only supports channel shuffle for 4D tensors in dimension C (1 or 3).
73 arm_compute::DataLayout aclDataLayout;
74 switch (descriptor.m_Parameters.m_Axis)
75 {
76 case 1:
77 aclDataLayout = ConvertDataLayout(armnn::DataLayout::NCHW);
78 break;
79 case 3:
80 aclDataLayout = ConvertDataLayout(armnn::DataLayout::NHWC);
81 break;
82 default:
Colm Donelanb4ef1632024-02-01 15:00:43 +000083 throw InvalidArgumentException("Value for axis: " + std::to_string(descriptor.m_Parameters.m_Axis) +
84 " is not valid");
Teresa Charlin1222dbd2021-09-02 13:58:52 +010085 }
86 input.info()->set_data_layout(aclDataLayout);
87 output.info()->set_data_layout(aclDataLayout);
88
Kevin May9f6862d2021-10-22 15:42:28 +010089 {
Mike Kelly7cbe7812023-07-25 17:37:33 +010090 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClChannelShuffleWorkload_configure");
Kevin May9f6862d2021-10-22 15:42:28 +010091 m_ChannelShuffleLayer.configure(clCompileContext, &input, &output, descriptor.m_Parameters.m_NumGroups);
92 }
Teresa Charlin1222dbd2021-09-02 13:58:52 +010093}
94
95void ClChannelShuffleWorkload::Execute() const
96{
Mike Kelly7cbe7812023-07-25 17:37:33 +010097 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClChannelShuffleWorkload_Execute");
Teresa Charlin1222dbd2021-09-02 13:58:52 +010098 RunClFunction(m_ChannelShuffleLayer, CHECK_LOCATION());
99}
100
101} // namespace armnn