Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClSplitterWorkload.hpp" |
| 7 | |
| 8 | #include "ClWorkloadUtils.hpp" |
| 9 | |
| 10 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 11 | #include <aclCommon/ArmComputeUtils.hpp> |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 12 | #include <armnn/utility/PolymorphicDowncast.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 13 | #include <armnn/backends/TensorHandle.hpp> |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 14 | #include <cl/ClTensorHandle.hpp> |
| 15 | |
| 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | using namespace armcomputetensorutils; |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | unsigned int CalcAclAxis(unsigned int numDimensions, unsigned int splitAxis) |
| 25 | { |
| 26 | return (numDimensions - splitAxis) - 1; |
| 27 | } |
| 28 | |
| 29 | } //namespace |
| 30 | |
| 31 | arm_compute::Status ClSplitterWorkloadValidate(const TensorInfo& input, |
| 32 | const std::vector<std::reference_wrapper<TensorInfo>>& outputs, |
| 33 | unsigned int splitAxis) |
| 34 | { |
| 35 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input); |
| 36 | |
| 37 | size_t numOutputs = outputs.size(); |
| 38 | |
| 39 | std::vector<arm_compute::TensorInfo> aclOutputs; |
| 40 | aclOutputs.reserve(numOutputs); |
| 41 | |
| 42 | std::vector<arm_compute::ITensorInfo*> aclOutputPtr; |
| 43 | aclOutputPtr.reserve(numOutputs); |
| 44 | |
| 45 | for (size_t i = 0u; i < outputs.size(); ++i) |
| 46 | { |
| 47 | aclOutputs.emplace_back(BuildArmComputeTensorInfo(outputs[i])); |
| 48 | aclOutputPtr.emplace_back(&aclOutputs.back()); |
| 49 | } |
| 50 | |
| 51 | unsigned int aclAxis = CalcAclAxis(input.GetNumDimensions(), splitAxis); |
| 52 | return arm_compute::CLSplit::validate(&aclInputInfo, aclOutputPtr, aclAxis); |
| 53 | } |
| 54 | |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 55 | ClSplitterWorkload::ClSplitterWorkload(const SplitterQueueDescriptor& descriptor, |
| 56 | const WorkloadInfo& info, |
| 57 | const arm_compute::CLCompileContext&) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 58 | : ClBaseWorkload<SplitterQueueDescriptor>(descriptor, info) |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 59 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 60 | // Report Profiling Details |
| 61 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClSplitterWorkload_Construct", |
| 62 | descriptor.m_Parameters, |
| 63 | info, |
| 64 | this->GetGuid()); |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 65 | bool allOutputsAreSubtensors = true; |
| 66 | |
| 67 | // Check that all outputs are sub-tensors |
| 68 | for (auto output : m_Data.m_Outputs) |
| 69 | { |
| 70 | if (output && !output->GetParent()) |
| 71 | { |
| 72 | // Non sub-tensor input found so we need to execute the split function |
| 73 | allOutputsAreSubtensors = false; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if (allOutputsAreSubtensors) |
| 79 | { |
| 80 | // Can skip configuring the split function since it's not executed |
| 81 | return; |
| 82 | } |
| 83 | |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 84 | arm_compute::ICLTensor& input = armnn::PolymorphicPointerDowncast<IClTensorHandle>( |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 85 | m_Data.m_Inputs[0])->GetTensor(); |
| 86 | |
| 87 | std::vector<arm_compute::ICLTensor *> aclOutputs; |
| 88 | for (auto output : m_Data.m_Outputs) |
| 89 | { |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 90 | arm_compute::ICLTensor& aclOutput = armnn::PolymorphicPointerDowncast<IClTensorHandle>(output)->GetTensor(); |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 91 | aclOutputs.emplace_back(&aclOutput); |
| 92 | } |
| 93 | |
| 94 | // Create the layer function |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 95 | |
| 96 | // Configure input and output tensors |
| 97 | std::set<unsigned int> splitAxis = ComputeSplitAxis(descriptor.m_Parameters, m_Data.m_Inputs[0]->GetShape()); |
| 98 | if (splitAxis.size() != 1) |
| 99 | { |
| 100 | throw InvalidArgumentException("Cannot derive split axis from SplitterDescriptor"); |
| 101 | } |
| 102 | |
| 103 | unsigned int aclAxis = CalcAclAxis(descriptor.m_Parameters.GetNumDimensions(), *splitAxis.begin()); |
Matthew Bentham | 9b3e738 | 2020-02-05 21:39:55 +0000 | [diff] [blame] | 104 | auto layer = std::make_unique<arm_compute::CLSplit>(); |
Kevin May | 9f6862d | 2021-10-22 15:42:28 +0100 | [diff] [blame] | 105 | { |
| 106 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClSplitterWorkload_configure"); |
| 107 | layer->configure(&input, aclOutputs, aclAxis); |
| 108 | } |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 109 | |
| 110 | // Prepare |
Matthew Bentham | 9b3e738 | 2020-02-05 21:39:55 +0000 | [diff] [blame] | 111 | layer->prepare(); |
| 112 | |
| 113 | m_Layer = std::move(layer); |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void ClSplitterWorkload::Execute() const |
| 117 | { |
| 118 | if (m_Layer) |
| 119 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 120 | ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClSplitterWorkload_Execute", this->GetGuid()); |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 121 | m_Layer->run(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } //namespace armnn |