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 armnn::MakeWorkload<RefPermuteFloat16Workload, RefPermuteFloat32Workload, RefPermuteUint8Workload>
121 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreatePooling2d(const Pooling2dQueueDescriptor& descriptor,
122 const WorkloadInfo& info) const
124 return MakeWorkload<RefPooling2dFloat32Workload, RefPooling2dUint8Workload>(descriptor, info);
127 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateConvolution2d(
128 const Convolution2dQueueDescriptor& descriptor, const WorkloadInfo& info) const
130 return MakeWorkload<RefConvolution2dFloat32Workload, RefConvolution2dUint8Workload>(descriptor, info);
133 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateDepthwiseConvolution2d(
134 const DepthwiseConvolution2dQueueDescriptor& descriptor, const WorkloadInfo& info) const
136 return MakeWorkload<RefDepthwiseConvolution2dFloat32Workload,
137 RefDepthwiseConvolution2dUint8Workload>(descriptor, info);
140 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateNormalization(
141 const NormalizationQueueDescriptor& descriptor, const WorkloadInfo& info) const
143 return MakeWorkload<RefNormalizationFloat32Workload, NullWorkload>(descriptor, info);
146 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateAddition(const AdditionQueueDescriptor& descriptor,
147 const WorkloadInfo& info) const
149 return MakeWorkload<RefAdditionFloat32Workload, RefAdditionUint8Workload>(descriptor, info);
152 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMultiplication(
153 const MultiplicationQueueDescriptor& descriptor, const WorkloadInfo& info) const
155 return MakeWorkload<RefMultiplicationFloat32Workload, RefMultiplicationUint8Workload>(descriptor, info);
158 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateBatchNormalization(
159 const BatchNormalizationQueueDescriptor& descriptor, const WorkloadInfo& info) const
161 return MakeWorkload<RefBatchNormalizationFloat32Workload, RefBatchNormalizationUint8Workload>(descriptor, info);
164 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMemCopy(const MemCopyQueueDescriptor& descriptor,
165 const WorkloadInfo& info) const
167 if (descriptor.m_Inputs.empty())
169 throw InvalidArgumentException("RefWorkloadFactory: CreateMemCopy() expected an input tensor.");
171 return std::make_unique<CopyMemGenericWorkload>(descriptor, info);
174 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateResizeBilinear(const ResizeBilinearQueueDescriptor& descriptor,
175 const WorkloadInfo& info) const
177 return MakeWorkload<RefResizeBilinearFloat32Workload, RefResizeBilinearUint8Workload>(descriptor, info);
180 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateFakeQuantization(
181 const FakeQuantizationQueueDescriptor& descriptor,
182 const WorkloadInfo& info) const
184 return MakeWorkload<RefFakeQuantizationFloat32Workload, NullWorkload>(descriptor, info);
187 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateL2Normalization(const L2NormalizationQueueDescriptor& descriptor,
188 const WorkloadInfo& info) const
190 return MakeWorkload<RefL2NormalizationFloat32Workload, NullWorkload>(descriptor, info);
193 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConstant(const ConstantQueueDescriptor& descriptor,
194 const WorkloadInfo& info) const
196 return MakeWorkload<RefConstantFloat32Workload, RefConstantUint8Workload>(descriptor, info);
199 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateReshape(const ReshapeQueueDescriptor& descriptor,
200 const WorkloadInfo& info) const
202 return MakeWorkload<RefReshapeFloat32Workload, RefReshapeUint8Workload>(descriptor, info);
205 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateFloor(const FloorQueueDescriptor& descriptor,
206 const WorkloadInfo& info) const
208 return MakeWorkload<RefFloorFloat32Workload, NullWorkload>(descriptor, info);
211 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateLstm(const LstmQueueDescriptor& descriptor,
212 const WorkloadInfo& info) const
214 return MakeWorkload<RefLstmFloat32Workload, NullWorkload>(descriptor, info);
217 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConvertFp16ToFp32(
218 const ConvertFp16ToFp32QueueDescriptor& descriptor,
219 const WorkloadInfo& info) const
221 return std::make_unique<RefConvertFp16ToFp32Workload>(descriptor, info);
224 std::unique_ptr<IWorkload> RefWorkloadFactory::CreateConvertFp32ToFp16(
225 const ConvertFp32ToFp16QueueDescriptor& descriptor,
226 const WorkloadInfo& info) const
228 return std::make_unique<RefConvertFp32ToFp16Workload>(descriptor, info);
231 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateDivision(
232 const DivisionQueueDescriptor& descriptor, const WorkloadInfo& info) const
234 return MakeWorkload<RefDivisionFloat32Workload, RefDivisionUint8Workload>(descriptor, info);
237 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateSubtraction(
238 const SubtractionQueueDescriptor& descriptor, const WorkloadInfo& info) const
240 return MakeWorkload<RefSubtractionFloat32Workload, RefSubtractionUint8Workload>(descriptor, info);
243 std::unique_ptr<armnn::IWorkload> RefWorkloadFactory::CreateMean(
244 const MeanQueueDescriptor& descriptor, const WorkloadInfo& info) const
246 return MakeWorkload<RefMeanFloat32Workload, RefMeanUint8Workload>(descriptor, info);
249 std::unique_ptr<IWorkload> RefWorkloadFactory::CreatePad(const PadQueueDescriptor& descriptor,
250 const WorkloadInfo& info) const
252 return MakeWorkload<RefPadWorkload, NullWorkload>(descriptor, info);