ArmNN
 22.05
armnn::experimental Namespace Reference

Classes

class  AsyncCallbackManager
 
class  AsyncExecutionCallback
 
class  IAsyncExecutionCallback
 
class  IWorkingMemHandle
 
class  Threadpool
 
struct  WorkingMemDescriptor
 
class  WorkingMemHandle
 

Typedefs

using IAsyncExecutionCallbackPtr = std::shared_ptr< IAsyncExecutionCallback >
 
using InferenceId = uint64_t
 

Functions

template<DataType ArmnnIType, DataType ArmnnOType, typename TInput = ResolveType <ArmnnIType>, typename TOutput = ResolveType <ArmnnOType>>
void AsyncThreadedEndToEndTestImpl (INetworkPtr network, const std::vector< std::map< int, std::vector< TInput >>> &inputTensorData, const std::vector< std::map< int, std::vector< TOutput >>> &expectedOutputData, std::vector< BackendId > backends, const size_t numberOfInferences, float tolerance=0.000001f)
 
template<DataType ArmnnIType, DataType ArmnnOType, typename TInput = ResolveType<ArmnnIType>, typename TOutput = ResolveType<ArmnnOType>>
void AsyncEndToEndTestImpl (INetworkPtr network, const std::map< int, std::vector< TInput >> &inputTensorData, const std::map< int, std::vector< TOutput >> &expectedOutputData, std::vector< BackendId > backends, float tolerance=0.000001f, size_t numThreads=1)
 
template<typename armnn::DataType DataType>
INetworkPtr CreateStridedSliceNetwork (const TensorShape &inputShape, const TensorShape &outputShape, const std::vector< int > &beginData, const std::vector< int > &endData, const std::vector< int > &stridesData, int beginMask=0, int endMask=0, int shrinkAxisMask=0, int ellipsisMask=0, int newAxisMask=0, const float qScale=1.0f, const int32_t qOffset=0)
 
template<armnn::DataType ArmnnType>
void StridedSlicedEndToEndTest (const std::vector< BackendId > &backends, size_t numThreads)
 
template<armnn::DataType ArmnnType>
void StridedSlicedMultiThreadedEndToEndTest (const std::vector< BackendId > &backends)
 

Typedef Documentation

◆ IAsyncExecutionCallbackPtr

Definition at line 17 of file IAsyncExecutionCallback.hpp.

◆ InferenceId

using InferenceId = uint64_t

Definition at line 24 of file AsyncExecutionCallback.hpp.

Function Documentation

◆ AsyncEndToEndTestImpl()

void armnn::experimental::AsyncEndToEndTestImpl ( INetworkPtr  network,
const std::map< int, std::vector< TInput >> &  inputTensorData,
const std::map< int, std::vector< TOutput >> &  expectedOutputData,
std::vector< BackendId backends,
float  tolerance = 0.000001f,
size_t  numThreads = 1 
)

Definition at line 123 of file StridedSliceAsyncEndToEndTest.hpp.

References ARMNN_ASSERT, IRuntime::Create(), AsyncCallbackManager::GetNotifiedCallback(), armnn::Optimize(), TensorInfo::SetConstant(), armnn::Success, and armnn::Undefined.

129 {
130  ARMNN_ASSERT(numThreads >= 1);
131  const unsigned int numberOfInferences = numThreads == 1 ? 1 : 1000;
132 
133  // Create Runtime in which test will run
135  IRuntimePtr runtime(IRuntime::Create(options));
136 
137  // Optimize the Network
138  IOptimizedNetworkPtr optNet = Optimize(*network, backends, runtime->GetDeviceSpec());
139 
140  // Creates AsyncNetwork
141  NetworkId networkId = 0;
142 
143  std::string errorMessage;
144 
145  const INetworkProperties networkProperties(true, MemorySource::Undefined, MemorySource::Undefined);
146 
147  runtime->LoadNetwork(networkId, std::move(optNet), errorMessage, networkProperties);
148 
149  InputTensors inputTensors;
150  inputTensors.reserve(inputTensorData.size());
151  for (auto&& it : inputTensorData)
152  {
153  TensorInfo inputTensorInfo = runtime->GetInputTensorInfo(networkId, it.first);
154  inputTensorInfo.SetConstant(true);
155  inputTensors.push_back({it.first,
156  ConstTensor(inputTensorInfo, it.second.data())});
157  }
158 
159  std::vector<OutputTensors> outputTensorsVec;
160  std::vector<std::map<int, std::vector<TOutput>>> outputStorageVec;
161 
162  outputTensorsVec.reserve(numberOfInferences);
163  outputStorageVec.reserve(numberOfInferences);
164  for (unsigned int i = 0; i < numberOfInferences; ++i)
165  {
166  OutputTensors outputTensors;
167  outputStorageVec.emplace_back(std::map<int, std::vector<TOutput>>());
168 
169  outputTensors.reserve(expectedOutputData.size());
170  for (auto&& it : expectedOutputData)
171  {
172  std::vector<TOutput> out(it.second.size());
173  outputStorageVec[i].emplace(it.first, out);
174  outputTensors.push_back({it.first,
175  Tensor(runtime->GetOutputTensorInfo(networkId, it.first),
176  outputStorageVec[i].at(it.first).data())});
177  }
178 
179  outputTensorsVec.push_back(outputTensors);
180  }
181 
182  if (numThreads == 1)
183  {
184  // Create WorkingMemHandle for this async network
185  std::unique_ptr<IWorkingMemHandle> workingMemHandle = runtime->CreateWorkingMemHandle(networkId);
186  IWorkingMemHandle& workingMemHandleRef = *workingMemHandle.get();
187 
188  // Run the async network
189  runtime->Execute(workingMemHandleRef, inputTensors, outputTensorsVec[0]);
190  }
191  else
192  {
193  std::vector<std::shared_ptr<IWorkingMemHandle>> memHandles;
194 
195  for (size_t i = 0; i < numThreads; ++i)
196  {
197  memHandles.emplace_back(runtime->CreateWorkingMemHandle(networkId));
198  }
199 
200  Threadpool threadpool(numThreads, runtime.get(), memHandles);
201  AsyncCallbackManager callbackManager;
202 
203  // For the asyncronous execution, we are adding a pool of working memory handles (1 per thread) in the
204  // LoadedNetwork with each scheduled inference having a random priority
205  for (size_t i = 0; i < numberOfInferences; ++i)
206  {
207  threadpool.Schedule(networkId,
208  inputTensors,
209  outputTensorsVec[i],
210  static_cast<QosExecPriority>(rand()%3),
211  callbackManager.GetNewCallback());
212  }
213 
214  // Wait until the execution signals a notify
215  for (size_t i = 0; i < numberOfInferences; ++i)
216  {
217  auto cb = callbackManager.GetNotifiedCallback();
218 
219  // Checks the results.
220  CHECK(cb->GetStatus() == Status::Success);
221  }
222  }
223 
224  for (auto&& outputStorage : outputStorageVec)
225  {
226  for (auto&& it : expectedOutputData)
227  {
228  std::vector<TOutput> out = outputStorage.at(it.first);
229 
230  for (unsigned int i = 0; i < out.size(); ++i)
231  {
232  //CHECK(Compare<ArmnnOType>(it.second[i], out[i], tolerance) == true);
233  CHECK(it.second[i] == doctest::Approx(out[i]).epsilon(tolerance));
234  }
235  }
236  }
237 }
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:33
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:392
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:319
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1847
int NetworkId
Definition: IRuntime.hpp:27
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:393
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:242
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
Definition: Tensor.cpp:514

◆ AsyncThreadedEndToEndTestImpl()

void armnn::experimental::AsyncThreadedEndToEndTestImpl ( INetworkPtr  network,
const std::vector< std::map< int, std::vector< TInput >>> &  inputTensorData,
const std::vector< std::map< int, std::vector< TOutput >>> &  expectedOutputData,
std::vector< BackendId backends,
const size_t  numberOfInferences,
float  tolerance = 0.000001f 
)

Definition at line 30 of file StridedSliceAsyncEndToEndTest.hpp.

References IRuntime::Create(), armnn::Optimize(), TensorInfo::SetConstant(), and armnn::Undefined.

36 {
37  // Create Runtime in which test will run
39  IRuntimePtr runtime(IRuntime::Create(options));
40 
41  // Optimize the Network
42  IOptimizedNetworkPtr optNet = Optimize(*network, backends, runtime->GetDeviceSpec());
43 
44  // Creates AsyncNetwork
45  NetworkId networkId = 0;
46  std::string errorMessage;
47  const INetworkProperties networkProperties(true, MemorySource::Undefined, MemorySource::Undefined);
48  runtime->LoadNetwork(networkId, std::move(optNet), errorMessage, networkProperties);
49 
50  std::vector<InputTensors> inputTensorsVec;
51  std::vector<OutputTensors> outputTensorsVec;
52  std::vector<std::map<int, std::vector<TOutput>>> outputStorageVec;
53  std::vector<std::unique_ptr<IWorkingMemHandle>> workingMemHandles;
54 
55  for (unsigned int i = 0; i < numberOfInferences; ++i)
56  {
57  InputTensors inputTensors;
58  OutputTensors outputTensors;
59  outputStorageVec.emplace_back(std::map<int, std::vector<TOutput>>());
60 
61  inputTensors.reserve(inputTensorData.size());
62  for (auto&& it : inputTensorData[i])
63  {
64  TensorInfo inputTensorInfo = runtime->GetInputTensorInfo(networkId, it.first);
65  inputTensorInfo.SetConstant(true);
66  inputTensors.push_back({it.first,
67  ConstTensor(inputTensorInfo, it.second.data())});
68  }
69 
70  outputTensors.reserve(expectedOutputData.size());
71  for (auto&& it : expectedOutputData[i])
72  {
73  std::vector<TOutput> out(it.second.size());
74  outputStorageVec[i].emplace(it.first, out);
75  outputTensors.push_back({it.first,
76  Tensor(runtime->GetOutputTensorInfo(networkId, it.first),
77  outputStorageVec[i].at(it.first).data())});
78  }
79 
80  inputTensorsVec.push_back(inputTensors);
81  outputTensorsVec.push_back(outputTensors);
82 
83  workingMemHandles.push_back(runtime->CreateWorkingMemHandle(networkId));
84  }
85 
86  std::vector<std::thread> threads;
87  for (unsigned int i = 0; i < numberOfInferences; ++i)
88  {
89  // Access the vectors before we do anything multi-threaded
90  InputTensors& inputTensors = inputTensorsVec[i];
91  OutputTensors& outputTensors = outputTensorsVec[i];
92  IWorkingMemHandle& workingMemHandle = *workingMemHandles[i].get();
93 
94  threads.emplace_back([&]()
95  {
96  // Run the async network
97  runtime->Execute(workingMemHandle, inputTensors, outputTensors);
98  });
99  }
100 
101  for (unsigned int i = 0; i < numberOfInferences; ++i)
102  {
103  threads[i].join();
104  }
105 
106  // Checks the results.
107  for (unsigned int i = 0; i < numberOfInferences; ++i)
108  {
109  for (auto &&it : expectedOutputData[i])
110  {
111  std::vector<TOutput> out = outputStorageVec[i].at(it.first);
112  for (unsigned int j = 0; j < out.size(); ++j)
113  {
114  CHECK(Compare<ArmnnOType>(it.second[j], out[j], tolerance) == true);
115  }
116  }
117  }
118 
119 }
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:33
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:392
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:319
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1847
int NetworkId
Definition: IRuntime.hpp:27
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:393
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:242
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
Definition: Tensor.cpp:514

◆ CreateStridedSliceNetwork()

INetworkPtr armnn::experimental::CreateStridedSliceNetwork ( const TensorShape inputShape,
const TensorShape outputShape,
const std::vector< int > &  beginData,
const std::vector< int > &  endData,
const std::vector< int > &  stridesData,
int  beginMask = 0,
int  endMask = 0,
int  shrinkAxisMask = 0,
int  ellipsisMask = 0,
int  newAxisMask = 0,
const float  qScale = 1.0f,
const int32_t  qOffset = 0 
)

Definition at line 240 of file StridedSliceAsyncEndToEndTest.hpp.

References Connect(), INetwork::Create(), StridedSliceDescriptor::m_Begin, StridedSliceDescriptor::m_BeginMask, StridedSliceDescriptor::m_EllipsisMask, StridedSliceDescriptor::m_End, StridedSliceDescriptor::m_EndMask, StridedSliceDescriptor::m_NewAxisMask, StridedSliceDescriptor::m_ShrinkAxisMask, and StridedSliceDescriptor::m_Stride.

252 {
253  using namespace armnn;
254  // Builds up the structure of the network.
256 
257  TensorInfo inputTensorInfo(inputShape, DataType, qScale, qOffset);
258  TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
259 
260  armnn::StridedSliceDescriptor stridedSliceDescriptor;
261  stridedSliceDescriptor.m_Begin = beginData;
262  stridedSliceDescriptor.m_End = endData;
263  stridedSliceDescriptor.m_Stride = stridesData;
264  stridedSliceDescriptor.m_BeginMask = beginMask;
265  stridedSliceDescriptor.m_EndMask = endMask;
266  stridedSliceDescriptor.m_ShrinkAxisMask = shrinkAxisMask;
267  stridedSliceDescriptor.m_EllipsisMask = ellipsisMask;
268  stridedSliceDescriptor.m_NewAxisMask = newAxisMask;
269 
270  IConnectableLayer* input = net->AddInputLayer(0, "Input_Layer");
271  IConnectableLayer* stridedSlice = net->AddStridedSliceLayer(stridedSliceDescriptor, "splitter");
272  IConnectableLayer* output = net->AddOutputLayer(0);
273 
274  Connect(input, stridedSlice, inputTensorInfo, 0, 0);
275  Connect(stridedSlice, output, outputTensorInfo, 0, 0);
276 
277  return net;
278 }
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:66
int32_t m_ShrinkAxisMask
Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1...
std::vector< int > m_Begin
Begin values for the input that will be sliced.
Copyright (c) 2021 ARM Limited and Contributors.
int32_t m_BeginMask
Begin mask value.
int32_t m_EndMask
End mask value.
DataType
Definition: Types.hpp:48
int32_t m_NewAxisMask
New axis mask value.
int32_t m_EllipsisMask
Ellipsis mask value.
std::vector< int > m_Stride
Stride values for the input that will be sliced.
std::vector< int > m_End
End values for the input that will be sliced.
A StridedSliceDescriptor for the StridedSliceLayer.
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
Definition: TestUtils.cpp:14
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:241
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:476

◆ StridedSlicedEndToEndTest()

void armnn::experimental::StridedSlicedEndToEndTest ( const std::vector< BackendId > &  backends,
size_t  numThreads 
)

Definition at line 281 of file StridedSliceAsyncEndToEndTest.hpp.

282 {
283  using namespace armnn;
284  using T = ResolveType<ArmnnType>;
285 
286  const TensorShape& inputShape = {3, 2, 3, 1};
287  const TensorShape& outputShape = {1, 2, 3, 1};
288  const std::vector<int>& beginData = {1, 0, 0, 0};
289  const std::vector<int>& endData = {2, 2, 3, 1};
290  const std::vector<int>& stridesData = {1, 1, 1, 1};
291  int beginMask = 0;
292  int endMask = 0;
293  int shrinkAxisMask = 0;
294  int ellipsisMask = 0;
295  int newAxisMask = 0;
296 
297  // Builds up the structure of the network
298  INetworkPtr net = CreateStridedSliceNetwork<ArmnnType>(inputShape,
299  outputShape,
300  beginData,
301  endData,
302  stridesData,
303  beginMask,
304  endMask,
305  shrinkAxisMask,
306  ellipsisMask,
307  newAxisMask);
308 
309  CHECK(net);
310  // Creates structures for input & output.
311  std::vector<T> inputData{
312  1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
313 
314  3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
315 
316  5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
317  };
318 
319  std::vector<T> outputExpected{
320  3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f
321  };
322 
323  std::map<int, std::vector<T>> inputTensorData = {{0, inputData}};
324  std::map<int, std::vector<T>> expectedOutputData = {{0, outputExpected}};
325 
326  AsyncEndToEndTestImpl<ArmnnType, ArmnnType>(move(net),
327  inputTensorData,
328  expectedOutputData,
329  backends,
330  0.000001f,
331  numThreads);
332 }
typename ResolveTypeImpl< DT >::Type ResolveType
Definition: ResolveType.hpp:79
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:241

◆ StridedSlicedMultiThreadedEndToEndTest()

void armnn::experimental::StridedSlicedMultiThreadedEndToEndTest ( const std::vector< BackendId > &  backends)

Definition at line 335 of file StridedSliceAsyncEndToEndTest.hpp.

336 {
337  using namespace armnn;
338  using T = ResolveType<ArmnnType>;
339 
340  const TensorShape& inputShape = {3, 2, 3, 1};
341  const TensorShape& outputShape = {1, 2, 3, 1};
342  const std::vector<int>& beginData = {1, 0, 0, 0};
343  const std::vector<int>& endData = {2, 2, 3, 1};
344  const std::vector<int>& stridesData = {1, 1, 1, 1};
345  int beginMask = 0;
346  int endMask = 0;
347  int shrinkAxisMask = 0;
348  int ellipsisMask = 0;
349  int newAxisMask = 0;
350 
351  // Builds up the structure of the network
352  INetworkPtr net = CreateStridedSliceNetwork<ArmnnType>(inputShape,
353  outputShape,
354  beginData,
355  endData,
356  stridesData,
357  beginMask,
358  endMask,
359  shrinkAxisMask,
360  ellipsisMask,
361  newAxisMask);
362 
363  CHECK(net);
364 
365  // Creates structures for input & output.
366  std::vector<T> inputData1{
367  1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
368 
369  3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
370 
371  5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
372  };
373 
374  std::vector<T> outputExpected1{ 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f };
375 
376  // Creates structures for input & output.
377  std::vector<T> inputData2{
378  1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
379 
380  8.0f, 8.0f, 8.0f, 7.0f, 7.0f, 7.0f,
381 
382  5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
383  };
384 
385  std::vector<T> outputExpected2{ 8.0f, 8.0f, 8.0f, 7.0f, 7.0f, 7.0f };
386 
387  std::vector<std::map<int, std::vector<T>>> inputTensors;
388  std::vector<std::map<int, std::vector<T>>> outputTensors;
389 
390  inputTensors.push_back(std::map<int, std::vector<T>> {{0, inputData1}});
391  inputTensors.push_back(std::map<int, std::vector<T>> {{0, inputData2}});
392  outputTensors.push_back(std::map<int, std::vector<T>> {{0, outputExpected1}});
393  outputTensors.push_back(std::map<int, std::vector<T>> {{0, outputExpected2}});
394 
395  AsyncThreadedEndToEndTestImpl<ArmnnType, ArmnnType>(move(net), inputTensors, outputTensors, backends, 2);
396 }
typename ResolveTypeImpl< DT >::Type ResolveType
Definition: ResolveType.hpp:79
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:241