Release 18.08
[platform/upstream/armnn.git] / src / armnn / backends / RefWorkloads / RefFullyConnectedUint8Workload.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5
6 #include "RefFullyConnectedUint8Workload.hpp"
7
8 #include "FullyConnected.hpp"
9 #include "RefWorkloadUtils.hpp"
10
11 #include "Profiling.hpp"
12
13 #include <vector>
14
15 namespace armnn
16 {
17 RefFullyConnectedUint8Workload::RefFullyConnectedUint8Workload(
18     const FullyConnectedQueueDescriptor& descriptor, const WorkloadInfo& info)
19      : Uint8Workload<FullyConnectedQueueDescriptor>(descriptor, info),
20         m_Weight(std::make_unique<ScopedCpuTensorHandle>(*(descriptor.m_Weight))),
21         m_Bias(descriptor.m_Parameters.m_BiasEnabled
22                ? std::make_unique<ScopedCpuTensorHandle>(*(descriptor.m_Bias)) : nullptr) {}
23
24 void RefFullyConnectedUint8Workload::Execute() const
25 {
26     ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefFullyConnectedUint8Workload_Execute");
27
28     const TensorInfo& inputInfo = GetTensorInfo(m_Data.m_Inputs[0]);
29     const TensorInfo& outputInfo = GetTensorInfo(m_Data.m_Outputs[0]);
30
31     const uint8_t* weightData = m_Weight->GetConstTensor<uint8_t>();
32
33     auto dequant = Dequantize(GetInputTensorDataU8(0, m_Data), inputInfo);
34
35     auto weight = Dequantize(weightData, m_Weight->GetTensorInfo());
36
37     std::vector<float> results(outputInfo.GetNumElements());
38
39     if (m_Data.m_Parameters.m_BiasEnabled)
40     {
41         const int32_t* biasData = m_Bias->GetConstTensor<int32_t>();
42         auto           bias     = Dequantize(biasData, m_Bias->GetTensorInfo());
43
44         FullyConnected(dequant.data(),
45                        results.data(),
46                        inputInfo,
47                        outputInfo,
48                        weight.data(),
49                        bias.data(),
50                        m_Data.m_Parameters.m_TransposeWeightMatrix);
51     }
52     else
53     {
54         FullyConnected(dequant.data(),
55                        results.data(),
56                        inputInfo,
57                        outputInfo,
58                        weight.data(),
59                        nullptr,
60                        m_Data.m_Parameters.m_TransposeWeightMatrix);
61     }
62
63     Quantize(GetOutputTensorDataU8(0, m_Data), results.data(), outputInfo);
64 }
65
66 } //namespace armnn