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 "Device.h" |
| 18 | |
| 19 | #include "Callbacks.h" |
| 20 | #include "Conversions.h" |
| 21 | #include "Utils.h" |
| 22 | |
| 23 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 24 | #include <android/hardware/neuralnetworks/1.1/types.h> |
| 25 | #include <android/hardware/neuralnetworks/1.2/IDevice.h> |
| 26 | #include <android/hardware/neuralnetworks/1.2/types.h> |
| 27 | #include <nnapi/IBuffer.h> |
| 28 | #include <nnapi/IDevice.h> |
| 29 | #include <nnapi/IPreparedModel.h> |
| 30 | #include <nnapi/OperandTypes.h> |
| 31 | #include <nnapi/Result.h> |
| 32 | #include <nnapi/Types.h> |
| 33 | #include <nnapi/hal/1.1/Conversions.h> |
| 34 | #include <nnapi/hal/CommonUtils.h> |
| 35 | #include <nnapi/hal/HandleError.h> |
| 36 | #include <nnapi/hal/ProtectCallback.h> |
| 37 | |
| 38 | #include <functional> |
| 39 | #include <memory> |
| 40 | #include <optional> |
| 41 | #include <string> |
| 42 | #include <vector> |
| 43 | |
| 44 | namespace android::hardware::neuralnetworks::V1_2::utils { |
Slava Shklyaev | 77e06d8 | 2020-11-30 15:33:17 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | nn::GeneralResult<nn::Capabilities> initCapabilities(V1_2::IDevice* device) { |
| 48 | CHECK(device != nullptr); |
| 49 | |
| 50 | nn::GeneralResult<nn::Capabilities> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 51 | << "uninitialized"; |
| 52 | const auto cb = [&result](V1_0::ErrorStatus status, const Capabilities& capabilities) { |
| 53 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 54 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Slava Shklyaev | 77e06d8 | 2020-11-30 15:33:17 +0000 | [diff] [blame] | 55 | result = NN_ERROR(canonical) << "getCapabilities_1_2 failed with " << toString(status); |
| 56 | } else { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 57 | result = nn::convert(capabilities); |
Slava Shklyaev | 77e06d8 | 2020-11-30 15:33:17 +0000 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
| 61 | const auto ret = device->getCapabilities_1_2(cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 62 | HANDLE_TRANSPORT_FAILURE(ret); |
Slava Shklyaev | 77e06d8 | 2020-11-30 15:33:17 +0000 | [diff] [blame] | 63 | |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | } // namespace |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 68 | |
| 69 | nn::GeneralResult<std::string> initVersionString(V1_2::IDevice* device) { |
| 70 | CHECK(device != nullptr); |
| 71 | |
| 72 | nn::GeneralResult<std::string> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 73 | << "uninitialized"; |
| 74 | const auto cb = [&result](V1_0::ErrorStatus status, const hidl_string& versionString) { |
| 75 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 76 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 77 | result = NN_ERROR(canonical) << "getVersionString failed with " << toString(status); |
| 78 | } else { |
| 79 | result = versionString; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | const auto ret = device->getVersionString(cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 84 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 85 | |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | nn::GeneralResult<nn::DeviceType> initDeviceType(V1_2::IDevice* device) { |
| 90 | CHECK(device != nullptr); |
| 91 | |
| 92 | nn::GeneralResult<nn::DeviceType> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 93 | << "uninitialized"; |
| 94 | const auto cb = [&result](V1_0::ErrorStatus status, DeviceType deviceType) { |
| 95 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 96 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 97 | result = NN_ERROR(canonical) << "getDeviceType failed with " << toString(status); |
| 98 | } else { |
| 99 | result = nn::convert(deviceType); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | const auto ret = device->getType(cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 104 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | nn::GeneralResult<std::vector<nn::Extension>> initExtensions(V1_2::IDevice* device) { |
| 110 | CHECK(device != nullptr); |
| 111 | |
| 112 | nn::GeneralResult<std::vector<nn::Extension>> result = |
| 113 | NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "uninitialized"; |
| 114 | const auto cb = [&result](V1_0::ErrorStatus status, const hidl_vec<Extension>& extensions) { |
| 115 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 116 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 117 | result = NN_ERROR(canonical) << "getExtensions failed with " << toString(status); |
| 118 | } else { |
| 119 | result = nn::convert(extensions); |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | const auto ret = device->getSupportedExtensions(cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 124 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 125 | |
| 126 | return result; |
| 127 | } |
| 128 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 129 | nn::GeneralResult<std::pair<uint32_t, uint32_t>> initNumberOfCacheFilesNeeded( |
| 130 | V1_2::IDevice* device) { |
| 131 | CHECK(device != nullptr); |
| 132 | |
| 133 | nn::GeneralResult<std::pair<uint32_t, uint32_t>> result = |
| 134 | NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "uninitialized"; |
| 135 | const auto cb = [&result](V1_0::ErrorStatus status, uint32_t numModelCache, |
| 136 | uint32_t numDataCache) { |
| 137 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 138 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 139 | result = NN_ERROR(canonical) |
| 140 | << "getNumberOfCacheFilesNeeded failed with " << toString(status); |
| 141 | } else { |
| 142 | result = std::make_pair(numModelCache, numDataCache); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | const auto ret = device->getNumberOfCacheFilesNeeded(cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 147 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 148 | |
| 149 | return result; |
| 150 | } |
| 151 | |
| 152 | nn::GeneralResult<std::shared_ptr<const Device>> Device::create(std::string name, |
| 153 | sp<V1_2::IDevice> device) { |
| 154 | if (name.empty()) { |
| 155 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 156 | << "V1_2::utils::Device::create must have non-empty name"; |
| 157 | } |
| 158 | if (device == nullptr) { |
| 159 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 160 | << "V1_2::utils::Device::create must have non-null device"; |
| 161 | } |
| 162 | |
| 163 | auto versionString = NN_TRY(initVersionString(device.get())); |
| 164 | const auto deviceType = NN_TRY(initDeviceType(device.get())); |
| 165 | auto extensions = NN_TRY(initExtensions(device.get())); |
| 166 | auto capabilities = NN_TRY(initCapabilities(device.get())); |
| 167 | const auto numberOfCacheFilesNeeded = NN_TRY(initNumberOfCacheFilesNeeded(device.get())); |
| 168 | |
| 169 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(device)); |
| 170 | return std::make_shared<const Device>( |
| 171 | PrivateConstructorTag{}, std::move(name), std::move(versionString), deviceType, |
| 172 | std::move(extensions), std::move(capabilities), numberOfCacheFilesNeeded, |
| 173 | std::move(device), std::move(deathHandler)); |
| 174 | } |
| 175 | |
| 176 | Device::Device(PrivateConstructorTag /*tag*/, std::string name, std::string versionString, |
| 177 | nn::DeviceType deviceType, std::vector<nn::Extension> extensions, |
| 178 | nn::Capabilities capabilities, |
| 179 | std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded, sp<V1_2::IDevice> device, |
| 180 | hal::utils::DeathHandler deathHandler) |
| 181 | : kName(std::move(name)), |
| 182 | kVersionString(std::move(versionString)), |
| 183 | kDeviceType(deviceType), |
| 184 | kExtensions(std::move(extensions)), |
| 185 | kCapabilities(std::move(capabilities)), |
| 186 | kNumberOfCacheFilesNeeded(numberOfCacheFilesNeeded), |
| 187 | kDevice(std::move(device)), |
| 188 | kDeathHandler(std::move(deathHandler)) {} |
| 189 | |
| 190 | const std::string& Device::getName() const { |
| 191 | return kName; |
| 192 | } |
| 193 | |
| 194 | const std::string& Device::getVersionString() const { |
| 195 | return kVersionString; |
| 196 | } |
| 197 | |
| 198 | nn::Version Device::getFeatureLevel() const { |
| 199 | return nn::Version::ANDROID_Q; |
| 200 | } |
| 201 | |
| 202 | nn::DeviceType Device::getType() const { |
| 203 | return kDeviceType; |
| 204 | } |
| 205 | |
| 206 | const std::vector<nn::Extension>& Device::getSupportedExtensions() const { |
| 207 | return kExtensions; |
| 208 | } |
| 209 | |
| 210 | const nn::Capabilities& Device::getCapabilities() const { |
| 211 | return kCapabilities; |
| 212 | } |
| 213 | |
| 214 | std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const { |
| 215 | return kNumberOfCacheFilesNeeded; |
| 216 | } |
| 217 | |
| 218 | nn::GeneralResult<void> Device::wait() const { |
| 219 | const auto ret = kDevice->ping(); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 220 | HANDLE_TRANSPORT_FAILURE(ret); |
| 221 | return {}; |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const { |
| 225 | // Ensure that model is ready for IPC. |
| 226 | std::optional<nn::Model> maybeModelInShared; |
| 227 | const nn::Model& modelInShared = |
| 228 | NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared)); |
| 229 | |
| 230 | const auto hidlModel = NN_TRY(convert(modelInShared)); |
| 231 | |
| 232 | nn::GeneralResult<std::vector<bool>> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 233 | << "uninitialized"; |
| 234 | auto cb = [&result, &model](V1_0::ErrorStatus status, |
| 235 | const hidl_vec<bool>& supportedOperations) { |
| 236 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 237 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 238 | result = NN_ERROR(canonical) |
| 239 | << "getSupportedOperations_1_2 failed with " << toString(status); |
| 240 | } else if (supportedOperations.size() != model.main.operations.size()) { |
| 241 | result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 242 | << "getSupportedOperations_1_2 returned vector of size " |
| 243 | << supportedOperations.size() << " but expected " |
| 244 | << model.main.operations.size(); |
| 245 | } else { |
| 246 | result = supportedOperations; |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | const auto ret = kDevice->getSupportedOperations_1_2(hidlModel, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 251 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 252 | |
| 253 | return result; |
| 254 | } |
| 255 | |
| 256 | nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel( |
| 257 | const nn::Model& model, nn::ExecutionPreference preference, nn::Priority /*priority*/, |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 258 | nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& modelCache, |
| 259 | const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 260 | // Ensure that model is ready for IPC. |
| 261 | std::optional<nn::Model> maybeModelInShared; |
| 262 | const nn::Model& modelInShared = |
| 263 | NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared)); |
| 264 | |
| 265 | const auto hidlModel = NN_TRY(convert(modelInShared)); |
| 266 | const auto hidlPreference = NN_TRY(V1_1::utils::convert(preference)); |
| 267 | const auto hidlModelCache = NN_TRY(convert(modelCache)); |
| 268 | const auto hidlDataCache = NN_TRY(convert(dataCache)); |
| 269 | const auto hidlToken = token; |
| 270 | |
| 271 | const auto cb = sp<PreparedModelCallback>::make(); |
| 272 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 273 | |
| 274 | const auto ret = kDevice->prepareModel_1_2(hidlModel, hidlPreference, hidlModelCache, |
| 275 | hidlDataCache, hidlToken, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 276 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 277 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 278 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 279 | return NN_ERROR(canonical) << "prepareModel_1_2 failed with " << toString(status); |
| 280 | } |
| 281 | |
| 282 | return cb->get(); |
| 283 | } |
| 284 | |
| 285 | nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache( |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 286 | nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& modelCache, |
| 287 | const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 288 | const auto hidlModelCache = NN_TRY(convert(modelCache)); |
| 289 | const auto hidlDataCache = NN_TRY(convert(dataCache)); |
| 290 | const auto hidlToken = token; |
| 291 | |
| 292 | const auto cb = sp<PreparedModelCallback>::make(); |
| 293 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 294 | |
| 295 | const auto ret = kDevice->prepareModelFromCache(hidlModelCache, hidlDataCache, hidlToken, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 296 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 297 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 298 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 299 | return NN_ERROR(canonical) << "prepareModelFromCache failed with " << toString(status); |
| 300 | } |
| 301 | |
| 302 | return cb->get(); |
| 303 | } |
| 304 | |
| 305 | nn::GeneralResult<nn::SharedBuffer> Device::allocate( |
| 306 | const nn::BufferDesc& /*desc*/, |
| 307 | const std::vector<nn::SharedPreparedModel>& /*preparedModels*/, |
| 308 | const std::vector<nn::BufferRole>& /*inputRoles*/, |
| 309 | const std::vector<nn::BufferRole>& /*outputRoles*/) const { |
| 310 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 311 | << "IDevice::allocate not supported on 1.2 HAL service"; |
| 312 | } |
| 313 | |
| 314 | } // namespace android::hardware::neuralnetworks::V1_2::utils |