telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include "WorkloadData.hpp" |
| 8 | #include "WorkloadInfo.hpp" |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | |
Narumol Prangnawarat | 867eba5 | 2020-02-03 12:29:56 +0000 | [diff] [blame] | 10 | #include <armnn/backends/IWorkload.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 11 | #include <Profiling.hpp> |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 12 | #include <ProfilingService.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 13 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | #include <algorithm> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | |
| 16 | namespace armnn |
| 17 | { |
| 18 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 19 | // NullWorkload used to denote an unsupported workload when used by the MakeWorkload<> template |
| 20 | // in the various workload factories. |
| 21 | // There should never be an instantiation of a NullWorkload. |
| 22 | class NullWorkload : public IWorkload |
| 23 | { |
| 24 | NullWorkload()=delete; |
| 25 | }; |
| 26 | |
| 27 | template <typename QueueDescriptor> |
| 28 | class BaseWorkload : public IWorkload |
| 29 | { |
| 30 | public: |
| 31 | |
| 32 | BaseWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 33 | : m_Data(descriptor), |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 34 | m_Guid(profiling::ProfilingService::GetNextGuid()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | { |
| 36 | m_Data.Validate(info); |
| 37 | } |
| 38 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 39 | void PostAllocationConfigure() override {} |
| 40 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | const QueueDescriptor& GetData() const { return m_Data; } |
| 42 | |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 43 | profiling::ProfilingGuid GetGuid() const final { return m_Guid; } |
| 44 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 45 | protected: |
| 46 | const QueueDescriptor m_Data; |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 47 | const profiling::ProfilingGuid m_Guid; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | // TypedWorkload used |
| 51 | template <typename QueueDescriptor, armnn::DataType... DataTypes> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 52 | class TypedWorkload : public BaseWorkload<QueueDescriptor> |
| 53 | { |
| 54 | public: |
| 55 | |
| 56 | TypedWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) |
| 57 | : BaseWorkload<QueueDescriptor>(descriptor, info) |
| 58 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | std::vector<armnn::DataType> dataTypes = {DataTypes...}; |
| 60 | armnn::DataType expectedInputType; |
| 61 | |
| 62 | if (!info.m_InputTensorInfos.empty()) |
| 63 | { |
| 64 | expectedInputType = info.m_InputTensorInfos.front().GetDataType(); |
| 65 | |
| 66 | if (std::find(dataTypes.begin(), dataTypes.end(), expectedInputType) == dataTypes.end()) |
| 67 | { |
| 68 | BOOST_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
| 69 | } |
| 70 | BOOST_ASSERT_MSG(std::all_of(std::next(info.m_InputTensorInfos.begin()), |
| 71 | info.m_InputTensorInfos.end(), |
| 72 | [&](auto it){ |
| 73 | return it.GetDataType() == expectedInputType; |
| 74 | }), |
| 75 | "Trying to create workload with incorrect type"); |
| 76 | } |
| 77 | armnn::DataType expectedOutputType; |
| 78 | |
| 79 | if (!info.m_OutputTensorInfos.empty()) |
| 80 | { |
| 81 | expectedOutputType = info.m_OutputTensorInfos.front().GetDataType(); |
| 82 | |
| 83 | if (!info.m_InputTensorInfos.empty()) |
| 84 | { |
| 85 | if (expectedOutputType != expectedInputType) |
| 86 | { |
| 87 | BOOST_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
| 88 | } |
| 89 | } |
| 90 | else if (std::find(dataTypes.begin(), dataTypes.end(), expectedOutputType) == dataTypes.end()) |
| 91 | { |
| 92 | BOOST_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
| 93 | } |
| 94 | BOOST_ASSERT_MSG(std::all_of(std::next(info.m_OutputTensorInfos.begin()), |
| 95 | info.m_OutputTensorInfos.end(), |
| 96 | [&](auto it){ |
| 97 | return it.GetDataType() == expectedOutputType; |
| 98 | }), |
| 99 | "Trying to create workload with incorrect type"); |
| 100 | } |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | template <typename QueueDescriptor, armnn::DataType InputDataType, armnn::DataType OutputDataType> |
| 105 | class MultiTypedWorkload : public BaseWorkload<QueueDescriptor> |
| 106 | { |
| 107 | public: |
| 108 | |
| 109 | MultiTypedWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) |
| 110 | : BaseWorkload<QueueDescriptor>(descriptor, info) |
| 111 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 112 | BOOST_ASSERT_MSG(std::all_of(info.m_InputTensorInfos.begin(), |
| 113 | info.m_InputTensorInfos.end(), |
| 114 | [&](auto it){ |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 115 | return it.GetDataType() == InputDataType; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 116 | }), |
| 117 | "Trying to create workload with incorrect type"); |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 118 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 119 | BOOST_ASSERT_MSG(std::all_of(info.m_OutputTensorInfos.begin(), |
| 120 | info.m_OutputTensorInfos.end(), |
| 121 | [&](auto it){ |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 122 | return it.GetDataType() == OutputDataType; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 123 | }), |
| 124 | "Trying to create workload with incorrect type"); |
| 125 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 128 | // FirstInputTypedWorkload used to check type of the first input |
| 129 | template <typename QueueDescriptor, armnn::DataType DataType> |
| 130 | class FirstInputTypedWorkload : public BaseWorkload<QueueDescriptor> |
| 131 | { |
| 132 | public: |
| 133 | |
| 134 | FirstInputTypedWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) |
| 135 | : BaseWorkload<QueueDescriptor>(descriptor, info) |
| 136 | { |
| 137 | if (!info.m_InputTensorInfos.empty()) |
| 138 | { |
| 139 | BOOST_ASSERT_MSG(info.m_InputTensorInfos.front().GetDataType() == DataType, |
| 140 | "Trying to create workload with incorrect type"); |
| 141 | } |
| 142 | |
| 143 | BOOST_ASSERT_MSG(std::all_of(info.m_OutputTensorInfos.begin(), |
| 144 | info.m_OutputTensorInfos.end(), |
| 145 | [&](auto it){ |
| 146 | return it.GetDataType() == DataType; |
| 147 | }), |
| 148 | "Trying to create workload with incorrect type"); |
| 149 | } |
| 150 | }; |
| 151 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 152 | template <typename QueueDescriptor> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 153 | using FloatWorkload = TypedWorkload<QueueDescriptor, |
| 154 | armnn::DataType::Float16, |
| 155 | armnn::DataType::Float32>; |
| 156 | |
| 157 | template <typename QueueDescriptor> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 158 | using Float32Workload = TypedWorkload<QueueDescriptor, armnn::DataType::Float32>; |
| 159 | |
| 160 | template <typename QueueDescriptor> |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 161 | using Uint8Workload = TypedWorkload<QueueDescriptor, armnn::DataType::QAsymmU8>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 162 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 163 | template <typename QueueDescriptor> |
narpra01 | db2b160 | 2019-01-23 15:23:11 +0000 | [diff] [blame] | 164 | using Int32Workload = TypedWorkload<QueueDescriptor, armnn::DataType::Signed32>; |
| 165 | |
| 166 | template <typename QueueDescriptor> |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 167 | using BooleanWorkload = TypedWorkload<QueueDescriptor, armnn::DataType::Boolean>; |
| 168 | |
| 169 | template <typename QueueDescriptor> |
| 170 | using BaseFloat32ComparisonWorkload = MultiTypedWorkload<QueueDescriptor, |
| 171 | armnn::DataType::Float32, |
| 172 | armnn::DataType::Boolean>; |
| 173 | |
| 174 | template <typename QueueDescriptor> |
| 175 | using BaseUint8ComparisonWorkload = MultiTypedWorkload<QueueDescriptor, |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 176 | armnn::DataType::QAsymmU8, |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 177 | armnn::DataType::Boolean>; |
| 178 | |
| 179 | template <typename QueueDescriptor> |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 180 | using BFloat16ToFloat32Workload = MultiTypedWorkload<QueueDescriptor, |
| 181 | armnn::DataType::BFloat16, |
| 182 | armnn::DataType::Float32>; |
| 183 | |
| 184 | template <typename QueueDescriptor> |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 185 | using Float32ToBFloat16Workload = MultiTypedWorkload<QueueDescriptor, |
| 186 | armnn::DataType::Float32, |
| 187 | armnn::DataType::BFloat16>; |
| 188 | |
| 189 | template <typename QueueDescriptor> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 190 | using Float16ToFloat32Workload = MultiTypedWorkload<QueueDescriptor, |
| 191 | armnn::DataType::Float16, |
| 192 | armnn::DataType::Float32>; |
| 193 | |
| 194 | template <typename QueueDescriptor> |
| 195 | using Float32ToFloat16Workload = MultiTypedWorkload<QueueDescriptor, |
| 196 | armnn::DataType::Float32, |
| 197 | armnn::DataType::Float16>; |
| 198 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 199 | template <typename QueueDescriptor> |
| 200 | using Uint8ToFloat32Workload = MultiTypedWorkload<QueueDescriptor, |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 201 | armnn::DataType::QAsymmU8, |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 202 | armnn::DataType::Float32>; |
| 203 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 204 | } //namespace armnn |