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