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 <aclCommon/ArmComputeTensorUtils.hpp> |
| 10 | #include <aclCommon/ArmComputeUtils.hpp> |
Mike Kelly | 0d4ed39 | 2020-11-13 15:26:41 +0000 | [diff] [blame] | 11 | #include <backendsCommon/CpuTensorHandle.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, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 25 | const BatchNormalizationDescriptor& desc, |
| 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 = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 29 | armcomputetensorutils::BuildArmComputeTensorInfo(input, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 30 | const arm_compute::TensorInfo aclOutputInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 31 | armcomputetensorutils::BuildArmComputeTensorInfo(output, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 32 | const arm_compute::TensorInfo aclMeanInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 33 | armcomputetensorutils::BuildArmComputeTensorInfo(mean, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 34 | const arm_compute::TensorInfo aclVarInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 35 | armcomputetensorutils::BuildArmComputeTensorInfo(var, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 36 | const arm_compute::TensorInfo aclBetaInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 37 | armcomputetensorutils::BuildArmComputeTensorInfo(beta, desc.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 38 | const arm_compute::TensorInfo aclGammaInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 39 | armcomputetensorutils::BuildArmComputeTensorInfo(gamma, desc.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, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 50 | desc.m_Eps, |
| 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 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | m_Mean = std::make_unique<arm_compute::CLTensor>(); |
| 61 | BuildArmComputeTensor(*m_Mean, m_Data.m_Mean->GetTensorInfo()); |
| 62 | |
| 63 | m_Variance = std::make_unique<arm_compute::CLTensor>(); |
| 64 | BuildArmComputeTensor(*m_Variance, m_Data.m_Variance->GetTensorInfo()); |
| 65 | |
| 66 | m_Gamma = std::make_unique<arm_compute::CLTensor>(); |
| 67 | BuildArmComputeTensor(*m_Gamma, m_Data.m_Gamma->GetTensorInfo()); |
| 68 | |
| 69 | m_Beta = std::make_unique<arm_compute::CLTensor>(); |
| 70 | BuildArmComputeTensor(*m_Beta, m_Data.m_Beta->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 71 | |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 72 | m_Data.ValidateInputsOutputs("ClBatchNormalizationFloatWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | |
| 74 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 75 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 76 | |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 77 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 78 | input.info()->set_data_layout(aclDataLayout); |
| 79 | output.info()->set_data_layout(aclDataLayout); |
| 80 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 81 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 82 | |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 83 | m_Layer.configure(clCompileContext, |
| 84 | &input, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 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 |