blob: 858571d401cb524ba392dd351114fe8f8da9c97b [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"
22#include "Utils.h"
23
24#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
25#include <android/hardware/neuralnetworks/1.0/types.h>
26#include <nnapi/IPreparedModel.h>
27#include <nnapi/Result.h>
28#include <nnapi/Types.h>
29#include <nnapi/hal/CommonUtils.h>
30#include <nnapi/hal/HandleError.h>
31#include <nnapi/hal/ProtectCallback.h>
32
33#include <memory>
34#include <tuple>
35#include <utility>
36#include <vector>
37
Michael Butleraad934b2020-12-13 23:06:06 -080038// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
39// lifetimes across processes and for protecting asynchronous calls across HIDL.
40
Michael Butler4b276a72020-08-06 23:22:35 -070041namespace android::hardware::neuralnetworks::V1_0::utils {
42
43nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
44 sp<V1_0::IPreparedModel> preparedModel) {
45 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080046 return NN_ERROR() << "V1_0::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070047 }
48
49 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
50 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel),
51 std::move(deathHandler));
52}
53
54PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, sp<V1_0::IPreparedModel> preparedModel,
55 hal::utils::DeathHandler deathHandler)
56 : kPreparedModel(std::move(preparedModel)), kDeathHandler(std::move(deathHandler)) {}
57
58nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
59 const nn::Request& request, nn::MeasureTiming /*measure*/,
60 const nn::OptionalTimePoint& /*deadline*/,
Michael Butler4024d8f2020-12-04 17:38:20 -080061 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070062 // Ensure that request is ready for IPC.
63 std::optional<nn::Request> maybeRequestInShared;
64 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
65 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
66
67 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
68
69 const auto cb = sp<ExecutionCallback>::make();
70 const auto scoped = kDeathHandler.protectCallback(cb.get());
71
72 const auto ret = kPreparedModel->execute(hidlRequest, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080073 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -080074 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -070075
76 auto result = NN_TRY(cb->get());
77 NN_TRY(hal::utils::makeExecutionFailure(
78 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
79
80 return result;
81}
82
83nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butler4024d8f2020-12-04 17:38:20 -080084PreparedModel::executeFenced(const nn::Request& /*request*/,
85 const std::vector<nn::SyncFence>& /*waitFor*/,
86 nn::MeasureTiming /*measure*/,
87 const nn::OptionalTimePoint& /*deadline*/,
88 const nn::OptionalDuration& /*loopTimeoutDuration*/,
89 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070090 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
91 << "IPreparedModel::executeFenced is not supported on 1.0 HAL service";
92}
93
Michael Butler2a8b6792020-12-18 20:31:14 -080094nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler95331512020-12-18 20:53:55 -080095 return Burst::create(shared_from_this());
Michael Butler2a8b6792020-12-18 20:31:14 -080096}
97
Michael Butler4b276a72020-08-06 23:22:35 -070098std::any PreparedModel::getUnderlyingResource() const {
99 sp<V1_0::IPreparedModel> resource = kPreparedModel;
100 return resource;
101}
102
103} // namespace android::hardware::neuralnetworks::V1_0::utils