blob: 71119cde38cb2977226b4c210438b9932cd4c9c8 [file] [log] [blame]
surmeh0149b9e102018-05-17 14:11:25 +01001//
Mike Kellye2d611e2021-10-14 12:35:58 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
surmeh0149b9e102018-05-17 14:11:25 +01004//
Mike Kellye2d611e2021-10-14 12:35:58 +01005
surmeh0149b9e102018-05-17 14:11:25 +01006#include "DriverTestHelpers.hpp"
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +01007
surmeh0149b9e102018-05-17 14:11:25 +01008#include <log/log.h>
9
Mike Kellye2d611e2021-10-14 12:35:58 +010010DOCTEST_TEST_SUITE("ConcurrentDriverTests")
Sadik Armagan9150bff2021-05-26 15:40:53 +010011{
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010012using ArmnnDriver = armnn_driver::ArmnnDriver;
surmeh0149b9e102018-05-17 14:11:25 +010013using DriverOptions = armnn_driver::DriverOptions;
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010014using HalPolicy = armnn_driver::hal_1_0::HalPolicy;
Sadik Armagan188675f2021-02-12 17:16:42 +000015using RequestArgument = V1_0::RequestArgument;
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010016
surmeh0149b9e102018-05-17 14:11:25 +010017using namespace android::nn;
telsoa01ce3e84a2018-08-31 09:31:35 +010018using namespace android::hardware;
surmeh0149b9e102018-05-17 14:11:25 +010019using namespace driverTestHelpers;
telsoa01ce3e84a2018-08-31 09:31:35 +010020using namespace armnn_driver;
surmeh0149b9e102018-05-17 14:11:25 +010021
22// Add our own test for concurrent execution
23// The main point of this test is to check that multiple requests can be
24// executed without waiting for the callback from previous execution.
25// The operations performed are not significant.
Mike Kellye2d611e2021-10-14 12:35:58 +010026DOCTEST_TEST_CASE("ConcurrentExecute")
surmeh0149b9e102018-05-17 14:11:25 +010027{
28 ALOGI("ConcurrentExecute: entry");
29
30 auto driver = std::make_unique<ArmnnDriver>(DriverOptions(armnn::Compute::CpuRef));
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010031 HalPolicy::Model model = {};
surmeh0149b9e102018-05-17 14:11:25 +010032
33 // add operands
34 int32_t actValue = 0;
35 float weightValue[] = {2, 4, 1};
36 float biasValue[] = {4};
37
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010038 AddInputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 3});
39 AddTensorOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 3}, weightValue);
40 AddTensorOperand<HalPolicy>(model, hidl_vec<uint32_t>{1}, biasValue);
41 AddIntOperand<HalPolicy>(model, actValue);
42 AddOutputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 1});
surmeh0149b9e102018-05-17 14:11:25 +010043
44 // make the fully connected operation
45 model.operations.resize(1);
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010046 model.operations[0].type = HalPolicy::OperationType::FULLY_CONNECTED;
surmeh0149b9e102018-05-17 14:11:25 +010047 model.operations[0].inputs = hidl_vec<uint32_t>{0, 1, 2, 3};
48 model.operations[0].outputs = hidl_vec<uint32_t>{4};
49
50 // make the prepared models
51 const size_t maxRequests = 5;
Sadik Armagand4636872020-04-27 10:15:41 +010052 size_t preparedModelsSize = 0;
Sadik Armagane6e54a82019-05-08 10:18:05 +010053 android::sp<V1_0::IPreparedModel> preparedModels[maxRequests];
surmeh0149b9e102018-05-17 14:11:25 +010054 for (size_t i = 0; i < maxRequests; ++i)
55 {
Sadik Armagand4636872020-04-27 10:15:41 +010056 auto preparedModel = PrepareModel(model, *driver);
57 if (preparedModel.get() != nullptr)
58 {
59 preparedModels[i] = PrepareModel(model, *driver);
60 preparedModelsSize++;
61 }
surmeh0149b9e102018-05-17 14:11:25 +010062 }
63
Mike Kellye2d611e2021-10-14 12:35:58 +010064 DOCTEST_CHECK(maxRequests == preparedModelsSize);
Sadik Armagand4636872020-04-27 10:15:41 +010065
surmeh0149b9e102018-05-17 14:11:25 +010066 // construct the request data
Sadik Armagan188675f2021-02-12 17:16:42 +000067 V1_0::DataLocation inloc = {};
68 inloc.poolIndex = 0;
69 inloc.offset = 0;
70 inloc.length = 3 * sizeof(float);
71 RequestArgument input = {};
72 input.location = inloc;
73 input.dimensions = hidl_vec<uint32_t>{};
surmeh0149b9e102018-05-17 14:11:25 +010074
Sadik Armagan188675f2021-02-12 17:16:42 +000075 V1_0::DataLocation outloc = {};
76 outloc.poolIndex = 1;
77 outloc.offset = 0;
78 outloc.length = 1 * sizeof(float);
79 RequestArgument output = {};
80 output.location = outloc;
81 output.dimensions = hidl_vec<uint32_t>{};
surmeh0149b9e102018-05-17 14:11:25 +010082
83 // build the requests
Kevin Mayec1e5b82020-02-26 17:00:39 +000084 V1_0::Request requests[maxRequests];
Narumol Prangnawarat558a1d42022-02-07 13:12:24 +000085 android::sp<IMemory> inMemory[maxRequests];
surmeh0149b9e102018-05-17 14:11:25 +010086 android::sp<IMemory> outMemory[maxRequests];
Narumol Prangnawarat558a1d42022-02-07 13:12:24 +000087 float indata[] = {2, 32, 16};
surmeh0149b9e102018-05-17 14:11:25 +010088 float* outdata[maxRequests];
89 for (size_t i = 0; i < maxRequests; ++i)
90 {
91 requests[i].inputs = hidl_vec<RequestArgument>{input};
92 requests[i].outputs = hidl_vec<RequestArgument>{output};
93 // set the input data (matching source test)
Narumol Prangnawarat558a1d42022-02-07 13:12:24 +000094 inMemory[i] = AddPoolAndSetData<float>(3, requests[i], indata);
surmeh0149b9e102018-05-17 14:11:25 +010095 // add memory for the output
Ellen Norris-Thompson976ad3e2019-08-21 15:21:14 +010096 outMemory[i] = AddPoolAndGetData<float>(1, requests[i]);
surmeh0149b9e102018-05-17 14:11:25 +010097 outdata[i] = static_cast<float*>(static_cast<void*>(outMemory[i]->getPointer()));
98 }
99
100 // invoke the execution of the requests
101 ALOGI("ConcurrentExecute: executing requests");
102 android::sp<ExecutionCallback> cb[maxRequests];
103 for (size_t i = 0; i < maxRequests; ++i)
104 {
105 cb[i] = ExecuteNoWait(preparedModels[i], requests[i]);
106 }
107
108 // wait for the requests to complete
109 ALOGI("ConcurrentExecute: waiting for callbacks");
110 for (size_t i = 0; i < maxRequests; ++i)
111 {
Mike Kellye2d611e2021-10-14 12:35:58 +0100112 DOCTEST_CHECK(cb[i]);
surmeh0149b9e102018-05-17 14:11:25 +0100113 cb[i]->wait();
114 }
115
116 // check the results
117 ALOGI("ConcurrentExecute: validating results");
118 for (size_t i = 0; i < maxRequests; ++i)
119 {
Mike Kellye2d611e2021-10-14 12:35:58 +0100120 DOCTEST_CHECK(outdata[i][0] == 152);
surmeh0149b9e102018-05-17 14:11:25 +0100121 }
122 ALOGI("ConcurrentExecute: exit");
123}
124
Sadik Armagan9150bff2021-05-26 15:40:53 +0100125}