blob: 46dd3f8254ebde9f929882f84a932a33d0bde670 [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"
21#include "Utils.h"
22
23#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
24#include <android/hardware/neuralnetworks/1.0/types.h>
25#include <nnapi/IPreparedModel.h>
26#include <nnapi/Result.h>
27#include <nnapi/Types.h>
28#include <nnapi/hal/CommonUtils.h>
29#include <nnapi/hal/HandleError.h>
30#include <nnapi/hal/ProtectCallback.h>
31
32#include <memory>
33#include <tuple>
34#include <utility>
35#include <vector>
36
37namespace android::hardware::neuralnetworks::V1_0::utils {
38
39nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
40 sp<V1_0::IPreparedModel> preparedModel) {
41 if (preparedModel == nullptr) {
42 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
43 << "V1_0::utils::PreparedModel::create must have non-null preparedModel";
44 }
45
46 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
47 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel),
48 std::move(deathHandler));
49}
50
51PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, sp<V1_0::IPreparedModel> preparedModel,
52 hal::utils::DeathHandler deathHandler)
53 : kPreparedModel(std::move(preparedModel)), kDeathHandler(std::move(deathHandler)) {}
54
55nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
56 const nn::Request& request, nn::MeasureTiming /*measure*/,
57 const nn::OptionalTimePoint& /*deadline*/,
58 const nn::OptionalTimeoutDuration& /*loopTimeoutDuration*/) const {
59 // Ensure that request is ready for IPC.
60 std::optional<nn::Request> maybeRequestInShared;
61 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
62 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
63
64 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
65
66 const auto cb = sp<ExecutionCallback>::make();
67 const auto scoped = kDeathHandler.protectCallback(cb.get());
68
69 const auto ret = kPreparedModel->execute(hidlRequest, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080070 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070071 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080072 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070073 return NN_ERROR(canonical) << "execute failed with " << toString(status);
74 }
75
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>>
84PreparedModel::executeFenced(
85 const nn::Request& /*request*/, const std::vector<nn::SyncFence>& /*waitFor*/,
86 nn::MeasureTiming /*measure*/, const nn::OptionalTimePoint& /*deadline*/,
87 const nn::OptionalTimeoutDuration& /*loopTimeoutDuration*/,
88 const nn::OptionalTimeoutDuration& /*timeoutDurationAfterFence*/) const {
89 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
90 << "IPreparedModel::executeFenced is not supported on 1.0 HAL service";
91}
92
93std::any PreparedModel::getUnderlyingResource() const {
94 sp<V1_0::IPreparedModel> resource = kPreparedModel;
95 return resource;
96}
97
98} // namespace android::hardware::neuralnetworks::V1_0::utils