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