blob: b8055fc081072343d4f5490173203a331a07817c [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 Butler49d95e02021-10-15 18:52:52 -070023#include "HandleError.h"
Michael Butlere8645c32021-10-15 18:42:32 -070024#include "ProtectCallback.h"
Michael Butler4b276a72020-08-06 23:22:35 -070025#include "Utils.h"
26
27#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
28#include <android/hardware/neuralnetworks/1.0/types.h>
29#include <nnapi/IPreparedModel.h>
30#include <nnapi/Result.h>
31#include <nnapi/Types.h>
32#include <nnapi/hal/CommonUtils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070033
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*/,
Miao Wangb5c8a822021-10-26 20:03:05 +000062 const nn::OptionalDuration& /*loopTimeoutDuration*/,
63 const std::vector<nn::TokenValuePair>& /*hints*/,
64 const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070065 // Ensure that request is ready for IPC.
66 std::optional<nn::Request> maybeRequestInShared;
Xusong Wang5f6bedb2021-03-03 16:20:37 -080067 hal::utils::RequestRelocation relocation;
Michael Butlerff9a5a52021-10-15 16:23:20 -070068 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
69 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
70 &maybeRequestInShared, &relocation));
Michael Butler4b276a72020-08-06 23:22:35 -070071
Michael Butlerff9a5a52021-10-15 16:23:20 -070072 const auto hidlRequest = NN_TRY(convert(requestInShared));
Michael Butler4b276a72020-08-06 23:22:35 -070073
Xusong Wang5f6bedb2021-03-03 16:20:37 -080074 return executeInternal(hidlRequest, relocation);
75}
76
77nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
78PreparedModel::executeInternal(const V1_0::Request& request,
79 const hal::utils::RequestRelocation& relocation) const {
80 if (relocation.input) {
81 relocation.input->flush();
82 }
83
Michael Butler4b276a72020-08-06 23:22:35 -070084 const auto cb = sp<ExecutionCallback>::make();
85 const auto scoped = kDeathHandler.protectCallback(cb.get());
86
Xusong Wang5f6bedb2021-03-03 16:20:37 -080087 const auto ret = kPreparedModel->execute(request, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080088 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler49d95e02021-10-15 18:52:52 -070089 HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -070090
91 auto result = NN_TRY(cb->get());
Xusong Wang5f6bedb2021-03-03 16:20:37 -080092 if (relocation.output) {
93 relocation.output->flush();
94 }
Michael Butler4b276a72020-08-06 23:22:35 -070095 return result;
96}
97
98nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Miao Wangb5c8a822021-10-26 20:03:05 +000099PreparedModel::executeFenced(
100 const nn::Request& /*request*/, const std::vector<nn::SyncFence>& /*waitFor*/,
101 nn::MeasureTiming /*measure*/, const nn::OptionalTimePoint& /*deadline*/,
102 const nn::OptionalDuration& /*loopTimeoutDuration*/,
103 const nn::OptionalDuration& /*timeoutDurationAfterFence*/,
104 const std::vector<nn::TokenValuePair>& /*hints*/,
105 const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700106 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
107 << "IPreparedModel::executeFenced is not supported on 1.0 HAL service";
108}
109
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800110nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution(
111 const nn::Request& request, nn::MeasureTiming /*measure*/,
Miao Wangb5c8a822021-10-26 20:03:05 +0000112 const nn::OptionalDuration& /*loopTimeoutDuration*/,
113 const std::vector<nn::TokenValuePair>& /*hints*/,
114 const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const {
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800115 // Ensure that request is ready for IPC.
116 std::optional<nn::Request> maybeRequestInShared;
117 hal::utils::RequestRelocation relocation;
118 const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared(
Xusong Wange3d0dad2021-05-07 14:13:22 -0700119 &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding,
120 &maybeRequestInShared, &relocation));
Xusong Wang5f6bedb2021-03-03 16:20:37 -0800121
122 auto hidlRequest = NN_TRY(convert(requestInShared));
123 return Execution::create(shared_from_this(), std::move(hidlRequest), std::move(relocation));
124}
125
Michael Butler2a8b6792020-12-18 20:31:14 -0800126nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler95331512020-12-18 20:53:55 -0800127 return Burst::create(shared_from_this());
Michael Butler2a8b6792020-12-18 20:31:14 -0800128}
129
Michael Butler4b276a72020-08-06 23:22:35 -0700130std::any PreparedModel::getUnderlyingResource() const {
131 sp<V1_0::IPreparedModel> resource = kPreparedModel;
132 return resource;
133}
134
135} // namespace android::hardware::neuralnetworks::V1_0::utils