ArmNN
 24.05
Splitter.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017, 2024 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "RefWorkloadUtils.hpp"
8 #include <armnn/Tensor.hpp>
9 #include "Splitter.hpp"
10 
11 #include <cmath>
12 #include <limits>
13 
14 #include "Decoders.hpp"
15 #include "Encoders.hpp"
16 
17 namespace armnn
18 {
19 
20 void Split(const SplitterQueueDescriptor& data,
21  std::vector<ITensorHandle*> inputs,
22  std::vector<ITensorHandle*> outputs)
23 {
24  const TensorInfo& inputInfo = GetTensorInfo(inputs[0]);
25 
26  std::unique_ptr<Decoder<float>> decoderPtr =
27  MakeDecoder<float>(inputInfo, inputs[0]->Map());
28  Decoder<float>& decoder = *decoderPtr;
29 
30  for (unsigned int index = 0; index < inputInfo.GetNumElements(); ++index)
31  {
32  unsigned int indices[MaxNumOfTensorDimensions] = { 0 };
33 
34  unsigned int indexRemainder = index;
35  unsigned int dimensionStride = inputInfo.GetNumElements();
36 
37  for (unsigned int i = 0; i<inputInfo.GetNumDimensions(); i++)
38  {
39  dimensionStride /= inputInfo.GetShape()[i];
40  indices[i] = indexRemainder / dimensionStride; // Use integer division to round down.
41  indexRemainder -= indices[i] * dimensionStride;
42  }
43 
44  for (unsigned int viewIdx = 0; viewIdx < data.m_ViewOrigins.size(); ++viewIdx)
45  {
46  SplitterQueueDescriptor::ViewOrigin const& view = data.m_ViewOrigins[viewIdx];
47 
48  //Split view extents are defined by the size of (the corresponding) input tensor.
49  const TensorInfo& outputInfo = GetTensorInfo(outputs[viewIdx]);
51  outputInfo.GetNumDimensions() == inputInfo.GetNumDimensions(),
52  "The number of output dimensions does not match the number of input dimensions.");
53 
54  // Check all dimensions to see if this element is inside the given input view.
55  bool insideView = true;
56  for (unsigned int i = 0; i<outputInfo.GetNumDimensions(); i++)
57  {
58  if (indices[i] < view.m_Origin[i])
59  {
60  insideView = false;
61  }
62  if (indices[i] >= view.m_Origin[i] + outputInfo.GetShape()[i])
63  {
64  insideView = false;
65  }
66  }
67 
68  if (insideView)
69  {
70  std::unique_ptr<Encoder<float>> encoderPtr =
71  MakeEncoder<float>(outputInfo, outputs[viewIdx]->Map());
72  Encoder<float>& encoder = *encoderPtr;
73 
74  unsigned int outIndex = 0;
75  unsigned int dimensionStride = 1;
76  float inputValue = 0.f;
77 
78  for (unsigned int i = outputInfo.GetNumDimensions(); i-- > 0;)
79  {
80  outIndex += dimensionStride * (indices[i] - view.m_Origin[i]);
81  dimensionStride *= outputInfo.GetShape()[i];
82  }
83 
84  decoder += index;
85  inputValue = decoder.Get();
86  decoder -= index;
87 
88  encoder += outIndex;
89  encoder.Set(inputValue);
90  break;
91  }
92  }
93  }
94 }
95 
96 }
armnn::Decoder< float >
armnn::TensorInfo::GetNumElements
unsigned int GetNumElements() const
Definition: Tensor.hpp:198
armnn::Encoder::Set
virtual void Set(IType right)=0
WorkloadData.hpp
armnn::TensorInfo
Definition: Tensor.hpp:152
Splitter.hpp
armnn::Split
void Split(const SplitterQueueDescriptor &data, std::vector< ITensorHandle * > inputs, std::vector< ITensorHandle * > outputs)
Definition: Splitter.cpp:20
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:197
armnn::MaxNumOfTensorDimensions
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:31
armnn::SplitterQueueDescriptor::m_ViewOrigins
std::vector< ViewOrigin > m_ViewOrigins
Definition: WorkloadData.hpp:124
armnn::SplitterQueueDescriptor::ViewOrigin
Definition: WorkloadData.hpp:113
armnn::SplitterQueueDescriptor
Definition: WorkloadData.hpp:111
armnn::Encoder< float >
armnn::GetTensorInfo
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
Definition: RefWorkloadUtils.hpp:33
armnn::Decoder::Get
virtual IType Get() const =0
RefWorkloadUtils.hpp
Tensor.hpp
armnn::SplitterQueueDescriptor::ViewOrigin::m_Origin
std::vector< unsigned int > m_Origin
Definition: WorkloadData.hpp:119
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
Decoders.hpp
armnn::LayerType::Map
@ Map
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
Encoders.hpp
ARMNN_THROW_INVALIDARG_MSG_IF_FALSE
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
Definition: Exceptions.hpp:210