IVGCVSW-2093 Add SpaceToBatchNd layer and corresponding no-op factory implementations
[platform/upstream/armnn.git] / src / backends / backendsCommon / MakeWorkloadHelper.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6
7 namespace armnn
8 {
9 namespace
10 {
11
12 // Make a workload of the specified WorkloadType.
13 template<typename WorkloadType>
14 struct MakeWorkloadForType
15 {
16     template<typename QueueDescriptorType, typename... Args>
17     static std::unique_ptr<WorkloadType> Func(const QueueDescriptorType& descriptor,
18                                               const WorkloadInfo& info,
19                                               Args&&... args)
20     {
21         return std::make_unique<WorkloadType>(descriptor, info, std::forward<Args>(args)...);
22     }
23 };
24
25 // Specialization for void workload type used for unsupported workloads.
26 template<>
27 struct MakeWorkloadForType<NullWorkload>
28 {
29     template<typename QueueDescriptorType, typename... Args>
30     static std::unique_ptr<NullWorkload> Func(const QueueDescriptorType& descriptor,
31                                               const WorkloadInfo& info,
32                                               Args&&... args)
33     {
34         return nullptr;
35     }
36 };
37
38 // Makes a workload for one the specified types based on the data type requirements of the tensorinfo.
39 // Specify type void as the WorkloadType for unsupported DataType/WorkloadType combos.
40 template <typename Float16Workload, typename Float32Workload, typename Uint8Workload, typename QueueDescriptorType,
41     typename... Args>
42 std::unique_ptr<IWorkload> MakeWorkloadHelper(const QueueDescriptorType& descriptor,
43                                               const WorkloadInfo& info,
44                                               Args&&... args)
45 {
46     const DataType dataType = !info.m_InputTensorInfos.empty() ?
47         info.m_InputTensorInfos[0].GetDataType()
48         : info.m_OutputTensorInfos[0].GetDataType();
49
50     BOOST_ASSERT(info.m_InputTensorInfos.empty() || info.m_OutputTensorInfos.empty()
51         || info.m_InputTensorInfos[0].GetDataType() == info.m_OutputTensorInfos[0].GetDataType());
52
53     switch (dataType)
54     {
55         case DataType::Float16:
56             return MakeWorkloadForType<Float16Workload>::Func(descriptor, info, std::forward<Args>(args)...);
57         case DataType::Float32:
58             return MakeWorkloadForType<Float32Workload>::Func(descriptor, info, std::forward<Args>(args)...);
59         case DataType::QuantisedAsymm8:
60             return MakeWorkloadForType<Uint8Workload>::Func(descriptor, info, std::forward<Args>(args)...);
61         default:
62             BOOST_ASSERT_MSG(false, "Unknown DataType.");
63             return nullptr;
64     }
65 }
66
67 // Makes a workload for one the specified types based on the data type requirements of the tensorinfo.
68 // Calling this method is the equivalent of calling the three typed MakeWorkload method with <FloatWorkload,
69 // FloatWorkload, Uint8Workload>.
70 // Specify type void as the WorkloadType for unsupported DataType/WorkloadType combos.
71 template <typename FloatWorkload, typename Uint8Workload, typename QueueDescriptorType, typename... Args>
72 std::unique_ptr<IWorkload> MakeWorkloadHelper(const QueueDescriptorType& descriptor,
73                                               const WorkloadInfo& info,
74                                               Args&&... args)
75 {
76     return MakeWorkloadHelper<FloatWorkload, FloatWorkload, Uint8Workload>(descriptor, info,
77        std::forward<Args>(args)...);
78 }
79
80
81 } //namespace
82 } //namespace armnn