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