blob: b8a5ae087cf7194347e956a19854965704b4a2ce [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 Butler76e491f2020-12-19 01:55:32 -080022#include "ExecutionBurstController.h"
23#include "ExecutionBurstUtils.h"
Michael Butler4b276a72020-08-06 23:22:35 -070024#include "Utils.h"
25
26#include <android/hardware/neuralnetworks/1.0/types.h>
27#include <android/hardware/neuralnetworks/1.1/types.h>
28#include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
29#include <android/hardware/neuralnetworks/1.2/types.h>
30#include <nnapi/IPreparedModel.h>
31#include <nnapi/Result.h>
32#include <nnapi/Types.h>
33#include <nnapi/hal/1.0/Conversions.h>
Michael Butler49d95e02021-10-15 18:52:52 -070034#include <nnapi/hal/1.0/HandleError.h>
Michael Butlere8645c32021-10-15 18:42:32 -070035#include <nnapi/hal/1.0/ProtectCallback.h>
Michael Butler4b276a72020-08-06 23:22:35 -070036#include <nnapi/hal/CommonUtils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070037
Michael Butler76e491f2020-12-19 01:55:32 -080038#include <chrono>
Michael Butler4b276a72020-08-06 23:22:35 -070039#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_2::utils {
Michael Butler4b276a72020-08-06 23:22:35 -070048
49nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
Michael Butler7fd03c22020-12-06 21:50:59 -080050 sp<V1_2::IPreparedModel> preparedModel, bool executeSynchronously) {
Michael Butler4b276a72020-08-06 23:22:35 -070051 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080052 return NN_ERROR() << "V1_2::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070053 }
54
55 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
Michael Butler7fd03c22020-12-06 21:50:59 -080056 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
57 std::move(preparedModel), std::move(deathHandler));
Michael Butler4b276a72020-08-06 23:22:35 -070058}
59
Michael Butler7fd03c22020-12-06 21:50:59 -080060PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
61 sp<V1_2::IPreparedModel> preparedModel,
Michael Butler4b276a72020-08-06 23:22:35 -070062 hal::utils::DeathHandler deathHandler)
Michael Butler7fd03c22020-12-06 21:50:59 -080063 : kExecuteSynchronously(executeSynchronously),
64 kPreparedModel(std::move(preparedModel)),
65 kDeathHandler(std::move(deathHandler)) {}
Michael Butler4b276a72020-08-06 23:22:35 -070066
67nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
68PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const {
Michael Butler7fd03c22020-12-06 21:50:59 -080069 auto cb = hal::utils::CallbackValue(executionCallback);
Michael Butler4b276a72020-08-06 23:22:35 -070070
71 const auto ret = kPreparedModel->executeSynchronously(request, measure, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080072 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070073
Michael Butler7fd03c22020-12-06 21:50:59 -080074 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -070075}
76
77nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
78PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const {
79 const auto cb = sp<ExecutionCallback>::make();
80 const auto scoped = kDeathHandler.protectCallback(cb.get());
81
82 const auto ret = kPreparedModel->execute_1_2(request, measure, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080083 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -080084 if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
Michael Butler49d95e02021-10-15 18:52:52 -070085 HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -070086 }
87
88 return cb->get();
89}
90
91nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
92 const nn::Request& request, nn::MeasureTiming measure,
93 const nn::OptionalTimePoint& /*deadline*/,
Michael Butler4024d8f2020-12-04 17:38:20 -080094 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070095 // Ensure that request is ready for IPC.
96 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang5f6bedb2021-03-03 16:20:37 -080097 hal::utils::RequestRelocation relocation;
Michael Butlerff9a5a52021-10-15 16:23:20 -070098 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
99 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
100 &maybeRequestInShared, &relocation));
Michael Butler4b276a72020-08-06 23:22:35 -0700101
Michael Butlerff9a5a52021-10-15 16:23:20 -0700102 const auto hidlRequest = NN_TRY(convert(requestInShared));
103 const auto hidlMeasure = NN_TRY(convert(measure));
Michael Butler4b276a72020-08-06 23:22:35 -0700104
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800105 return executeInternal(hidlRequest, hidlMeasure, relocation);
106}
107
108nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
109PreparedModel::executeInternal(const V1_0::Request& request, MeasureTiming measure,
110 const hal::utils::RequestRelocation& relocation) const {
111 if (relocation.input) {
112 relocation.input->flush();
113 }
114
115 auto result = kExecuteSynchronously ? executeSynchronously(request, measure)
116 : executeAsynchronously(request, measure);
Michael Butler7fd03c22020-12-06 21:50:59 -0800117 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler4b276a72020-08-06 23:22:35 -0700118
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800119 if (relocation.output) {
120 relocation.output->flush();
121 }
Michael Butler7fd03c22020-12-06 21:50:59 -0800122 return std::make_pair(std::move(outputShapes), timing);
Michael Butler4b276a72020-08-06 23:22:35 -0700123}
124
125nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butler4024d8f2020-12-04 17:38:20 -0800126PreparedModel::executeFenced(const nn::Request& /*request*/,
127 const std::vector<nn::SyncFence>& /*waitFor*/,
128 nn::MeasureTiming /*measure*/,
129 const nn::OptionalTimePoint& /*deadline*/,
130 const nn::OptionalDuration& /*loopTimeoutDuration*/,
131 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700132 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
133 << "IPreparedModel::executeFenced is not supported on 1.2 HAL service";
134}
135
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800136nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution(
137 const nn::Request& request, nn::MeasureTiming measure,
138 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
139 // Ensure that request is ready for IPC.
140 std::optional<nn::Request> maybeRequestInShared;
141 hal::utils::RequestRelocation relocation;
142 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
Xusong Wange3d0dad2021-05-07 14:13:22 -0700143 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
144 &maybeRequestInShared, &relocation));
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800145
146 auto hidlRequest = NN_TRY(convert(requestInShared));
147 auto hidlMeasure = NN_TRY(convert(measure));
148 return Execution::create(shared_from_this(), std::move(hidlRequest), std::move(relocation),
149 hidlMeasure);
150}
151
Michael Butler2a8b6792020-12-18 20:31:14 -0800152nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler76e491f2020-12-19 01:55:32 -0800153 auto self = shared_from_this();
Michael Butler8414a6e2021-03-10 18:41:05 -0800154 auto fallback = [preparedModel = std::move(self)](
155 const nn::Request& request, nn::MeasureTiming measure,
156 const nn::OptionalTimePoint& deadline,
157 const nn::OptionalDuration& loopTimeoutDuration)
Michael Butler76e491f2020-12-19 01:55:32 -0800158 -> nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> {
Michael Butler8414a6e2021-03-10 18:41:05 -0800159 return preparedModel->execute(request, measure, deadline, loopTimeoutDuration);
Michael Butler76e491f2020-12-19 01:55:32 -0800160 };
161 const auto pollingTimeWindow = getBurstControllerPollingTimeWindow();
Xusong Wangb2e80852021-03-23 15:07:10 -0700162 return ExecutionBurstController::create(shared_from_this(), kPreparedModel, pollingTimeWindow);
Michael Butler2a8b6792020-12-18 20:31:14 -0800163}
164
Michael Butler4b276a72020-08-06 23:22:35 -0700165std::any PreparedModel::getUnderlyingResource() const {
Michael Butler7fd03c22020-12-06 21:50:59 -0800166 sp<V1_2::IPreparedModel> resource = kPreparedModel;
Michael Butler4b276a72020-08-06 23:22:35 -0700167 return resource;
168}
169
170} // namespace android::hardware::neuralnetworks::V1_2::utils