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