blob: 792665ed1eec0753b92db6573721a4aa70cd0aa5 [file] [log] [blame]
Michael Butler4b276a72020-08-06 23:22:35 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "PreparedModel.h"
18
19#include "Callbacks.h"
20#include "Conversions.h"
Xusong Wang5f6bedb2021-03-03 16:20:37 -080021#include "Execution.h"
Michael Butler4b276a72020-08-06 23:22:35 -070022#include "Utils.h"
23
24#include <android/hardware/neuralnetworks/1.0/types.h>
25#include <android/hardware/neuralnetworks/1.1/types.h>
26#include <android/hardware/neuralnetworks/1.2/types.h>
27#include <android/hardware/neuralnetworks/1.3/IPreparedModel.h>
28#include <android/hardware/neuralnetworks/1.3/types.h>
29#include <nnapi/IPreparedModel.h>
30#include <nnapi/Result.h>
Michael Butler6547b2a2020-11-22 19:36:30 -080031#include <nnapi/TypeUtils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070032#include <nnapi/Types.h>
Michael Butlere8645c32021-10-15 18:42:32 -070033#include <nnapi/hal/1.0/ProtectCallback.h>
Michael Butler4b276a72020-08-06 23:22:35 -070034#include <nnapi/hal/1.2/Conversions.h>
Michael Butler76e491f2020-12-19 01:55:32 -080035#include <nnapi/hal/1.2/ExecutionBurstController.h>
36#include <nnapi/hal/1.2/ExecutionBurstUtils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070037#include <nnapi/hal/CommonUtils.h>
38#include <nnapi/hal/HandleError.h>
Michael Butler4b276a72020-08-06 23:22:35 -070039
40#include <memory>
41#include <tuple>
42#include <utility>
43#include <vector>
44
Michael Butleraad934b2020-12-13 23:06:06 -080045// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
46// lifetimes across processes and for protecting asynchronous calls across HIDL.
47
Michael Butler4b276a72020-08-06 23:22:35 -070048namespace android::hardware::neuralnetworks::V1_3::utils {
49namespace {
50
Michael Butler4b276a72020-08-06 23:22:35 -070051nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> convertFencedExecutionCallbackResults(
Michael Butler7fd03c22020-12-06 21:50:59 -080052 ErrorStatus status, const V1_2::Timing& timingLaunched, const V1_2::Timing& timingFenced) {
53 HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status);
Michael Butler6547b2a2020-11-22 19:36:30 -080054 return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced)));
Michael Butler4b276a72020-08-06 23:22:35 -070055}
56
Michael Butler7fd03c22020-12-06 21:50:59 -080057nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> fencedExecutionCallback(
58 ErrorStatus status, const hidl_handle& syncFence,
59 const sp<IFencedExecutionCallback>& callback) {
60 HANDLE_HAL_STATUS(status) << "fenced execution failed with " << toString(status);
61
Michael Butler4b276a72020-08-06 23:22:35 -070062 auto resultSyncFence = nn::SyncFence::createAsSignaled();
63 if (syncFence.getNativeHandle() != nullptr) {
Michael Butler6547b2a2020-11-22 19:36:30 -080064 auto sharedHandle = NN_TRY(nn::convert(syncFence));
Michael Butlerff9a5a52021-10-15 16:23:20 -070065 resultSyncFence = NN_TRY(nn::SyncFence::create(std::move(sharedHandle)));
Michael Butler4b276a72020-08-06 23:22:35 -070066 }
67
68 if (callback == nullptr) {
69 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "callback is null";
70 }
71
72 // Create callback which can be used to retrieve the execution error status and timings.
73 nn::ExecuteFencedInfoCallback resultCallback =
74 [callback]() -> nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> {
Michael Butler7fd03c22020-12-06 21:50:59 -080075 auto cb = hal::utils::CallbackValue(convertFencedExecutionCallbackResults);
Michael Butler4b276a72020-08-06 23:22:35 -070076
77 const auto ret = callback->getExecutionInfo(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080078 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070079
Michael Butler7fd03c22020-12-06 21:50:59 -080080 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -070081 };
82
83 return std::make_pair(std::move(resultSyncFence), std::move(resultCallback));
84}
85
86} // namespace
87
88nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
Michael Butler7fd03c22020-12-06 21:50:59 -080089 sp<V1_3::IPreparedModel> preparedModel, bool executeSynchronously) {
Michael Butler4b276a72020-08-06 23:22:35 -070090 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080091 return NN_ERROR() << "V1_3::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070092 }
93
94 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
Michael Butler7fd03c22020-12-06 21:50:59 -080095 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
96 std::move(preparedModel), std::move(deathHandler));
Michael Butler4b276a72020-08-06 23:22:35 -070097}
98
Michael Butler7fd03c22020-12-06 21:50:59 -080099PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
100 sp<V1_3::IPreparedModel> preparedModel,
Michael Butler4b276a72020-08-06 23:22:35 -0700101 hal::utils::DeathHandler deathHandler)
Michael Butler7fd03c22020-12-06 21:50:59 -0800102 : kExecuteSynchronously(executeSynchronously),
103 kPreparedModel(std::move(preparedModel)),
104 kDeathHandler(std::move(deathHandler)) {}
Michael Butler4b276a72020-08-06 23:22:35 -0700105
106nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
107PreparedModel::executeSynchronously(const Request& request, V1_2::MeasureTiming measure,
108 const OptionalTimePoint& deadline,
109 const OptionalTimeoutDuration& loopTimeoutDuration) const {
Michael Butler7fd03c22020-12-06 21:50:59 -0800110 auto cb = hal::utils::CallbackValue(executionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700111
112 const auto ret = kPreparedModel->executeSynchronously_1_3(request, measure, deadline,
113 loopTimeoutDuration, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800114 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700115
Michael Butler7fd03c22020-12-06 21:50:59 -0800116 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -0700117}
118
119nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
120PreparedModel::executeAsynchronously(const Request& request, V1_2::MeasureTiming measure,
121 const OptionalTimePoint& deadline,
122 const OptionalTimeoutDuration& loopTimeoutDuration) const {
123 const auto cb = sp<ExecutionCallback>::make();
124 const auto scoped = kDeathHandler.protectCallback(cb.get());
125
126 const auto ret =
127 kPreparedModel->execute_1_3(request, measure, deadline, loopTimeoutDuration, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800128 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800129 if (status != ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
130 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -0700131 }
132
133 return cb->get();
134}
135
136nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
137 const nn::Request& request, nn::MeasureTiming measure,
138 const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800139 const nn::OptionalDuration& loopTimeoutDuration) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700140 // Ensure that request is ready for IPC.
141 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800142 hal::utils::RequestRelocation relocation;
Michael Butlerff9a5a52021-10-15 16:23:20 -0700143 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
144 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
145 &maybeRequestInShared, &relocation));
Michael Butler4b276a72020-08-06 23:22:35 -0700146
Michael Butlerff9a5a52021-10-15 16:23:20 -0700147 const auto hidlRequest = NN_TRY(convert(requestInShared));
148 const auto hidlMeasure = NN_TRY(convert(measure));
149 const auto hidlDeadline = NN_TRY(convert(deadline));
150 const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
Michael Butler4b276a72020-08-06 23:22:35 -0700151
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800152 return executeInternal(hidlRequest, hidlMeasure, hidlDeadline, hidlLoopTimeoutDuration,
153 relocation);
154}
155
156nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
157PreparedModel::executeInternal(const Request& request, V1_2::MeasureTiming measure,
158 const OptionalTimePoint& deadline,
159 const OptionalTimeoutDuration& loopTimeoutDuration,
160 const hal::utils::RequestRelocation& relocation) const {
161 if (relocation.input) {
162 relocation.input->flush();
163 }
164
Michael Butler7fd03c22020-12-06 21:50:59 -0800165 auto result = kExecuteSynchronously
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800166 ? executeSynchronously(request, measure, deadline, loopTimeoutDuration)
167 : executeAsynchronously(request, measure, deadline, loopTimeoutDuration);
Michael Butler7fd03c22020-12-06 21:50:59 -0800168 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler4b276a72020-08-06 23:22:35 -0700169
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800170 if (relocation.output) {
171 relocation.output->flush();
172 }
Michael Butler7fd03c22020-12-06 21:50:59 -0800173 return std::make_pair(std::move(outputShapes), timing);
Michael Butler4b276a72020-08-06 23:22:35 -0700174}
175
176nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
177PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
178 nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800179 const nn::OptionalDuration& loopTimeoutDuration,
180 const nn::OptionalDuration& timeoutDurationAfterFence) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700181 // Ensure that request is ready for IPC.
182 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800183 hal::utils::RequestRelocation relocation;
184 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
Xusong Wange3d0dad2021-05-07 14:13:22 -0700185 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
186 &maybeRequestInShared, &relocation));
Michael Butler4b276a72020-08-06 23:22:35 -0700187
188 const auto hidlRequest = NN_TRY(convert(requestInShared));
Michael Butler15965822021-10-14 23:45:11 -0700189 const auto hidlWaitFor = NN_TRY(convertSyncFences(waitFor));
Michael Butler7fd03c22020-12-06 21:50:59 -0800190 const auto hidlMeasure = NN_TRY(convert(measure));
Michael Butler4b276a72020-08-06 23:22:35 -0700191 const auto hidlDeadline = NN_TRY(convert(deadline));
192 const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
193 const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
194
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800195 return executeFencedInternal(hidlRequest, hidlWaitFor, hidlMeasure, hidlDeadline,
196 hidlLoopTimeoutDuration, hidlTimeoutDurationAfterFence,
197 relocation);
198}
199
200nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
201PreparedModel::executeFencedInternal(const Request& request, const hidl_vec<hidl_handle>& waitFor,
202 V1_2::MeasureTiming measure, const OptionalTimePoint& deadline,
203 const OptionalTimeoutDuration& loopTimeoutDuration,
204 const OptionalTimeoutDuration& timeoutDurationAfterFence,
205 const hal::utils::RequestRelocation& relocation) const {
206 if (relocation.input) {
207 relocation.input->flush();
208 }
209
Michael Butler7fd03c22020-12-06 21:50:59 -0800210 auto cb = hal::utils::CallbackValue(fencedExecutionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700211
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800212 const auto ret =
213 kPreparedModel->executeFenced(request, waitFor, measure, deadline, loopTimeoutDuration,
214 timeoutDurationAfterFence, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800215 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800216 auto [syncFence, callback] = NN_TRY(cb.take());
Michael Butler4b276a72020-08-06 23:22:35 -0700217
218 // If executeFenced required the request memory to be moved into shared memory, block here until
219 // the fenced execution has completed and flush the memory back.
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800220 if (relocation.output) {
Michael Butler4b276a72020-08-06 23:22:35 -0700221 const auto state = syncFence.syncWait({});
222 if (state != nn::SyncFence::FenceState::SIGNALED) {
223 return NN_ERROR() << "syncWait failed with " << state;
224 }
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800225 relocation.output->flush();
Michael Butler4b276a72020-08-06 23:22:35 -0700226 }
227
228 return std::make_pair(std::move(syncFence), std::move(callback));
229}
230
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800231nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution(
232 const nn::Request& request, nn::MeasureTiming measure,
233 const nn::OptionalDuration& loopTimeoutDuration) const {
234 // Ensure that request is ready for IPC.
235 std::optional<nn::Request> maybeRequestInShared;
236 hal::utils::RequestRelocation relocation;
237 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
Xusong Wange3d0dad2021-05-07 14:13:22 -0700238 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
239 &maybeRequestInShared, &relocation));
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800240
241 auto hidlRequest = NN_TRY(convert(requestInShared));
242 auto hidlMeasure = NN_TRY(convert(measure));
243 auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
244 return Execution::create(shared_from_this(), std::move(hidlRequest), std::move(relocation),
245 hidlMeasure, std::move(hidlLoopTimeoutDuration));
246}
247
Michael Butler2a8b6792020-12-18 20:31:14 -0800248nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler76e491f2020-12-19 01:55:32 -0800249 auto self = shared_from_this();
Michael Butler8414a6e2021-03-10 18:41:05 -0800250 auto fallback = [preparedModel = std::move(self)](
251 const nn::Request& request, nn::MeasureTiming measure,
252 const nn::OptionalTimePoint& deadline,
253 const nn::OptionalDuration& loopTimeoutDuration)
Michael Butler76e491f2020-12-19 01:55:32 -0800254 -> nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> {
Michael Butler8414a6e2021-03-10 18:41:05 -0800255 return preparedModel->execute(request, measure, deadline, loopTimeoutDuration);
Michael Butler76e491f2020-12-19 01:55:32 -0800256 };
257 const auto pollingTimeWindow = V1_2::utils::getBurstControllerPollingTimeWindow();
Xusong Wangb2e80852021-03-23 15:07:10 -0700258 return V1_2::utils::ExecutionBurstController::create(shared_from_this(), kPreparedModel,
Michael Butler76e491f2020-12-19 01:55:32 -0800259 pollingTimeWindow);
Michael Butler2a8b6792020-12-18 20:31:14 -0800260}
261
Michael Butler4b276a72020-08-06 23:22:35 -0700262std::any PreparedModel::getUnderlyingResource() const {
263 sp<V1_3::IPreparedModel> resource = kPreparedModel;
264 return resource;
265}
266
267} // namespace android::hardware::neuralnetworks::V1_3::utils