blob: 1623de5e9fa81d3e2fd2cc204f4384dafbb9a3cb [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>
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 Wang5f6bedb2021-03-03 16:20:37 -0800143 hal::utils::RequestRelocation relocation;
144 const nn::Request& requestInShared =
145 NN_TRY(hal::utils::makeExecutionFailure(hal::utils::convertRequestFromPointerToShared(
Xusong Wange3d0dad2021-05-07 14:13:22 -0700146 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
147 &maybeRequestInShared, &relocation)));
Michael Butler4b276a72020-08-06 23:22:35 -0700148
149 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
Michael Butler7fd03c22020-12-06 21:50:59 -0800150 const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
Michael Butler4b276a72020-08-06 23:22:35 -0700151 const auto hidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
152 const auto hidlLoopTimeoutDuration =
153 NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration)));
154
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800155 return executeInternal(hidlRequest, hidlMeasure, hidlDeadline, hidlLoopTimeoutDuration,
156 relocation);
157}
158
159nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
160PreparedModel::executeInternal(const Request& request, V1_2::MeasureTiming measure,
161 const OptionalTimePoint& deadline,
162 const OptionalTimeoutDuration& loopTimeoutDuration,
163 const hal::utils::RequestRelocation& relocation) const {
164 if (relocation.input) {
165 relocation.input->flush();
166 }
167
Michael Butler7fd03c22020-12-06 21:50:59 -0800168 auto result = kExecuteSynchronously
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800169 ? executeSynchronously(request, measure, deadline, loopTimeoutDuration)
170 : executeAsynchronously(request, measure, deadline, loopTimeoutDuration);
Michael Butler7fd03c22020-12-06 21:50:59 -0800171 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler4b276a72020-08-06 23:22:35 -0700172
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800173 if (relocation.output) {
174 relocation.output->flush();
175 }
Michael Butler7fd03c22020-12-06 21:50:59 -0800176 return std::make_pair(std::move(outputShapes), timing);
Michael Butler4b276a72020-08-06 23:22:35 -0700177}
178
179nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
180PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
181 nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800182 const nn::OptionalDuration& loopTimeoutDuration,
183 const nn::OptionalDuration& timeoutDurationAfterFence) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700184 // Ensure that request is ready for IPC.
185 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800186 hal::utils::RequestRelocation relocation;
187 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
Xusong Wange3d0dad2021-05-07 14:13:22 -0700188 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
189 &maybeRequestInShared, &relocation));
Michael Butler4b276a72020-08-06 23:22:35 -0700190
191 const auto hidlRequest = NN_TRY(convert(requestInShared));
Slava Shklyaev49817a02020-10-27 18:44:01 +0000192 const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor));
Michael Butler7fd03c22020-12-06 21:50:59 -0800193 const auto hidlMeasure = NN_TRY(convert(measure));
Michael Butler4b276a72020-08-06 23:22:35 -0700194 const auto hidlDeadline = NN_TRY(convert(deadline));
195 const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
196 const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
197
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800198 return executeFencedInternal(hidlRequest, hidlWaitFor, hidlMeasure, hidlDeadline,
199 hidlLoopTimeoutDuration, hidlTimeoutDurationAfterFence,
200 relocation);
201}
202
203nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
204PreparedModel::executeFencedInternal(const Request& request, const hidl_vec<hidl_handle>& waitFor,
205 V1_2::MeasureTiming measure, const OptionalTimePoint& deadline,
206 const OptionalTimeoutDuration& loopTimeoutDuration,
207 const OptionalTimeoutDuration& timeoutDurationAfterFence,
208 const hal::utils::RequestRelocation& relocation) const {
209 if (relocation.input) {
210 relocation.input->flush();
211 }
212
Michael Butler7fd03c22020-12-06 21:50:59 -0800213 auto cb = hal::utils::CallbackValue(fencedExecutionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700214
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800215 const auto ret =
216 kPreparedModel->executeFenced(request, waitFor, measure, deadline, loopTimeoutDuration,
217 timeoutDurationAfterFence, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800218 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800219 auto [syncFence, callback] = NN_TRY(cb.take());
Michael Butler4b276a72020-08-06 23:22:35 -0700220
221 // If executeFenced required the request memory to be moved into shared memory, block here until
222 // the fenced execution has completed and flush the memory back.
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800223 if (relocation.output) {
Michael Butler4b276a72020-08-06 23:22:35 -0700224 const auto state = syncFence.syncWait({});
225 if (state != nn::SyncFence::FenceState::SIGNALED) {
226 return NN_ERROR() << "syncWait failed with " << state;
227 }
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800228 relocation.output->flush();
Michael Butler4b276a72020-08-06 23:22:35 -0700229 }
230
231 return std::make_pair(std::move(syncFence), std::move(callback));
232}
233
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800234nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution(
235 const nn::Request& request, nn::MeasureTiming measure,
236 const nn::OptionalDuration& loopTimeoutDuration) const {
237 // Ensure that request is ready for IPC.
238 std::optional<nn::Request> maybeRequestInShared;
239 hal::utils::RequestRelocation relocation;
240 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
Xusong Wange3d0dad2021-05-07 14:13:22 -0700241 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
242 &maybeRequestInShared, &relocation));
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800243
244 auto hidlRequest = NN_TRY(convert(requestInShared));
245 auto hidlMeasure = NN_TRY(convert(measure));
246 auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
247 return Execution::create(shared_from_this(), std::move(hidlRequest), std::move(relocation),
248 hidlMeasure, std::move(hidlLoopTimeoutDuration));
249}
250
Michael Butler2a8b6792020-12-18 20:31:14 -0800251nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler76e491f2020-12-19 01:55:32 -0800252 auto self = shared_from_this();
Michael Butler8414a6e2021-03-10 18:41:05 -0800253 auto fallback = [preparedModel = std::move(self)](
254 const nn::Request& request, nn::MeasureTiming measure,
255 const nn::OptionalTimePoint& deadline,
256 const nn::OptionalDuration& loopTimeoutDuration)
Michael Butler76e491f2020-12-19 01:55:32 -0800257 -> nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> {
Michael Butler8414a6e2021-03-10 18:41:05 -0800258 return preparedModel->execute(request, measure, deadline, loopTimeoutDuration);
Michael Butler76e491f2020-12-19 01:55:32 -0800259 };
260 const auto pollingTimeWindow = V1_2::utils::getBurstControllerPollingTimeWindow();
Xusong Wangb2e80852021-03-23 15:07:10 -0700261 return V1_2::utils::ExecutionBurstController::create(shared_from_this(), kPreparedModel,
Michael Butler76e491f2020-12-19 01:55:32 -0800262 pollingTimeWindow);
Michael Butler2a8b6792020-12-18 20:31:14 -0800263}
264
Michael Butler4b276a72020-08-06 23:22:35 -0700265std::any PreparedModel::getUnderlyingResource() const {
266 sp<V1_3::IPreparedModel> resource = kPreparedModel;
267 return resource;
268}
269
270} // namespace android::hardware::neuralnetworks::V1_3::utils