blob: fece43cb02877a6a5a1147f5d4e6eb756bbaccdd [file] [log] [blame]
Ferran Balaguerb2845652019-02-27 09:42:06 +00001//
Colm Donelanb4ef1632024-02-01 15:00:43 +00002// Copyright © 2017, 2024 Arm Ltd. All rights reserved.
Ferran Balaguerb2845652019-02-27 09:42:06 +00003// SPDX-License-Identifier: MIT
4//
5
Jim Flynne242f2d2019-05-22 14:24:13 +01006#include "Concatenate.hpp"
Ferran Balaguerb2845652019-02-27 09:42:06 +00007#include "RefWorkloadUtils.hpp"
Jim Flynncbb66aa2019-05-15 13:03:54 +01008#include "Decoders.hpp"
9#include "Encoders.hpp"
Ferran Balaguerb2845652019-02-27 09:42:06 +000010
11namespace armnn
12{
13
Finn Williamsb8181f72021-04-07 10:23:21 +010014void Concatenate(const ConcatQueueDescriptor &data,
15 std::vector<ITensorHandle*> inputs,
16 std::vector<ITensorHandle*> outputs)
Ferran Balaguerb2845652019-02-27 09:42:06 +000017{
Finn Williamsb8181f72021-04-07 10:23:21 +010018 const TensorInfo& outputInfo0 = GetTensorInfo(outputs[0]);
Ferran Balaguerb2845652019-02-27 09:42:06 +000019
Finn Williamsb8181f72021-04-07 10:23:21 +010020 std::unique_ptr<Encoder<float>> encoderPtr = MakeEncoder<float>(outputInfo0, outputs[0]->Map());
Jim Flynncbb66aa2019-05-15 13:03:54 +010021 Encoder<float>& encoder = *encoderPtr;
22
Ferran Balaguerb2845652019-02-27 09:42:06 +000023 for (unsigned int index = 0 ; index < outputInfo0.GetNumElements(); ++index)
24 {
25 unsigned int indices[MaxNumOfTensorDimensions] = { 0 };
26
27 unsigned int indexRemainder = index;
28 unsigned int dimensionStride = outputInfo0.GetNumElements();
29
30 for (unsigned int i = 0; i < outputInfo0.GetNumDimensions(); i++)
31 {
32 dimensionStride /= outputInfo0.GetShape()[i];
33 indices[i] = indexRemainder / dimensionStride; // Use integer division to round down.
34 indexRemainder -= indices[i] * dimensionStride;
35 }
36
37 for (unsigned int viewIdx = 0; viewIdx < data.m_ViewOrigins.size(); ++viewIdx)
38 {
Jim Flynne242f2d2019-05-22 14:24:13 +010039 ConcatQueueDescriptor::ViewOrigin const& view = data.m_ViewOrigins[viewIdx];
Ferran Balaguerb2845652019-02-27 09:42:06 +000040
41 //Split view extents are defined by the size of (the corresponding) input tensor.
Finn Williamsb8181f72021-04-07 10:23:21 +010042 const TensorInfo& inputInfo = GetTensorInfo(inputs[viewIdx]);
Colm Donelanb4ef1632024-02-01 15:00:43 +000043 ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(
44 inputInfo.GetNumDimensions() == outputInfo0.GetNumDimensions(),
45 "The number of output dimensions does not match the number of input dimensions.");
Ferran Balaguerb2845652019-02-27 09:42:06 +000046
47 // Check all dimensions to see if this element is inside the given input view.
48 bool insideView = true;
49 for (unsigned int i = 0; i < inputInfo.GetNumDimensions(); i++)
50 {
51 if (indices[i] < view.m_Origin[i])
52 {
53 insideView = false;
54 }
55 if (indices[i] >= view.m_Origin[i] + inputInfo.GetShape()[i])
56 {
57 insideView = false;
58 }
59 }
60
61 if (insideView)
62 {
Jim Flynncbb66aa2019-05-15 13:03:54 +010063 std::unique_ptr<Decoder<float>> decoderPtr =
Finn Williamsb8181f72021-04-07 10:23:21 +010064 MakeDecoder<float>(inputInfo,inputs[viewIdx]->Map());
Jim Flynncbb66aa2019-05-15 13:03:54 +010065 Decoder<float>& decoder = *decoderPtr;
Ferran Balaguerb2845652019-02-27 09:42:06 +000066 unsigned int inIndex = 0;
67 unsigned int dimensionStride = 1;
68
69 for (unsigned int i = inputInfo.GetNumDimensions(); i-- > 0;)
70 {
71 inIndex += dimensionStride * (indices[i] - view.m_Origin[i]);
72 dimensionStride *= inputInfo.GetShape()[i];
73 }
Jim Flynncbb66aa2019-05-15 13:03:54 +010074 decoder += inIndex;
75 encoder.Set(decoder.Get());
Ferran Balaguerb2845652019-02-27 09:42:06 +000076
77 //What should we do if input views overlap on the output tensor?
78 //We could error, take the average, or shm else...
79 //For now just stop after finding first view (input) that matches.
80 break;
81 }
82 }
Jim Flynncbb66aa2019-05-15 13:03:54 +010083 ++encoder;
Ferran Balaguerb2845652019-02-27 09:42:06 +000084 }
85}
86
Ferran Balaguerb2845652019-02-27 09:42:06 +000087} //namespace armnn