Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 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 | |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 19 | #include "InvalidBurst.h" |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 20 | #include "InvalidExecution.h" |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 21 | #include "ResilientBurst.h" |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 22 | #include "ResilientExecution.h" |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 23 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | #include <android-base/thread_annotations.h> |
| 26 | #include <nnapi/IPreparedModel.h> |
| 27 | #include <nnapi/Result.h> |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 28 | #include <nnapi/TypeUtils.h> |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 29 | #include <nnapi/Types.h> |
| 30 | |
| 31 | #include <functional> |
| 32 | #include <memory> |
| 33 | #include <mutex> |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 34 | #include <sstream> |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 35 | #include <utility> |
| 36 | #include <vector> |
| 37 | |
| 38 | namespace android::hardware::neuralnetworks::utils { |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 39 | namespace { |
| 40 | |
| 41 | template <typename FnType> |
| 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 |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 67 | |
| 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 | } |
Michael Butler | 11761e3 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 74 | auto preparedModel = NN_TRY(makePreparedModel()); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 75 | CHECK(preparedModel != nullptr); |
| 76 | return std::make_shared<ResilientPreparedModel>( |
| 77 | PrivateConstructorTag{}, std::move(makePreparedModel), std::move(preparedModel)); |
| 78 | } |
| 79 | |
| 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 | |
| 88 | nn::SharedPreparedModel ResilientPreparedModel::getPreparedModel() const { |
| 89 | std::lock_guard guard(mMutex); |
| 90 | return mPreparedModel; |
| 91 | } |
| 92 | |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 93 | nn::GeneralResult<nn::SharedPreparedModel> ResilientPreparedModel::recover( |
| 94 | const nn::IPreparedModel* failingPreparedModel) const { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 95 | std::lock_guard guard(mMutex); |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 96 | |
| 97 | // Another caller updated the failing prepared model. |
| 98 | if (mPreparedModel.get() != failingPreparedModel) { |
| 99 | return mPreparedModel; |
| 100 | } |
| 101 | |
| 102 | mPreparedModel = NN_TRY(kMakePreparedModel()); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 103 | return mPreparedModel; |
| 104 | } |
| 105 | |
| 106 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 107 | ResilientPreparedModel::execute(const nn::Request& request, nn::MeasureTiming measure, |
| 108 | const nn::OptionalTimePoint& deadline, |
Michael Butler | ca11420 | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 109 | const nn::OptionalDuration& loopTimeoutDuration) const { |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 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); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
Michael Butler | ca11420 | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 118 | 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 { |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 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); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 132 | 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 | |
Michael Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 147 | nn::GeneralResult<nn::SharedBurst> ResilientPreparedModel::configureExecutionBurst() const { |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 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(); |
Michael Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 153 | }; |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 154 | return ResilientBurst::create(std::move(makeBurst)); |
| 155 | #else |
| 156 | return configureExecutionBurstInternal(); |
| 157 | #endif |
Michael Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 160 | 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 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 173 | std::any ResilientPreparedModel::getUnderlyingResource() const { |
| 174 | return getPreparedModel()->getUnderlyingResource(); |
| 175 | } |
| 176 | |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 177 | bool ResilientPreparedModel::isValidInternal() const { |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | 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 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 191 | } // namespace android::hardware::neuralnetworks::utils |