blob: 71a4ea872bbd8cec3a8bef813d14663c4ea1fdee [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"
Michael Butler76e491f2020-12-19 01:55:32 -080021#include "ExecutionBurstController.h"
22#include "ExecutionBurstUtils.h"
Michael Butler4b276a72020-08-06 23:22:35 -070023#include "Utils.h"
24
25#include <android/hardware/neuralnetworks/1.0/types.h>
26#include <android/hardware/neuralnetworks/1.1/types.h>
27#include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
28#include <android/hardware/neuralnetworks/1.2/types.h>
29#include <nnapi/IPreparedModel.h>
30#include <nnapi/Result.h>
31#include <nnapi/Types.h>
32#include <nnapi/hal/1.0/Conversions.h>
33#include <nnapi/hal/CommonUtils.h>
34#include <nnapi/hal/HandleError.h>
35#include <nnapi/hal/ProtectCallback.h>
36
Michael Butler76e491f2020-12-19 01:55:32 -080037#include <chrono>
Michael Butler4b276a72020-08-06 23:22:35 -070038#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_2::utils {
Michael Butler4b276a72020-08-06 23:22:35 -070047
48nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
Michael Butler7fd03c22020-12-06 21:50:59 -080049 sp<V1_2::IPreparedModel> preparedModel, bool executeSynchronously) {
Michael Butler4b276a72020-08-06 23:22:35 -070050 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080051 return NN_ERROR() << "V1_2::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070052 }
53
54 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
Michael Butler7fd03c22020-12-06 21:50:59 -080055 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
56 std::move(preparedModel), std::move(deathHandler));
Michael Butler4b276a72020-08-06 23:22:35 -070057}
58
Michael Butler7fd03c22020-12-06 21:50:59 -080059PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
60 sp<V1_2::IPreparedModel> preparedModel,
Michael Butler4b276a72020-08-06 23:22:35 -070061 hal::utils::DeathHandler deathHandler)
Michael Butler7fd03c22020-12-06 21:50:59 -080062 : kExecuteSynchronously(executeSynchronously),
63 kPreparedModel(std::move(preparedModel)),
64 kDeathHandler(std::move(deathHandler)) {}
Michael Butler4b276a72020-08-06 23:22:35 -070065
66nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
67PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const {
Michael Butler7fd03c22020-12-06 21:50:59 -080068 auto cb = hal::utils::CallbackValue(executionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -070069
70 const auto ret = kPreparedModel->executeSynchronously(request, measure, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080071 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070072
Michael Butler7fd03c22020-12-06 21:50:59 -080073 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -070074}
75
76nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
77PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const {
78 const auto cb = sp<ExecutionCallback>::make();
79 const auto scoped = kDeathHandler.protectCallback(cb.get());
80
81 const auto ret = kPreparedModel->execute_1_2(request, measure, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080082 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -080083 if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
84 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -070085 }
86
87 return cb->get();
88}
89
90nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
91 const nn::Request& request, nn::MeasureTiming measure,
92 const nn::OptionalTimePoint& /*deadline*/,
Michael Butler4024d8f2020-12-04 17:38:20 -080093 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070094 // Ensure that request is ready for IPC.
95 std::optional<nn::Request> maybeRequestInShared;
96 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
97 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
98
Michael Butler7fd03c22020-12-06 21:50:59 -080099 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
Michael Butler4b276a72020-08-06 23:22:35 -0700100 const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
101
Michael Butler7fd03c22020-12-06 21:50:59 -0800102 auto result = kExecuteSynchronously ? executeSynchronously(hidlRequest, hidlMeasure)
103 : executeAsynchronously(hidlRequest, hidlMeasure);
104 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler4b276a72020-08-06 23:22:35 -0700105
Michael Butler7fd03c22020-12-06 21:50:59 -0800106 NN_TRY(hal::utils::makeExecutionFailure(
107 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
Michael Butler4b276a72020-08-06 23:22:35 -0700108
Michael Butler7fd03c22020-12-06 21:50:59 -0800109 return std::make_pair(std::move(outputShapes), timing);
Michael Butler4b276a72020-08-06 23:22:35 -0700110}
111
112nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butler4024d8f2020-12-04 17:38:20 -0800113PreparedModel::executeFenced(const nn::Request& /*request*/,
114 const std::vector<nn::SyncFence>& /*waitFor*/,
115 nn::MeasureTiming /*measure*/,
116 const nn::OptionalTimePoint& /*deadline*/,
117 const nn::OptionalDuration& /*loopTimeoutDuration*/,
118 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700119 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
120 << "IPreparedModel::executeFenced is not supported on 1.2 HAL service";
121}
122
Michael Butler2a8b6792020-12-18 20:31:14 -0800123nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler76e491f2020-12-19 01:55:32 -0800124 auto self = shared_from_this();
125 auto fallback = [preparedModel = std::move(self)](const nn::Request& request,
126 nn::MeasureTiming measure)
127 -> nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> {
128 return preparedModel->execute(request, measure, {}, {});
129 };
130 const auto pollingTimeWindow = getBurstControllerPollingTimeWindow();
131 return ExecutionBurstController::create(kPreparedModel, std::move(fallback), pollingTimeWindow);
Michael Butler2a8b6792020-12-18 20:31:14 -0800132}
133
Michael Butler4b276a72020-08-06 23:22:35 -0700134std::any PreparedModel::getUnderlyingResource() const {
Michael Butler7fd03c22020-12-06 21:50:59 -0800135 sp<V1_2::IPreparedModel> resource = kPreparedModel;
Michael Butler4b276a72020-08-06 23:22:35 -0700136 return resource;
137}
138
139} // namespace android::hardware::neuralnetworks::V1_2::utils