blob: 7987ab4d7f0cfd77f2cf110af5a167db73ba0bba [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
Michael Butler95331512020-12-18 20:53:55 -080019#include "Burst.h"
Michael Butler4b276a72020-08-06 23:22:35 -070020#include "Callbacks.h"
21#include "Conversions.h"
Xusong Wang5f6bedb2021-03-03 16:20:37 -080022#include "Execution.h"
Michael Butler4b276a72020-08-06 23:22:35 -070023#include "Utils.h"
24
25#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
26#include <android/hardware/neuralnetworks/1.0/types.h>
27#include <nnapi/IPreparedModel.h>
28#include <nnapi/Result.h>
29#include <nnapi/Types.h>
30#include <nnapi/hal/CommonUtils.h>
31#include <nnapi/hal/HandleError.h>
32#include <nnapi/hal/ProtectCallback.h>
33
34#include <memory>
35#include <tuple>
36#include <utility>
37#include <vector>
38
Michael Butleraad934b2020-12-13 23:06:06 -080039// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
40// lifetimes across processes and for protecting asynchronous calls across HIDL.
41
Michael Butler4b276a72020-08-06 23:22:35 -070042namespace android::hardware::neuralnetworks::V1_0::utils {
43
44nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
45 sp<V1_0::IPreparedModel> preparedModel) {
46 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080047 return NN_ERROR() << "V1_0::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070048 }
49
50 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
51 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel),
52 std::move(deathHandler));
53}
54
55PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, sp<V1_0::IPreparedModel> preparedModel,
56 hal::utils::DeathHandler deathHandler)
57 : kPreparedModel(std::move(preparedModel)), kDeathHandler(std::move(deathHandler)) {}
58
59nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
60 const nn::Request& request, nn::MeasureTiming /*measure*/,
61 const nn::OptionalTimePoint& /*deadline*/,
Michael Butler4024d8f2020-12-04 17:38:20 -080062 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070063 // Ensure that request is ready for IPC.
64 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang5f6bedb2021-03-03 16:20:37 -080065 hal::utils::RequestRelocation relocation;
66 const nn::Request& requestInShared =
67 NN_TRY(hal::utils::makeExecutionFailure(hal::utils::convertRequestFromPointerToShared(
68 &request, &maybeRequestInShared, &relocation)));
Michael Butler4b276a72020-08-06 23:22:35 -070069
70 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
71
Xusong Wang5f6bedb2021-03-03 16:20:37 -080072 return executeInternal(hidlRequest, relocation);
73}
74
75nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
76PreparedModel::executeInternal(const V1_0::Request& request,
77 const hal::utils::RequestRelocation& relocation) const {
78 if (relocation.input) {
79 relocation.input->flush();
80 }
81
Michael Butler4b276a72020-08-06 23:22:35 -070082 const auto cb = sp<ExecutionCallback>::make();
83 const auto scoped = kDeathHandler.protectCallback(cb.get());
84
Xusong Wang5f6bedb2021-03-03 16:20:37 -080085 const auto ret = kPreparedModel->execute(request, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080086 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -080087 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -070088
89 auto result = NN_TRY(cb->get());
Xusong Wang5f6bedb2021-03-03 16:20:37 -080090 if (relocation.output) {
91 relocation.output->flush();
92 }
Michael Butler4b276a72020-08-06 23:22:35 -070093 return result;
94}
95
96nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butler4024d8f2020-12-04 17:38:20 -080097PreparedModel::executeFenced(const nn::Request& /*request*/,
98 const std::vector<nn::SyncFence>& /*waitFor*/,
99 nn::MeasureTiming /*measure*/,
100 const nn::OptionalTimePoint& /*deadline*/,
101 const nn::OptionalDuration& /*loopTimeoutDuration*/,
102 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700103 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
104 << "IPreparedModel::executeFenced is not supported on 1.0 HAL service";
105}
106
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800107nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution(
108 const nn::Request& request, nn::MeasureTiming /*measure*/,
109 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
110 // Ensure that request is ready for IPC.
111 std::optional<nn::Request> maybeRequestInShared;
112 hal::utils::RequestRelocation relocation;
113 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
114 &request, &maybeRequestInShared, &relocation));
115
116 auto hidlRequest = NN_TRY(convert(requestInShared));
117 return Execution::create(shared_from_this(), std::move(hidlRequest), std::move(relocation));
118}
119
Michael Butler2a8b6792020-12-18 20:31:14 -0800120nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler95331512020-12-18 20:53:55 -0800121 return Burst::create(shared_from_this());
Michael Butler2a8b6792020-12-18 20:31:14 -0800122}
123
Michael Butler4b276a72020-08-06 23:22:35 -0700124std::any PreparedModel::getUnderlyingResource() const {
125 sp<V1_0::IPreparedModel> resource = kPreparedModel;
126 return resource;
127}
128
129} // namespace android::hardware::neuralnetworks::V1_0::utils