blob: 5163e8ac9d3300fc39b25f3450d37e0383c2f8e8 [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 Wang727a7b22021-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>
33#include <nnapi/hal/1.2/Conversions.h>
Michael Butler76e491f2020-12-19 01:55:32 -080034#include <nnapi/hal/1.2/ExecutionBurstController.h>
35#include <nnapi/hal/1.2/ExecutionBurstUtils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070036#include <nnapi/hal/CommonUtils.h>
37#include <nnapi/hal/HandleError.h>
38#include <nnapi/hal/ProtectCallback.h>
39
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 Butler4b276a72020-08-06 23:22:35 -070065 resultSyncFence = NN_TRY(hal::utils::makeGeneralFailure(
Michael Butler6547b2a2020-11-22 19:36:30 -080066 nn::SyncFence::create(std::move(sharedHandle)), nn::ErrorStatus::GENERAL_FAILURE));
Michael Butler4b276a72020-08-06 23:22:35 -070067 }
68
69 if (callback == nullptr) {
70 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "callback is null";
71 }
72
73 // Create callback which can be used to retrieve the execution error status and timings.
74 nn::ExecuteFencedInfoCallback resultCallback =
75 [callback]() -> nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> {
Michael Butler7fd03c22020-12-06 21:50:59 -080076 auto cb = hal::utils::CallbackValue(convertFencedExecutionCallbackResults);
Michael Butler4b276a72020-08-06 23:22:35 -070077
78 const auto ret = callback->getExecutionInfo(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080079 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070080
Michael Butler7fd03c22020-12-06 21:50:59 -080081 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -070082 };
83
84 return std::make_pair(std::move(resultSyncFence), std::move(resultCallback));
85}
86
87} // namespace
88
89nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
Michael Butler7fd03c22020-12-06 21:50:59 -080090 sp<V1_3::IPreparedModel> preparedModel, bool executeSynchronously) {
Michael Butler4b276a72020-08-06 23:22:35 -070091 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080092 return NN_ERROR() << "V1_3::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070093 }
94
95 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
Michael Butler7fd03c22020-12-06 21:50:59 -080096 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
97 std::move(preparedModel), std::move(deathHandler));
Michael Butler4b276a72020-08-06 23:22:35 -070098}
99
Michael Butler7fd03c22020-12-06 21:50:59 -0800100PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
101 sp<V1_3::IPreparedModel> preparedModel,
Michael Butler4b276a72020-08-06 23:22:35 -0700102 hal::utils::DeathHandler deathHandler)
Michael Butler7fd03c22020-12-06 21:50:59 -0800103 : kExecuteSynchronously(executeSynchronously),
104 kPreparedModel(std::move(preparedModel)),
105 kDeathHandler(std::move(deathHandler)) {}
Michael Butler4b276a72020-08-06 23:22:35 -0700106
107nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
108PreparedModel::executeSynchronously(const Request& request, V1_2::MeasureTiming measure,
109 const OptionalTimePoint& deadline,
110 const OptionalTimeoutDuration& loopTimeoutDuration) const {
Michael Butler7fd03c22020-12-06 21:50:59 -0800111 auto cb = hal::utils::CallbackValue(executionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700112
113 const auto ret = kPreparedModel->executeSynchronously_1_3(request, measure, deadline,
114 loopTimeoutDuration, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800115 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700116
Michael Butler7fd03c22020-12-06 21:50:59 -0800117 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -0700118}
119
120nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
121PreparedModel::executeAsynchronously(const Request& request, V1_2::MeasureTiming measure,
122 const OptionalTimePoint& deadline,
123 const OptionalTimeoutDuration& loopTimeoutDuration) const {
124 const auto cb = sp<ExecutionCallback>::make();
125 const auto scoped = kDeathHandler.protectCallback(cb.get());
126
127 const auto ret =
128 kPreparedModel->execute_1_3(request, measure, deadline, loopTimeoutDuration, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800129 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800130 if (status != ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
131 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -0700132 }
133
134 return cb->get();
135}
136
137nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
138 const nn::Request& request, nn::MeasureTiming measure,
139 const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800140 const nn::OptionalDuration& loopTimeoutDuration) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700141 // Ensure that request is ready for IPC.
142 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang727a7b22021-03-03 16:20:37 -0800143 hal::utils::RequestRelocation relocation;
144 const nn::Request& requestInShared =
145 NN_TRY(hal::utils::makeExecutionFailure(hal::utils::convertRequestFromPointerToShared(
146 &request, &maybeRequestInShared, &relocation)));
Michael Butler4b276a72020-08-06 23:22:35 -0700147
148 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
Michael Butler7fd03c22020-12-06 21:50:59 -0800149 const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
Michael Butler4b276a72020-08-06 23:22:35 -0700150 const auto hidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
151 const auto hidlLoopTimeoutDuration =
152 NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration)));
153
Xusong Wang727a7b22021-03-03 16:20:37 -0800154 return executeInternal(hidlRequest, hidlMeasure, hidlDeadline, hidlLoopTimeoutDuration,
155 relocation);
156}
157
158nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
159PreparedModel::executeInternal(const Request& request, V1_2::MeasureTiming measure,
160 const OptionalTimePoint& deadline,
161 const OptionalTimeoutDuration& loopTimeoutDuration,
162 const hal::utils::RequestRelocation& relocation) const {
163 if (relocation.input) {
164 relocation.input->flush();
165 }
166
Michael Butler7fd03c22020-12-06 21:50:59 -0800167 auto result = kExecuteSynchronously
Xusong Wang727a7b22021-03-03 16:20:37 -0800168 ? executeSynchronously(request, measure, deadline, loopTimeoutDuration)
169 : executeAsynchronously(request, measure, deadline, loopTimeoutDuration);
Michael Butler7fd03c22020-12-06 21:50:59 -0800170 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler4b276a72020-08-06 23:22:35 -0700171
Xusong Wang727a7b22021-03-03 16:20:37 -0800172 if (relocation.output) {
173 relocation.output->flush();
174 }
Michael Butler7fd03c22020-12-06 21:50:59 -0800175 return std::make_pair(std::move(outputShapes), timing);
Michael Butler4b276a72020-08-06 23:22:35 -0700176}
177
178nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
179PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
180 nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800181 const nn::OptionalDuration& loopTimeoutDuration,
182 const nn::OptionalDuration& timeoutDurationAfterFence) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700183 // Ensure that request is ready for IPC.
184 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang727a7b22021-03-03 16:20:37 -0800185 hal::utils::RequestRelocation relocation;
186 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
187 &request, &maybeRequestInShared, &relocation));
Michael Butler4b276a72020-08-06 23:22:35 -0700188
189 const auto hidlRequest = NN_TRY(convert(requestInShared));
Slava Shklyaev49817a02020-10-27 18:44:01 +0000190 const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor));
Michael Butler7fd03c22020-12-06 21:50:59 -0800191 const auto hidlMeasure = NN_TRY(convert(measure));
Michael Butler4b276a72020-08-06 23:22:35 -0700192 const auto hidlDeadline = NN_TRY(convert(deadline));
193 const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
194 const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
195
Xusong Wang727a7b22021-03-03 16:20:37 -0800196 return executeFencedInternal(hidlRequest, hidlWaitFor, hidlMeasure, hidlDeadline,
197 hidlLoopTimeoutDuration, hidlTimeoutDurationAfterFence,
198 relocation);
199}
200
201nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
202PreparedModel::executeFencedInternal(const Request& request, const hidl_vec<hidl_handle>& waitFor,
203 V1_2::MeasureTiming measure, const OptionalTimePoint& deadline,
204 const OptionalTimeoutDuration& loopTimeoutDuration,
205 const OptionalTimeoutDuration& timeoutDurationAfterFence,
206 const hal::utils::RequestRelocation& relocation) const {
207 if (relocation.input) {
208 relocation.input->flush();
209 }
210
Michael Butler7fd03c22020-12-06 21:50:59 -0800211 auto cb = hal::utils::CallbackValue(fencedExecutionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700212
Xusong Wang727a7b22021-03-03 16:20:37 -0800213 const auto ret =
214 kPreparedModel->executeFenced(request, waitFor, measure, deadline, loopTimeoutDuration,
215 timeoutDurationAfterFence, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800216 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800217 auto [syncFence, callback] = NN_TRY(cb.take());
Michael Butler4b276a72020-08-06 23:22:35 -0700218
219 // If executeFenced required the request memory to be moved into shared memory, block here until
220 // the fenced execution has completed and flush the memory back.
Xusong Wang727a7b22021-03-03 16:20:37 -0800221 if (relocation.output) {
Michael Butler4b276a72020-08-06 23:22:35 -0700222 const auto state = syncFence.syncWait({});
223 if (state != nn::SyncFence::FenceState::SIGNALED) {
224 return NN_ERROR() << "syncWait failed with " << state;
225 }
Xusong Wang727a7b22021-03-03 16:20:37 -0800226 relocation.output->flush();
Michael Butler4b276a72020-08-06 23:22:35 -0700227 }
228
229 return std::make_pair(std::move(syncFence), std::move(callback));
230}
231
Xusong Wang727a7b22021-03-03 16:20:37 -0800232nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution(
233 const nn::Request& request, nn::MeasureTiming measure,
234 const nn::OptionalDuration& loopTimeoutDuration) const {
235 // Ensure that request is ready for IPC.
236 std::optional<nn::Request> maybeRequestInShared;
237 hal::utils::RequestRelocation relocation;
238 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
239 &request, &maybeRequestInShared, &relocation));
240
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();
258 return V1_2::utils::ExecutionBurstController::create(kPreparedModel, std::move(fallback),
259 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