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 "RefBackendId.hpp"
10 #include "workloads/RefWorkloads.hpp"
13 #include <boost/log/trivial.hpp>
20 static const BackendId s_Id{RefBackendId()};
23 template <typename F32Workload, typename U8Workload, typename QueueDescriptorType>
24 std::unique_ptr<IWorkload> RefWorkloadFactory::MakeWorkload(const QueueDescriptorType& descriptor,
25 const WorkloadInfo& info) const
27 return armnn::MakeWorkloadHelper<NullWorkload, F32Workload, U8Workload>(descriptor, info);
30 RefWorkloadFactory::RefWorkloadFactory()
34 const BackendId& RefWorkloadFactory::GetBackendId() const
39 bool RefWorkloadFactory::IsLayerSupported(const Layer& layer,
40 Optional<DataType> dataType,
41 std::string& outReasonIfUnsupported)
43 return IWorkloadFactory::IsLayerSupported(s_Id, layer, dataType, outReasonIfUnsupported);
46 std::unique_ptr<ITensorHandle> RefWorkloadFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const
48 return std::make_unique<ScopedCpuTensorHandle>(tensorInfo);
51 std::unique_ptr<ITensorHandle> RefWorkloadFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
52 DataLayout dataLayout) const
54 return std::make_unique<ScopedCpuTensorHandle>(tensorInfo);
57 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateInput(const InputQueueDescriptor& descriptor,
58 const WorkloadInfo& info) const
60 if (info.m_InputTensorInfos.empty() )
62 throw InvalidArgumentException("RefWorkloadFactory::CreateInput: Input cannot be zero length");
64 if (info.m_OutputTensorInfos.empty())
66 throw InvalidArgumentException("RefWorkloadFactory::CreateInput: Output cannot be zero length");
69 if (info.m_InputTensorInfos[0].GetNumBytes() != info.m_OutputTensorInfos[0].GetNumBytes())
71 throw InvalidArgumentException("RefWorkloadFactory::CreateInput: data input and output differ in byte count.");
74 return MakeWorkload<CopyMemGenericWorkload, CopyMemGenericWorkload>(descriptor, info);
77 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateOutput(const OutputQueueDescriptor& descriptor,
78 const WorkloadInfo& info) const
80 if (info.m_InputTensorInfos.empty() )
82 throw InvalidArgumentException("RefWorkloadFactory::CreateOutput: Input cannot be zero length");
84 if (info.m_OutputTensorInfos.empty())
86 throw InvalidArgumentException("RefWorkloadFactory::CreateOutput: Output cannot be zero length");
88 if (info.m_InputTensorInfos[0].GetNumBytes() != info.m_OutputTensorInfos[0].GetNumBytes())
90 throw InvalidArgumentException("RefWorkloadFactory::CreateOutput: data input and output differ in byte count.");
93 return MakeWorkload<CopyMemGenericWorkload, CopyMemGenericWorkload>(descriptor, info);
96 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateActivation(const ActivationQueueDescriptor& descriptor,
97 const WorkloadInfo& info) const
99 return MakeWorkload<RefActivationFloat32Workload, RefActivationUint8Workload>(descriptor, info);
102 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateSoftmax(const SoftmaxQueueDescriptor& descriptor,
103 const WorkloadInfo& info) const
105 return MakeWorkload<RefSoftmaxFloat32Workload, RefSoftmaxUint8Workload>(descriptor, info);
108 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateSplitter(const SplitterQueueDescriptor& descriptor,
109 const WorkloadInfo& info) const
111 return MakeWorkload<RefSplitterFloat32Workload, RefSplitterUint8Workload>(descriptor, info);
114 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMerger(const MergerQueueDescriptor& descriptor,
115 const WorkloadInfo& info) const
117 return MakeWorkload<RefMergerFloat32Workload, RefMergerUint8Workload>(descriptor, info);
120 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateFullyConnected(
121 const FullyConnectedQueueDescriptor& descriptor, const WorkloadInfo& info) const
123 return MakeWorkload<RefFullyConnectedFloat32Workload, RefFullyConnectedUint8Workload>(descriptor, info);
126 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreatePermute(const PermuteQueueDescriptor& descriptor,
127 const WorkloadInfo& info) const
129 return MakeWorkloadHelper<RefPermuteFloat16Workload, RefPermuteFloat32Workload, RefPermuteUint8Workload>
133 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreatePooling2d(const Pooling2dQueueDescriptor& descriptor,
134 const WorkloadInfo& info) const
136 return MakeWorkload<RefPooling2dFloat32Workload, RefPooling2dUint8Workload>(descriptor, info);
139 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateConvolution2d(
140 const Convolution2dQueueDescriptor& descriptor, const WorkloadInfo& info) const
142 return MakeWorkload<RefConvolution2dFloat32Workload, RefConvolution2dUint8Workload>(descriptor, info);
145 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateDepthwiseConvolution2d(
146 const DepthwiseConvolution2dQueueDescriptor& descriptor, const WorkloadInfo& info) const
148 return MakeWorkload<RefDepthwiseConvolution2dFloat32Workload,
149 RefDepthwiseConvolution2dUint8Workload>(descriptor, info);
152 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateNormalization(
153 const NormalizationQueueDescriptor& descriptor, const WorkloadInfo& info) const
155 return MakeWorkload<RefNormalizationFloat32Workload, NullWorkload>(descriptor, info);
158 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateAddition(const AdditionQueueDescriptor& descriptor,
159 const WorkloadInfo& info) const
161 return MakeWorkload<RefAdditionFloat32Workload, RefAdditionUint8Workload>(descriptor, info);
164 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMultiplication(
165 const MultiplicationQueueDescriptor& descriptor, const WorkloadInfo& info) const
167 return MakeWorkload<RefMultiplicationFloat32Workload, RefMultiplicationUint8Workload>(descriptor, info);
170 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateBatchNormalization(
171 const BatchNormalizationQueueDescriptor& descriptor, const WorkloadInfo& info) const
173 return MakeWorkload<RefBatchNormalizationFloat32Workload, RefBatchNormalizationUint8Workload>(descriptor, info);
176 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMemCopy(const MemCopyQueueDescriptor& descriptor,
177 const WorkloadInfo& info) const
179 if (descriptor.m_Inputs.empty())
181 throw InvalidArgumentException("RefWorkloadFactory: CreateMemCopy() expected an input tensor.");
183 return std::make_unique<CopyMemGenericWorkload>(descriptor, info);
186 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateResizeBilinear(const ResizeBilinearQueueDescriptor& descriptor,
187 const WorkloadInfo& info) const
189 return MakeWorkload<RefResizeBilinearFloat32Workload, RefResizeBilinearUint8Workload>(descriptor, info);
192 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateFakeQuantization(
193 const FakeQuantizationQueueDescriptor& descriptor,
194 const WorkloadInfo& info) const
196 return MakeWorkload<RefFakeQuantizationFloat32Workload, NullWorkload>(descriptor, info);
199 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateL2Normalization(const L2NormalizationQueueDescriptor& descriptor,
200 const WorkloadInfo& info) const
202 return MakeWorkload<RefL2NormalizationFloat32Workload, NullWorkload>(descriptor, info);
205 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConstant(const ConstantQueueDescriptor& descriptor,
206 const WorkloadInfo& info) const
208 return MakeWorkload<RefConstantFloat32Workload, RefConstantUint8Workload>(descriptor, info);
211 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateReshape(const ReshapeQueueDescriptor& descriptor,
212 const WorkloadInfo& info) const
214 return MakeWorkload<RefReshapeFloat32Workload, RefReshapeUint8Workload>(descriptor, info);
217 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateFloor(const FloorQueueDescriptor& descriptor,
218 const WorkloadInfo& info) const
220 return MakeWorkload<RefFloorFloat32Workload, NullWorkload>(descriptor, info);
223 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateLstm(const LstmQueueDescriptor& descriptor,
224 const WorkloadInfo& info) const
226 return MakeWorkload<RefLstmFloat32Workload, NullWorkload>(descriptor, info);
229 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConvertFp16ToFp32(
230 const ConvertFp16ToFp32QueueDescriptor& descriptor,
231 const WorkloadInfo& info) const
233 return std::make_unique<RefConvertFp16ToFp32Workload>(descriptor, info);
236 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConvertFp32ToFp16(
237 const ConvertFp32ToFp16QueueDescriptor& descriptor,
238 const WorkloadInfo& info) const
240 return std::make_unique<RefConvertFp32ToFp16Workload>(descriptor, info);
243 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateDivision(
244 const DivisionQueueDescriptor& descriptor, const WorkloadInfo& info) const
246 return MakeWorkload<RefDivisionFloat32Workload, RefDivisionUint8Workload>(descriptor, info);
249 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateSubtraction(
250 const SubtractionQueueDescriptor& descriptor, const WorkloadInfo& info) const
252 return MakeWorkload<RefSubtractionFloat32Workload, RefSubtractionUint8Workload>(descriptor, info);
255 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMean(
256 const MeanQueueDescriptor& descriptor, const WorkloadInfo& info) const
258 return MakeWorkload<RefMeanFloat32Workload, RefMeanUint8Workload>(descriptor, info);
261 std::unique_ptr<IWorkload> RefWorkloadFactory::CreatePad(const PadQueueDescriptor& descriptor,
262 const WorkloadInfo& info) const
264 return MakeWorkload<RefPadFloat32Workload, RefPadUint8Workload>(descriptor, info);