blob: 725e4f546a0d5a1bcc99ec26ba9954ee275fe431 [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>
Michael Butler95331512020-12-18 20:53:55 -080032#include <nnapi/hal/1.0/Burst.h>
Michael Butler4b276a72020-08-06 23:22:35 -070033#include <nnapi/hal/1.2/Conversions.h>
34#include <nnapi/hal/CommonUtils.h>
35#include <nnapi/hal/HandleError.h>
36#include <nnapi/hal/ProtectCallback.h>
37
38#include <memory>
39#include <tuple>
40#include <utility>
41#include <vector>
42
Michael Butleraad934b2020-12-13 23:06:06 -080043// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
44// lifetimes across processes and for protecting asynchronous calls across HIDL.
45
Michael Butler4b276a72020-08-06 23:22:35 -070046namespace android::hardware::neuralnetworks::V1_3::utils {
47namespace {
48
Michael Butler4b276a72020-08-06 23:22:35 -070049nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> convertFencedExecutionCallbackResults(
Michael Butler7fd03c22020-12-06 21:50:59 -080050 ErrorStatus status, const V1_2::Timing& timingLaunched, const V1_2::Timing& timingFenced) {
51 HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status);
Michael Butler6547b2a2020-11-22 19:36:30 -080052 return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced)));
Michael Butler4b276a72020-08-06 23:22:35 -070053}
54
Michael Butler7fd03c22020-12-06 21:50:59 -080055nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> fencedExecutionCallback(
56 ErrorStatus status, const hidl_handle& syncFence,
57 const sp<IFencedExecutionCallback>& callback) {
58 HANDLE_HAL_STATUS(status) << "fenced execution failed with " << toString(status);
59
Michael Butler4b276a72020-08-06 23:22:35 -070060 auto resultSyncFence = nn::SyncFence::createAsSignaled();
61 if (syncFence.getNativeHandle() != nullptr) {
Michael Butler6547b2a2020-11-22 19:36:30 -080062 auto sharedHandle = NN_TRY(nn::convert(syncFence));
Michael Butler4b276a72020-08-06 23:22:35 -070063 resultSyncFence = NN_TRY(hal::utils::makeGeneralFailure(
Michael Butler6547b2a2020-11-22 19:36:30 -080064 nn::SyncFence::create(std::move(sharedHandle)), nn::ErrorStatus::GENERAL_FAILURE));
Michael Butler4b276a72020-08-06 23:22:35 -070065 }
66
67 if (callback == nullptr) {
68 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "callback is null";
69 }
70
71 // Create callback which can be used to retrieve the execution error status and timings.
72 nn::ExecuteFencedInfoCallback resultCallback =
73 [callback]() -> nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> {
Michael Butler7fd03c22020-12-06 21:50:59 -080074 auto cb = hal::utils::CallbackValue(convertFencedExecutionCallbackResults);
Michael Butler4b276a72020-08-06 23:22:35 -070075
76 const auto ret = callback->getExecutionInfo(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080077 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070078
Michael Butler7fd03c22020-12-06 21:50:59 -080079 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -070080 };
81
82 return std::make_pair(std::move(resultSyncFence), std::move(resultCallback));
83}
84
85} // namespace
86
87nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
Michael Butler7fd03c22020-12-06 21:50:59 -080088 sp<V1_3::IPreparedModel> preparedModel, bool executeSynchronously) {
Michael Butler4b276a72020-08-06 23:22:35 -070089 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080090 return NN_ERROR() << "V1_3::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070091 }
92
93 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
Michael Butler7fd03c22020-12-06 21:50:59 -080094 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
95 std::move(preparedModel), std::move(deathHandler));
Michael Butler4b276a72020-08-06 23:22:35 -070096}
97
Michael Butler7fd03c22020-12-06 21:50:59 -080098PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
99 sp<V1_3::IPreparedModel> preparedModel,
Michael Butler4b276a72020-08-06 23:22:35 -0700100 hal::utils::DeathHandler deathHandler)
Michael Butler7fd03c22020-12-06 21:50:59 -0800101 : kExecuteSynchronously(executeSynchronously),
102 kPreparedModel(std::move(preparedModel)),
103 kDeathHandler(std::move(deathHandler)) {}
Michael Butler4b276a72020-08-06 23:22:35 -0700104
105nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
106PreparedModel::executeSynchronously(const Request& request, V1_2::MeasureTiming measure,
107 const OptionalTimePoint& deadline,
108 const OptionalTimeoutDuration& loopTimeoutDuration) const {
Michael Butler7fd03c22020-12-06 21:50:59 -0800109 auto cb = hal::utils::CallbackValue(executionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700110
111 const auto ret = kPreparedModel->executeSynchronously_1_3(request, measure, deadline,
112 loopTimeoutDuration, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800113 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700114
Michael Butler7fd03c22020-12-06 21:50:59 -0800115 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -0700116}
117
118nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
119PreparedModel::executeAsynchronously(const Request& request, V1_2::MeasureTiming measure,
120 const OptionalTimePoint& deadline,
121 const OptionalTimeoutDuration& loopTimeoutDuration) const {
122 const auto cb = sp<ExecutionCallback>::make();
123 const auto scoped = kDeathHandler.protectCallback(cb.get());
124
125 const auto ret =
126 kPreparedModel->execute_1_3(request, measure, deadline, loopTimeoutDuration, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800127 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800128 if (status != ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
129 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -0700130 }
131
132 return cb->get();
133}
134
135nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
136 const nn::Request& request, nn::MeasureTiming measure,
137 const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800138 const nn::OptionalDuration& loopTimeoutDuration) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700139 // Ensure that request is ready for IPC.
140 std::optional<nn::Request> maybeRequestInShared;
141 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
142 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
143
144 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
Michael Butler7fd03c22020-12-06 21:50:59 -0800145 const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
Michael Butler4b276a72020-08-06 23:22:35 -0700146 const auto hidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
147 const auto hidlLoopTimeoutDuration =
148 NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration)));
149
Michael Butler7fd03c22020-12-06 21:50:59 -0800150 auto result = kExecuteSynchronously
151 ? executeSynchronously(hidlRequest, hidlMeasure, hidlDeadline,
152 hidlLoopTimeoutDuration)
153 : executeAsynchronously(hidlRequest, hidlMeasure, hidlDeadline,
154 hidlLoopTimeoutDuration);
155 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler4b276a72020-08-06 23:22:35 -0700156
Michael Butler7fd03c22020-12-06 21:50:59 -0800157 NN_TRY(hal::utils::makeExecutionFailure(
158 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
Michael Butler4b276a72020-08-06 23:22:35 -0700159
Michael Butler7fd03c22020-12-06 21:50:59 -0800160 return std::make_pair(std::move(outputShapes), timing);
Michael Butler4b276a72020-08-06 23:22:35 -0700161}
162
163nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
164PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
165 nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800166 const nn::OptionalDuration& loopTimeoutDuration,
167 const nn::OptionalDuration& timeoutDurationAfterFence) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700168 // Ensure that request is ready for IPC.
169 std::optional<nn::Request> maybeRequestInShared;
170 const nn::Request& requestInShared =
171 NN_TRY(hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared));
172
173 const auto hidlRequest = NN_TRY(convert(requestInShared));
Slava Shklyaev49817a02020-10-27 18:44:01 +0000174 const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor));
Michael Butler7fd03c22020-12-06 21:50:59 -0800175 const auto hidlMeasure = NN_TRY(convert(measure));
Michael Butler4b276a72020-08-06 23:22:35 -0700176 const auto hidlDeadline = NN_TRY(convert(deadline));
177 const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
178 const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
179
Michael Butler7fd03c22020-12-06 21:50:59 -0800180 auto cb = hal::utils::CallbackValue(fencedExecutionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700181
182 const auto ret = kPreparedModel->executeFenced(hidlRequest, hidlWaitFor, hidlMeasure,
183 hidlDeadline, hidlLoopTimeoutDuration,
184 hidlTimeoutDurationAfterFence, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800185 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800186 auto [syncFence, callback] = NN_TRY(cb.take());
Michael Butler4b276a72020-08-06 23:22:35 -0700187
188 // If executeFenced required the request memory to be moved into shared memory, block here until
189 // the fenced execution has completed and flush the memory back.
190 if (maybeRequestInShared.has_value()) {
191 const auto state = syncFence.syncWait({});
192 if (state != nn::SyncFence::FenceState::SIGNALED) {
193 return NN_ERROR() << "syncWait failed with " << state;
194 }
195 NN_TRY(hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared));
196 }
197
198 return std::make_pair(std::move(syncFence), std::move(callback));
199}
200
Michael Butler2a8b6792020-12-18 20:31:14 -0800201nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler95331512020-12-18 20:53:55 -0800202 return V1_0::utils::Burst::create(shared_from_this());
Michael Butler2a8b6792020-12-18 20:31:14 -0800203}
204
Michael Butler4b276a72020-08-06 23:22:35 -0700205std::any PreparedModel::getUnderlyingResource() const {
206 sp<V1_3::IPreparedModel> resource = kPreparedModel;
207 return resource;
208}
209
210} // namespace android::hardware::neuralnetworks::V1_3::utils