Michael Butler | 4b276a7 | 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 "ResilientDevice.h" |
| 18 | |
Michael Butler | 3760058 | 2020-11-19 20:46:58 -0800 | [diff] [blame] | 19 | #include "InvalidBuffer.h" |
| 20 | #include "InvalidDevice.h" |
| 21 | #include "InvalidPreparedModel.h" |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 22 | #include "ResilientBuffer.h" |
| 23 | #include "ResilientPreparedModel.h" |
| 24 | |
| 25 | #include <android-base/logging.h> |
| 26 | #include <nnapi/IBuffer.h> |
| 27 | #include <nnapi/IDevice.h> |
| 28 | #include <nnapi/IPreparedModel.h> |
| 29 | #include <nnapi/Result.h> |
| 30 | #include <nnapi/TypeUtils.h> |
| 31 | #include <nnapi/Types.h> |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <memory> |
| 35 | #include <string> |
| 36 | #include <vector> |
| 37 | |
| 38 | namespace android::hardware::neuralnetworks::utils { |
| 39 | namespace { |
| 40 | |
| 41 | template <typename FnType> |
| 42 | auto protect(const ResilientDevice& resilientDevice, const FnType& fn, bool blocking) |
| 43 | -> decltype(fn(*resilientDevice.getDevice())) { |
| 44 | auto device = resilientDevice.getDevice(); |
| 45 | auto result = fn(*device); |
| 46 | |
| 47 | // Immediately return if device is not dead. |
| 48 | if (result.has_value() || result.error().code != nn::ErrorStatus::DEAD_OBJECT) { |
| 49 | return result; |
| 50 | } |
| 51 | |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 52 | // Attempt recovery and return if it fails. |
| 53 | auto maybeDevice = resilientDevice.recover(device.get(), blocking); |
| 54 | if (!maybeDevice.has_value()) { |
| 55 | const auto& [resultErrorMessage, resultErrorCode] = result.error(); |
| 56 | const auto& [recoveryErrorMessage, recoveryErrorCode] = maybeDevice.error(); |
| 57 | return nn::error(resultErrorCode) |
| 58 | << resultErrorMessage << ", and failed to recover dead device with error " |
| 59 | << recoveryErrorCode << ": " << recoveryErrorMessage; |
| 60 | } |
| 61 | device = std::move(maybeDevice).value(); |
| 62 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 63 | return fn(*device); |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | nn::GeneralResult<std::shared_ptr<const ResilientDevice>> ResilientDevice::create( |
| 69 | Factory makeDevice) { |
| 70 | if (makeDevice == nullptr) { |
| 71 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 72 | << "utils::ResilientDevice::create must have non-empty makeDevice"; |
| 73 | } |
| 74 | auto device = NN_TRY(makeDevice(/*blocking=*/true)); |
| 75 | CHECK(device != nullptr); |
| 76 | |
| 77 | auto name = device->getName(); |
| 78 | auto versionString = device->getVersionString(); |
| 79 | auto extensions = device->getSupportedExtensions(); |
| 80 | auto capabilities = device->getCapabilities(); |
| 81 | |
| 82 | return std::make_shared<ResilientDevice>(PrivateConstructorTag{}, std::move(makeDevice), |
| 83 | std::move(name), std::move(versionString), |
| 84 | std::move(extensions), std::move(capabilities), |
| 85 | std::move(device)); |
| 86 | } |
| 87 | |
| 88 | ResilientDevice::ResilientDevice(PrivateConstructorTag /*tag*/, Factory makeDevice, |
| 89 | std::string name, std::string versionString, |
| 90 | std::vector<nn::Extension> extensions, |
| 91 | nn::Capabilities capabilities, nn::SharedDevice device) |
| 92 | : kMakeDevice(std::move(makeDevice)), |
| 93 | kName(std::move(name)), |
| 94 | kVersionString(std::move(versionString)), |
| 95 | kExtensions(std::move(extensions)), |
| 96 | kCapabilities(std::move(capabilities)), |
| 97 | mDevice(std::move(device)) { |
| 98 | CHECK(kMakeDevice != nullptr); |
| 99 | CHECK(mDevice != nullptr); |
| 100 | } |
| 101 | |
| 102 | nn::SharedDevice ResilientDevice::getDevice() const { |
| 103 | std::lock_guard guard(mMutex); |
| 104 | return mDevice; |
| 105 | } |
| 106 | |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 107 | nn::GeneralResult<nn::SharedDevice> ResilientDevice::recover(const nn::IDevice* failingDevice, |
| 108 | bool blocking) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 109 | std::lock_guard guard(mMutex); |
| 110 | |
| 111 | // Another caller updated the failing device. |
| 112 | if (mDevice.get() != failingDevice) { |
| 113 | return mDevice; |
| 114 | } |
| 115 | |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 116 | auto device = NN_TRY(kMakeDevice(blocking)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 117 | |
Michael Butler | 3760058 | 2020-11-19 20:46:58 -0800 | [diff] [blame] | 118 | // If recovered device has different metadata than what is cached (i.e., because it was |
| 119 | // updated), mark the device as invalid and preserve the cached data. |
| 120 | auto compare = [this, &device](auto fn) REQUIRES(mMutex) { |
| 121 | return std::invoke(fn, mDevice) != std::invoke(fn, device); |
| 122 | }; |
| 123 | if (compare(&IDevice::getName) || compare(&IDevice::getVersionString) || |
| 124 | compare(&IDevice::getFeatureLevel) || compare(&IDevice::getType) || |
Michael Butler | 9adab0c | 2021-01-11 19:35:53 -0800 | [diff] [blame] | 125 | compare(&IDevice::isUpdatable) || compare(&IDevice::getSupportedExtensions) || |
| 126 | compare(&IDevice::getCapabilities)) { |
Michael Butler | 3760058 | 2020-11-19 20:46:58 -0800 | [diff] [blame] | 127 | LOG(ERROR) << "Recovered device has different metadata than what is cached. Marking " |
| 128 | "IDevice object as invalid."; |
| 129 | device = std::make_shared<const InvalidDevice>( |
Michael Butler | 9adab0c | 2021-01-11 19:35:53 -0800 | [diff] [blame] | 130 | kName, kVersionString, mDevice->getFeatureLevel(), mDevice->getType(), |
| 131 | mDevice->isUpdatable(), kExtensions, kCapabilities, |
| 132 | mDevice->getNumberOfCacheFilesNeeded()); |
Michael Butler | 3760058 | 2020-11-19 20:46:58 -0800 | [diff] [blame] | 133 | mIsValid = false; |
| 134 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 135 | |
| 136 | mDevice = std::move(device); |
| 137 | return mDevice; |
| 138 | } |
| 139 | |
| 140 | const std::string& ResilientDevice::getName() const { |
| 141 | return kName; |
| 142 | } |
| 143 | |
| 144 | const std::string& ResilientDevice::getVersionString() const { |
| 145 | return kVersionString; |
| 146 | } |
| 147 | |
| 148 | nn::Version ResilientDevice::getFeatureLevel() const { |
| 149 | return getDevice()->getFeatureLevel(); |
| 150 | } |
| 151 | |
| 152 | nn::DeviceType ResilientDevice::getType() const { |
| 153 | return getDevice()->getType(); |
| 154 | } |
| 155 | |
Michael Butler | 9adab0c | 2021-01-11 19:35:53 -0800 | [diff] [blame] | 156 | bool ResilientDevice::isUpdatable() const { |
| 157 | return getDevice()->isUpdatable(); |
| 158 | } |
| 159 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 160 | const std::vector<nn::Extension>& ResilientDevice::getSupportedExtensions() const { |
| 161 | return kExtensions; |
| 162 | } |
| 163 | |
| 164 | const nn::Capabilities& ResilientDevice::getCapabilities() const { |
| 165 | return kCapabilities; |
| 166 | } |
| 167 | |
| 168 | std::pair<uint32_t, uint32_t> ResilientDevice::getNumberOfCacheFilesNeeded() const { |
| 169 | return getDevice()->getNumberOfCacheFilesNeeded(); |
| 170 | } |
| 171 | |
| 172 | nn::GeneralResult<void> ResilientDevice::wait() const { |
| 173 | const auto fn = [](const nn::IDevice& device) { return device.wait(); }; |
| 174 | return protect(*this, fn, /*blocking=*/true); |
| 175 | } |
| 176 | |
| 177 | nn::GeneralResult<std::vector<bool>> ResilientDevice::getSupportedOperations( |
| 178 | const nn::Model& model) const { |
| 179 | const auto fn = [&model](const nn::IDevice& device) { |
| 180 | return device.getSupportedOperations(model); |
| 181 | }; |
| 182 | return protect(*this, fn, /*blocking=*/false); |
| 183 | } |
| 184 | |
| 185 | nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModel( |
| 186 | const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority, |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 187 | nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache, |
| 188 | const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const { |
Michael Butler | 418c749 | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 189 | #if 0 |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 190 | auto self = shared_from_this(); |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 191 | ResilientPreparedModel::Factory makePreparedModel = [device = std::move(self), model, |
| 192 | preference, priority, deadline, modelCache, |
| 193 | dataCache, token] { |
| 194 | return device->prepareModelInternal(model, preference, priority, deadline, modelCache, |
| 195 | dataCache, token); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 196 | }; |
| 197 | return ResilientPreparedModel::create(std::move(makePreparedModel)); |
Michael Butler | 418c749 | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 198 | #else |
| 199 | return prepareModelInternal(model, preference, priority, deadline, modelCache, dataCache, |
| 200 | token); |
| 201 | #endif |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelFromCache( |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 205 | nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache, |
| 206 | const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const { |
Michael Butler | 418c749 | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 207 | #if 0 |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 208 | auto self = shared_from_this(); |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 209 | ResilientPreparedModel::Factory makePreparedModel = [device = std::move(self), deadline, |
| 210 | modelCache, dataCache, token] { |
| 211 | return device->prepareModelFromCacheInternal(deadline, modelCache, dataCache, token); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 212 | }; |
| 213 | return ResilientPreparedModel::create(std::move(makePreparedModel)); |
Michael Butler | 418c749 | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 214 | #else |
| 215 | return prepareModelFromCacheInternal(deadline, modelCache, dataCache, token); |
| 216 | #endif |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | nn::GeneralResult<nn::SharedBuffer> ResilientDevice::allocate( |
| 220 | const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels, |
| 221 | const std::vector<nn::BufferRole>& inputRoles, |
| 222 | const std::vector<nn::BufferRole>& outputRoles) const { |
Michael Butler | 418c749 | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 223 | #if 0 |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 224 | auto self = shared_from_this(); |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 225 | ResilientBuffer::Factory makeBuffer = [device = std::move(self), desc, preparedModels, |
| 226 | inputRoles, outputRoles] { |
| 227 | return device->allocateInternal(desc, preparedModels, inputRoles, outputRoles); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 228 | }; |
| 229 | return ResilientBuffer::create(std::move(makeBuffer)); |
Michael Butler | 418c749 | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 230 | #else |
| 231 | return allocateInternal(desc, preparedModels, inputRoles, outputRoles); |
| 232 | #endif |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Michael Butler | 3760058 | 2020-11-19 20:46:58 -0800 | [diff] [blame] | 235 | bool ResilientDevice::isValidInternal() const { |
| 236 | std::lock_guard hold(mMutex); |
| 237 | return mIsValid; |
| 238 | } |
| 239 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 240 | nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelInternal( |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 241 | const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority, |
| 242 | nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache, |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 243 | const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const { |
Michael Butler | 3760058 | 2020-11-19 20:46:58 -0800 | [diff] [blame] | 244 | if (!isValidInternal()) { |
| 245 | return std::make_shared<const InvalidPreparedModel>(); |
| 246 | } |
Michael Butler | 418c749 | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 247 | const auto fn = [&model, preference, priority, &deadline, &modelCache, &dataCache, |
| 248 | &token](const nn::IDevice& device) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 249 | return device.prepareModel(model, preference, priority, deadline, modelCache, dataCache, |
| 250 | token); |
| 251 | }; |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 252 | return protect(*this, fn, /*blocking=*/false); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelFromCacheInternal( |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 256 | nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache, |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 257 | const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const { |
Michael Butler | 3760058 | 2020-11-19 20:46:58 -0800 | [diff] [blame] | 258 | if (!isValidInternal()) { |
| 259 | return std::make_shared<const InvalidPreparedModel>(); |
| 260 | } |
Michael Butler | 418c749 | 2020-12-29 18:56:44 -0800 | [diff] [blame] | 261 | const auto fn = [&deadline, &modelCache, &dataCache, &token](const nn::IDevice& device) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 262 | return device.prepareModelFromCache(deadline, modelCache, dataCache, token); |
| 263 | }; |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 264 | return protect(*this, fn, /*blocking=*/false); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | nn::GeneralResult<nn::SharedBuffer> ResilientDevice::allocateInternal( |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 268 | const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels, |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 269 | const std::vector<nn::BufferRole>& inputRoles, |
| 270 | const std::vector<nn::BufferRole>& outputRoles) const { |
Michael Butler | 3760058 | 2020-11-19 20:46:58 -0800 | [diff] [blame] | 271 | if (!isValidInternal()) { |
| 272 | return std::make_shared<const InvalidBuffer>(); |
| 273 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 274 | const auto fn = [&desc, &preparedModels, &inputRoles, &outputRoles](const nn::IDevice& device) { |
| 275 | return device.allocate(desc, preparedModels, inputRoles, outputRoles); |
| 276 | }; |
Michael Butler | bf59946 | 2020-12-15 15:20:26 -0800 | [diff] [blame] | 277 | return protect(*this, fn, /*blocking=*/false); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | } // namespace android::hardware::neuralnetworks::utils |