blob: 64275a372954661aa5d5d3ac1d3bb71f9fbd44c9 [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"
21#include "Utils.h"
22
23#include <android/hardware/neuralnetworks/1.0/types.h>
24#include <android/hardware/neuralnetworks/1.1/types.h>
25#include <android/hardware/neuralnetworks/1.2/types.h>
26#include <android/hardware/neuralnetworks/1.3/IPreparedModel.h>
27#include <android/hardware/neuralnetworks/1.3/types.h>
28#include <nnapi/IPreparedModel.h>
29#include <nnapi/Result.h>
Michael Butler6547b2a2020-11-22 19:36:30 -080030#include <nnapi/TypeUtils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070031#include <nnapi/Types.h>
32#include <nnapi/hal/1.2/Conversions.h>
Michael Butler76e491f2020-12-19 01:55:32 -080033#include <nnapi/hal/1.2/ExecutionBurstController.h>
34#include <nnapi/hal/1.2/ExecutionBurstUtils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070035#include <nnapi/hal/CommonUtils.h>
36#include <nnapi/hal/HandleError.h>
37#include <nnapi/hal/ProtectCallback.h>
38
39#include <memory>
40#include <tuple>
41#include <utility>
42#include <vector>
43
Michael Butleraad934b2020-12-13 23:06:06 -080044// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
45// lifetimes across processes and for protecting asynchronous calls across HIDL.
46
Michael Butler4b276a72020-08-06 23:22:35 -070047namespace android::hardware::neuralnetworks::V1_3::utils {
48namespace {
49
Michael Butler4b276a72020-08-06 23:22:35 -070050nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> convertFencedExecutionCallbackResults(
Michael Butler7fd03c22020-12-06 21:50:59 -080051 ErrorStatus status, const V1_2::Timing& timingLaunched, const V1_2::Timing& timingFenced) {
52 HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status);
Michael Butler6547b2a2020-11-22 19:36:30 -080053 return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced)));
Michael Butler4b276a72020-08-06 23:22:35 -070054}
55
Michael Butler7fd03c22020-12-06 21:50:59 -080056nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> fencedExecutionCallback(
57 ErrorStatus status, const hidl_handle& syncFence,
58 const sp<IFencedExecutionCallback>& callback) {
59 HANDLE_HAL_STATUS(status) << "fenced execution failed with " << toString(status);
60
Michael Butler4b276a72020-08-06 23:22:35 -070061 auto resultSyncFence = nn::SyncFence::createAsSignaled();
62 if (syncFence.getNativeHandle() != nullptr) {
Michael Butler6547b2a2020-11-22 19:36:30 -080063 auto sharedHandle = NN_TRY(nn::convert(syncFence));
Michael Butler4b276a72020-08-06 23:22:35 -070064 resultSyncFence = NN_TRY(hal::utils::makeGeneralFailure(
Michael Butler6547b2a2020-11-22 19:36:30 -080065 nn::SyncFence::create(std::move(sharedHandle)), nn::ErrorStatus::GENERAL_FAILURE));
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;
142 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
143 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
144
145 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
Michael Butler7fd03c22020-12-06 21:50:59 -0800146 const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
Michael Butler4b276a72020-08-06 23:22:35 -0700147 const auto hidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
148 const auto hidlLoopTimeoutDuration =
149 NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration)));
150
Michael Butler7fd03c22020-12-06 21:50:59 -0800151 auto result = kExecuteSynchronously
152 ? executeSynchronously(hidlRequest, hidlMeasure, hidlDeadline,
153 hidlLoopTimeoutDuration)
154 : executeAsynchronously(hidlRequest, hidlMeasure, hidlDeadline,
155 hidlLoopTimeoutDuration);
156 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler4b276a72020-08-06 23:22:35 -0700157
Michael Butler7fd03c22020-12-06 21:50:59 -0800158 NN_TRY(hal::utils::makeExecutionFailure(
159 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
Michael Butler4b276a72020-08-06 23:22:35 -0700160
Michael Butler7fd03c22020-12-06 21:50:59 -0800161 return std::make_pair(std::move(outputShapes), timing);
Michael Butler4b276a72020-08-06 23:22:35 -0700162}
163
164nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
165PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
166 nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800167 const nn::OptionalDuration& loopTimeoutDuration,
168 const nn::OptionalDuration& timeoutDurationAfterFence) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700169 // Ensure that request is ready for IPC.
170 std::optional<nn::Request> maybeRequestInShared;
171 const nn::Request& requestInShared =
172 NN_TRY(hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared));
173
174 const auto hidlRequest = NN_TRY(convert(requestInShared));
Slava Shklyaev49817a02020-10-27 18:44:01 +0000175 const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor));
Michael Butler7fd03c22020-12-06 21:50:59 -0800176 const auto hidlMeasure = NN_TRY(convert(measure));
Michael Butler4b276a72020-08-06 23:22:35 -0700177 const auto hidlDeadline = NN_TRY(convert(deadline));
178 const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
179 const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
180
Michael Butler7fd03c22020-12-06 21:50:59 -0800181 auto cb = hal::utils::CallbackValue(fencedExecutionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700182
183 const auto ret = kPreparedModel->executeFenced(hidlRequest, hidlWaitFor, hidlMeasure,
184 hidlDeadline, hidlLoopTimeoutDuration,
185 hidlTimeoutDurationAfterFence, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800186 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800187 auto [syncFence, callback] = NN_TRY(cb.take());
Michael Butler4b276a72020-08-06 23:22:35 -0700188
189 // If executeFenced required the request memory to be moved into shared memory, block here until
190 // the fenced execution has completed and flush the memory back.
191 if (maybeRequestInShared.has_value()) {
192 const auto state = syncFence.syncWait({});
193 if (state != nn::SyncFence::FenceState::SIGNALED) {
194 return NN_ERROR() << "syncWait failed with " << state;
195 }
196 NN_TRY(hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared));
197 }
198
199 return std::make_pair(std::move(syncFence), std::move(callback));
200}
201
Michael Butler2a8b6792020-12-18 20:31:14 -0800202nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler76e491f2020-12-19 01:55:32 -0800203 auto self = shared_from_this();
204 auto fallback = [preparedModel = std::move(self)](const nn::Request& request,
205 nn::MeasureTiming measure)
206 -> nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> {
207 return preparedModel->execute(request, measure, {}, {});
208 };
209 const auto pollingTimeWindow = V1_2::utils::getBurstControllerPollingTimeWindow();
210 return V1_2::utils::ExecutionBurstController::create(kPreparedModel, std::move(fallback),
211 pollingTimeWindow);
Michael Butler2a8b6792020-12-18 20:31:14 -0800212}
213
Michael Butler4b276a72020-08-06 23:22:35 -0700214std::any PreparedModel::getUnderlyingResource() const {
215 sp<V1_3::IPreparedModel> resource = kPreparedModel;
216 return resource;
217}
218
219} // namespace android::hardware::neuralnetworks::V1_3::utils