Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [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 "ResilientBurst.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/thread_annotations.h> |
| 21 | #include <nnapi/IBurst.h> |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 22 | #include <nnapi/IPreparedModel.h> |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 23 | #include <nnapi/Result.h> |
| 24 | #include <nnapi/TypeUtils.h> |
| 25 | #include <nnapi/Types.h> |
| 26 | |
| 27 | #include <functional> |
| 28 | #include <memory> |
| 29 | #include <mutex> |
| 30 | #include <optional> |
| 31 | #include <utility> |
| 32 | |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 33 | #include "InvalidExecution.h" |
| 34 | #include "ResilientExecution.h" |
| 35 | |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 36 | namespace android::hardware::neuralnetworks::utils { |
| 37 | namespace { |
| 38 | |
| 39 | template <typename FnType> |
| 40 | auto protect(const ResilientBurst& resilientBurst, const FnType& fn) |
| 41 | -> decltype(fn(*resilientBurst.getBurst())) { |
| 42 | auto burst = resilientBurst.getBurst(); |
| 43 | auto result = fn(*burst); |
| 44 | |
| 45 | // Immediately return if burst 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 maybeBurst = resilientBurst.recover(burst.get()); |
| 52 | if (!maybeBurst.has_value()) { |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 53 | const auto& [message, code] = maybeBurst.error(); |
| 54 | std::ostringstream oss; |
| 55 | oss << ", and failed to recover dead burst object with error " << code << ": " << message; |
| 56 | result.error().message += oss.str(); |
| 57 | return result; |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 58 | } |
| 59 | burst = std::move(maybeBurst).value(); |
| 60 | |
| 61 | return fn(*burst); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | nn::GeneralResult<std::shared_ptr<const ResilientBurst>> ResilientBurst::create(Factory makeBurst) { |
| 67 | if (makeBurst == nullptr) { |
| 68 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 69 | << "utils::ResilientBurst::create must have non-empty makeBurst"; |
| 70 | } |
| 71 | auto burst = NN_TRY(makeBurst()); |
| 72 | CHECK(burst != nullptr); |
| 73 | return std::make_shared<ResilientBurst>(PrivateConstructorTag{}, std::move(makeBurst), |
| 74 | std::move(burst)); |
| 75 | } |
| 76 | |
| 77 | ResilientBurst::ResilientBurst(PrivateConstructorTag /*tag*/, Factory makeBurst, |
| 78 | nn::SharedBurst burst) |
| 79 | : kMakeBurst(std::move(makeBurst)), mBurst(std::move(burst)) { |
| 80 | CHECK(kMakeBurst != nullptr); |
| 81 | CHECK(mBurst != nullptr); |
| 82 | } |
| 83 | |
| 84 | nn::SharedBurst ResilientBurst::getBurst() const { |
| 85 | std::lock_guard guard(mMutex); |
| 86 | return mBurst; |
| 87 | } |
| 88 | |
| 89 | nn::GeneralResult<nn::SharedBurst> ResilientBurst::recover(const nn::IBurst* failingBurst) const { |
| 90 | std::lock_guard guard(mMutex); |
| 91 | |
| 92 | // Another caller updated the failing burst. |
| 93 | if (mBurst.get() != failingBurst) { |
| 94 | return mBurst; |
| 95 | } |
| 96 | |
| 97 | mBurst = NN_TRY(kMakeBurst()); |
| 98 | return mBurst; |
| 99 | } |
| 100 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 101 | ResilientBurst::OptionalCacheHold ResilientBurst::cacheMemory( |
| 102 | const nn::SharedMemory& memory) const { |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 103 | return getBurst()->cacheMemory(memory); |
| 104 | } |
| 105 | |
| 106 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> ResilientBurst::execute( |
Michael Butler | 8414a6e | 2021-03-10 18:41:05 -0800 | [diff] [blame] | 107 | const nn::Request& request, nn::MeasureTiming measure, |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame^] | 108 | const nn::OptionalTimePoint& deadline, const nn::OptionalDuration& loopTimeoutDuration, |
| 109 | const std::vector<nn::TokenValuePair>& hints, |
| 110 | const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const { |
| 111 | const auto fn = [&request, measure, deadline, loopTimeoutDuration, &hints, |
| 112 | &extensionNameToPrefix](const nn::IBurst& burst) { |
| 113 | return burst.execute(request, measure, deadline, loopTimeoutDuration, hints, |
| 114 | extensionNameToPrefix); |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 115 | }; |
| 116 | return protect(*this, fn); |
| 117 | } |
| 118 | |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 119 | nn::GeneralResult<nn::SharedExecution> ResilientBurst::createReusableExecution( |
| 120 | const nn::Request& request, nn::MeasureTiming measure, |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame^] | 121 | const nn::OptionalDuration& loopTimeoutDuration, |
| 122 | const std::vector<nn::TokenValuePair>& hints, |
| 123 | const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const { |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 124 | #if 0 |
| 125 | auto self = shared_from_this(); |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame^] | 126 | ResilientExecution::Factory makeExecution = [burst = std::move(self), request, measure, |
| 127 | loopTimeoutDuration, &hints, |
| 128 | &extensionNameToPrefix] { |
| 129 | return burst->createReusableExecutionInternal(request, measure, loopTimeoutDuration, hints, |
| 130 | extensionNameToPrefix); |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 131 | }; |
| 132 | return ResilientExecution::create(std::move(makeExecution)); |
| 133 | #else |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame^] | 134 | return createReusableExecutionInternal(request, measure, loopTimeoutDuration, hints, |
| 135 | extensionNameToPrefix); |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 136 | #endif |
| 137 | } |
| 138 | |
| 139 | nn::GeneralResult<nn::SharedExecution> ResilientBurst::createReusableExecutionInternal( |
| 140 | const nn::Request& request, nn::MeasureTiming measure, |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame^] | 141 | const nn::OptionalDuration& loopTimeoutDuration, |
| 142 | const std::vector<nn::TokenValuePair>& hints, |
| 143 | const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const { |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 144 | if (!isValidInternal()) { |
| 145 | return std::make_shared<const InvalidExecution>(); |
| 146 | } |
Miao Wang | 0e671f3 | 2021-10-26 20:03:05 +0000 | [diff] [blame^] | 147 | const auto fn = [&request, measure, &loopTimeoutDuration, &hints, |
| 148 | &extensionNameToPrefix](const nn::IBurst& burst) { |
| 149 | return burst.createReusableExecution(request, measure, loopTimeoutDuration, hints, |
| 150 | extensionNameToPrefix); |
Xusong Wang | ead6d37 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 151 | }; |
| 152 | return protect(*this, fn); |
| 153 | } |
| 154 | |
| 155 | bool ResilientBurst::isValidInternal() const { |
| 156 | return true; |
| 157 | } |
| 158 | |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 159 | } // namespace android::hardware::neuralnetworks::utils |