blob: b5843c0fd4a9d9d6348e128b4f5e15ff20d90ee6 [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 "ResilientPreparedModel.h"
18
Michael Butler44f324f2020-12-18 20:53:55 -080019#include "InvalidBurst.h"
Xusong Wang727a7b22021-03-03 16:20:37 -080020#include "InvalidExecution.h"
Michael Butler44f324f2020-12-18 20:53:55 -080021#include "ResilientBurst.h"
Xusong Wang727a7b22021-03-03 16:20:37 -080022#include "ResilientExecution.h"
Michael Butler44f324f2020-12-18 20:53:55 -080023
Michael Butler3670c382020-08-06 23:22:35 -070024#include <android-base/logging.h>
25#include <android-base/thread_annotations.h>
26#include <nnapi/IPreparedModel.h>
27#include <nnapi/Result.h>
Michael Butler667dc2d2020-12-29 18:56:44 -080028#include <nnapi/TypeUtils.h>
Michael Butler3670c382020-08-06 23:22:35 -070029#include <nnapi/Types.h>
30
31#include <functional>
32#include <memory>
33#include <mutex>
Michael Butler667dc2d2020-12-29 18:56:44 -080034#include <sstream>
Michael Butler3670c382020-08-06 23:22:35 -070035#include <utility>
36#include <vector>
37
38namespace android::hardware::neuralnetworks::utils {
Michael Butler667dc2d2020-12-29 18:56:44 -080039namespace {
40
41template <typename FnType>
42auto protect(const ResilientPreparedModel& resilientPreparedModel, const FnType& fn)
43 -> decltype(fn(*resilientPreparedModel.getPreparedModel())) {
44 auto preparedModel = resilientPreparedModel.getPreparedModel();
45 auto result = fn(*preparedModel);
46
47 // Immediately return if prepared model is not dead.
48 if (result.has_value() || result.error().code != nn::ErrorStatus::DEAD_OBJECT) {
49 return result;
50 }
51
52 // Attempt recovery and return if it fails.
53 auto maybePreparedModel = resilientPreparedModel.recover(preparedModel.get());
54 if (!maybePreparedModel.has_value()) {
55 const auto& [message, code] = maybePreparedModel.error();
56 std::ostringstream oss;
57 oss << ", and failed to recover dead prepared model with error " << code << ": " << message;
58 result.error().message += oss.str();
59 return result;
60 }
61 preparedModel = std::move(maybePreparedModel).value();
62
63 return fn(*preparedModel);
64}
65
66} // namespace
Michael Butler3670c382020-08-06 23:22:35 -070067
68nn::GeneralResult<std::shared_ptr<const ResilientPreparedModel>> ResilientPreparedModel::create(
69 Factory makePreparedModel) {
70 if (makePreparedModel == nullptr) {
71 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
72 << "utils::ResilientPreparedModel::create must have non-empty makePreparedModel";
73 }
Michael Butler11761e32020-12-15 15:20:26 -080074 auto preparedModel = NN_TRY(makePreparedModel());
Michael Butler3670c382020-08-06 23:22:35 -070075 CHECK(preparedModel != nullptr);
76 return std::make_shared<ResilientPreparedModel>(
77 PrivateConstructorTag{}, std::move(makePreparedModel), std::move(preparedModel));
78}
79
80ResilientPreparedModel::ResilientPreparedModel(PrivateConstructorTag /*tag*/,
81 Factory makePreparedModel,
82 nn::SharedPreparedModel preparedModel)
83 : kMakePreparedModel(std::move(makePreparedModel)), mPreparedModel(std::move(preparedModel)) {
84 CHECK(kMakePreparedModel != nullptr);
85 CHECK(mPreparedModel != nullptr);
86}
87
88nn::SharedPreparedModel ResilientPreparedModel::getPreparedModel() const {
89 std::lock_guard guard(mMutex);
90 return mPreparedModel;
91}
92
Michael Butler667dc2d2020-12-29 18:56:44 -080093nn::GeneralResult<nn::SharedPreparedModel> ResilientPreparedModel::recover(
94 const nn::IPreparedModel* failingPreparedModel) const {
Michael Butler3670c382020-08-06 23:22:35 -070095 std::lock_guard guard(mMutex);
Michael Butler667dc2d2020-12-29 18:56:44 -080096
97 // Another caller updated the failing prepared model.
98 if (mPreparedModel.get() != failingPreparedModel) {
99 return mPreparedModel;
100 }
101
102 mPreparedModel = NN_TRY(kMakePreparedModel());
Michael Butler3670c382020-08-06 23:22:35 -0700103 return mPreparedModel;
104}
105
106nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
Miao Wang0e671f32021-10-26 20:03:05 +0000107ResilientPreparedModel::execute(
108 const nn::Request& request, nn::MeasureTiming measure,
109 const nn::OptionalTimePoint& deadline, const nn::OptionalDuration& loopTimeoutDuration,
110 const std::vector<nn::TokenValuePair>& hints,
111 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const {
112 const auto fn = [&request, measure, &deadline, &loopTimeoutDuration, &hints,
113 &extensionNameToPrefix](const nn::IPreparedModel& preparedModel) {
114 return preparedModel.execute(request, measure, deadline, loopTimeoutDuration, hints,
115 extensionNameToPrefix);
Michael Butler667dc2d2020-12-29 18:56:44 -0800116 };
117 return protect(*this, fn);
Michael Butler3670c382020-08-06 23:22:35 -0700118}
119
120nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Miao Wang0e671f32021-10-26 20:03:05 +0000121ResilientPreparedModel::executeFenced(
122 const nn::Request& request, const std::vector<nn::SyncFence>& waitFor,
123 nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline,
124 const nn::OptionalDuration& loopTimeoutDuration,
125 const nn::OptionalDuration& timeoutDurationAfterFence,
126 const std::vector<nn::TokenValuePair>& hints,
127 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const {
Michael Butler667dc2d2020-12-29 18:56:44 -0800128 const auto fn = [&request, &waitFor, measure, &deadline, &loopTimeoutDuration,
Miao Wang0e671f32021-10-26 20:03:05 +0000129 &timeoutDurationAfterFence, &hints,
130 &extensionNameToPrefix](const nn::IPreparedModel& preparedModel) {
Michael Butler667dc2d2020-12-29 18:56:44 -0800131 return preparedModel.executeFenced(request, waitFor, measure, deadline, loopTimeoutDuration,
Miao Wang0e671f32021-10-26 20:03:05 +0000132 timeoutDurationAfterFence, hints, extensionNameToPrefix);
Michael Butler667dc2d2020-12-29 18:56:44 -0800133 };
134 return protect(*this, fn);
Michael Butler3670c382020-08-06 23:22:35 -0700135}
136
Xusong Wang727a7b22021-03-03 16:20:37 -0800137nn::GeneralResult<nn::SharedExecution> ResilientPreparedModel::createReusableExecution(
138 const nn::Request& request, nn::MeasureTiming measure,
Miao Wang0e671f32021-10-26 20:03:05 +0000139 const nn::OptionalDuration& loopTimeoutDuration,
140 const std::vector<nn::TokenValuePair>& hints,
141 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const {
Xusong Wang727a7b22021-03-03 16:20:37 -0800142#if 0
143 auto self = shared_from_this();
Miao Wang0e671f32021-10-26 20:03:05 +0000144 ResilientExecution::Factory makeExecution = [preparedModel = std::move(self), request, measure,
145 loopTimeoutDuration, hints,
146 extensionNameToPrefix] {
147 return preparedModel->createReusableExecutionInternal(request, measure, loopTimeoutDuration,
148 hints, extensionNameToPrefix);
Xusong Wang727a7b22021-03-03 16:20:37 -0800149 };
150 return ResilientExecution::create(std::move(makeExecution));
151#else
Miao Wang0e671f32021-10-26 20:03:05 +0000152 return createReusableExecutionInternal(request, measure, loopTimeoutDuration, hints,
153 extensionNameToPrefix);
Xusong Wang727a7b22021-03-03 16:20:37 -0800154#endif
155}
156
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800157nn::GeneralResult<nn::SharedBurst> ResilientPreparedModel::configureExecutionBurst() const {
Michael Butler44f324f2020-12-18 20:53:55 -0800158#if 0
159 auto self = shared_from_this();
160 ResilientBurst::Factory makeBurst =
161 [preparedModel = std::move(self)]() -> nn::GeneralResult<nn::SharedBurst> {
162 return preparedModel->configureExecutionBurst();
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800163 };
Michael Butler44f324f2020-12-18 20:53:55 -0800164 return ResilientBurst::create(std::move(makeBurst));
165#else
166 return configureExecutionBurstInternal();
167#endif
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800168}
169
Xusong Wang727a7b22021-03-03 16:20:37 -0800170nn::GeneralResult<nn::SharedExecution> ResilientPreparedModel::createReusableExecutionInternal(
171 const nn::Request& request, nn::MeasureTiming measure,
Miao Wang0e671f32021-10-26 20:03:05 +0000172 const nn::OptionalDuration& loopTimeoutDuration,
173 const std::vector<nn::TokenValuePair>& hints,
174 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const {
Xusong Wang727a7b22021-03-03 16:20:37 -0800175 if (!isValidInternal()) {
176 return std::make_shared<const InvalidExecution>();
177 }
Miao Wang0e671f32021-10-26 20:03:05 +0000178 const auto fn = [&request, measure, &loopTimeoutDuration, &hints,
179 &extensionNameToPrefix](const nn::IPreparedModel& preparedModel) {
180 return preparedModel.createReusableExecution(request, measure, loopTimeoutDuration, hints,
181 extensionNameToPrefix);
Xusong Wang727a7b22021-03-03 16:20:37 -0800182 };
183 return protect(*this, fn);
184}
185
Michael Butler3670c382020-08-06 23:22:35 -0700186std::any ResilientPreparedModel::getUnderlyingResource() const {
187 return getPreparedModel()->getUnderlyingResource();
188}
189
Michael Butler44f324f2020-12-18 20:53:55 -0800190bool ResilientPreparedModel::isValidInternal() const {
191 return true;
192}
193
194nn::GeneralResult<nn::SharedBurst> ResilientPreparedModel::configureExecutionBurstInternal() const {
195 if (!isValidInternal()) {
196 return std::make_shared<const InvalidBurst>();
197 }
198 const auto fn = [](const nn::IPreparedModel& preparedModel) {
199 return preparedModel.configureExecutionBurst();
200 };
201 return protect(*this, fn);
202}
203
Michael Butler3670c382020-08-06 23:22:35 -0700204} // namespace android::hardware::neuralnetworks::utils