ArmNN
 22.02
MockBackend.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "MockBackendId.hpp"
11 
12 #include <LayerSupportCommon.hpp>
18 
19 namespace armnn
20 {
21 
23 {
24 public:
27 };
28 
30 {
31 public:
33  : m_BackendProfiling(std::move(backendProfiling))
34  , m_CapturePeriod(0)
35  , m_IsTimelineEnabled(true)
36  {}
37 
38  ~MockBackendProfilingContext() = default;
39 
41  {
42  return m_BackendProfiling;
43  }
44 
45  uint16_t RegisterCounters(uint16_t currentMaxGlobalCounterId)
46  {
47  std::unique_ptr<profiling::IRegisterBackendCounters> counterRegistrar =
48  m_BackendProfiling->GetCounterRegistrationInterface(static_cast<uint16_t>(currentMaxGlobalCounterId));
49 
50  std::string categoryName("MockCounters");
51  counterRegistrar->RegisterCategory(categoryName);
52 
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",
56  "Another notional counter");
57 
58  std::string units("microseconds");
59  uint16_t nextMaxGlobalCounterId =
60  counterRegistrar->RegisterCounter(2, categoryName, 0, 0, 1.f, "Mock MultiCore Counter",
61  "A dummy four core counter", units, 4);
62  return nextMaxGlobalCounterId;
63  }
64 
65  Optional<std::string> ActivateCounters(uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
66  {
67  if (capturePeriod == 0 || counterIds.size() == 0)
68  {
69  m_ActiveCounters.clear();
70  }
71  else if (capturePeriod == 15939u)
72  {
73  return armnn::Optional<std::string>("ActivateCounters example test error");
74  }
75  m_CapturePeriod = capturePeriod;
76  m_ActiveCounters = counterIds;
78  }
79 
80  std::vector<profiling::Timestamp> ReportCounterValues()
81  {
82  std::vector<profiling::CounterValue> counterValues;
83 
84  for (auto counterId : m_ActiveCounters)
85  {
86  counterValues.emplace_back(profiling::CounterValue{ counterId, counterId + 1u });
87  }
88 
89  uint64_t timestamp = m_CapturePeriod;
90  return { profiling::Timestamp{ timestamp, counterValues } };
91  }
92 
93  bool EnableProfiling(bool)
94  {
95  auto sendTimelinePacket = m_BackendProfiling->GetSendTimelinePacket();
96  sendTimelinePacket->SendTimelineEntityBinaryPacket(4256);
97  sendTimelinePacket->Commit();
98  return true;
99  }
100 
101  bool EnableTimelineReporting(bool isEnabled)
102  {
103  m_IsTimelineEnabled = isEnabled;
104  return isEnabled;
105  }
106 
108  {
109  return m_IsTimelineEnabled;
110  }
111 
112 private:
113  IBackendInternal::IBackendProfilingPtr m_BackendProfiling;
114  uint32_t m_CapturePeriod;
115  std::vector<uint16_t> m_ActiveCounters;
116  std::atomic<bool> m_IsTimelineEnabled;
117 };
118 
120 {
121 public:
122  // Getter for the singleton instance
124  {
125  static MockBackendProfilingService instance;
126  return instance;
127  }
128 
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 
143 class MockBackend : public IBackendInternal
144 {
145 public:
146  MockBackend() = default;
147  ~MockBackend() = default;
148 
149  static const BackendId& GetIdStatic();
150  const BackendId& GetId() const override
151  {
152  return GetIdStatic();
153  }
154 
155  IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override;
156 
158  CreateWorkloadFactory(const IBackendInternal::IMemoryManagerSharedPtr& memoryManager = nullptr) const override;
159 
160  IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const override;
162  CreateBackendProfilingContext(const IRuntime::CreationOptions& creationOptions,
163  IBackendProfilingPtr& backendProfiling) override;
164 
165  IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override;
166 
167  OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const override;
168 
169  std::unique_ptr<ICustomAllocator> GetDefaultAllocator() const override;
170 };
171 
173 {
174 public:
175  bool IsLayerSupported(const LayerType& type,
176  const std::vector<TensorInfo>& infos,
177  const BaseDescriptor& descriptor,
178  const Optional<LstmInputParamsInfo>& /*lstmParamsInfo*/,
179  const Optional<QuantizedLstmInputParamsInfo>& /*quantizedLstmParamsInfo*/,
180  Optional<std::string&> reasonIfUnsupported) const override
181  {
182  switch(type)
183  {
184  case LayerType::Input:
185  return IsInputSupported(infos[0], reasonIfUnsupported);
186  case LayerType::Output:
187  return IsOutputSupported(infos[0], reasonIfUnsupported);
188  case LayerType::Addition:
189  return IsAdditionSupported(infos[0], infos[1], infos[2], reasonIfUnsupported);
191  {
192  if (infos.size() != 4)
193  {
194  throw InvalidArgumentException("Invalid number of TransposeConvolution2d "
195  "TensorInfos. TensorInfos should be of format: "
196  "{input, output, weights, biases}.");
197  }
198 
199  auto desc = *(PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor));
200  if (infos[3] == TensorInfo())
201  {
202  return IsConvolution2dSupported(infos[0],
203  infos[1],
204  desc,
205  infos[2],
206  EmptyOptional(),
207  reasonIfUnsupported);
208  }
209  else
210  {
211  return IsConvolution2dSupported(infos[0],
212  infos[1],
213  desc,
214  infos[2],
215  infos[3],
216  reasonIfUnsupported);
217  }
218  }
219  default:
220  return false;
221  }
222  }
223 
224  bool IsInputSupported(const TensorInfo& /*input*/,
225  Optional<std::string&> /*reasonIfUnsupported = EmptyOptional()*/) const override
226  {
227  return true;
228  }
229 
230  bool IsOutputSupported(const TensorInfo& /*input*/,
231  Optional<std::string&> /*reasonIfUnsupported = EmptyOptional()*/) const override
232  {
233  return true;
234  }
235 
236  bool IsAdditionSupported(const TensorInfo& /*input0*/,
237  const TensorInfo& /*input1*/,
238  const TensorInfo& /*output*/,
239  Optional<std::string&> /*reasonIfUnsupported = EmptyOptional()*/) const override
240  {
241  return true;
242  }
243 
244  bool IsConvolution2dSupported(const TensorInfo& /*input*/,
245  const TensorInfo& /*output*/,
246  const Convolution2dDescriptor& /*descriptor*/,
247  const TensorInfo& /*weights*/,
248  const Optional<TensorInfo>& /*biases*/,
249  Optional<std::string&> /*reasonIfUnsupported = EmptyOptional()*/) const override
250  {
251  return true;
252  }
253 };
254 
255 } // namespace armnn
MockBackendProfilingContext(IBackendInternal::IBackendProfilingPtr &backendProfiling)
Definition: MockBackend.hpp:32
bool IsOutputSupported(const BackendId &backend, const TensorInfo &output, char *reasonIfUnsupported=nullptr, size_t reasonIfUnsupportedMaxLength=1024)
Deprecated in favor of IBackend and ILayerSupport interfaces.
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr
void SetProfilingContextPtr(std::shared_ptr< MockBackendProfilingContext > shared)
bool IsOutputSupported(const TensorInfo &, Optional< std::string &>) const override
A Convolution2dDescriptor for the Convolution2dLayer.
bool IsInputSupported(const TensorInfo &, Optional< std::string &>) const override
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr
const BackendId & GetId() const override
Base class for all descriptors.
Definition: Descriptors.hpp:22
The SubgraphView class represents a subgraph of a Graph.
Optional< std::string > ActivateCounters(uint32_t capturePeriod, const std::vector< uint16_t > &counterIds)
Definition: MockBackend.hpp:65
std::unique_ptr< armnn::profiling::IBackendProfiling > IBackendProfilingPtr
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
bool IsConvolution2dSupported(const TensorInfo &, const TensorInfo &, const Convolution2dDescriptor &, const TensorInfo &, const Optional< TensorInfo > &, Optional< std::string &>) const override
static MockBackendProfilingService & Instance()
bool IsConvolution2dSupported(const BackendId &backend, const TensorInfo &input, const TensorInfo &output, const Convolution2dDescriptor &descriptor, const TensorInfo &weights, const Optional< TensorInfo > &biases, char *reasonIfUnsupported=nullptr, size_t reasonIfUnsupportedMaxLength=1024)
Deprecated in favor of IBackend and ILayerSupport interfaces.
bool IsAdditionSupported(const TensorInfo &, const TensorInfo &, const TensorInfo &, Optional< std::string &>) const override
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
bool IsInputSupported(const BackendId &backend, const TensorInfo &input, char *reasonIfUnsupported=nullptr, size_t reasonIfUnsupportedMaxLength=1024)
Deprecated in favor of IBackend and ILayerSupport interfaces.
MockBackendProfilingContext * GetContext()
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
std::vector< profiling::Timestamp > ReportCounterValues()
Definition: MockBackend.hpp:80
IBackendInternal::IBackendProfilingPtr & GetBackendProfiling()
Definition: MockBackend.hpp:40
bool IsAdditionSupported(const BackendId &backend, const TensorInfo &input0, const TensorInfo &input1, const TensorInfo &output, char *reasonIfUnsupported=nullptr, size_t reasonIfUnsupportedMaxLength=1024)
Deprecated in favor of IBackend and ILayerSupport interfaces.
bool EnableTimelineReporting(bool isEnabled)
bool IsLayerSupported(const LayerType &type, const std::vector< TensorInfo > &infos, const BaseDescriptor &descriptor, const Optional< LstmInputParamsInfo > &, const Optional< QuantizedLstmInputParamsInfo > &, Optional< std::string &> reasonIfUnsupported) const override
std::shared_ptr< armnn::profiling::IBackendProfilingContext > IBackendProfilingContextPtr
This is the bridge between backend and backend profiling we&#39;ll keep it in the backend namespace...
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:458
uint16_t RegisterCounters(uint16_t currentMaxGlobalCounterId)
Definition: MockBackend.hpp:45
std::unique_ptr< IBackendContext > IBackendContextPtr