Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 8 | #include "MockBackendId.hpp" |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 9 | #include "armnn/backends/profiling/IBackendProfiling.hpp" |
| 10 | #include "armnn/backends/profiling/IBackendProfilingContext.hpp" |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 11 | |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 12 | #include <LayerSupportCommon.hpp> |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 13 | #include <armnn/backends/IBackendInternal.hpp> |
| 14 | #include <armnn/backends/OptimizationViews.hpp> |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 15 | #include <armnn/backends/profiling/IBackendProfiling.hpp> |
| 16 | #include <backends/BackendProfiling.hpp> |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 17 | #include <backendsCommon/LayerSupportBase.hpp> |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 18 | |
| 19 | namespace armnn |
| 20 | { |
| 21 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 22 | class MockBackendInitialiser |
| 23 | { |
| 24 | public: |
| 25 | MockBackendInitialiser(); |
| 26 | ~MockBackendInitialiser(); |
| 27 | }; |
| 28 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 29 | class MockBackendProfilingContext : public profiling::IBackendProfilingContext |
| 30 | { |
| 31 | public: |
| 32 | MockBackendProfilingContext(IBackendInternal::IBackendProfilingPtr& backendProfiling) |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 33 | : m_BackendProfiling(std::move(backendProfiling)) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 34 | , m_CapturePeriod(0) |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 35 | , m_IsTimelineEnabled(true) |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 36 | {} |
| 37 | |
| 38 | ~MockBackendProfilingContext() = default; |
| 39 | |
| 40 | IBackendInternal::IBackendProfilingPtr& GetBackendProfiling() |
| 41 | { |
| 42 | return m_BackendProfiling; |
| 43 | } |
| 44 | |
| 45 | uint16_t RegisterCounters(uint16_t currentMaxGlobalCounterId) |
| 46 | { |
| 47 | std::unique_ptr<profiling::IRegisterBackendCounters> counterRegistrar = |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 48 | m_BackendProfiling->GetCounterRegistrationInterface(static_cast<uint16_t>(currentMaxGlobalCounterId)); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 49 | |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 50 | std::string categoryName("MockCounters"); |
| 51 | counterRegistrar->RegisterCategory(categoryName); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 52 | |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 53 | counterRegistrar->RegisterCounter(0, categoryName, 0, 0, 1.f, "Mock Counter One", "Some notional counter"); |
| 54 | |
| 55 | counterRegistrar->RegisterCounter(1, categoryName, 0, 0, 1.f, "Mock Counter Two", |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 56 | "Another notional counter"); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 57 | |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 58 | std::string units("microseconds"); |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 59 | uint16_t nextMaxGlobalCounterId = |
| 60 | counterRegistrar->RegisterCounter(2, categoryName, 0, 0, 1.f, "Mock MultiCore Counter", |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 61 | "A dummy four core counter", units, 4); |
| 62 | return nextMaxGlobalCounterId; |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 65 | Optional<std::string> ActivateCounters(uint32_t capturePeriod, const std::vector<uint16_t>& counterIds) |
| 66 | { |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 67 | if (capturePeriod == 0 || counterIds.size() == 0) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 68 | { |
| 69 | m_ActiveCounters.clear(); |
| 70 | } |
| 71 | else if (capturePeriod == 15939u) |
| 72 | { |
| 73 | return armnn::Optional<std::string>("ActivateCounters example test error"); |
| 74 | } |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 75 | m_CapturePeriod = capturePeriod; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 76 | m_ActiveCounters = counterIds; |
| 77 | return armnn::Optional<std::string>(); |
| 78 | } |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 79 | |
| 80 | std::vector<profiling::Timestamp> ReportCounterValues() |
| 81 | { |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 82 | std::vector<profiling::CounterValue> counterValues; |
| 83 | |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 84 | for (auto counterId : m_ActiveCounters) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 85 | { |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 86 | counterValues.emplace_back(profiling::CounterValue{ counterId, counterId + 1u }); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | uint64_t timestamp = m_CapturePeriod; |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 90 | return { profiling::Timestamp{ timestamp, counterValues } }; |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 91 | } |
| 92 | |
David Monahan | b015e5d | 2020-02-26 10:24:03 +0000 | [diff] [blame] | 93 | bool EnableProfiling(bool) |
| 94 | { |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 95 | auto sendTimelinePacket = m_BackendProfiling->GetSendTimelinePacket(); |
| 96 | sendTimelinePacket->SendTimelineEntityBinaryPacket(4256); |
| 97 | sendTimelinePacket->Commit(); |
David Monahan | b015e5d | 2020-02-26 10:24:03 +0000 | [diff] [blame] | 98 | return true; |
| 99 | } |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 100 | |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 101 | bool EnableTimelineReporting(bool isEnabled) |
| 102 | { |
| 103 | m_IsTimelineEnabled = isEnabled; |
| 104 | return isEnabled; |
| 105 | } |
| 106 | |
| 107 | bool TimelineReportingEnabled() |
| 108 | { |
| 109 | return m_IsTimelineEnabled; |
| 110 | } |
| 111 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 112 | private: |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 113 | IBackendInternal::IBackendProfilingPtr m_BackendProfiling; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 114 | uint32_t m_CapturePeriod; |
| 115 | std::vector<uint16_t> m_ActiveCounters; |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 116 | bool m_IsTimelineEnabled; |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 117 | }; |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 118 | |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 119 | class MockBackendProfilingService |
| 120 | { |
| 121 | public: |
| 122 | // Getter for the singleton instance |
| 123 | static MockBackendProfilingService& Instance() |
| 124 | { |
| 125 | static MockBackendProfilingService instance; |
| 126 | return instance; |
| 127 | } |
| 128 | |
| 129 | MockBackendProfilingContext* GetContext() |
| 130 | { |
| 131 | return m_sharedContext.get(); |
| 132 | } |
| 133 | |
| 134 | void SetProfilingContextPtr(std::shared_ptr<MockBackendProfilingContext> shared) |
| 135 | { |
| 136 | m_sharedContext = shared; |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | std::shared_ptr<MockBackendProfilingContext> m_sharedContext; |
| 141 | }; |
| 142 | |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 143 | class MockBackend : public IBackendInternal |
| 144 | { |
| 145 | public: |
| 146 | MockBackend() = default; |
| 147 | ~MockBackend() = default; |
| 148 | |
| 149 | static const BackendId& GetIdStatic(); |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 150 | const BackendId& GetId() const override |
| 151 | { |
| 152 | return GetIdStatic(); |
| 153 | } |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 154 | |
| 155 | IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override; |
| 156 | |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 157 | IBackendInternal::IWorkloadFactoryPtr |
| 158 | CreateWorkloadFactory(const IBackendInternal::IMemoryManagerSharedPtr& memoryManager = nullptr) const override; |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 159 | |
| 160 | IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const override; |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 161 | IBackendInternal::IBackendProfilingContextPtr |
| 162 | CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions, |
| 163 | IBackendProfilingPtr& backendProfiling) override; |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 164 | |
| 165 | IBackendInternal::Optimizations GetOptimizations() const override; |
| 166 | IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override; |
| 167 | |
| 168 | OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const override; |
| 169 | }; |
| 170 | |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 171 | class MockLayerSupport : public LayerSupportBase |
| 172 | { |
David Monahan | 7f2c35a | 2019-05-24 10:46:28 +0100 | [diff] [blame] | 173 | public: |
Derek Lamberti | 901ea11 | 2019-12-10 22:07:09 +0000 | [diff] [blame] | 174 | bool IsInputSupported(const TensorInfo& /*input*/, |
| 175 | Optional<std::string&> /*reasonIfUnsupported = EmptyOptional()*/) const override |
David Monahan | 7f2c35a | 2019-05-24 10:46:28 +0100 | [diff] [blame] | 176 | { |
| 177 | return true; |
| 178 | } |
| 179 | |
Derek Lamberti | 901ea11 | 2019-12-10 22:07:09 +0000 | [diff] [blame] | 180 | bool IsOutputSupported(const TensorInfo& /*input*/, |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 181 | Optional<std::string&> /*reasonIfUnsupported = EmptyOptional()*/) const override |
David Monahan | 7f2c35a | 2019-05-24 10:46:28 +0100 | [diff] [blame] | 182 | { |
| 183 | return true; |
| 184 | } |
| 185 | |
Derek Lamberti | 901ea11 | 2019-12-10 22:07:09 +0000 | [diff] [blame] | 186 | bool IsAdditionSupported(const TensorInfo& /*input0*/, |
| 187 | const TensorInfo& /*input1*/, |
| 188 | const TensorInfo& /*output*/, |
| 189 | Optional<std::string&> /*reasonIfUnsupported = EmptyOptional()*/) const override |
David Monahan | 7f2c35a | 2019-05-24 10:46:28 +0100 | [diff] [blame] | 190 | { |
| 191 | return true; |
| 192 | } |
| 193 | |
Derek Lamberti | 901ea11 | 2019-12-10 22:07:09 +0000 | [diff] [blame] | 194 | bool IsConvolution2dSupported(const TensorInfo& /*input*/, |
| 195 | const TensorInfo& /*output*/, |
| 196 | const Convolution2dDescriptor& /*descriptor*/, |
| 197 | const TensorInfo& /*weights*/, |
| 198 | const Optional<TensorInfo>& /*biases*/, |
| 199 | Optional<std::string&> /*reasonIfUnsupported = EmptyOptional()*/) const override |
David Monahan | 7f2c35a | 2019-05-24 10:46:28 +0100 | [diff] [blame] | 200 | { |
| 201 | return true; |
| 202 | } |
| 203 | }; |
| 204 | |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 205 | } // namespace armnn |