blob: 6841c5e0074f3b4ccfdd70bcc7fee99347ec97bc [file] [log] [blame]
Michael Butler3670c382020-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/types.h>
24#include <android/hardware/neuralnetworks/1.1/types.h>
25#include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
26#include <android/hardware/neuralnetworks/1.2/types.h>
27#include <nnapi/IPreparedModel.h>
28#include <nnapi/Result.h>
29#include <nnapi/Types.h>
Michael Butler44f324f2020-12-18 20:53:55 -080030#include <nnapi/hal/1.0/Burst.h>
Michael Butler3670c382020-08-06 23:22:35 -070031#include <nnapi/hal/1.0/Conversions.h>
32#include <nnapi/hal/CommonUtils.h>
33#include <nnapi/hal/HandleError.h>
34#include <nnapi/hal/ProtectCallback.h>
35
36#include <memory>
37#include <tuple>
38#include <utility>
39#include <vector>
40
Michael Butler7a655bb2020-12-13 23:06:06 -080041// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
42// lifetimes across processes and for protecting asynchronous calls across HIDL.
43
Michael Butler3670c382020-08-06 23:22:35 -070044namespace android::hardware::neuralnetworks::V1_2::utils {
Michael Butler3670c382020-08-06 23:22:35 -070045
46nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
Michael Butler98ed9ba2020-12-06 21:50:59 -080047 sp<V1_2::IPreparedModel> preparedModel, bool executeSynchronously) {
Michael Butler3670c382020-08-06 23:22:35 -070048 if (preparedModel == nullptr) {
Michael Butler98ed9ba2020-12-06 21:50:59 -080049 return NN_ERROR() << "V1_2::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler3670c382020-08-06 23:22:35 -070050 }
51
52 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
Michael Butler98ed9ba2020-12-06 21:50:59 -080053 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
54 std::move(preparedModel), std::move(deathHandler));
Michael Butler3670c382020-08-06 23:22:35 -070055}
56
Michael Butler98ed9ba2020-12-06 21:50:59 -080057PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
58 sp<V1_2::IPreparedModel> preparedModel,
Michael Butler3670c382020-08-06 23:22:35 -070059 hal::utils::DeathHandler deathHandler)
Michael Butler98ed9ba2020-12-06 21:50:59 -080060 : kExecuteSynchronously(executeSynchronously),
61 kPreparedModel(std::move(preparedModel)),
62 kDeathHandler(std::move(deathHandler)) {}
Michael Butler3670c382020-08-06 23:22:35 -070063
64nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
65PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const {
Michael Butler98ed9ba2020-12-06 21:50:59 -080066 auto cb = hal::utils::CallbackValue(executionCallback);
Michael Butler3670c382020-08-06 23:22:35 -070067
68 const auto ret = kPreparedModel->executeSynchronously(request, measure, cb);
Michael Butler61f508e2020-11-22 20:25:34 -080069 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -070070
Michael Butler98ed9ba2020-12-06 21:50:59 -080071 return cb.take();
Michael Butler3670c382020-08-06 23:22:35 -070072}
73
74nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
75PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const {
76 const auto cb = sp<ExecutionCallback>::make();
77 const auto scoped = kDeathHandler.protectCallback(cb.get());
78
79 const auto ret = kPreparedModel->execute_1_2(request, measure, cb);
Michael Butler61f508e2020-11-22 20:25:34 -080080 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler98ed9ba2020-12-06 21:50:59 -080081 if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
82 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler3670c382020-08-06 23:22:35 -070083 }
84
85 return cb->get();
86}
87
88nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
89 const nn::Request& request, nn::MeasureTiming measure,
90 const nn::OptionalTimePoint& /*deadline*/,
Michael Butlerca114202020-12-04 17:38:20 -080091 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
Michael Butler3670c382020-08-06 23:22:35 -070092 // Ensure that request is ready for IPC.
93 std::optional<nn::Request> maybeRequestInShared;
94 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
95 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
96
Michael Butler98ed9ba2020-12-06 21:50:59 -080097 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
Michael Butler3670c382020-08-06 23:22:35 -070098 const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
99
Michael Butler98ed9ba2020-12-06 21:50:59 -0800100 auto result = kExecuteSynchronously ? executeSynchronously(hidlRequest, hidlMeasure)
101 : executeAsynchronously(hidlRequest, hidlMeasure);
102 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler3670c382020-08-06 23:22:35 -0700103
Michael Butler98ed9ba2020-12-06 21:50:59 -0800104 NN_TRY(hal::utils::makeExecutionFailure(
105 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
Michael Butler3670c382020-08-06 23:22:35 -0700106
Michael Butler98ed9ba2020-12-06 21:50:59 -0800107 return std::make_pair(std::move(outputShapes), timing);
Michael Butler3670c382020-08-06 23:22:35 -0700108}
109
110nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butlerca114202020-12-04 17:38:20 -0800111PreparedModel::executeFenced(const nn::Request& /*request*/,
112 const std::vector<nn::SyncFence>& /*waitFor*/,
113 nn::MeasureTiming /*measure*/,
114 const nn::OptionalTimePoint& /*deadline*/,
115 const nn::OptionalDuration& /*loopTimeoutDuration*/,
116 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
Michael Butler3670c382020-08-06 23:22:35 -0700117 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
118 << "IPreparedModel::executeFenced is not supported on 1.2 HAL service";
119}
120
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800121nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
Michael Butler44f324f2020-12-18 20:53:55 -0800122 return V1_0::utils::Burst::create(shared_from_this());
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800123}
124
Michael Butler3670c382020-08-06 23:22:35 -0700125std::any PreparedModel::getUnderlyingResource() const {
Michael Butler98ed9ba2020-12-06 21:50:59 -0800126 sp<V1_2::IPreparedModel> resource = kPreparedModel;
Michael Butler3670c382020-08-06 23:22:35 -0700127 return resource;
128}
129
130} // namespace android::hardware::neuralnetworks::V1_2::utils