2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include <backends/CpuTensorHandle.hpp>
6 #include <backends/MemCopyWorkload.hpp>
7 #include <backends/MakeWorkloadHelper.hpp>
8 #include "RefWorkloadFactory.hpp"
9 #include "workloads/RefWorkloads.hpp"
12 #include <boost/log/trivial.hpp>
17 template <typename F32Workload, typename U8Workload, typename QueueDescriptorType>
18 std::unique_ptr<IWorkload> RefWorkloadFactory::MakeWorkload(const QueueDescriptorType& descriptor,
19 const WorkloadInfo& info) const
21 return armnn::MakeWorkload<NullWorkload, F32Workload, U8Workload>(descriptor, info);
24 RefWorkloadFactory::RefWorkloadFactory()
28 bool RefWorkloadFactory::IsLayerSupported(const Layer& layer, boost::optional<DataType> dataType,
29 std::string& outReasonIfUnsupported)
31 return IWorkloadFactory::IsLayerSupported(Compute::CpuRef, layer, dataType, outReasonIfUnsupported);
34 std::unique_ptr<ITensorHandle> RefWorkloadFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const
36 return std::make_unique<ScopedCpuTensorHandle>(tensorInfo);
39 std::unique_ptr<ITensorHandle> RefWorkloadFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
40 DataLayout dataLayout) const
42 return std::make_unique<ScopedCpuTensorHandle>(tensorInfo);
45 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateInput(const InputQueueDescriptor& descriptor,
46 const WorkloadInfo& info) const
48 if (info.m_InputTensorInfos.empty() )
50 throw InvalidArgumentException("RefWorkloadFactory::CreateInput: Input cannot be zero length");
52 if (info.m_OutputTensorInfos.empty())
54 throw InvalidArgumentException("RefWorkloadFactory::CreateInput: Output cannot be zero length");
57 if (info.m_InputTensorInfos[0].GetNumBytes() != info.m_OutputTensorInfos[0].GetNumBytes())
59 throw InvalidArgumentException("RefWorkloadFactory::CreateInput: data input and output differ in byte count.");
62 return MakeWorkload<CopyMemGenericWorkload, CopyMemGenericWorkload>(descriptor, info);
65 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateOutput(const OutputQueueDescriptor& descriptor,
66 const WorkloadInfo& info) const
68 if (info.m_InputTensorInfos.empty() )
70 throw InvalidArgumentException("RefWorkloadFactory::CreateOutput: Input cannot be zero length");
72 if (info.m_OutputTensorInfos.empty())
74 throw InvalidArgumentException("RefWorkloadFactory::CreateOutput: Output cannot be zero length");
76 if (info.m_InputTensorInfos[0].GetNumBytes() != info.m_OutputTensorInfos[0].GetNumBytes())
78 throw InvalidArgumentException("RefWorkloadFactory::CreateOutput: data input and output differ in byte count.");
81 return MakeWorkload<CopyMemGenericWorkload, CopyMemGenericWorkload>(descriptor, info);
84 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateActivation(const ActivationQueueDescriptor& descriptor,
85 const WorkloadInfo& info) const
87 return MakeWorkload<RefActivationFloat32Workload, RefActivationUint8Workload>(descriptor, info);
90 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateSoftmax(const SoftmaxQueueDescriptor& descriptor,
91 const WorkloadInfo& info) const
93 return MakeWorkload<RefSoftmaxFloat32Workload, RefSoftmaxUint8Workload>(descriptor, info);
96 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateSplitter(const SplitterQueueDescriptor& descriptor,
97 const WorkloadInfo& info) const
99 return MakeWorkload<RefSplitterFloat32Workload, RefSplitterUint8Workload>(descriptor, info);
102 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMerger(const MergerQueueDescriptor& descriptor,
103 const WorkloadInfo& info) const
105 return MakeWorkload<RefMergerFloat32Workload, RefMergerUint8Workload>(descriptor, info);
108 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateFullyConnected(
109 const FullyConnectedQueueDescriptor& descriptor, const WorkloadInfo& info) const
111 return MakeWorkload<RefFullyConnectedFloat32Workload, RefFullyConnectedUint8Workload>(descriptor, info);
114 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreatePermute(const PermuteQueueDescriptor& descriptor,
115 const WorkloadInfo& info) const
117 return MakeWorkload<RefPermuteFloat32Workload, RefPermuteUint8Workload>(descriptor, info);
120 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreatePooling2d(const Pooling2dQueueDescriptor& descriptor,
121 const WorkloadInfo& info) const
123 return MakeWorkload<RefPooling2dFloat32Workload, RefPooling2dUint8Workload>(descriptor, info);
126 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateConvolution2d(
127 const Convolution2dQueueDescriptor& descriptor, const WorkloadInfo& info) const
129 return MakeWorkload<RefConvolution2dFloat32Workload, RefConvolution2dUint8Workload>(descriptor, info);
132 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateDepthwiseConvolution2d(
133 const DepthwiseConvolution2dQueueDescriptor& descriptor, const WorkloadInfo& info) const
135 return MakeWorkload<RefDepthwiseConvolution2dFloat32Workload,
136 RefDepthwiseConvolution2dUint8Workload>(descriptor, info);
139 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateNormalization(
140 const NormalizationQueueDescriptor& descriptor, const WorkloadInfo& info) const
142 return MakeWorkload<RefNormalizationFloat32Workload, NullWorkload>(descriptor, info);
145 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateAddition(const AdditionQueueDescriptor& descriptor,
146 const WorkloadInfo& info) const
148 return MakeWorkload<RefAdditionFloat32Workload, RefAdditionUint8Workload>(descriptor, info);
151 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMultiplication(
152 const MultiplicationQueueDescriptor& descriptor, const WorkloadInfo& info) const
154 return MakeWorkload<RefMultiplicationFloat32Workload, RefMultiplicationUint8Workload>(descriptor, info);
157 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateBatchNormalization(
158 const BatchNormalizationQueueDescriptor& descriptor, const WorkloadInfo& info) const
160 return MakeWorkload<RefBatchNormalizationFloat32Workload, RefBatchNormalizationUint8Workload>(descriptor, info);
163 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMemCopy(const MemCopyQueueDescriptor& descriptor,
164 const WorkloadInfo& info) const
166 if (descriptor.m_Inputs.empty())
168 throw InvalidArgumentException("RefWorkloadFactory: CreateMemCopy() expected an input tensor.");
170 return std::make_unique<CopyMemGenericWorkload>(descriptor, info);
173 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateResizeBilinear(const ResizeBilinearQueueDescriptor& descriptor,
174 const WorkloadInfo& info) const
176 return MakeWorkload<RefResizeBilinearFloat32Workload, RefResizeBilinearUint8Workload>(descriptor, info);
179 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateFakeQuantization(
180 const FakeQuantizationQueueDescriptor& descriptor,
181 const WorkloadInfo& info) const
183 return MakeWorkload<RefFakeQuantizationFloat32Workload, NullWorkload>(descriptor, info);
186 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateL2Normalization(const L2NormalizationQueueDescriptor& descriptor,
187 const WorkloadInfo& info) const
189 return MakeWorkload<RefL2NormalizationFloat32Workload, NullWorkload>(descriptor, info);
192 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConstant(const ConstantQueueDescriptor& descriptor,
193 const WorkloadInfo& info) const
195 return MakeWorkload<RefConstantFloat32Workload, RefConstantUint8Workload>(descriptor, info);
198 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateReshape(const ReshapeQueueDescriptor& descriptor,
199 const WorkloadInfo& info) const
201 return MakeWorkload<RefReshapeFloat32Workload, RefReshapeUint8Workload>(descriptor, info);
204 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateFloor(const FloorQueueDescriptor& descriptor,
205 const WorkloadInfo& info) const
207 return MakeWorkload<RefFloorFloat32Workload, NullWorkload>(descriptor, info);
210 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateLstm(const LstmQueueDescriptor& descriptor,
211 const WorkloadInfo& info) const
213 return MakeWorkload<RefLstmFloat32Workload, NullWorkload>(descriptor, info);
216 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConvertFp16ToFp32(
217 const ConvertFp16ToFp32QueueDescriptor& descriptor,
218 const WorkloadInfo& info) const
220 return std::make_unique<RefConvertFp16ToFp32Workload>(descriptor, info);
223 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConvertFp32ToFp16(
224 const ConvertFp32ToFp16QueueDescriptor& descriptor,
225 const WorkloadInfo& info) const
227 return std::make_unique<RefConvertFp32ToFp16Workload>(descriptor, info);
230 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateDivision(
231 const DivisionQueueDescriptor& descriptor, const WorkloadInfo& info) const
233 return MakeWorkload<RefDivisionFloat32Workload, RefDivisionUint8Workload>(descriptor, info);
236 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateSubtraction(
237 const SubtractionQueueDescriptor& descriptor, const WorkloadInfo& info) const
239 return MakeWorkload<RefSubtractionFloat32Workload, RefSubtractionUint8Workload>(descriptor, info);
242 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMean(
243 const MeanQueueDescriptor& descriptor, const WorkloadInfo& info) const
245 return MakeWorkload<NullWorkload, NullWorkload>(descriptor, info);
248 std::unique_ptr<IWorkload> RefWorkloadFactory::CreatePad(const PadQueueDescriptor& descriptor,
249 const WorkloadInfo& info) const
251 return MakeWorkload<NullWorkload, NullWorkload>(descriptor, info);