1 /*
2  * Copyright (C) 2021 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 "ResilientExecution.h"
18 
19 #include "InvalidBurst.h"
20 #include "ResilientBurst.h"
21 
22 #include <android-base/logging.h>
23 #include <android-base/thread_annotations.h>
24 #include <nnapi/IExecution.h>
25 #include <nnapi/Result.h>
26 #include <nnapi/TypeUtils.h>
27 #include <nnapi/Types.h>
28 
29 #include <functional>
30 #include <memory>
31 #include <mutex>
32 #include <sstream>
33 #include <utility>
34 #include <vector>
35 
36 namespace android::hardware::neuralnetworks::utils {
37 namespace {
38 
39 template <typename FnType>
protect(const ResilientExecution & resilientExecution,const FnType & fn)40 auto protect(const ResilientExecution& resilientExecution, const FnType& fn)
41         -> decltype(fn(*resilientExecution.getExecution())) {
42     auto execution = resilientExecution.getExecution();
43     auto result = fn(*execution);
44 
45     // Immediately return if prepared model is not dead.
46     if (result.has_value() || result.error().code != nn::ErrorStatus::DEAD_OBJECT) {
47         return result;
48     }
49 
50     // Attempt recovery and return if it fails.
51     auto maybeExecution = resilientExecution.recover(execution.get());
52     if (!maybeExecution.has_value()) {
53         const auto& [message, code] = maybeExecution.error();
54         std::ostringstream oss;
55         oss << ", and failed to recover dead prepared model with error " << code << ": " << message;
56         result.error().message += oss.str();
57         return result;
58     }
59     execution = std::move(maybeExecution).value();
60 
61     return fn(*execution);
62 }
63 
64 }  // namespace
65 
create(Factory makeExecution)66 nn::GeneralResult<std::shared_ptr<const ResilientExecution>> ResilientExecution::create(
67         Factory makeExecution) {
68     if (makeExecution == nullptr) {
69         return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
70                << "utils::ResilientExecution::create must have non-empty makeExecution";
71     }
72     auto execution = NN_TRY(makeExecution());
73     CHECK(execution != nullptr);
74     return std::make_shared<ResilientExecution>(PrivateConstructorTag{}, std::move(makeExecution),
75                                                 std::move(execution));
76 }
77 
ResilientExecution(PrivateConstructorTag,Factory makeExecution,nn::SharedExecution execution)78 ResilientExecution::ResilientExecution(PrivateConstructorTag /*tag*/, Factory makeExecution,
79                                        nn::SharedExecution execution)
80     : kMakeExecution(std::move(makeExecution)), mExecution(std::move(execution)) {
81     CHECK(kMakeExecution != nullptr);
82     CHECK(mExecution != nullptr);
83 }
84 
getExecution() const85 nn::SharedExecution ResilientExecution::getExecution() const {
86     std::lock_guard guard(mMutex);
87     return mExecution;
88 }
89 
recover(const nn::IExecution * failingExecution) const90 nn::GeneralResult<nn::SharedExecution> ResilientExecution::recover(
91         const nn::IExecution* failingExecution) const {
92     std::lock_guard guard(mMutex);
93 
94     // Another caller updated the failing prepared model.
95     if (mExecution.get() != failingExecution) {
96         return mExecution;
97     }
98 
99     mExecution = NN_TRY(kMakeExecution());
100     return mExecution;
101 }
102 
103 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
compute(const nn::OptionalTimePoint & deadline) const104 ResilientExecution::compute(const nn::OptionalTimePoint& deadline) const {
105     const auto fn = [&deadline](const nn::IExecution& execution) {
106         return execution.compute(deadline);
107     };
108     return protect(*this, fn);
109 }
110 
111 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
computeFenced(const std::vector<nn::SyncFence> & waitFor,const nn::OptionalTimePoint & deadline,const nn::OptionalDuration & timeoutDurationAfterFence) const112 ResilientExecution::computeFenced(const std::vector<nn::SyncFence>& waitFor,
113                                   const nn::OptionalTimePoint& deadline,
114                                   const nn::OptionalDuration& timeoutDurationAfterFence) const {
115     const auto fn = [&waitFor, &deadline,
116                      &timeoutDurationAfterFence](const nn::IExecution& execution) {
117         return execution.computeFenced(waitFor, deadline, timeoutDurationAfterFence);
118     };
119     return protect(*this, fn);
120 }
121 
isValidInternal() const122 bool ResilientExecution::isValidInternal() const {
123     return true;
124 }
125 
126 }  // namespace android::hardware::neuralnetworks::utils
127