telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 6 | #include "ClBatchNormalizationFloatWorkload.hpp" |
Matthew Bentham | 14e4669 | 2018-09-20 15:35:30 +0100 | [diff] [blame] | 7 | #include "ClWorkloadUtils.hpp" |
| 8 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 9 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 10 | #include <aclCommon/ArmComputeUtils.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 11 | #include <armnn/backends/TensorHandle.hpp> |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 12 | #include <cl/ClLayerSupport.hpp> |
Mike Kelly | 0d4ed39 | 2020-11-13 15:26:41 +0000 | [diff] [blame] | 13 | #include <cl/ClTensorHandle.hpp> |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 14 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | namespace armnn |
| 16 | { |
| 17 | using namespace armcomputetensorutils; |
| 18 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 19 | arm_compute::Status ClBatchNormalizationValidate(const TensorInfo& input, |
| 20 | const TensorInfo& output, |
| 21 | const TensorInfo& mean, |
| 22 | const TensorInfo& var, |
| 23 | const TensorInfo& beta, |
| 24 | const TensorInfo& gamma, |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 25 | const BatchNormalizationDescriptor& descriptor, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 26 | const ActivationDescriptor* activationDescriptor) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 27 | { |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 28 | const arm_compute::TensorInfo aclInputInfo = |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 29 | armcomputetensorutils::BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 30 | const arm_compute::TensorInfo aclOutputInfo = |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 31 | armcomputetensorutils::BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 32 | const arm_compute::TensorInfo aclMeanInfo = |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 33 | armcomputetensorutils::BuildArmComputeTensorInfo(mean, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 34 | const arm_compute::TensorInfo aclVarInfo = |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 35 | armcomputetensorutils::BuildArmComputeTensorInfo(var, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 36 | const arm_compute::TensorInfo aclBetaInfo = |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 37 | armcomputetensorutils::BuildArmComputeTensorInfo(beta, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 38 | const arm_compute::TensorInfo aclGammaInfo = |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 39 | armcomputetensorutils::BuildArmComputeTensorInfo(gamma, descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 40 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 41 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 42 | activationDescriptor); |
| 43 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | return arm_compute::CLBatchNormalizationLayer::validate(&aclInputInfo, |
| 45 | &aclOutputInfo, |
| 46 | &aclMeanInfo, |
| 47 | &aclVarInfo, |
| 48 | &aclBetaInfo, |
| 49 | &aclGammaInfo, |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 50 | descriptor.m_Eps, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 51 | activationInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 52 | } |
| 53 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 54 | ClBatchNormalizationFloatWorkload::ClBatchNormalizationFloatWorkload( |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 55 | const BatchNormalizationQueueDescriptor& descriptor, |
| 56 | const WorkloadInfo& info, |
| 57 | const arm_compute::CLCompileContext& clCompileContext) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | : FloatWorkload<BatchNormalizationQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 60 | // Report Profiling Details |
| 61 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClBatchNormalizationWorkload_Construct", |
| 62 | descriptor.m_Parameters, |
| 63 | info, |
| 64 | this->GetGuid()); |
| 65 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 66 | m_Mean = std::make_unique<arm_compute::CLTensor>(); |
| 67 | BuildArmComputeTensor(*m_Mean, m_Data.m_Mean->GetTensorInfo()); |
| 68 | |
| 69 | m_Variance = std::make_unique<arm_compute::CLTensor>(); |
| 70 | BuildArmComputeTensor(*m_Variance, m_Data.m_Variance->GetTensorInfo()); |
| 71 | |
| 72 | m_Gamma = std::make_unique<arm_compute::CLTensor>(); |
| 73 | BuildArmComputeTensor(*m_Gamma, m_Data.m_Gamma->GetTensorInfo()); |
| 74 | |
| 75 | m_Beta = std::make_unique<arm_compute::CLTensor>(); |
| 76 | BuildArmComputeTensor(*m_Beta, m_Data.m_Beta->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 78 | m_Data.ValidateInputsOutputs("ClBatchNormalizationFloatWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 79 | |
| 80 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 81 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 82 | |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 83 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 84 | input.info()->set_data_layout(aclDataLayout); |
| 85 | output.info()->set_data_layout(aclDataLayout); |
| 86 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 87 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 88 | |
Kevin May | 9f6862d | 2021-10-22 15:42:28 +0100 | [diff] [blame] | 89 | { |
| 90 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClBatchNormalizationFloatWorkload_configure"); |
| 91 | m_Layer.configure(clCompileContext, |
| 92 | &input, |
| 93 | &output, |
| 94 | m_Mean.get(), |
| 95 | m_Variance.get(), |
| 96 | m_Beta.get(), |
| 97 | m_Gamma.get(), |
| 98 | m_Data.m_Parameters.m_Eps, |
| 99 | activationInfo); |
| 100 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 101 | |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 102 | InitializeArmComputeClTensorData(*m_Mean, m_Data.m_Mean); |
| 103 | InitializeArmComputeClTensorData(*m_Variance, m_Data.m_Variance); |
| 104 | InitializeArmComputeClTensorData(*m_Beta, m_Data.m_Beta); |
| 105 | InitializeArmComputeClTensorData(*m_Gamma, m_Data.m_Gamma); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 106 | |
| 107 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 108 | // delete all the input tensors that will no longer be needed |
| 109 | m_Layer.prepare(); |
| 110 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | } |
| 112 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 113 | void ClBatchNormalizationFloatWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 114 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 115 | ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClBatchNormalizationFloatWorkload_Execute", this->GetGuid()); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 116 | RunClFunction(m_Layer, CHECK_LOCATION()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 117 | } |
| 118 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 119 | void ClBatchNormalizationFloatWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 120 | { |
| 121 | FreeTensorIfUnused(m_Mean); |
| 122 | FreeTensorIfUnused(m_Variance); |
| 123 | FreeTensorIfUnused(m_Gamma); |
| 124 | FreeTensorIfUnused(m_Beta); |
| 125 | } |
| 126 | |
David Monahan | ec81999 | 2022-02-10 14:47:13 +0000 | [diff] [blame] | 127 | void ClBatchNormalizationFloatWorkload::ReplaceInputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot) |
| 128 | { |
| 129 | ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot]; |
| 130 | this->m_Data.m_Inputs[slot] = tensorHandle; |
| 131 | try |
| 132 | { |
| 133 | Reconfigure(); |
| 134 | } |
| 135 | catch(armnn::UnimplementedException& e) |
| 136 | { |
| 137 | // Cannot reconfigure, revert the slot back and throw the exception. |
| 138 | this->m_Data.m_Inputs[slot] = backupHandle; |
| 139 | throw e; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Replace output tensor handle with the given TensorHandle |
| 144 | void ClBatchNormalizationFloatWorkload::ReplaceOutputTensorHandle(ITensorHandle* tensorHandle, unsigned int slot) |
| 145 | { |
| 146 | ITensorHandle* backupHandle = this->m_Data.m_Inputs[slot]; |
| 147 | this->m_Data.m_Inputs[slot] = tensorHandle; |
| 148 | try |
| 149 | { |
| 150 | Reconfigure(); |
| 151 | } |
| 152 | catch(armnn::UnimplementedException& e) |
| 153 | { |
| 154 | // Cannot reconfigure, revert the slot back and throw the exception. |
| 155 | this->m_Data.m_Inputs[slot] = backupHandle; |
| 156 | throw e; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void ClBatchNormalizationFloatWorkload::Reconfigure() |
| 161 | { |
| 162 | throw armnn::UnimplementedException("Reconfigure not implemented for this workload"); |
| 163 | } |
| 164 | |
Matthew Bentham | 14e4669 | 2018-09-20 15:35:30 +0100 | [diff] [blame] | 165 | } //namespace armnn |