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>> |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 107 | ResilientPreparedModel::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 Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 116 | }; |
| 117 | return protect(*this, fn); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 121 | ResilientPreparedModel::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 Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 128 | const auto fn = [&request, &waitFor, measure, &deadline, &loopTimeoutDuration, |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 129 | &timeoutDurationAfterFence, &hints, |
| 130 | &extensionNameToPrefix](const nn::IPreparedModel& preparedModel) { |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 131 | return preparedModel.executeFenced(request, waitFor, measure, deadline, loopTimeoutDuration, |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 132 | timeoutDurationAfterFence, hints, extensionNameToPrefix); |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 133 | }; |
| 134 | return protect(*this, fn); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 137 | nn::GeneralResult<nn::SharedExecution> ResilientPreparedModel::createReusableExecution( |
| 138 | const nn::Request& request, nn::MeasureTiming measure, |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 139 | const nn::OptionalDuration& loopTimeoutDuration, |
| 140 | const std::vector<nn::TokenValuePair>& hints, |
| 141 | const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const { |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 142 | #if 0 |
| 143 | auto self = shared_from_this(); |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 144 | 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 Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 149 | }; |
| 150 | return ResilientExecution::create(std::move(makeExecution)); |
| 151 | #else |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 152 | return createReusableExecutionInternal(request, measure, loopTimeoutDuration, hints, |
| 153 | extensionNameToPrefix); |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 154 | #endif |
| 155 | } |
| 156 | |
Michael Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 157 | nn::GeneralResult<nn::SharedBurst> ResilientPreparedModel::configureExecutionBurst() const { |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 158 | #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 Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 163 | }; |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 164 | return ResilientBurst::create(std::move(makeBurst)); |
| 165 | #else |
| 166 | return configureExecutionBurstInternal(); |
| 167 | #endif |
Michael Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 170 | nn::GeneralResult<nn::SharedExecution> ResilientPreparedModel::createReusableExecutionInternal( |
| 171 | const nn::Request& request, nn::MeasureTiming measure, |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 172 | const nn::OptionalDuration& loopTimeoutDuration, |
| 173 | const std::vector<nn::TokenValuePair>& hints, |
| 174 | const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const { |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 175 | if (!isValidInternal()) { |
| 176 | return std::make_shared<const InvalidExecution>(); |
| 177 | } |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 178 | const auto fn = [&request, measure, &loopTimeoutDuration, &hints, |
| 179 | &extensionNameToPrefix](const nn::IPreparedModel& preparedModel) { |
| 180 | return preparedModel.createReusableExecution(request, measure, loopTimeoutDuration, hints, |
| 181 | extensionNameToPrefix); |
Xusong Wang | 727a7b2 | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 182 | }; |
| 183 | return protect(*this, fn); |
| 184 | } |
| 185 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 186 | std::any ResilientPreparedModel::getUnderlyingResource() const { |
| 187 | return getPreparedModel()->getUnderlyingResource(); |
| 188 | } |
| 189 | |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 190 | bool ResilientPreparedModel::isValidInternal() const { |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | nn::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 Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 204 | } // namespace android::hardware::neuralnetworks::utils |