blob: 60eb138b42d44426d4ca93d009ec40c7b03a4fcc [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
Matthew Benthamab8cdc12018-09-17 11:17:41 +01006#include "ClFullyConnectedWorkload.hpp"
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00007#include <cl/ClTensorHandle.hpp>
8#include <backendsCommon/CpuTensorHandle.hpp>
9#include <aclCommon/ArmComputeTensorUtils.hpp>
10#include <aclCommon/ArmComputeUtils.hpp>
11#include <cl/ClLayerSupport.hpp>
telsoa014fcda012018-03-09 14:13:49 +000012
Matthew Bentham14e46692018-09-20 15:35:30 +010013#include "ClWorkloadUtils.hpp"
14
telsoa014fcda012018-03-09 14:13:49 +000015namespace armnn
16{
17using namespace armcomputetensorutils;
18
telsoa01c577f2c2018-08-31 09:22:23 +010019arm_compute::Status ClFullyConnectedWorkloadValidate(const TensorInfo& input,
20 const TensorInfo& output,
21 const TensorInfo& weights,
22 const TensorInfo& biases,
23 const FullyConnectedDescriptor& descriptor)
24{
25 const arm_compute::TensorInfo aclInput = BuildArmComputeTensorInfo(input);
26 const arm_compute::TensorInfo aclOutput = BuildArmComputeTensorInfo(output);
27 const arm_compute::TensorInfo aclWeights = BuildArmComputeTensorInfo(weights);
28
29 arm_compute::TensorInfo aclBiases;
30 arm_compute::TensorInfo *optionalAclBiases = nullptr;
31 if (descriptor.m_BiasEnabled)
32 {
33 aclBiases = BuildArmComputeTensorInfo(biases);
34 optionalAclBiases = &aclBiases;
35 }
36
37 const arm_compute::FullyConnectedLayerInfo fullyConnectedLayerInfo =
38 ConvertFullyConnectedDescriptorToAclFullyConnectedLayerInfo(descriptor);
39
40 return arm_compute::CLFullyConnectedLayer::validate(&aclInput,
41 &aclWeights,
42 optionalAclBiases,
43 &aclOutput,
44 fullyConnectedLayerInfo);
45}
46
Matthew Benthamab8cdc12018-09-17 11:17:41 +010047ClFullyConnectedWorkload::ClFullyConnectedWorkload(const FullyConnectedQueueDescriptor& descriptor,
surmeh013537c2c2018-05-18 16:31:43 +010048 const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager)
Matthew Benthamab8cdc12018-09-17 11:17:41 +010049 : BaseWorkload<FullyConnectedQueueDescriptor>(descriptor, info)
telsoa01c577f2c2018-08-31 09:22:23 +010050 , m_FullyConnectedLayer(memoryManager)
telsoa014fcda012018-03-09 14:13:49 +000051{
telsoa01c577f2c2018-08-31 09:22:23 +010052 m_WeightsTensor = std::make_unique<arm_compute::CLTensor>();
53 BuildArmComputeTensor(*m_WeightsTensor, m_Data.m_Weight->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000054
telsoa014fcda012018-03-09 14:13:49 +000055 if (m_Data.m_Parameters.m_BiasEnabled)
56 {
telsoa01c577f2c2018-08-31 09:22:23 +010057 m_BiasesTensor = std::make_unique<arm_compute::CLTensor>();
58 BuildArmComputeTensor(*m_BiasesTensor, m_Data.m_Bias->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000059 }
60
Matthew Benthamab8cdc12018-09-17 11:17:41 +010061 m_Data.ValidateInputsOutputs("ClFullyConnectedWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000062
63 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
64 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa01c577f2c2018-08-31 09:22:23 +010065
telsoa014fcda012018-03-09 14:13:49 +000066 // Construct
telsoa01c577f2c2018-08-31 09:22:23 +010067 arm_compute::FullyConnectedLayerInfo fc_info;
68 fc_info.transpose_weights = m_Data.m_Parameters.m_TransposeWeightMatrix;
69 m_FullyConnectedLayer.configure(&input, m_WeightsTensor.get(), m_BiasesTensor.get(), &output, fc_info);
telsoa014fcda012018-03-09 14:13:49 +000070
Matthew Bentham785df502018-09-21 10:29:58 +010071 InitializeArmComputeClTensorData(*m_WeightsTensor, m_Data.m_Weight);
telsoa014fcda012018-03-09 14:13:49 +000072
telsoa01c577f2c2018-08-31 09:22:23 +010073 if (m_BiasesTensor)
telsoa014fcda012018-03-09 14:13:49 +000074 {
Matthew Bentham785df502018-09-21 10:29:58 +010075 InitializeArmComputeClTensorData(*m_BiasesTensor, m_Data.m_Bias);
telsoa014fcda012018-03-09 14:13:49 +000076 }
telsoa01c577f2c2018-08-31 09:22:23 +010077
78 // Force Compute Library to perform the necessary copying and reshaping, after which
79 // delete all the input tensors that will no longer be needed
80 m_FullyConnectedLayer.prepare();
81 FreeUnusedTensors();
telsoa014fcda012018-03-09 14:13:49 +000082}
83
Matthew Benthamab8cdc12018-09-17 11:17:41 +010084void ClFullyConnectedWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +000085{
Matthew Benthamab8cdc12018-09-17 11:17:41 +010086 ARMNN_SCOPED_PROFILING_EVENT_CL("ClFullyConnectedWorkload_Execute");
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +010087 RunClFunction(m_FullyConnectedLayer, CHECK_LOCATION());
telsoa01c577f2c2018-08-31 09:22:23 +010088}
89
Matthew Benthamab8cdc12018-09-17 11:17:41 +010090void ClFullyConnectedWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +010091{
92 FreeTensorIfUnused(m_WeightsTensor);
93 FreeTensorIfUnused(m_BiasesTensor);
telsoa014fcda012018-03-09 14:13:49 +000094}
95
surmeh013537c2c2018-05-18 16:31:43 +010096} //namespace armnn