blob: f05f654a0c5ec843967e7648eb90255de4b141ca [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Colm Donelanb4ef1632024-02-01 15:00:43 +00002// Copyright © 2017, 2024 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
6#pragma once
7
8#include "RefWorkloadUtils.hpp"
Colm Donelan0c479742021-12-10 12:43:54 +00009#include <armnn/backends/WorkloadData.hpp>
telsoa014fcda012018-03-09 14:13:49 +000010#include <armnn/Tensor.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010011#include <armnn/utility/Assert.hpp>
telsoa014fcda012018-03-09 14:13:49 +000012
13namespace armnn
14{
15
16template <typename DataType>
Finn Williamsb8181f72021-04-07 10:23:21 +010017void Splitter(const SplitterQueueDescriptor& data,
18 std::vector<ITensorHandle*> inputs,
19 std::vector<ITensorHandle*> outputs)
telsoa014fcda012018-03-09 14:13:49 +000020{
Finn Williamsb8181f72021-04-07 10:23:21 +010021 const TensorInfo& inputInfo0 = GetTensorInfo(inputs[0]);
telsoa014fcda012018-03-09 14:13:49 +000022
23 for (unsigned int index = 0; index < inputInfo0.GetNumElements(); ++index)
24 {
surmeh013537c2c2018-05-18 16:31:43 +010025 unsigned int indices[MaxNumOfTensorDimensions] = { 0 };
telsoa014fcda012018-03-09 14:13:49 +000026
27 unsigned int indexRemainder = index;
28 unsigned int dimensionStride = inputInfo0.GetNumElements();
29
30 for (unsigned int i = 0; i<inputInfo0.GetNumDimensions(); i++)
31 {
32 dimensionStride /= inputInfo0.GetShape()[i];
telsoa01c577f2c2018-08-31 09:22:23 +010033 indices[i] = indexRemainder / dimensionStride; // Use integer division to round down.
telsoa014fcda012018-03-09 14:13:49 +000034 indexRemainder -= indices[i] * dimensionStride;
35 }
36
37 for (unsigned int viewIdx = 0; viewIdx < data.m_ViewOrigins.size(); ++viewIdx)
38 {
39 SplitterQueueDescriptor::ViewOrigin const& view = data.m_ViewOrigins[viewIdx];
40
telsoa01c577f2c2018-08-31 09:22:23 +010041 //Split view extents are defined by the size of (the corresponding) input tensor.
Finn Williamsb8181f72021-04-07 10:23:21 +010042 const TensorInfo& outputInfo = GetTensorInfo(outputs[viewIdx]);
Colm Donelanb4ef1632024-02-01 15:00:43 +000043 ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(
44 outputInfo.GetNumDimensions() == inputInfo0.GetNumDimensions(),
45 "The number of output dimensions does not match the number of input dimensions.");
telsoa014fcda012018-03-09 14:13:49 +000046
telsoa01c577f2c2018-08-31 09:22:23 +010047 // Check all dimensions to see if this element is inside the given input view.
telsoa014fcda012018-03-09 14:13:49 +000048 bool insideView = true;
49 for (unsigned int i = 0; i<outputInfo.GetNumDimensions(); i++)
50 {
51 if (indices[i] < view.m_Origin[i])
52 {
53 insideView = false;
54 }
55 if (indices[i] >= view.m_Origin[i] + outputInfo.GetShape()[i])
56 {
57 insideView = false;
58 }
59 }
60
61 if (insideView)
62 {
63 unsigned int outIndex = 0;
64 unsigned int dimensionStride = 1;
65
66 for (unsigned int i = outputInfo.GetNumDimensions(); i-- > 0;)
67 {
68 outIndex += dimensionStride * (indices[i] - view.m_Origin[i]);
69 dimensionStride *= outputInfo.GetShape()[i];
70 }
71
telsoa01c577f2c2018-08-31 09:22:23 +010072 //We are within the view, to copy input data to the output corresponding to this view.
telsoa014fcda012018-03-09 14:13:49 +000073 DataType* outputData = GetOutputTensorData<DataType>(viewIdx, data);
telsoa014fcda012018-03-09 14:13:49 +000074 const DataType* inputData = GetInputTensorData<DataType>(0, data);
telsoa014fcda012018-03-09 14:13:49 +000075 outputData[outIndex] = inputData[index];
76 }
77 }
78 }
79}
80
Finn Williamsb8181f72021-04-07 10:23:21 +010081void Split(const SplitterQueueDescriptor& data,
82 std::vector<ITensorHandle*> inputs,
83 std::vector<ITensorHandle*> outputs);
telsoa014fcda012018-03-09 14:13:49 +000084} //namespace armnn