blob: 9f8514d009be134a836b8c388dff1eff37f7ec7f [file] [log] [blame]
Simon Obute51f67772021-09-03 15:50:13 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Simon Obute51f67772021-09-03 15:50:13 +01006#include <armnn/backends/ITensorHandleFactory.hpp>
7#include <armnnUtils/Transpose.hpp>
8#include "RefChannelShuffleWorkload.hpp"
9#include "RefWorkloadUtils.hpp"
10#include "Profiling.hpp"
11#include "Decoders.hpp"
12#include "Encoders.hpp"
13
14namespace armnn
15{
16void RefChannelShuffleWorkload::Execute() const
17{
18 Execute(m_Data.m_Inputs, m_Data.m_Outputs);
19}
20
21void RefChannelShuffleWorkload::ExecuteAsync(WorkingMemDescriptor &workingMemDescriptor)
22{
23 Execute(workingMemDescriptor.m_Inputs, workingMemDescriptor.m_Outputs);
24}
25
26// Reference implementation for channel shuffle taken from
27// https://android.googlesource.com/platform/frameworks/ml/+/refs/heads/master/nn/common/operations/ChannelShuffle.cpp
28void RefChannelShuffleWorkload::Execute(std::vector<ITensorHandle*> inputs,
29 std::vector<ITensorHandle*> outputs) const
30{
31 ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefChannelShuffleWorkload_Execute");
32
33 const TensorInfo& inputInfo = GetTensorInfo(inputs[0]);
34 const TensorInfo& outputInfo = GetTensorInfo(outputs[0]);
35 std::unique_ptr<Decoder<float>> decoderPtr = MakeDecoder<float>(inputInfo, inputs[0]->Map());
36 Decoder<float>& decoder = *decoderPtr;
37
38 std::unique_ptr<Encoder<float>> encoderPtr = MakeEncoder<float>(outputInfo, outputs[0]->Map());
39 Encoder<float>& encoder = *encoderPtr;
40
41 auto getNumberOfElements = [](const TensorShape& tensorShape,uint32_t startAxis, uint32_t lastAxis)
42 {
43 uint32_t count = 1;
44 for (uint32_t i = startAxis; i < lastAxis; i++)
45 {
46 count *= tensorShape[i];
47 }
48 return count;
49 };
50 const TensorShape tensorShape = GetTensorInfo(inputs[0]).GetShape();
51 uint32_t channelsAxis = m_Data.m_Parameters.m_Axis; // channelsAxis to perform channel shuffle on
52
53 const uint32_t numGroups = m_Data.m_Parameters.m_NumGroups;
54 const uint32_t groupSize = tensorShape[channelsAxis] / numGroups;
55
56 uint32_t outerSize = getNumberOfElements(tensorShape, 0, channelsAxis);
57 uint32_t innerSize = getNumberOfElements(tensorShape, channelsAxis + 1, tensorShape.GetNumDimensions());
58
59 for (uint32_t outer = 0; outer < outerSize; ++outer)
60 {
61 for (uint32_t inner = 0; inner < innerSize; ++inner)
62 {
63 uint32_t decoderStep1 = outer * tensorShape[channelsAxis] * innerSize + inner;
64 decoder += decoderStep1;
65 uint32_t encoderStep1 = outer * tensorShape[channelsAxis] * innerSize + inner;
66 encoder += encoderStep1;
67 for (uint32_t i = 0; i < groupSize; i++)
68 {
69 for (uint32_t j = 0; j < numGroups; j++, encoder += innerSize, encoderStep1 += innerSize)
70 {
71 decoder += innerSize * (i + j * groupSize);
72 float decoded = decoder.Get();
73 encoder.Set(decoded);
74 decoder -= innerSize * (i + j * groupSize);
75 }
76 }
77 decoder -= decoderStep1;
78 encoder -= encoderStep1;
79 }
80 }
81}
82}