blob: ad54c392828dcb8ac676648efe60c80acedcf4d7 [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>
30#include <nnapi/hal/1.0/Conversions.h>
31#include <nnapi/hal/CommonUtils.h>
32#include <nnapi/hal/HandleError.h>
33#include <nnapi/hal/ProtectCallback.h>
34
35#include <memory>
36#include <tuple>
37#include <utility>
38#include <vector>
39
Michael Butler7a655bb2020-12-13 23:06:06 -080040// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
41// lifetimes across processes and for protecting asynchronous calls across HIDL.
42
Michael Butler3670c382020-08-06 23:22:35 -070043namespace android::hardware::neuralnetworks::V1_2::utils {
Michael Butler3670c382020-08-06 23:22:35 -070044
45nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
Michael Butler98ed9ba2020-12-06 21:50:59 -080046 sp<V1_2::IPreparedModel> preparedModel, bool executeSynchronously) {
Michael Butler3670c382020-08-06 23:22:35 -070047 if (preparedModel == nullptr) {
Michael Butler98ed9ba2020-12-06 21:50:59 -080048 return NN_ERROR() << "V1_2::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler3670c382020-08-06 23:22:35 -070049 }
50
51 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
Michael Butler98ed9ba2020-12-06 21:50:59 -080052 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously,
53 std::move(preparedModel), std::move(deathHandler));
Michael Butler3670c382020-08-06 23:22:35 -070054}
55
Michael Butler98ed9ba2020-12-06 21:50:59 -080056PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously,
57 sp<V1_2::IPreparedModel> preparedModel,
Michael Butler3670c382020-08-06 23:22:35 -070058 hal::utils::DeathHandler deathHandler)
Michael Butler98ed9ba2020-12-06 21:50:59 -080059 : kExecuteSynchronously(executeSynchronously),
60 kPreparedModel(std::move(preparedModel)),
61 kDeathHandler(std::move(deathHandler)) {}
Michael Butler3670c382020-08-06 23:22:35 -070062
63nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
64PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const {
Michael Butler98ed9ba2020-12-06 21:50:59 -080065 auto cb = hal::utils::CallbackValue(executionCallback);
Michael Butler3670c382020-08-06 23:22:35 -070066
67 const auto ret = kPreparedModel->executeSynchronously(request, measure, cb);
Michael Butler61f508e2020-11-22 20:25:34 -080068 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -070069
Michael Butler98ed9ba2020-12-06 21:50:59 -080070 return cb.take();
Michael Butler3670c382020-08-06 23:22:35 -070071}
72
73nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
74PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const {
75 const auto cb = sp<ExecutionCallback>::make();
76 const auto scoped = kDeathHandler.protectCallback(cb.get());
77
78 const auto ret = kPreparedModel->execute_1_2(request, measure, cb);
Michael Butler61f508e2020-11-22 20:25:34 -080079 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler98ed9ba2020-12-06 21:50:59 -080080 if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
81 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler3670c382020-08-06 23:22:35 -070082 }
83
84 return cb->get();
85}
86
87nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
88 const nn::Request& request, nn::MeasureTiming measure,
89 const nn::OptionalTimePoint& /*deadline*/,
Michael Butlerca114202020-12-04 17:38:20 -080090 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
Michael Butler3670c382020-08-06 23:22:35 -070091 // Ensure that request is ready for IPC.
92 std::optional<nn::Request> maybeRequestInShared;
93 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
94 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
95
Michael Butler98ed9ba2020-12-06 21:50:59 -080096 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
Michael Butler3670c382020-08-06 23:22:35 -070097 const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure)));
98
Michael Butler98ed9ba2020-12-06 21:50:59 -080099 auto result = kExecuteSynchronously ? executeSynchronously(hidlRequest, hidlMeasure)
100 : executeAsynchronously(hidlRequest, hidlMeasure);
101 auto [outputShapes, timing] = NN_TRY(std::move(result));
Michael Butler3670c382020-08-06 23:22:35 -0700102
Michael Butler98ed9ba2020-12-06 21:50:59 -0800103 NN_TRY(hal::utils::makeExecutionFailure(
104 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
Michael Butler3670c382020-08-06 23:22:35 -0700105
Michael Butler98ed9ba2020-12-06 21:50:59 -0800106 return std::make_pair(std::move(outputShapes), timing);
Michael Butler3670c382020-08-06 23:22:35 -0700107}
108
109nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butlerca114202020-12-04 17:38:20 -0800110PreparedModel::executeFenced(const nn::Request& /*request*/,
111 const std::vector<nn::SyncFence>& /*waitFor*/,
112 nn::MeasureTiming /*measure*/,
113 const nn::OptionalTimePoint& /*deadline*/,
114 const nn::OptionalDuration& /*loopTimeoutDuration*/,
115 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
Michael Butler3670c382020-08-06 23:22:35 -0700116 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
117 << "IPreparedModel::executeFenced is not supported on 1.2 HAL service";
118}
119
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800120nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const {
121 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Not yet implemented";
122}
123
Michael Butler3670c382020-08-06 23:22:35 -0700124std::any PreparedModel::getUnderlyingResource() const {
Michael Butler98ed9ba2020-12-06 21:50:59 -0800125 sp<V1_2::IPreparedModel> resource = kPreparedModel;
Michael Butler3670c382020-08-06 23:22:35 -0700126 return resource;
127}
128
129} // namespace android::hardware::neuralnetworks::V1_2::utils