9d8174ce7dea4bdc618710ece8c0d06e75ac46f2
[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         boost::ignore_unused(descriptor);
35         boost::ignore_unused(info);
36         boost::ignore_unused(args...);
37         return nullptr;
38     }
39 };
40
41 // Makes a workload for one the specified types based on the data type requirements of the tensorinfo.
42 // Specify type void as the WorkloadType for unsupported DataType/WorkloadType combos.
43 template <typename Float16Workload, typename Float32Workload, typename Uint8Workload, typename Int32Workload,
44           typename BooleanWorkload, typename QueueDescriptorType, typename... Args>
45 std::unique_ptr<IWorkload> MakeWorkloadHelper(const QueueDescriptorType& descriptor,
46                                               const WorkloadInfo& info,
47                                               Args&&... args)
48 {
49     const DataType dataType = !info.m_InputTensorInfos.empty() ?
50         info.m_InputTensorInfos[0].GetDataType()
51         : 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         case DataType::Signed32:
62             return MakeWorkloadForType<Int32Workload>::Func(descriptor, info, std::forward<Args>(args)...);
63         case DataType::Boolean:
64             return MakeWorkloadForType<BooleanWorkload>::Func(descriptor, info, std::forward<Args>(args)...);
65         case DataType::QuantisedSymm16:
66             return nullptr;
67         default:
68             BOOST_ASSERT_MSG(false, "Unknown DataType.");
69             return nullptr;
70     }
71 }
72
73 // Makes a workload for one the specified types based on the data type requirements of the tensorinfo.
74 // Calling this method is the equivalent of calling the five typed MakeWorkload method with <FloatWorkload,
75 // FloatWorkload, Uint8Workload, NullWorkload, NullWorkload>.
76 // Specify type void as the WorkloadType for unsupported DataType/WorkloadType combos.
77 template <typename FloatWorkload, typename Uint8Workload, typename QueueDescriptorType, typename... Args>
78 std::unique_ptr<IWorkload> MakeWorkloadHelper(const QueueDescriptorType& descriptor,
79                                               const WorkloadInfo& info,
80                                               Args&&... args)
81 {
82     return MakeWorkloadHelper<FloatWorkload, FloatWorkload, Uint8Workload, NullWorkload, NullWorkload>(
83         descriptor,
84         info,
85         std::forward<Args>(args)...);
86 }
87
88 } //namespace
89 } //namespace armnn