Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 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> |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame^] | 13 | #include <backendsCommon/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&) |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 58 | : BaseWorkload<SplitterQueueDescriptor>(descriptor, info) |
| 59 | { |
| 60 | bool allOutputsAreSubtensors = true; |
| 61 | |
| 62 | // Check that all outputs are sub-tensors |
| 63 | for (auto output : m_Data.m_Outputs) |
| 64 | { |
| 65 | if (output && !output->GetParent()) |
| 66 | { |
| 67 | // Non sub-tensor input found so we need to execute the split function |
| 68 | allOutputsAreSubtensors = false; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (allOutputsAreSubtensors) |
| 74 | { |
| 75 | // Can skip configuring the split function since it's not executed |
| 76 | return; |
| 77 | } |
| 78 | |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 79 | arm_compute::ICLTensor& input = armnn::PolymorphicPointerDowncast<IClTensorHandle>( |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 80 | m_Data.m_Inputs[0])->GetTensor(); |
| 81 | |
| 82 | std::vector<arm_compute::ICLTensor *> aclOutputs; |
| 83 | for (auto output : m_Data.m_Outputs) |
| 84 | { |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 85 | arm_compute::ICLTensor& aclOutput = armnn::PolymorphicPointerDowncast<IClTensorHandle>(output)->GetTensor(); |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 86 | aclOutputs.emplace_back(&aclOutput); |
| 87 | } |
| 88 | |
| 89 | // Create the layer function |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 90 | |
| 91 | // Configure input and output tensors |
| 92 | std::set<unsigned int> splitAxis = ComputeSplitAxis(descriptor.m_Parameters, m_Data.m_Inputs[0]->GetShape()); |
| 93 | if (splitAxis.size() != 1) |
| 94 | { |
| 95 | throw InvalidArgumentException("Cannot derive split axis from SplitterDescriptor"); |
| 96 | } |
| 97 | |
| 98 | unsigned int aclAxis = CalcAclAxis(descriptor.m_Parameters.GetNumDimensions(), *splitAxis.begin()); |
Matthew Bentham | 9b3e738 | 2020-02-05 21:39:55 +0000 | [diff] [blame] | 99 | auto layer = std::make_unique<arm_compute::CLSplit>(); |
| 100 | layer->configure(&input, aclOutputs, aclAxis); |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 101 | |
| 102 | // Prepare |
Matthew Bentham | 9b3e738 | 2020-02-05 21:39:55 +0000 | [diff] [blame] | 103 | layer->prepare(); |
| 104 | |
| 105 | m_Layer = std::move(layer); |
Narumol Prangnawarat | 7413583 | 2019-05-23 15:07:33 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void ClSplitterWorkload::Execute() const |
| 109 | { |
| 110 | if (m_Layer) |
| 111 | { |
| 112 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClSplitterWorkload_Execute"); |
| 113 | m_Layer->run(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } //namespace armnn |