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