ArmNN
 20.11
ClSplitterWorkload.cpp
Go to the documentation of this file.
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 
12 #include <arm_compute/runtime/CL/functions/CLSplit.h>
15 #include <cl/ClTensorHandle.hpp>
16 
17 
18 namespace armnn
19 {
20 
21 using namespace armcomputetensorutils;
22 
23 namespace
24 {
25  unsigned int CalcAclAxis(unsigned int numDimensions, unsigned int splitAxis)
26  {
27  return (numDimensions - splitAxis) - 1;
28  }
29 
30 } //namespace
31 
33  const std::vector<std::reference_wrapper<TensorInfo>>& outputs,
34  unsigned int splitAxis)
35 {
36  const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
37 
38  size_t numOutputs = outputs.size();
39 
40  std::vector<arm_compute::TensorInfo> aclOutputs;
41  aclOutputs.reserve(numOutputs);
42 
43  std::vector<arm_compute::ITensorInfo*> aclOutputPtr;
44  aclOutputPtr.reserve(numOutputs);
45 
46  for (size_t i = 0u; i < outputs.size(); ++i)
47  {
48  aclOutputs.emplace_back(BuildArmComputeTensorInfo(outputs[i]));
49  aclOutputPtr.emplace_back(&aclOutputs.back());
50  }
51 
52  unsigned int aclAxis = CalcAclAxis(input.GetNumDimensions(), splitAxis);
53  return arm_compute::CLSplit::validate(&aclInputInfo, aclOutputPtr, aclAxis);
54 }
55 
57  : BaseWorkload<SplitterQueueDescriptor>(descriptor, info)
58 {
59  bool allOutputsAreSubtensors = true;
60 
61  // Check that all outputs are sub-tensors
62  for (auto output : m_Data.m_Outputs)
63  {
64  if (output && !output->GetParent())
65  {
66  // Non sub-tensor input found so we need to execute the split function
67  allOutputsAreSubtensors = false;
68  break;
69  }
70  }
71 
72  if (allOutputsAreSubtensors)
73  {
74  // Can skip configuring the split function since it's not executed
75  return;
76  }
77 
78  arm_compute::ICLTensor& input = armnn::PolymorphicPointerDowncast<IClTensorHandle>(
79  m_Data.m_Inputs[0])->GetTensor();
80 
81  std::vector<arm_compute::ICLTensor *> aclOutputs;
82  for (auto output : m_Data.m_Outputs)
83  {
84  arm_compute::ICLTensor& aclOutput = armnn::PolymorphicPointerDowncast<IClTensorHandle>(output)->GetTensor();
85  aclOutputs.emplace_back(&aclOutput);
86  }
87 
88  // Create the layer function
89 
90  // Configure input and output tensors
91  std::set<unsigned int> splitAxis = ComputeSplitAxis(descriptor.m_Parameters, m_Data.m_Inputs[0]->GetShape());
92  if (splitAxis.size() != 1)
93  {
94  throw InvalidArgumentException("Cannot derive split axis from SplitterDescriptor");
95  }
96 
97  unsigned int aclAxis = CalcAclAxis(descriptor.m_Parameters.GetNumDimensions(), *splitAxis.begin());
98  auto layer = std::make_unique<arm_compute::CLSplit>();
99  layer->configure(&input, aclOutputs, aclAxis);
100 
101  // Prepare
102  layer->prepare();
103 
104  m_Layer = std::move(layer);
105 }
106 
108 {
109  if (m_Layer)
110  {
111  ARMNN_SCOPED_PROFILING_EVENT_CL("ClSplitterWorkload_Execute");
112  m_Layer->run();
113  }
114 }
115 
116 } //namespace armnn
ClSplitterWorkload(const SplitterQueueDescriptor &descriptor, const WorkloadInfo &info)
arm_compute::Status ClSplitterWorkloadValidate(const TensorInfo &input, const std::vector< std::reference_wrapper< TensorInfo >> &outputs, unsigned int splitAxis)
uint32_t GetNumDimensions() const
Get the number of dimensions.
#define ARMNN_SCOPED_PROFILING_EVENT_CL(name)
const SplitterQueueDescriptor m_Data
Definition: Workload.hpp:46
Copyright (c) 2020 ARM Limited.
std::set< unsigned int > ComputeSplitAxis(const armnn::SplitterDescriptor &desc, const TensorShape &input)
Status
enumeration
Definition: Types.hpp:26
std::vector< ITensorHandle * > m_Outputs
void Execute() const override
Contains information about inputs and outputs to a layer.
std::vector< ITensorHandle * > m_Inputs
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:191