blob: 1ae19bc6ca1b6540d661e31456653426554737e9 [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>>
107ResilientPreparedModel::execute(const nn::Request& request, nn::MeasureTiming measure,
108 const nn::OptionalTimePoint& deadline,
Michael Butlerca114202020-12-04 17:38:20 -0800109 const nn::OptionalDuration& loopTimeoutDuration) const {
Michael Butler667dc2d2020-12-29 18:56:44 -0800110 const auto fn = [&request, measure, &deadline,
111 &loopTimeoutDuration](const nn::IPreparedModel& preparedModel) {
112 return preparedModel.execute(request, measure, deadline, loopTimeoutDuration);
113 };
114 return protect(*this, fn);
Michael Butler3670c382020-08-06 23:22:35 -0700115}
116
117nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butlerca114202020-12-04 17:38:20 -0800118ResilientPreparedModel::executeFenced(const nn::Request& request,
119 const std::vector<nn::SyncFence>& waitFor,
120 nn::MeasureTiming measure,
121 const nn::OptionalTimePoint& deadline,
122 const nn::OptionalDuration& loopTimeoutDuration,
123 const nn::OptionalDuration& timeoutDurationAfterFence) const {
Michael Butler667dc2d2020-12-29 18:56:44 -0800124 const auto fn = [&request, &waitFor, measure, &deadline, &loopTimeoutDuration,
125 &timeoutDurationAfterFence](const nn::IPreparedModel& preparedModel) {
126 return preparedModel.executeFenced(request, waitFor, measure, deadline, loopTimeoutDuration,
127 timeoutDurationAfterFence);
128 };
129 return protect(*this, fn);
Michael Butler3670c382020-08-06 23:22:35 -0700130}
131
Xusong Wang727a7b22021-03-03 16:20:37 -0800132nn::GeneralResult<nn::SharedExecution> ResilientPreparedModel::createReusableExecution(
133 const nn::Request& request, nn::MeasureTiming measure,
134 const nn::OptionalDuration& loopTimeoutDuration) const {
135#if 0
136 auto self = shared_from_this();
137 ResilientExecution::Factory makeExecution =
138 [preparedModel = std::move(self), request, measure, loopTimeoutDuration] {
139 return preparedModel->createReusableExecutionInternal(request, measure, loopTimeoutDuration);
140 };
141 return ResilientExecution::create(std::move(makeExecution));
142#else
143 return createReusableExecutionInternal(request, measure, loopTimeoutDuration);
144#endif
145}
146
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800147nn::GeneralResult<nn::SharedBurst> ResilientPreparedModel::configureExecutionBurst() const {
Michael Butler44f324f2020-12-18 20:53:55 -0800148#if 0
149 auto self = shared_from_this();
150 ResilientBurst::Factory makeBurst =
151 [preparedModel = std::move(self)]() -> nn::GeneralResult<nn::SharedBurst> {
152 return preparedModel->configureExecutionBurst();
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800153 };
Michael Butler44f324f2020-12-18 20:53:55 -0800154 return ResilientBurst::create(std::move(makeBurst));
155#else
156 return configureExecutionBurstInternal();
157#endif
Michael Butlerb6a7ed52020-12-18 20:31:14 -0800158}
159
Xusong Wang727a7b22021-03-03 16:20:37 -0800160nn::GeneralResult<nn::SharedExecution> ResilientPreparedModel::createReusableExecutionInternal(
161 const nn::Request& request, nn::MeasureTiming measure,
162 const nn::OptionalDuration& loopTimeoutDuration) const {
163 if (!isValidInternal()) {
164 return std::make_shared<const InvalidExecution>();
165 }
166 const auto fn = [&request, measure,
167 &loopTimeoutDuration](const nn::IPreparedModel& preparedModel) {
168 return preparedModel.createReusableExecution(request, measure, loopTimeoutDuration);
169 };
170 return protect(*this, fn);
171}
172
Michael Butler3670c382020-08-06 23:22:35 -0700173std::any ResilientPreparedModel::getUnderlyingResource() const {
174 return getPreparedModel()->getUnderlyingResource();
175}
176
Michael Butler44f324f2020-12-18 20:53:55 -0800177bool ResilientPreparedModel::isValidInternal() const {
178 return true;
179}
180
181nn::GeneralResult<nn::SharedBurst> ResilientPreparedModel::configureExecutionBurstInternal() const {
182 if (!isValidInternal()) {
183 return std::make_shared<const InvalidBurst>();
184 }
185 const auto fn = [](const nn::IPreparedModel& preparedModel) {
186 return preparedModel.configureExecutionBurst();
187 };
188 return protect(*this, fn);
189}
190
Michael Butler3670c382020-08-06 23:22:35 -0700191} // namespace android::hardware::neuralnetworks::utils