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 
19 #include "InvalidBuffer.h"
20 #include "InvalidDevice.h"
21 #include "InvalidPreparedModel.h"
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>
protect(const ResilientDevice & resilientDevice,const FnType & fn,bool blocking)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 
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 
63     return fn(*device);
64 }
65 
66 }  // namespace
67 
create(Factory makeDevice)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 
ResilientDevice(PrivateConstructorTag,Factory makeDevice,std::string name,std::string versionString,std::vector<nn::Extension> extensions,nn::Capabilities capabilities,nn::SharedDevice device)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 
getDevice() const102 nn::SharedDevice ResilientDevice::getDevice() const {
103     std::lock_guard guard(mMutex);
104     return mDevice;
105 }
106 
recover(const nn::IDevice * failingDevice,bool blocking) const107 nn::GeneralResult<nn::SharedDevice> ResilientDevice::recover(const nn::IDevice* failingDevice,
108                                                              bool blocking) const {
109     std::lock_guard guard(mMutex);
110 
111     // Another caller updated the failing device.
112     if (mDevice.get() != failingDevice) {
113         return mDevice;
114     }
115 
116     auto device = NN_TRY(kMakeDevice(blocking));
117 
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) ||
125         compare(&IDevice::getSupportedExtensions) || compare(&IDevice::getCapabilities)) {
126         LOG(ERROR) << "Recovered device has different metadata than what is cached. Marking "
127                       "IDevice object as invalid.";
128         device = std::make_shared<const InvalidDevice>(
129                 kName, kVersionString, mDevice->getFeatureLevel(), mDevice->getType(), kExtensions,
130                 kCapabilities, mDevice->getNumberOfCacheFilesNeeded());
131         mIsValid = false;
132     }
133 
134     mDevice = std::move(device);
135     return mDevice;
136 }
137 
getName() const138 const std::string& ResilientDevice::getName() const {
139     return kName;
140 }
141 
getVersionString() const142 const std::string& ResilientDevice::getVersionString() const {
143     return kVersionString;
144 }
145 
getFeatureLevel() const146 nn::Version ResilientDevice::getFeatureLevel() const {
147     return getDevice()->getFeatureLevel();
148 }
149 
getType() const150 nn::DeviceType ResilientDevice::getType() const {
151     return getDevice()->getType();
152 }
153 
getSupportedExtensions() const154 const std::vector<nn::Extension>& ResilientDevice::getSupportedExtensions() const {
155     return kExtensions;
156 }
157 
getCapabilities() const158 const nn::Capabilities& ResilientDevice::getCapabilities() const {
159     return kCapabilities;
160 }
161 
getNumberOfCacheFilesNeeded() const162 std::pair<uint32_t, uint32_t> ResilientDevice::getNumberOfCacheFilesNeeded() const {
163     return getDevice()->getNumberOfCacheFilesNeeded();
164 }
165 
wait() const166 nn::GeneralResult<void> ResilientDevice::wait() const {
167     const auto fn = [](const nn::IDevice& device) { return device.wait(); };
168     return protect(*this, fn, /*blocking=*/true);
169 }
170 
getSupportedOperations(const nn::Model & model) const171 nn::GeneralResult<std::vector<bool>> ResilientDevice::getSupportedOperations(
172         const nn::Model& model) const {
173     const auto fn = [&model](const nn::IDevice& device) {
174         return device.getSupportedOperations(model);
175     };
176     return protect(*this, fn, /*blocking=*/false);
177 }
178 
prepareModel(const nn::Model & model,nn::ExecutionPreference preference,nn::Priority priority,nn::OptionalTimePoint deadline,const std::vector<nn::SharedHandle> & modelCache,const std::vector<nn::SharedHandle> & dataCache,const nn::CacheToken & token,const std::vector<nn::TokenValuePair> & hints,const std::vector<nn::ExtensionNameAndPrefix> & extensionNameToPrefix) const179 nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModel(
180         const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
181         nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
182         const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token,
183         const std::vector<nn::TokenValuePair>& hints,
184         const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const {
185 #if 0
186     auto self = shared_from_this();
187     ResilientPreparedModel::Factory makePreparedModel = [device = std::move(self), model,
188                                                          preference, priority, deadline, modelCache,
189                                                          dataCache, token, hints, extensionNameToPrefix] {
190         return device->prepareModelInternal(model, preference, priority, deadline, modelCache,
191                                             dataCache, token, hints, extensionNameToPrefix);
192     };
193     return ResilientPreparedModel::create(std::move(makePreparedModel));
194 #else
195     return prepareModelInternal(model, preference, priority, deadline, modelCache, dataCache, token,
196                                 hints, extensionNameToPrefix);
197 #endif
198 }
199 
prepareModelFromCache(nn::OptionalTimePoint deadline,const std::vector<nn::SharedHandle> & modelCache,const std::vector<nn::SharedHandle> & dataCache,const nn::CacheToken & token) const200 nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelFromCache(
201         nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
202         const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
203 #if 0
204     auto self = shared_from_this();
205     ResilientPreparedModel::Factory makePreparedModel = [device = std::move(self), deadline,
206                                                          modelCache, dataCache, token] {
207         return device->prepareModelFromCacheInternal(deadline, modelCache, dataCache, token);
208     };
209     return ResilientPreparedModel::create(std::move(makePreparedModel));
210 #else
211     return prepareModelFromCacheInternal(deadline, modelCache, dataCache, token);
212 #endif
213 }
214 
allocate(const nn::BufferDesc & desc,const std::vector<nn::SharedPreparedModel> & preparedModels,const std::vector<nn::BufferRole> & inputRoles,const std::vector<nn::BufferRole> & outputRoles) const215 nn::GeneralResult<nn::SharedBuffer> ResilientDevice::allocate(
216         const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
217         const std::vector<nn::BufferRole>& inputRoles,
218         const std::vector<nn::BufferRole>& outputRoles) const {
219 #if 0
220     auto self = shared_from_this();
221     ResilientBuffer::Factory makeBuffer = [device = std::move(self), desc, preparedModels,
222                                            inputRoles, outputRoles] {
223         return device->allocateInternal(desc, preparedModels, inputRoles, outputRoles);
224     };
225     return ResilientBuffer::create(std::move(makeBuffer));
226 #else
227     return allocateInternal(desc, preparedModels, inputRoles, outputRoles);
228 #endif
229 }
230 
isValidInternal() const231 bool ResilientDevice::isValidInternal() const {
232     std::lock_guard hold(mMutex);
233     return mIsValid;
234 }
235 
prepareModelInternal(const nn::Model & model,nn::ExecutionPreference preference,nn::Priority priority,nn::OptionalTimePoint deadline,const std::vector<nn::SharedHandle> & modelCache,const std::vector<nn::SharedHandle> & dataCache,const nn::CacheToken & token,const std::vector<nn::TokenValuePair> & hints,const std::vector<nn::ExtensionNameAndPrefix> & extensionNameToPrefix) const236 nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelInternal(
237         const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
238         nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
239         const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token,
240         const std::vector<nn::TokenValuePair>& hints,
241         const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const {
242     if (!isValidInternal()) {
243         return std::make_shared<const InvalidPreparedModel>();
244     }
245     const auto fn = [&model, preference, priority, &deadline, &modelCache, &dataCache, &token,
246                      &hints, &extensionNameToPrefix](const nn::IDevice& device) {
247         return device.prepareModel(model, preference, priority, deadline, modelCache, dataCache,
248                                    token, hints, extensionNameToPrefix);
249     };
250     return protect(*this, fn, /*blocking=*/false);
251 }
252 
prepareModelFromCacheInternal(nn::OptionalTimePoint deadline,const std::vector<nn::SharedHandle> & modelCache,const std::vector<nn::SharedHandle> & dataCache,const nn::CacheToken & token) const253 nn::GeneralResult<nn::SharedPreparedModel> ResilientDevice::prepareModelFromCacheInternal(
254         nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
255         const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
256     if (!isValidInternal()) {
257         return std::make_shared<const InvalidPreparedModel>();
258     }
259     const auto fn = [&deadline, &modelCache, &dataCache, &token](const nn::IDevice& device) {
260         return device.prepareModelFromCache(deadline, modelCache, dataCache, token);
261     };
262     return protect(*this, fn, /*blocking=*/false);
263 }
264 
allocateInternal(const nn::BufferDesc & desc,const std::vector<nn::SharedPreparedModel> & preparedModels,const std::vector<nn::BufferRole> & inputRoles,const std::vector<nn::BufferRole> & outputRoles) const265 nn::GeneralResult<nn::SharedBuffer> ResilientDevice::allocateInternal(
266         const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
267         const std::vector<nn::BufferRole>& inputRoles,
268         const std::vector<nn::BufferRole>& outputRoles) const {
269     if (!isValidInternal()) {
270         return std::make_shared<const InvalidBuffer>();
271     }
272     const auto fn = [&desc, &preparedModels, &inputRoles, &outputRoles](const nn::IDevice& device) {
273         return device.allocate(desc, preparedModels, inputRoles, outputRoles);
274     };
275     return protect(*this, fn, /*blocking=*/false);
276 }
277 
278 }  // namespace android::hardware::neuralnetworks::utils
279