1 /*
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 
19 #include "InvalidBurst.h"
20 #include "InvalidExecution.h"
21 #include "ResilientBurst.h"
22 #include "ResilientExecution.h"
23 
24 #include <android-base/logging.h>
25 #include <android-base/thread_annotations.h>
26 #include <nnapi/IPreparedModel.h>
27 #include <nnapi/Result.h>
28 #include <nnapi/TypeUtils.h>
29 #include <nnapi/Types.h>
30 
31 #include <functional>
32 #include <memory>
33 #include <mutex>
34 #include <sstream>
35 #include <utility>
36 #include <vector>
37 
38 namespace android::hardware::neuralnetworks::utils {
39 namespace {
40 
41 template <typename FnType>
protect(const ResilientPreparedModel & resilientPreparedModel,const FnType & fn)42 auto 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
67 
create(Factory makePreparedModel)68 nn::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     }
74     auto preparedModel = NN_TRY(makePreparedModel());
75     CHECK(preparedModel != nullptr);
76     return std::make_shared<ResilientPreparedModel>(
77             PrivateConstructorTag{}, std::move(makePreparedModel), std::move(preparedModel));
78 }
79 
ResilientPreparedModel(PrivateConstructorTag,Factory makePreparedModel,nn::SharedPreparedModel preparedModel)80 ResilientPreparedModel::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 
getPreparedModel() const88 nn::SharedPreparedModel ResilientPreparedModel::getPreparedModel() const {
89     std::lock_guard guard(mMutex);
90     return mPreparedModel;
91 }
92 
recover(const nn::IPreparedModel * failingPreparedModel) const93 nn::GeneralResult<nn::SharedPreparedModel> ResilientPreparedModel::recover(
94         const nn::IPreparedModel* failingPreparedModel) const {
95     std::lock_guard guard(mMutex);
96 
97     // Another caller updated the failing prepared model.
98     if (mPreparedModel.get() != failingPreparedModel) {
99         return mPreparedModel;
100     }
101 
102     mPreparedModel = NN_TRY(kMakePreparedModel());
103     return mPreparedModel;
104 }
105 
106 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
execute(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & loopTimeoutDuration) const107 ResilientPreparedModel::execute(const nn::Request& request, nn::MeasureTiming measure,
108                                 const nn::OptionalTimePoint& deadline,
109                                 const nn::OptionalDuration& loopTimeoutDuration) const {
110     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);
115 }
116 
117 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
executeFenced(const nn::Request & request,const std::vector<nn::SyncFence> & waitFor,nn::MeasureTiming measure,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & loopTimeoutDuration,const nn::OptionalDuration & timeoutDurationAfterFence) const118 ResilientPreparedModel::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 {
124     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);
130 }
131 
createReusableExecution(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalDuration & loopTimeoutDuration) const132 nn::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 
configureExecutionBurst() const147 nn::GeneralResult<nn::SharedBurst> ResilientPreparedModel::configureExecutionBurst() const {
148 #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();
153     };
154     return ResilientBurst::create(std::move(makeBurst));
155 #else
156     return configureExecutionBurstInternal();
157 #endif
158 }
159 
createReusableExecutionInternal(const nn::Request & request,nn::MeasureTiming measure,const nn::OptionalDuration & loopTimeoutDuration) const160 nn::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 
getUnderlyingResource() const173 std::any ResilientPreparedModel::getUnderlyingResource() const {
174     return getPreparedModel()->getUnderlyingResource();
175 }
176 
isValidInternal() const177 bool ResilientPreparedModel::isValidInternal() const {
178     return true;
179 }
180 
configureExecutionBurstInternal() const181 nn::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 
191 }  // namespace android::hardware::neuralnetworks::utils
192