blob: 7afa777868ab59329d7588561b0066a7db63c0af [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>
33#include <nnapi/hal/CommonUtils.h>
34#include <nnapi/hal/HandleError.h>
35#include <nnapi/hal/ProtectCallback.h>
36
37#include <memory>
38#include <tuple>
39#include <utility>
40#include <vector>
41
Michael Butleraad934b2020-12-13 23:06:06 -080042// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
43// lifetimes across processes and for protecting asynchronous calls across HIDL.
44
Michael Butler4b276a72020-08-06 23:22:35 -070045namespace android::hardware::neuralnetworks::V1_3::utils {
46namespace {
47
Michael Butler4b276a72020-08-06 23:22:35 -070048nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> convertFencedExecutionCallbackResults(
Michael Butler7fd03c22020-12-06 21:50:59 -080049 ErrorStatus status, const V1_2::Timing& timingLaunched, const V1_2::Timing& timingFenced) {
50 HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status);
Michael Butler6547b2a2020-11-22 19:36:30 -080051 return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced)));
Michael Butler4b276a72020-08-06 23:22:35 -070052}
53
Michael Butler7fd03c22020-12-06 21:50:59 -080054nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> fencedExecutionCallback(
55 ErrorStatus status, const hidl_handle& syncFence,
56 const sp<IFencedExecutionCallback>& callback) {
57 HANDLE_HAL_STATUS(status) << "fenced execution failed with " << toString(status);
58
Michael Butler4b276a72020-08-06 23:22:35 -070059 auto resultSyncFence = nn::SyncFence::createAsSignaled();
60 if (syncFence.getNativeHandle() != nullptr) {
Michael Butler6547b2a2020-11-22 19:36:30 -080061 auto sharedHandle = NN_TRY(nn::convert(syncFence));
Michael Butler4b276a72020-08-06 23:22:35 -070062 resultSyncFence = NN_TRY(hal::utils::makeGeneralFailure(
Michael Butler6547b2a2020-11-22 19:36:30 -080063 nn::SyncFence::create(std::move(sharedHandle)), nn::ErrorStatus::GENERAL_FAILURE));
Michael Butler4b276a72020-08-06 23:22:35 -070064 }
65
66 if (callback == nullptr) {
67 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "callback is null";
68 }
69
70 // Create callback which can be used to retrieve the execution error status and timings.
71 nn::ExecuteFencedInfoCallback resultCallback =
72 [callback]() -> nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> {
Michael Butler7fd03c22020-12-06 21:50:59 -080073 auto cb = hal::utils::CallbackValue(convertFencedExecutionCallbackResults);
Michael Butler4b276a72020-08-06 23:22:35 -070074
75 const auto ret = callback->getExecutionInfo(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080076 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070077
Michael Butler7fd03c22020-12-06 21:50:59 -080078 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -070079 };
80
81 return std::make_pair(std::move(resultSyncFence), std::move(resultCallback));
82}
83
84} // namespace
85
86nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
Michael Butler7fd03c22020-12-06 21:50:59 -080087 sp<V1_3::IPreparedModel> preparedModel, bool executeSynchronously) {
Michael Butler4b276a72020-08-06 23:22:35 -070088 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080089 return NN_ERROR() << "V1_3::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070090 }
91
92 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
Michael Butler7fd03c22020-12-06 21:50:59 -080093 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
94 std::move(preparedModel), std::move(deathHandler));
Michael Butler4b276a72020-08-06 23:22:35 -070095}
96
Michael Butler7fd03c22020-12-06 21:50:59 -080097PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
98 sp<V1_3::IPreparedModel> preparedModel,
Michael Butler4b276a72020-08-06 23:22:35 -070099 hal::utils::DeathHandler deathHandler)
Michael Butler7fd03c22020-12-06 21:50:59 -0800100 : kExecuteSynchronously(executeSynchronously),
101 kPreparedModel(std::move(preparedModel)),
102 kDeathHandler(std::move(deathHandler)) {}
Michael Butler4b276a72020-08-06 23:22:35 -0700103
104nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
105PreparedModel::executeSynchronously(const Request& request, V1_2::MeasureTiming measure,
106 const OptionalTimePoint& deadline,
107 const OptionalTimeoutDuration& loopTimeoutDuration) const {
Michael Butler7fd03c22020-12-06 21:50:59 -0800108 auto cb = hal::utils::CallbackValue(executionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700109
110 const auto ret = kPreparedModel->executeSynchronously_1_3(request, measure, deadline,
111 loopTimeoutDuration, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800112 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700113
Michael Butler7fd03c22020-12-06 21:50:59 -0800114 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -0700115}
116
117nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
118PreparedModel::executeAsynchronously(const Request& request, V1_2::MeasureTiming measure,
119 const OptionalTimePoint& deadline,
120 const OptionalTimeoutDuration& loopTimeoutDuration) const {
121 const auto cb = sp<ExecutionCallback>::make();
122 const auto scoped = kDeathHandler.protectCallback(cb.get());
123
124 const auto ret =
125 kPreparedModel->execute_1_3(request, measure, deadline, loopTimeoutDuration, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800126 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800127 if (status != ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
128 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -0700129 }
130
131 return cb->get();
132}
133
134nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
135 const nn::Request& request, nn::MeasureTiming measure,
136 const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800137 const nn::OptionalDuration& loopTimeoutDuration) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700138 // Ensure that request is ready for IPC.
139 std::optional<nn::Request> maybeRequestInShared;
140 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
141 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
142
143 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
Michael Butler7fd03c22020-12-06 21:50:59 -0800144 const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
Michael Butler4b276a72020-08-06 23:22:35 -0700145 const auto hidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline)));
146 const auto hidlLoopTimeoutDuration =
147 NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration)));
148
Michael Butler7fd03c22020-12-06 21:50:59 -0800149 auto result = kExecuteSynchronously
150 ? executeSynchronously(hidlRequest, hidlMeasure, hidlDeadline,
151 hidlLoopTimeoutDuration)
152 : executeAsynchronously(hidlRequest, hidlMeasure, hidlDeadline,
153 hidlLoopTimeoutDuration);
154 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler4b276a72020-08-06 23:22:35 -0700155
Michael Butler7fd03c22020-12-06 21:50:59 -0800156 NN_TRY(hal::utils::makeExecutionFailure(
157 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
Michael Butler4b276a72020-08-06 23:22:35 -0700158
Michael Butler7fd03c22020-12-06 21:50:59 -0800159 return std::make_pair(std::move(outputShapes), timing);
Michael Butler4b276a72020-08-06 23:22:35 -0700160}
161
162nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
163PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
164 nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
Michael Butler4024d8f2020-12-04 17:38:20 -0800165 const nn::OptionalDuration& loopTimeoutDuration,
166 const nn::OptionalDuration& timeoutDurationAfterFence) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700167 // Ensure that request is ready for IPC.
168 std::optional<nn::Request> maybeRequestInShared;
169 const nn::Request& requestInShared =
170 NN_TRY(hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared));
171
172 const auto hidlRequest = NN_TRY(convert(requestInShared));
Slava Shklyaev49817a02020-10-27 18:44:01 +0000173 const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor));
Michael Butler7fd03c22020-12-06 21:50:59 -0800174 const auto hidlMeasure = NN_TRY(convert(measure));
Michael Butler4b276a72020-08-06 23:22:35 -0700175 const auto hidlDeadline = NN_TRY(convert(deadline));
176 const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration));
177 const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence));
178
Michael Butler7fd03c22020-12-06 21:50:59 -0800179 auto cb = hal::utils::CallbackValue(fencedExecutionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700180
181 const auto ret = kPreparedModel->executeFenced(hidlRequest, hidlWaitFor, hidlMeasure,
182 hidlDeadline, hidlLoopTimeoutDuration,
183 hidlTimeoutDurationAfterFence, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800184 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800185 auto [syncFence, callback] = NN_TRY(cb.take());
Michael Butler4b276a72020-08-06 23:22:35 -0700186
187 // If executeFenced required the request memory to be moved into shared memory, block here until
188 // the fenced execution has completed and flush the memory back.
189 if (maybeRequestInShared.has_value()) {
190 const auto state = syncFence.syncWait({});
191 if (state != nn::SyncFence::FenceState::SIGNALED) {
192 return NN_ERROR() << "syncWait failed with " << state;
193 }
194 NN_TRY(hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared));
195 }
196
197 return std::make_pair(std::move(syncFence), std::move(callback));
198}
199
Michael Butler2a8b6792020-12-18 20:31:14 -0800200nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
201 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Not yet implemented";
202}
203
Michael Butler4b276a72020-08-06 23:22:35 -0700204std::any PreparedModel::getUnderlyingResource() const {
205 sp<V1_3::IPreparedModel> resource = kPreparedModel;
206 return resource;
207}
208
209} // namespace android::hardware::neuralnetworks::V1_3::utils