ArmNN
 25.11
Loading...
Searching...
No Matches
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
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
17namespace armnn
18{
19
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}
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
virtual IType Get() const =0
virtual void Set(IType right)=0
const TensorShape & GetShape() const
Definition Tensor.hpp:193
unsigned int GetNumDimensions() const
Definition Tensor.hpp:197
unsigned int GetNumElements() const
Definition Tensor.hpp:198
Copyright (c) 2021 ARM Limited and Contributors.
void Split(const SplitterQueueDescriptor &data, std::vector< ITensorHandle * > inputs, std::vector< ITensorHandle * > outputs)
Definition Splitter.cpp:20
std::unique_ptr< Decoder< T > > MakeDecoder(const TensorInfo &info, const void *data=nullptr)
std::unique_ptr< Encoder< T > > MakeEncoder(const TensorInfo &info, void *data=nullptr)
constexpr unsigned int MaxNumOfTensorDimensions
Definition Types.hpp:31
armnn::TensorInfo GetTensorInfo(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout, const armnn::DataType dataType)
std::vector< ViewOrigin > m_ViewOrigins