telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. 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 <cl/ClTensorHandle.hpp> |
| 10 | |
| 11 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 12 | |
| 13 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 14 | #include <aclCommon/ArmComputeUtils.hpp> |
| 15 | |
| 16 | #include <cl/ClLayerSupport.hpp> |
| 17 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
| 20 | using namespace armcomputetensorutils; |
| 21 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 22 | arm_compute::Status ClBatchNormalizationValidate(const TensorInfo& input, |
| 23 | const TensorInfo& output, |
| 24 | const TensorInfo& mean, |
| 25 | const TensorInfo& var, |
| 26 | const TensorInfo& beta, |
| 27 | const TensorInfo& gamma, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame^] | 28 | const BatchNormalizationDescriptor& desc, |
| 29 | const ActivationDescriptor* activationDescriptor) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | { |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 31 | const arm_compute::TensorInfo aclInputInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 32 | armcomputetensorutils::BuildArmComputeTensorInfo(input, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 33 | const arm_compute::TensorInfo aclOutputInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 34 | armcomputetensorutils::BuildArmComputeTensorInfo(output, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 35 | const arm_compute::TensorInfo aclMeanInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 36 | armcomputetensorutils::BuildArmComputeTensorInfo(mean, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 37 | const arm_compute::TensorInfo aclVarInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 38 | armcomputetensorutils::BuildArmComputeTensorInfo(var, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 39 | const arm_compute::TensorInfo aclBetaInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 40 | armcomputetensorutils::BuildArmComputeTensorInfo(beta, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 41 | const arm_compute::TensorInfo aclGammaInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 42 | armcomputetensorutils::BuildArmComputeTensorInfo(gamma, desc.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 43 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame^] | 44 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 45 | activationDescriptor); |
| 46 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 47 | return arm_compute::CLBatchNormalizationLayer::validate(&aclInputInfo, |
| 48 | &aclOutputInfo, |
| 49 | &aclMeanInfo, |
| 50 | &aclVarInfo, |
| 51 | &aclBetaInfo, |
| 52 | &aclGammaInfo, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame^] | 53 | desc.m_Eps, |
| 54 | activationInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 55 | } |
| 56 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 57 | ClBatchNormalizationFloatWorkload::ClBatchNormalizationFloatWorkload( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 58 | const BatchNormalizationQueueDescriptor& descriptor, const WorkloadInfo& info) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | : FloatWorkload<BatchNormalizationQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 61 | m_Mean = std::make_unique<arm_compute::CLTensor>(); |
| 62 | BuildArmComputeTensor(*m_Mean, m_Data.m_Mean->GetTensorInfo()); |
| 63 | |
| 64 | m_Variance = std::make_unique<arm_compute::CLTensor>(); |
| 65 | BuildArmComputeTensor(*m_Variance, m_Data.m_Variance->GetTensorInfo()); |
| 66 | |
| 67 | m_Gamma = std::make_unique<arm_compute::CLTensor>(); |
| 68 | BuildArmComputeTensor(*m_Gamma, m_Data.m_Gamma->GetTensorInfo()); |
| 69 | |
| 70 | m_Beta = std::make_unique<arm_compute::CLTensor>(); |
| 71 | BuildArmComputeTensor(*m_Beta, m_Data.m_Beta->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 72 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 73 | m_Data.ValidateInputsOutputs("ClBatchNormalizationFloatWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 74 | |
| 75 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 76 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 78 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 79 | input.info()->set_data_layout(aclDataLayout); |
| 80 | output.info()->set_data_layout(aclDataLayout); |
| 81 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame^] | 82 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 83 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 84 | m_Layer.configure(&input, |
| 85 | &output, |
| 86 | m_Mean.get(), |
| 87 | m_Variance.get(), |
| 88 | m_Beta.get(), |
| 89 | m_Gamma.get(), |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame^] | 90 | m_Data.m_Parameters.m_Eps, |
| 91 | activationInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 92 | |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 93 | InitializeArmComputeClTensorData(*m_Mean, m_Data.m_Mean); |
| 94 | InitializeArmComputeClTensorData(*m_Variance, m_Data.m_Variance); |
| 95 | InitializeArmComputeClTensorData(*m_Beta, m_Data.m_Beta); |
| 96 | InitializeArmComputeClTensorData(*m_Gamma, m_Data.m_Gamma); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 97 | |
| 98 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 99 | // delete all the input tensors that will no longer be needed |
| 100 | m_Layer.prepare(); |
| 101 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 102 | } |
| 103 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 104 | void ClBatchNormalizationFloatWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 105 | { |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 106 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClBatchNormalizationFloatWorkload_Execute"); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 107 | RunClFunction(m_Layer, CHECK_LOCATION()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 108 | } |
| 109 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 110 | void ClBatchNormalizationFloatWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | { |
| 112 | FreeTensorIfUnused(m_Mean); |
| 113 | FreeTensorIfUnused(m_Variance); |
| 114 | FreeTensorIfUnused(m_Gamma); |
| 115 | FreeTensorIfUnused(m_Beta); |
| 116 | } |
| 117 | |
Matthew Bentham | 14e4669 | 2018-09-20 15:35:30 +0100 | [diff] [blame] | 118 | } //namespace armnn |