ArmNN
 24.08
NeonNormalizationFloatWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include "NeonWorkloadUtils.hpp"
12 
13 #include <arm_compute/runtime/NEON/functions/NENormalizationLayer.h>
14 
15 using namespace armnn::armcomputetensorutils;
16 
17 namespace armnn
18 {
19 
20 namespace
21 {
22 using ACLMemManagerOnDemand = std::shared_ptr<arm_compute::MemoryManagerOnDemand>;
23 
24 bool IsNeonNormalizationDescriptorSupported(const NormalizationDescriptor& parameters,
25  Optional<std::string&> reasonIfUnsupported)
26 {
27  if (parameters.m_NormMethodType != NormalizationAlgorithmMethod::LocalBrightness)
28  {
29  if (reasonIfUnsupported)
30  {
31  reasonIfUnsupported.value() = "Unsupported normalisation method type, only LocalBrightness is supported";
32  }
33  return false;
34  }
35  if (parameters.m_NormSize % 2 == 0)
36  {
37  if (reasonIfUnsupported)
38  {
39  reasonIfUnsupported.value() = "Normalization size must be an odd number.";
40  }
41  return false;
42  }
43 
44  return true;
45 }
46 
47 } // anonymous namespace
48 
50  const TensorInfo& output,
51  const NormalizationDescriptor& descriptor)
52 {
53  const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
54  const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
55 
56  arm_compute::NormalizationLayerInfo normalizationInfo = BuildArmComputeNormalizationLayerInfo(descriptor);
57 
58  return arm_compute::NENormalizationLayer::validate(&aclInput, &aclOutput, normalizationInfo);
59 }
60 
62  const WorkloadInfo& info,
63  ACLMemManagerOnDemand& memoryManager)
65 {
66  // Report Profiling Details
67  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonNormalizationWorkload_Construct",
68  descriptor.m_Parameters,
69  info,
70  this->GetGuid());
71 
72  m_Data.ValidateInputsOutputs("NeonNormalizationFloatWorkload", 1, 1);
73  std::string reasonIfUnsupported;
74  if (!IsNeonNormalizationDescriptorSupported(m_Data.m_Parameters, Optional<std::string&>(reasonIfUnsupported)))
75  {
76  throw UnimplementedException(reasonIfUnsupported);
77  }
78 
79  // Input and output tensors have to have the same dimensionality.
80  if (info.m_InputTensorInfos[0].GetShape()[1] != info.m_OutputTensorInfos[0].GetShape()[1]
81  || info.m_InputTensorInfos[0].GetShape()[0] != info.m_OutputTensorInfos[0].GetShape()[0]
82  || info.m_InputTensorInfos[0].GetShape()[3] != info.m_OutputTensorInfos[0].GetShape()[3]
83  || info.m_InputTensorInfos[0].GetShape()[2] != info.m_OutputTensorInfos[0].GetShape()[2])
84  {
85  throw InvalidArgumentException("Normalization requires input and output tensors to have equal dimensionality.");
86  }
87 
88  arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
89  arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
90  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
91  input.info()->set_data_layout(aclDataLayout);
92  output.info()->set_data_layout(aclDataLayout);
93 
94  const arm_compute::NormType normType =
95  ConvertNormalizationAlgorithmChannelToAclNormType(m_Data.m_Parameters.m_NormChannelType);
96  arm_compute::NormalizationLayerInfo normalizationInfo(normType,
97  m_Data.m_Parameters.m_NormSize,
98  m_Data.m_Parameters.m_Alpha,
99  m_Data.m_Parameters.m_Beta,
100  m_Data.m_Parameters.m_K,
101  false);
102  auto layer = std::make_unique<arm_compute::NENormalizationLayer>(memoryManager);
103  layer->configure(&input, &output, normalizationInfo);
104  m_NormalizationLayer.reset(layer.release());
105 }
106 
108 {
109  ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID("NeonNormalizationFloatWorkload_Execute");
110  m_NormalizationLayer->run();
111 }
112 
114 {
115  ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
116  this->m_Data.m_Inputs[slot] = tensorHandle;
117  try
118  {
119  Reconfigure();
120  }
122  {
123  // Cannot reconfigure, revert the slot back and throw the exception.
124  this->m_Data.m_Inputs[slot] = backupHandle;
125  throw e;
126  }
127 }
128 
129 // Replace output tensor handle with the given TensorHandle
131 {
132  ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot];
133  this->m_Data.m_Inputs[slot] = tensorHandle;
134  try
135  {
136  Reconfigure();
137  }
139  {
140  // Cannot reconfigure, revert the slot back and throw the exception.
141  this->m_Data.m_Inputs[slot] = backupHandle;
142  throw e;
143  }
144 }
145 
146 void NeonNormalizationFloatWorkload::Reconfigure()
147 {
148  throw armnn::UnimplementedException("Reconfigure not implemented for this workload");
149 }
150 
151 } //namespace armnn
armnn::NormalizationQueueDescriptor
Definition: WorkloadData.hpp:252
armnn::Optional
Definition: Optional.hpp:270
armnn::ACLMemManagerOnDemand
std::shared_ptr< arm_compute::MemoryManagerOnDemand > ACLMemManagerOnDemand
Definition: NeonFullyConnectedWorkload.cpp:22
NeonNormalizationFloatWorkload.hpp
armnn::DataLayout
DataLayout
Definition: Types.hpp:62
armnn::NeonNormalizationFloatWorkload::Execute
virtual void Execute() const override
Definition: NeonNormalizationFloatWorkload.cpp:107
armnn::QueueDescriptor::ValidateInputsOutputs
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Definition: WorkloadData.cpp:447
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::NormalizationAlgorithmMethod::LocalBrightness
@ LocalBrightness
Krichevsky 2012: Local Brightness Normalization.
armnn::NormalizationDescriptor
A NormalizationDescriptor for the NormalizationLayer.
Definition: Descriptors.hpp:769
armnn::ITensorHandle
Definition: ITensorHandle.hpp:16
armnn::TypedWorkload
Definition: Workload.hpp:101
armnn::NeonNormalizationFloatWorkload::NeonNormalizationFloatWorkload
NeonNormalizationFloatWorkload(const NormalizationQueueDescriptor &descriptor, const WorkloadInfo &info, std::shared_ptr< arm_compute::MemoryManagerOnDemand > &memoryManager)
Definition: NeonNormalizationFloatWorkload.cpp:61
armnn::NeonNormalizationWorkloadValidate
arm_compute::Status NeonNormalizationWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const NormalizationDescriptor &descriptor)
Definition: NeonNormalizationFloatWorkload.cpp:49
armnn::QueueDescriptorWithParameters::m_Parameters
LayerDescriptor m_Parameters
Definition: WorkloadData.hpp:66
armnn::NeonNormalizationFloatWorkload::ReplaceOutputTensorHandle
void ReplaceOutputTensorHandle(ITensorHandle *tensorHandle, unsigned int slot) override
Definition: NeonNormalizationFloatWorkload.cpp:130
armnn::WorkloadInfo
Contains information about TensorInfos of a layer.
Definition: WorkloadInfo.hpp:16
PolymorphicDowncast.hpp
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::NormalizationDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:805
ArmComputeUtils.hpp
armnn::BoostLogSeverityMapping::info
@ info
armnn::QueueDescriptor::m_Outputs
std::vector< ITensorHandle * > m_Outputs
Definition: WorkloadData.hpp:27
ARMNN_REPORT_PROFILING_WORKLOAD_DESC
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
armnn::Status
Status
Definition: Types.hpp:42
armnn::BaseWorkload::m_Data
QueueDescriptor m_Data
Definition: Workload.hpp:89
NeonWorkloadUtils.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::ConvertNormalizationAlgorithmChannelToAclNormType
arm_compute::NormType ConvertNormalizationAlgorithmChannelToAclNormType(NormalizationAlgorithmChannel channelType)
Definition: ArmComputeUtils.hpp:182
ArmComputeTensorUtils.hpp
armnn::UnimplementedException
Definition: Exceptions.hpp:98
ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID
#define ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
Definition: NeonWorkloadUtils.hpp:33
armnn::QueueDescriptor::m_Inputs
std::vector< ITensorHandle * > m_Inputs
Definition: WorkloadData.hpp:26
armnn::NeonNormalizationFloatWorkload::ReplaceInputTensorHandle
void ReplaceInputTensorHandle(ITensorHandle *tensorHandle, unsigned int slot) override
Definition: NeonNormalizationFloatWorkload.cpp:113