blob: a552a6a7da960e72d57fe7d8aa7f8e1575aec98d [file] [log] [blame]
Mike Kelly386ff1a2021-03-29 15:04:50 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include <ResolveType.hpp>
9
10#include <armnn/IWorkingMemHandle.hpp>
11#include <armnn/INetwork.hpp>
Keith Davise813d672021-04-22 10:10:34 +010012#include <armnn/IAsyncExecutionCallback.hpp>
Mike Kelly386ff1a2021-03-29 15:04:50 +010013
Keith Davise813d672021-04-22 10:10:34 +010014#include <AsyncExecutionCallback.hpp>
Mike Kelly386ff1a2021-03-29 15:04:50 +010015#include <backendsCommon/test/CommonTestUtils.hpp>
16
Sadik Armagan1625efc2021-06-10 18:24:34 +010017#include <doctest/doctest.h>
Mike Kelly386ff1a2021-03-29 15:04:50 +010018
19#include <vector>
20
21namespace armnn
22{
23
24namespace experimental
25{
26
27template<DataType ArmnnIType, DataType ArmnnOType,
28 typename TInput = ResolveType <ArmnnIType>, typename TOutput = ResolveType <ArmnnOType>>
Finn Williamsb8181f72021-04-07 10:23:21 +010029void AsyncThreadedEndToEndTestImpl(INetworkPtr network,
30 const std::vector<std::map<int, std::vector<TInput>>>& inputTensorData,
31 const std::vector<std::map<int, std::vector<TOutput>>>& expectedOutputData,
32 std::vector<BackendId> backends,
33 const size_t numberOfInferences,
34 float tolerance = 0.000001f)
35{
36 // Create Runtime in which test will run
37 IRuntime::CreationOptions options;
38 IRuntimePtr runtime(IRuntime::Create(options));
39
40 // Optimize the Network
41 IOptimizedNetworkPtr optNet = Optimize(*network, backends, runtime->GetDeviceSpec());
42
43
44 // Creates AsyncNetwork
45 NetworkId networkId = 0;
46 std::string errorMessage;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010047 const INetworkProperties networkProperties(true, MemorySource::Undefined, MemorySource::Undefined);
Finn Williamsb8181f72021-04-07 10:23:21 +010048 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 inputTensors.push_back({it.first,
65 ConstTensor(runtime->GetInputTensorInfo(networkId, it.first), it.second.data())});
66 }
67
68 outputTensors.reserve(expectedOutputData.size());
69 for (auto&& it : expectedOutputData[i])
70 {
71 std::vector<TOutput> out(it.second.size());
72 outputStorageVec[i].emplace(it.first, out);
73 outputTensors.push_back({it.first,
74 Tensor(runtime->GetOutputTensorInfo(networkId, it.first),
75 outputStorageVec[i].at(it.first).data())});
76 }
77
78 inputTensorsVec.push_back(inputTensors);
79 outputTensorsVec.push_back(outputTensors);
80
81 workingMemHandles.push_back(runtime->CreateWorkingMemHandle(networkId));
82 }
83
84 std::vector<std::thread> threads;
85 for (unsigned int i = 0; i < numberOfInferences; ++i)
86 {
87 // Access the vectors before we do anything multi-threaded
88 InputTensors& inputTensors = inputTensorsVec[i];
89 OutputTensors& outputTensors = outputTensorsVec[i];
90 IWorkingMemHandle& workingMemHandle = *workingMemHandles[i].get();
91
92 threads.emplace_back([&]()
93 {
94 // Run the async network
95 runtime->Execute(workingMemHandle, inputTensors, outputTensors);
96 });
97 }
98
99 for (unsigned int i = 0; i < numberOfInferences; ++i)
100 {
101 threads[i].join();
102 }
103
104 // Checks the results.
105 for (unsigned int i = 0; i < numberOfInferences; ++i)
106 {
107 for (auto &&it : expectedOutputData[i])
108 {
109 std::vector<TOutput> out = outputStorageVec[i].at(it.first);
110 for (unsigned int j = 0; j < out.size(); ++j)
111 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100112 CHECK(Compare<ArmnnOType>(it.second[j], out[j], tolerance) == true);
Finn Williamsb8181f72021-04-07 10:23:21 +0100113 }
114 }
115 }
116
117}
118
Finn Williamsb8181f72021-04-07 10:23:21 +0100119template<DataType ArmnnIType, DataType ArmnnOType,
Keith Davise813d672021-04-22 10:10:34 +0100120 typename TInput = ResolveType<ArmnnIType>, typename TOutput = ResolveType<ArmnnOType>>
Mike Kelly386ff1a2021-03-29 15:04:50 +0100121void AsyncEndToEndTestImpl(INetworkPtr network,
122 const std::map<int, std::vector<TInput>>& inputTensorData,
123 const std::map<int, std::vector<TOutput>>& expectedOutputData,
124 std::vector<BackendId> backends,
Keith Davise813d672021-04-22 10:10:34 +0100125 float tolerance = 0.000001f,
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100126 size_t numThreads = 1)
Mike Kelly386ff1a2021-03-29 15:04:50 +0100127{
128 // Create Runtime in which test will run
129 IRuntime::CreationOptions options;
Keith Davise813d672021-04-22 10:10:34 +0100130 IRuntimePtr runtime(IRuntime::Create(options));
Mike Kelly386ff1a2021-03-29 15:04:50 +0100131
132 // Optimize the Network
133 IOptimizedNetworkPtr optNet = Optimize(*network, backends, runtime->GetDeviceSpec());
134
135 // Creates AsyncNetwork
136 NetworkId networkId = 0;
Keith Davise813d672021-04-22 10:10:34 +0100137
Mike Kelly386ff1a2021-03-29 15:04:50 +0100138 std::string errorMessage;
Keith Davise813d672021-04-22 10:10:34 +0100139
140 const INetworkProperties networkProperties(true, MemorySource::Undefined, MemorySource::Undefined, numThreads);
141
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100142 runtime->LoadNetwork(networkId, std::move(optNet), errorMessage, networkProperties);
Mike Kelly386ff1a2021-03-29 15:04:50 +0100143
144 InputTensors inputTensors;
145 inputTensors.reserve(inputTensorData.size());
146 for (auto&& it : inputTensorData)
147 {
148 inputTensors.push_back({it.first,
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100149 ConstTensor(runtime->GetInputTensorInfo(networkId, it.first), it.second.data())});
Mike Kelly386ff1a2021-03-29 15:04:50 +0100150 }
151
152 OutputTensors outputTensors;
153 outputTensors.reserve(expectedOutputData.size());
154 std::map<int, std::vector<TOutput>> outputStorage;
155 for (auto&& it : expectedOutputData)
156 {
157 std::vector<TOutput> out(it.second.size());
158 outputStorage.emplace(it.first, out);
159 outputTensors.push_back({it.first,
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100160 Tensor(runtime->GetOutputTensorInfo(networkId, it.first),
Mike Kelly386ff1a2021-03-29 15:04:50 +0100161 outputStorage.at(it.first).data())});
162 }
163
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100164 if (numThreads <= 1)
Keith Davise813d672021-04-22 10:10:34 +0100165 {
166 // Create WorkingMemHandle for this async network
167 std::unique_ptr<IWorkingMemHandle> workingMemHandle = runtime->CreateWorkingMemHandle(networkId);
168 IWorkingMemHandle& workingMemHandleRef = *workingMemHandle.get();
Mike Kelly386ff1a2021-03-29 15:04:50 +0100169
Keith Davise813d672021-04-22 10:10:34 +0100170 // Run the async network
171 runtime->Execute(workingMemHandleRef, inputTensors, outputTensors);
172 }
173 else
174 {
175 std::vector<IAsyncExecutionCallbackPtr> callbacks;
Mike Kelly386ff1a2021-03-29 15:04:50 +0100176
Keith Davise813d672021-04-22 10:10:34 +0100177 // Create 1000 callbacks that will be checked post scheduling
178 for (size_t i = 0; i < 1000; ++i)
179 {
180 callbacks.emplace_back(std::make_shared<AsyncExecutionCallback>());
181 }
182
183 // For the asyncronous execution, we are adding a pool of working memory handles (1 per thread) in the
184 // LoadedNetwork with a each scheduled inference having a spefic priority
185 for (IAsyncExecutionCallbackPtr cb : callbacks)
186 {
187 runtime->Schedule(networkId,
188 inputTensors,
189 outputTensors,
190 static_cast<QosExecPriority>(rand()%3),
191 cb);
192 }
193
194 // Wait until the execution signals a notify
195 for (IAsyncExecutionCallbackPtr cb : callbacks)
196 {
197 cb->Wait();
198
199 // Checks the results.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100200 CHECK(cb->GetStatus() == Status::Success);
Keith Davise813d672021-04-22 10:10:34 +0100201 }
202 }
203
Mike Kelly386ff1a2021-03-29 15:04:50 +0100204 for (auto&& it : expectedOutputData)
205 {
206 std::vector<TOutput> out = outputStorage.at(it.first);
Keith Davise813d672021-04-22 10:10:34 +0100207
Mike Kelly386ff1a2021-03-29 15:04:50 +0100208 for (unsigned int i = 0; i < out.size(); ++i)
209 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100210 CHECK(Compare<ArmnnOType>(it.second[i], out[i], tolerance) == true);
Mike Kelly386ff1a2021-03-29 15:04:50 +0100211 }
212 }
213}
214
215template<typename armnn::DataType DataType>
216INetworkPtr CreateStridedSliceNetwork(const TensorShape& inputShape,
217 const TensorShape& outputShape,
218 const std::vector<int>& beginData,
219 const std::vector<int>& endData,
220 const std::vector<int>& stridesData,
221 int beginMask = 0,
222 int endMask = 0,
223 int shrinkAxisMask = 0,
224 int ellipsisMask = 0,
225 int newAxisMask = 0,
226 const float qScale = 1.0f,
227 const int32_t qOffset = 0)
228{
229 using namespace armnn;
230 // Builds up the structure of the network.
231 INetworkPtr net(INetwork::Create());
232
233 TensorInfo inputTensorInfo(inputShape, DataType, qScale, qOffset);
234 TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
235
236 armnn::StridedSliceDescriptor stridedSliceDescriptor;
237 stridedSliceDescriptor.m_Begin = beginData;
238 stridedSliceDescriptor.m_End = endData;
239 stridedSliceDescriptor.m_Stride = stridesData;
240 stridedSliceDescriptor.m_BeginMask = beginMask;
241 stridedSliceDescriptor.m_EndMask = endMask;
242 stridedSliceDescriptor.m_ShrinkAxisMask = shrinkAxisMask;
243 stridedSliceDescriptor.m_EllipsisMask = ellipsisMask;
244 stridedSliceDescriptor.m_NewAxisMask = newAxisMask;
245
246 IConnectableLayer* input = net->AddInputLayer(0, "Input_Layer");
247 IConnectableLayer* stridedSlice = net->AddStridedSliceLayer(stridedSliceDescriptor, "splitter");
248 IConnectableLayer* output = net->AddOutputLayer(0);
249
250 Connect(input, stridedSlice, inputTensorInfo, 0, 0);
251 Connect(stridedSlice, output, outputTensorInfo, 0, 0);
252
253 return net;
254}
255
256template<armnn::DataType ArmnnType>
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100257void StridedSlicedEndToEndTest(const std::vector<BackendId>& backends, size_t numThreads)
Mike Kelly386ff1a2021-03-29 15:04:50 +0100258{
259 using namespace armnn;
260 using T = ResolveType<ArmnnType>;
261
262 const TensorShape& inputShape = {3, 2, 3, 1};
263 const TensorShape& outputShape = {1, 2, 3, 1};
264 const std::vector<int>& beginData = {1, 0, 0, 0};
265 const std::vector<int>& endData = {2, 2, 3, 1};
266 const std::vector<int>& stridesData = {1, 1, 1, 1};
267 int beginMask = 0;
268 int endMask = 0;
269 int shrinkAxisMask = 0;
270 int ellipsisMask = 0;
271 int newAxisMask = 0;
272
273 // Builds up the structure of the network
274 INetworkPtr net = CreateStridedSliceNetwork<ArmnnType>(inputShape,
275 outputShape,
276 beginData,
277 endData,
278 stridesData,
279 beginMask,
280 endMask,
281 shrinkAxisMask,
282 ellipsisMask,
283 newAxisMask);
284
Sadik Armagan1625efc2021-06-10 18:24:34 +0100285 CHECK(net);
Mike Kelly386ff1a2021-03-29 15:04:50 +0100286 // Creates structures for input & output.
287 std::vector<T> inputData{
288 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
289
290 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
291
292 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
293 };
294
295 std::vector<T> outputExpected{
296 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f
297 };
298
299 std::map<int, std::vector<T>> inputTensorData = {{0, inputData}};
300 std::map<int, std::vector<T>> expectedOutputData = {{0, outputExpected}};
301
Kevin Mayb4b3ac92021-05-21 16:42:21 +0100302 AsyncEndToEndTestImpl<ArmnnType, ArmnnType>(move(net),
303 inputTensorData,
304 expectedOutputData,
305 backends,
306 0.000001f,
307 numThreads);
Finn Williamsb8181f72021-04-07 10:23:21 +0100308}
309
310template<armnn::DataType ArmnnType>
311void StridedSlicedMultiThreadedEndToEndTest(const std::vector<BackendId>& backends)
312{
313 using namespace armnn;
314 using T = ResolveType<ArmnnType>;
315
316 const TensorShape& inputShape = {3, 2, 3, 1};
317 const TensorShape& outputShape = {1, 2, 3, 1};
318 const std::vector<int>& beginData = {1, 0, 0, 0};
319 const std::vector<int>& endData = {2, 2, 3, 1};
320 const std::vector<int>& stridesData = {1, 1, 1, 1};
321 int beginMask = 0;
322 int endMask = 0;
323 int shrinkAxisMask = 0;
324 int ellipsisMask = 0;
325 int newAxisMask = 0;
326
327 // Builds up the structure of the network
328 INetworkPtr net = CreateStridedSliceNetwork<ArmnnType>(inputShape,
329 outputShape,
330 beginData,
331 endData,
332 stridesData,
333 beginMask,
334 endMask,
335 shrinkAxisMask,
336 ellipsisMask,
337 newAxisMask);
338
Sadik Armagan1625efc2021-06-10 18:24:34 +0100339 CHECK(net);
Finn Williamsb8181f72021-04-07 10:23:21 +0100340
341 // Creates structures for input & output.
342 std::vector<T> inputData1{
343 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
344
345 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f,
346
347 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
348 };
349
350 std::vector<T> outputExpected1{ 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f };
351
352 // Creates structures for input & output.
353 std::vector<T> inputData2{
354 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f,
355
356 8.0f, 8.0f, 8.0f, 7.0f, 7.0f, 7.0f,
357
358 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f
359 };
360
361 std::vector<T> outputExpected2{ 8.0f, 8.0f, 8.0f, 7.0f, 7.0f, 7.0f };
362
363 std::vector<std::map<int, std::vector<T>>> inputTensors;
364 std::vector<std::map<int, std::vector<T>>> outputTensors;
365
366 inputTensors.push_back(std::map<int, std::vector<T>> {{0, inputData1}});
367 inputTensors.push_back(std::map<int, std::vector<T>> {{0, inputData2}});
368 outputTensors.push_back(std::map<int, std::vector<T>> {{0, outputExpected1}});
369 outputTensors.push_back(std::map<int, std::vector<T>> {{0, outputExpected2}});
370
371 AsyncThreadedEndToEndTestImpl<ArmnnType, ArmnnType>(move(net), inputTensors, outputTensors, backends, 2);
Mike Kelly386ff1a2021-03-29 15:04:50 +0100372}
373
374} // experimental namespace
375
376} // armnn namespace
377