ArmNN
 21.02
Splitter.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "RefWorkloadUtils.hpp"
10 #include <armnn/Tensor.hpp>
11 #include <armnn/utility/Assert.hpp>
12 
13 namespace armnn
14 {
15 
16 template <typename DataType>
18 {
19  const TensorInfo& inputInfo0 = GetTensorInfo(data.m_Inputs[0]);
20 
21  for (unsigned int index = 0; index < inputInfo0.GetNumElements(); ++index)
22  {
23  unsigned int indices[MaxNumOfTensorDimensions] = { 0 };
24 
25  unsigned int indexRemainder = index;
26  unsigned int dimensionStride = inputInfo0.GetNumElements();
27 
28  for (unsigned int i = 0; i<inputInfo0.GetNumDimensions(); i++)
29  {
30  dimensionStride /= inputInfo0.GetShape()[i];
31  indices[i] = indexRemainder / dimensionStride; // Use integer division to round down.
32  indexRemainder -= indices[i] * dimensionStride;
33  }
34 
35  for (unsigned int viewIdx = 0; viewIdx < data.m_ViewOrigins.size(); ++viewIdx)
36  {
37  SplitterQueueDescriptor::ViewOrigin const& view = data.m_ViewOrigins[viewIdx];
38 
39  //Split view extents are defined by the size of (the corresponding) input tensor.
40  const TensorInfo& outputInfo = GetTensorInfo(data.m_Outputs[viewIdx]);
41  ARMNN_ASSERT(outputInfo.GetNumDimensions() == inputInfo0.GetNumDimensions());
42 
43  // Check all dimensions to see if this element is inside the given input view.
44  bool insideView = true;
45  for (unsigned int i = 0; i<outputInfo.GetNumDimensions(); i++)
46  {
47  if (indices[i] < view.m_Origin[i])
48  {
49  insideView = false;
50  }
51  if (indices[i] >= view.m_Origin[i] + outputInfo.GetShape()[i])
52  {
53  insideView = false;
54  }
55  }
56 
57  if (insideView)
58  {
59  unsigned int outIndex = 0;
60  unsigned int dimensionStride = 1;
61 
62  for (unsigned int i = outputInfo.GetNumDimensions(); i-- > 0;)
63  {
64  outIndex += dimensionStride * (indices[i] - view.m_Origin[i]);
65  dimensionStride *= outputInfo.GetShape()[i];
66  }
67 
68  //We are within the view, to copy input data to the output corresponding to this view.
69  DataType* outputData = GetOutputTensorData<DataType>(viewIdx, data);
70  ARMNN_ASSERT(outputData);
71 
72  const DataType* inputData = GetInputTensorData<DataType>(0, data);
73  ARMNN_ASSERT(inputData);
74 
75  outputData[outIndex] = inputData[index];
76  }
77  }
78  }
79 }
80 
81 void Split(const SplitterQueueDescriptor& data);
82 } //namespace armnn
void Split(const SplitterQueueDescriptor &data)
Definition: Splitter.cpp:21
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
Copyright (c) 2021 ARM Limited and Contributors.
DataType
Definition: Types.hpp:32
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::vector< ViewOrigin > m_ViewOrigins
std::vector< ITensorHandle * > m_Outputs
std::vector< ITensorHandle * > m_Inputs
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
void Splitter(const SplitterQueueDescriptor &data)
Definition: Splitter.hpp:17
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:191
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:18
unsigned int GetNumElements() const
Definition: Tensor.hpp:192
std::vector< unsigned int > m_Origin