blob: 13965afd37a36b4644c44ff4d194a9e06e3c8bfa [file] [log] [blame]
Michael Butler4b276a72020-08-06 23:22:35 -07001/*
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 Butler37600582020-11-19 20:46:58 -080019#include "InvalidBuffer.h"
20#include "InvalidDevice.h"
21#include "InvalidPreparedModel.h"
Michael Butler4b276a72020-08-06 23:22:35 -070022#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
38namespace android::hardware::neuralnetworks::utils {
39namespace {
40
41template <typename FnType>
42auto 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 Butlerbf599462020-12-15 15:20:26 -080052 // 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 Butler4b276a72020-08-06 23:22:35 -070063 return fn(*device);
64}
65
66} // namespace
67
68nn::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
88ResilientDevice::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
102nn::SharedDevice ResilientDevice::getDevice() const {
103 std::lock_guard guard(mMutex);
104 return mDevice;
105}
106
Michael Butlerbf599462020-12-15 15:20:26 -0800107nn::GeneralResult<nn::SharedDevice> ResilientDevice::recover(const nn::IDevice* failingDevice,
108 bool blocking) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700109 std::lock_guard guard(mMutex);
110
111 // Another caller updated the failing device.
112 if (mDevice.get() != failingDevice) {
113 return mDevice;
114 }
115
Michael Butlerbf599462020-12-15 15:20:26 -0800116 auto device = NN_TRY(kMakeDevice(blocking));
Michael Butler4b276a72020-08-06 23:22:35 -0700117
Michael Butler37600582020-11-19 20:46:58 -0800118 // 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 Butler9adab0c2021-01-11 19:35:53 -0800125 compare(&IDevice::isUpdatable) || compare(&IDevice::getSupportedExtensions) ||
126 compare(&IDevice::getCapabilities)) {
Michael Butler37600582020-11-19 20:46:58 -0800127 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 Butler9adab0c2021-01-11 19:35:53 -0800130 kName, kVersionString, mDevice->getFeatureLevel(), mDevice->getType(),
131 mDevice->isUpdatable(), kExtensions, kCapabilities,
132 mDevice->getNumberOfCacheFilesNeeded());
Michael Butler37600582020-11-19 20:46:58 -0800133 mIsValid = false;
134 }
Michael Butler4b276a72020-08-06 23:22:35 -0700135
136 mDevice = std::move(device);
137 return mDevice;
138}
139
140const std::string& ResilientDevice::getName() const {
141 return kName;
142}
143
144const std::string& ResilientDevice::getVersionString() const {
145 return kVersionString;
146}
147
148nn::Version ResilientDevice::getFeatureLevel() const {
149 return getDevice()->getFeatureLevel();
150}
151
152nn::DeviceType ResilientDevice::getType() const {
153 return getDevice()->getType();
154}
155
Michael Butler9adab0c2021-01-11 19:35:53 -0800156bool ResilientDevice::isUpdatable() const {
157 return getDevice()->isUpdatable();
158}
159
Michael Butler4b276a72020-08-06 23:22:35 -0700160const std::vector<nn::Extension>& ResilientDevice::getSupportedExtensions() const {
161 return kExtensions;
162}
163
164const nn::Capabilities& ResilientDevice::getCapabilities() const {
165 return kCapabilities;
166}
167
168std::pair<uint32_t, uint32_t> ResilientDevice::getNumberOfCacheFilesNeeded() const {
169 return getDevice()->getNumberOfCacheFilesNeeded();
170}
171
172nn::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
177nn::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
185nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModel(
186 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000187 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
188 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler418c7492020-12-29 18:56:44 -0800189#if 0
Michael Butler4b276a72020-08-06 23:22:35 -0700190 auto self = shared_from_this();
Michael Butlerbf599462020-12-15 15:20:26 -0800191 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 Butler4b276a72020-08-06 23:22:35 -0700196 };
197 return ResilientPreparedModel::create(std::move(makePreparedModel));
Michael Butler418c7492020-12-29 18:56:44 -0800198#else
199 return prepareModelInternal(model, preference, priority, deadline, modelCache, dataCache,
200 token);
201#endif
Michael Butler4b276a72020-08-06 23:22:35 -0700202}
203
204nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelFromCache(
Slava Shklyaev49817a02020-10-27 18:44:01 +0000205 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
206 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler418c7492020-12-29 18:56:44 -0800207#if 0
Michael Butler4b276a72020-08-06 23:22:35 -0700208 auto self = shared_from_this();
Michael Butlerbf599462020-12-15 15:20:26 -0800209 ResilientPreparedModel::Factory makePreparedModel = [device = std::move(self), deadline,
210 modelCache, dataCache, token] {
211 return device->prepareModelFromCacheInternal(deadline, modelCache, dataCache, token);
Michael Butler4b276a72020-08-06 23:22:35 -0700212 };
213 return ResilientPreparedModel::create(std::move(makePreparedModel));
Michael Butler418c7492020-12-29 18:56:44 -0800214#else
215 return prepareModelFromCacheInternal(deadline, modelCache, dataCache, token);
216#endif
Michael Butler4b276a72020-08-06 23:22:35 -0700217}
218
219nn::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 Butler418c7492020-12-29 18:56:44 -0800223#if 0
Michael Butler4b276a72020-08-06 23:22:35 -0700224 auto self = shared_from_this();
Michael Butlerbf599462020-12-15 15:20:26 -0800225 ResilientBuffer::Factory makeBuffer = [device = std::move(self), desc, preparedModels,
226 inputRoles, outputRoles] {
227 return device->allocateInternal(desc, preparedModels, inputRoles, outputRoles);
Michael Butler4b276a72020-08-06 23:22:35 -0700228 };
229 return ResilientBuffer::create(std::move(makeBuffer));
Michael Butler418c7492020-12-29 18:56:44 -0800230#else
231 return allocateInternal(desc, preparedModels, inputRoles, outputRoles);
232#endif
Michael Butler4b276a72020-08-06 23:22:35 -0700233}
234
Michael Butler37600582020-11-19 20:46:58 -0800235bool ResilientDevice::isValidInternal() const {
236 std::lock_guard hold(mMutex);
237 return mIsValid;
238}
239
Michael Butler4b276a72020-08-06 23:22:35 -0700240nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelInternal(
Michael Butlerbf599462020-12-15 15:20:26 -0800241 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
242 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000243 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler37600582020-11-19 20:46:58 -0800244 if (!isValidInternal()) {
245 return std::make_shared<const InvalidPreparedModel>();
246 }
Michael Butler418c7492020-12-29 18:56:44 -0800247 const auto fn = [&model, preference, priority, &deadline, &modelCache, &dataCache,
248 &token](const nn::IDevice& device) {
Michael Butler4b276a72020-08-06 23:22:35 -0700249 return device.prepareModel(model, preference, priority, deadline, modelCache, dataCache,
250 token);
251 };
Michael Butlerbf599462020-12-15 15:20:26 -0800252 return protect(*this, fn, /*blocking=*/false);
Michael Butler4b276a72020-08-06 23:22:35 -0700253}
254
255nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelFromCacheInternal(
Michael Butlerbf599462020-12-15 15:20:26 -0800256 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000257 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler37600582020-11-19 20:46:58 -0800258 if (!isValidInternal()) {
259 return std::make_shared<const InvalidPreparedModel>();
260 }
Michael Butler418c7492020-12-29 18:56:44 -0800261 const auto fn = [&deadline, &modelCache, &dataCache, &token](const nn::IDevice& device) {
Michael Butler4b276a72020-08-06 23:22:35 -0700262 return device.prepareModelFromCache(deadline, modelCache, dataCache, token);
263 };
Michael Butlerbf599462020-12-15 15:20:26 -0800264 return protect(*this, fn, /*blocking=*/false);
Michael Butler4b276a72020-08-06 23:22:35 -0700265}
266
267nn::GeneralResult<nn::SharedBuffer> ResilientDevice::allocateInternal(
Michael Butlerbf599462020-12-15 15:20:26 -0800268 const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
Michael Butler4b276a72020-08-06 23:22:35 -0700269 const std::vector<nn::BufferRole>& inputRoles,
270 const std::vector<nn::BufferRole>& outputRoles) const {
Michael Butler37600582020-11-19 20:46:58 -0800271 if (!isValidInternal()) {
272 return std::make_shared<const InvalidBuffer>();
273 }
Michael Butler4b276a72020-08-06 23:22:35 -0700274 const auto fn = [&desc, &preparedModels, &inputRoles, &outputRoles](const nn::IDevice& device) {
275 return device.allocate(desc, preparedModels, inputRoles, outputRoles);
276 };
Michael Butlerbf599462020-12-15 15:20:26 -0800277 return protect(*this, fn, /*blocking=*/false);
Michael Butler4b276a72020-08-06 23:22:35 -0700278}
279
280} // namespace android::hardware::neuralnetworks::utils