ArmNN
 22.02
RefChannelShuffleWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
9 #include "RefWorkloadUtils.hpp"
10 #include "Profiling.hpp"
11 #include "Decoders.hpp"
12 #include "Encoders.hpp"
13 
14 namespace armnn
15 {
17 {
19 }
20 
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
28 void 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 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
CPU Execution: Reference C++ kernels.
Copyright (c) 2021 ARM Limited and Contributors.
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
uint32_t m_NumGroups
Number of groups for the channel shuffle operation.
std::vector< ITensorHandle * > m_Outputs
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
void ExecuteAsync(WorkingMemDescriptor &workingMemDescriptor) override
uint32_t m_Axis
Axis to apply channel shuffle operation on.
std::vector< ITensorHandle * > m_Inputs
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers