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