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 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/thread_annotations.h> |
| 21 | #include <nnapi/IPreparedModel.h> |
| 22 | #include <nnapi/Result.h> |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 23 | #include <nnapi/TypeUtils.h> |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 24 | #include <nnapi/Types.h> |
| 25 | |
| 26 | #include <functional> |
| 27 | #include <memory> |
| 28 | #include <mutex> |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 29 | #include <sstream> |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 30 | #include <utility> |
| 31 | #include <vector> |
| 32 | |
| 33 | namespace android::hardware::neuralnetworks::utils { |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | template <typename FnType> |
| 37 | auto protect(const ResilientPreparedModel& resilientPreparedModel, const FnType& fn) |
| 38 | -> decltype(fn(*resilientPreparedModel.getPreparedModel())) { |
| 39 | auto preparedModel = resilientPreparedModel.getPreparedModel(); |
| 40 | auto result = fn(*preparedModel); |
| 41 | |
| 42 | // Immediately return if prepared model is not dead. |
| 43 | if (result.has_value() || result.error().code != nn::ErrorStatus::DEAD_OBJECT) { |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | // Attempt recovery and return if it fails. |
| 48 | auto maybePreparedModel = resilientPreparedModel.recover(preparedModel.get()); |
| 49 | if (!maybePreparedModel.has_value()) { |
| 50 | const auto& [message, code] = maybePreparedModel.error(); |
| 51 | std::ostringstream oss; |
| 52 | oss << ", and failed to recover dead prepared model with error " << code << ": " << message; |
| 53 | result.error().message += oss.str(); |
| 54 | return result; |
| 55 | } |
| 56 | preparedModel = std::move(maybePreparedModel).value(); |
| 57 | |
| 58 | return fn(*preparedModel); |
| 59 | } |
| 60 | |
| 61 | } // namespace |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 62 | |
| 63 | nn::GeneralResult<std::shared_ptr<const ResilientPreparedModel>> ResilientPreparedModel::create( |
| 64 | Factory makePreparedModel) { |
| 65 | if (makePreparedModel == nullptr) { |
| 66 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 67 | << "utils::ResilientPreparedModel::create must have non-empty makePreparedModel"; |
| 68 | } |
Michael Butler | 11761e3 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 69 | auto preparedModel = NN_TRY(makePreparedModel()); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 70 | CHECK(preparedModel != nullptr); |
| 71 | return std::make_shared<ResilientPreparedModel>( |
| 72 | PrivateConstructorTag{}, std::move(makePreparedModel), std::move(preparedModel)); |
| 73 | } |
| 74 | |
| 75 | ResilientPreparedModel::ResilientPreparedModel(PrivateConstructorTag /*tag*/, |
| 76 | Factory makePreparedModel, |
| 77 | nn::SharedPreparedModel preparedModel) |
| 78 | : kMakePreparedModel(std::move(makePreparedModel)), mPreparedModel(std::move(preparedModel)) { |
| 79 | CHECK(kMakePreparedModel != nullptr); |
| 80 | CHECK(mPreparedModel != nullptr); |
| 81 | } |
| 82 | |
| 83 | nn::SharedPreparedModel ResilientPreparedModel::getPreparedModel() const { |
| 84 | std::lock_guard guard(mMutex); |
| 85 | return mPreparedModel; |
| 86 | } |
| 87 | |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 88 | nn::GeneralResult<nn::SharedPreparedModel> ResilientPreparedModel::recover( |
| 89 | const nn::IPreparedModel* failingPreparedModel) const { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 90 | std::lock_guard guard(mMutex); |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 91 | |
| 92 | // Another caller updated the failing prepared model. |
| 93 | if (mPreparedModel.get() != failingPreparedModel) { |
| 94 | return mPreparedModel; |
| 95 | } |
| 96 | |
| 97 | mPreparedModel = NN_TRY(kMakePreparedModel()); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 98 | return mPreparedModel; |
| 99 | } |
| 100 | |
| 101 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 102 | ResilientPreparedModel::execute(const nn::Request& request, nn::MeasureTiming measure, |
| 103 | const nn::OptionalTimePoint& deadline, |
Michael Butler | ca11420 | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 104 | const nn::OptionalDuration& loopTimeoutDuration) const { |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 105 | const auto fn = [&request, measure, &deadline, |
| 106 | &loopTimeoutDuration](const nn::IPreparedModel& preparedModel) { |
| 107 | return preparedModel.execute(request, measure, deadline, loopTimeoutDuration); |
| 108 | }; |
| 109 | return protect(*this, fn); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
Michael Butler | ca11420 | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 113 | ResilientPreparedModel::executeFenced(const nn::Request& request, |
| 114 | const std::vector<nn::SyncFence>& waitFor, |
| 115 | nn::MeasureTiming measure, |
| 116 | const nn::OptionalTimePoint& deadline, |
| 117 | const nn::OptionalDuration& loopTimeoutDuration, |
| 118 | const nn::OptionalDuration& timeoutDurationAfterFence) const { |
Michael Butler | 667dc2d | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 119 | const auto fn = [&request, &waitFor, measure, &deadline, &loopTimeoutDuration, |
| 120 | &timeoutDurationAfterFence](const nn::IPreparedModel& preparedModel) { |
| 121 | return preparedModel.executeFenced(request, waitFor, measure, deadline, loopTimeoutDuration, |
| 122 | timeoutDurationAfterFence); |
| 123 | }; |
| 124 | return protect(*this, fn); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Michael Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame^] | 127 | nn::GeneralResult<nn::SharedBurst> ResilientPreparedModel::configureExecutionBurst() const { |
| 128 | const auto fn = [](const nn::IPreparedModel& preparedModel) { |
| 129 | return preparedModel.configureExecutionBurst(); |
| 130 | }; |
| 131 | return protect(*this, fn); |
| 132 | } |
| 133 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 134 | std::any ResilientPreparedModel::getUnderlyingResource() const { |
| 135 | return getPreparedModel()->getUnderlyingResource(); |
| 136 | } |
| 137 | |
| 138 | } // namespace android::hardware::neuralnetworks::utils |