Teresa Charlin | 1222dbd | 2021-09-02 13:58:52 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // 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 | |
| 15 | using namespace armnn::armcomputetensorutils; |
| 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | arm_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 | |
| 54 | ClChannelShuffleWorkload::ClChannelShuffleWorkload(const ChannelShuffleQueueDescriptor& descriptor, |
| 55 | const WorkloadInfo& info, |
| 56 | const arm_compute::CLCompileContext& clCompileContext) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 57 | : ClBaseWorkload<ChannelShuffleQueueDescriptor>(descriptor, info) |
Teresa Charlin | 1222dbd | 2021-09-02 13:58:52 +0100 | [diff] [blame] | 58 | { |
| 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: |
| 83 | ARMNN_ASSERT_MSG(false, "Unsupported axis"); |
| 84 | break; |
| 85 | } |
| 86 | input.info()->set_data_layout(aclDataLayout); |
| 87 | output.info()->set_data_layout(aclDataLayout); |
| 88 | |
Kevin May | 9f6862d | 2021-10-22 15:42:28 +0100 | [diff] [blame] | 89 | { |
| 90 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClChannelShuffleWorkload_configure"); |
| 91 | m_ChannelShuffleLayer.configure(clCompileContext, &input, &output, descriptor.m_Parameters.m_NumGroups); |
| 92 | } |
Teresa Charlin | 1222dbd | 2021-09-02 13:58:52 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void ClChannelShuffleWorkload::Execute() const |
| 96 | { |
| 97 | ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClChannelShuffleWorkload_Execute", this->GetGuid()); |
| 98 | RunClFunction(m_ChannelShuffleLayer, CHECK_LOCATION()); |
| 99 | } |
| 100 | |
| 101 | } // namespace armnn |