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 | |
Mike Kelly | 386ff1a | 2021-03-29 15:04:50 +0100 | [diff] [blame] | 40 | void ExecuteAsync(WorkingMemDescriptor&) override {}; |
| 41 | |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 42 | void PostAllocationConfigure() override {} |
| 43 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 44 | const QueueDescriptor& GetData() const { return m_Data; } |
| 45 | |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 46 | profiling::ProfilingGuid GetGuid() const final { return m_Guid; } |
| 47 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 48 | protected: |
| 49 | const QueueDescriptor m_Data; |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 50 | const profiling::ProfilingGuid m_Guid; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 53 | // TypedWorkload used |
| 54 | template <typename QueueDescriptor, armnn::DataType... DataTypes> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 55 | class TypedWorkload : public BaseWorkload<QueueDescriptor> |
| 56 | { |
| 57 | public: |
| 58 | |
| 59 | TypedWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) |
| 60 | : BaseWorkload<QueueDescriptor>(descriptor, info) |
| 61 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | std::vector<armnn::DataType> dataTypes = {DataTypes...}; |
| 63 | armnn::DataType expectedInputType; |
| 64 | |
| 65 | if (!info.m_InputTensorInfos.empty()) |
| 66 | { |
| 67 | expectedInputType = info.m_InputTensorInfos.front().GetDataType(); |
| 68 | |
| 69 | if (std::find(dataTypes.begin(), dataTypes.end(), expectedInputType) == dataTypes.end()) |
| 70 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 71 | ARMNN_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | } |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 73 | ARMNN_ASSERT_MSG(std::all_of(std::next(info.m_InputTensorInfos.begin()), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 74 | info.m_InputTensorInfos.end(), |
| 75 | [&](auto it){ |
| 76 | return it.GetDataType() == expectedInputType; |
| 77 | }), |
| 78 | "Trying to create workload with incorrect type"); |
| 79 | } |
| 80 | armnn::DataType expectedOutputType; |
| 81 | |
| 82 | if (!info.m_OutputTensorInfos.empty()) |
| 83 | { |
| 84 | expectedOutputType = info.m_OutputTensorInfos.front().GetDataType(); |
| 85 | |
| 86 | if (!info.m_InputTensorInfos.empty()) |
| 87 | { |
| 88 | if (expectedOutputType != expectedInputType) |
| 89 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 90 | ARMNN_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | else if (std::find(dataTypes.begin(), dataTypes.end(), expectedOutputType) == dataTypes.end()) |
| 94 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 95 | ARMNN_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 96 | } |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 97 | ARMNN_ASSERT_MSG(std::all_of(std::next(info.m_OutputTensorInfos.begin()), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | info.m_OutputTensorInfos.end(), |
| 99 | [&](auto it){ |
| 100 | return it.GetDataType() == expectedOutputType; |
| 101 | }), |
| 102 | "Trying to create workload with incorrect type"); |
| 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | template <typename QueueDescriptor, armnn::DataType InputDataType, armnn::DataType OutputDataType> |
| 108 | class MultiTypedWorkload : public BaseWorkload<QueueDescriptor> |
| 109 | { |
| 110 | public: |
| 111 | |
| 112 | MultiTypedWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) |
| 113 | : BaseWorkload<QueueDescriptor>(descriptor, info) |
| 114 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 115 | ARMNN_ASSERT_MSG(std::all_of(info.m_InputTensorInfos.begin(), |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 116 | info.m_InputTensorInfos.end(), |
| 117 | [&](auto it){ |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 118 | return it.GetDataType() == InputDataType; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 119 | }), |
| 120 | "Trying to create workload with incorrect type"); |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 121 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 122 | ARMNN_ASSERT_MSG(std::all_of(info.m_OutputTensorInfos.begin(), |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 123 | info.m_OutputTensorInfos.end(), |
| 124 | [&](auto it){ |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 125 | return it.GetDataType() == OutputDataType; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 126 | }), |
| 127 | "Trying to create workload with incorrect type"); |
| 128 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 131 | // FirstInputTypedWorkload used to check type of the first input |
| 132 | template <typename QueueDescriptor, armnn::DataType DataType> |
| 133 | class FirstInputTypedWorkload : public BaseWorkload<QueueDescriptor> |
| 134 | { |
| 135 | public: |
| 136 | |
| 137 | FirstInputTypedWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) |
| 138 | : BaseWorkload<QueueDescriptor>(descriptor, info) |
| 139 | { |
| 140 | if (!info.m_InputTensorInfos.empty()) |
| 141 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 142 | ARMNN_ASSERT_MSG(info.m_InputTensorInfos.front().GetDataType() == DataType, |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 143 | "Trying to create workload with incorrect type"); |
| 144 | } |
| 145 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 146 | ARMNN_ASSERT_MSG(std::all_of(info.m_OutputTensorInfos.begin(), |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 147 | info.m_OutputTensorInfos.end(), |
| 148 | [&](auto it){ |
| 149 | return it.GetDataType() == DataType; |
| 150 | }), |
| 151 | "Trying to create workload with incorrect type"); |
| 152 | } |
| 153 | }; |
| 154 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 155 | template <typename QueueDescriptor> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 156 | using FloatWorkload = TypedWorkload<QueueDescriptor, |
| 157 | armnn::DataType::Float16, |
| 158 | armnn::DataType::Float32>; |
| 159 | |
| 160 | template <typename QueueDescriptor> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 161 | using Float32Workload = TypedWorkload<QueueDescriptor, armnn::DataType::Float32>; |
| 162 | |
| 163 | template <typename QueueDescriptor> |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 164 | using Uint8Workload = TypedWorkload<QueueDescriptor, armnn::DataType::QAsymmU8>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 165 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 166 | template <typename QueueDescriptor> |
narpra01 | db2b160 | 2019-01-23 15:23:11 +0000 | [diff] [blame] | 167 | using Int32Workload = TypedWorkload<QueueDescriptor, armnn::DataType::Signed32>; |
| 168 | |
| 169 | template <typename QueueDescriptor> |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 170 | using BooleanWorkload = TypedWorkload<QueueDescriptor, armnn::DataType::Boolean>; |
| 171 | |
| 172 | template <typename QueueDescriptor> |
| 173 | using BaseFloat32ComparisonWorkload = MultiTypedWorkload<QueueDescriptor, |
| 174 | armnn::DataType::Float32, |
| 175 | armnn::DataType::Boolean>; |
| 176 | |
| 177 | template <typename QueueDescriptor> |
| 178 | using BaseUint8ComparisonWorkload = MultiTypedWorkload<QueueDescriptor, |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 179 | armnn::DataType::QAsymmU8, |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 180 | armnn::DataType::Boolean>; |
| 181 | |
| 182 | template <typename QueueDescriptor> |
Narumol Prangnawarat | 7ddbbae | 2020-03-13 10:26:05 +0000 | [diff] [blame] | 183 | using BFloat16ToFloat32Workload = MultiTypedWorkload<QueueDescriptor, |
| 184 | armnn::DataType::BFloat16, |
| 185 | armnn::DataType::Float32>; |
| 186 | |
| 187 | template <typename QueueDescriptor> |
Narumol Prangnawarat | ea54a01 | 2020-03-16 16:36:10 +0000 | [diff] [blame] | 188 | using Float32ToBFloat16Workload = MultiTypedWorkload<QueueDescriptor, |
| 189 | armnn::DataType::Float32, |
| 190 | armnn::DataType::BFloat16>; |
| 191 | |
| 192 | template <typename QueueDescriptor> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 193 | using Float16ToFloat32Workload = MultiTypedWorkload<QueueDescriptor, |
| 194 | armnn::DataType::Float16, |
| 195 | armnn::DataType::Float32>; |
| 196 | |
| 197 | template <typename QueueDescriptor> |
| 198 | using Float32ToFloat16Workload = MultiTypedWorkload<QueueDescriptor, |
| 199 | armnn::DataType::Float32, |
| 200 | armnn::DataType::Float16>; |
| 201 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 202 | template <typename QueueDescriptor> |
| 203 | using Uint8ToFloat32Workload = MultiTypedWorkload<QueueDescriptor, |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 204 | armnn::DataType::QAsymmU8, |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 205 | armnn::DataType::Float32>; |
| 206 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 207 | } //namespace armnn |