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 | |
| 23 | virtual void Execute() const = 0; |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 24 | |
| 25 | virtual void RegisterDebugCallback(const DebugCallbackFunction& func) {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | // NullWorkload used to denote an unsupported workload when used by the MakeWorkload<> template |
| 29 | // in the various workload factories. |
| 30 | // There should never be an instantiation of a NullWorkload. |
| 31 | class NullWorkload : public IWorkload |
| 32 | { |
| 33 | NullWorkload()=delete; |
| 34 | }; |
| 35 | |
| 36 | template <typename QueueDescriptor> |
| 37 | class BaseWorkload : public IWorkload |
| 38 | { |
| 39 | public: |
| 40 | |
| 41 | BaseWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) |
| 42 | : m_Data(descriptor) |
| 43 | { |
| 44 | m_Data.Validate(info); |
| 45 | } |
| 46 | |
| 47 | const QueueDescriptor& GetData() const { return m_Data; } |
| 48 | |
| 49 | protected: |
| 50 | const QueueDescriptor m_Data; |
| 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 | { |
| 71 | BOOST_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
| 72 | } |
| 73 | BOOST_ASSERT_MSG(std::all_of(std::next(info.m_InputTensorInfos.begin()), |
| 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 | { |
| 90 | BOOST_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
| 91 | } |
| 92 | } |
| 93 | else if (std::find(dataTypes.begin(), dataTypes.end(), expectedOutputType) == dataTypes.end()) |
| 94 | { |
| 95 | BOOST_ASSERT_MSG(false, "Trying to create workload with incorrect type"); |
| 96 | } |
| 97 | BOOST_ASSERT_MSG(std::all_of(std::next(info.m_OutputTensorInfos.begin()), |
| 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 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | BOOST_ASSERT_MSG(std::all_of(info.m_InputTensorInfos.begin(), |
| 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 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 122 | BOOST_ASSERT_MSG(std::all_of(info.m_OutputTensorInfos.begin(), |
| 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 | { |
| 142 | BOOST_ASSERT_MSG(info.m_InputTensorInfos.front().GetDataType() == DataType, |
| 143 | "Trying to create workload with incorrect type"); |
| 144 | } |
| 145 | |
| 146 | BOOST_ASSERT_MSG(std::all_of(info.m_OutputTensorInfos.begin(), |
| 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> |
| 164 | using Uint8Workload = TypedWorkload<QueueDescriptor, armnn::DataType::QuantisedAsymm8>; |
| 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, |
| 179 | armnn::DataType::QuantisedAsymm8, |
| 180 | armnn::DataType::Boolean>; |
| 181 | |
| 182 | template <typename QueueDescriptor> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 183 | using Float16ToFloat32Workload = MultiTypedWorkload<QueueDescriptor, |
| 184 | armnn::DataType::Float16, |
| 185 | armnn::DataType::Float32>; |
| 186 | |
| 187 | template <typename QueueDescriptor> |
| 188 | using Float32ToFloat16Workload = MultiTypedWorkload<QueueDescriptor, |
| 189 | armnn::DataType::Float32, |
| 190 | armnn::DataType::Float16>; |
| 191 | |
Narumol Prangnawarat | bc67cef | 2019-01-31 15:31:54 +0000 | [diff] [blame] | 192 | template <typename QueueDescriptor> |
| 193 | using Uint8ToFloat32Workload = MultiTypedWorkload<QueueDescriptor, |
| 194 | armnn::DataType::QuantisedAsymm8, |
| 195 | armnn::DataType::Float32>; |
| 196 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 197 | } //namespace armnn |