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 "Buffer.h" |
| 20 | #include "Callbacks.h" |
| 21 | #include "Conversions.h" |
| 22 | #include "PreparedModel.h" |
| 23 | #include "Utils.h" |
| 24 | |
| 25 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 26 | #include <android/hardware/neuralnetworks/1.1/types.h> |
| 27 | #include <android/hardware/neuralnetworks/1.2/types.h> |
| 28 | #include <android/hardware/neuralnetworks/1.3/IDevice.h> |
| 29 | #include <android/hardware/neuralnetworks/1.3/types.h> |
| 30 | #include <nnapi/IBuffer.h> |
| 31 | #include <nnapi/IDevice.h> |
| 32 | #include <nnapi/IPreparedModel.h> |
| 33 | #include <nnapi/OperandTypes.h> |
| 34 | #include <nnapi/Result.h> |
| 35 | #include <nnapi/Types.h> |
| 36 | #include <nnapi/hal/1.1/Conversions.h> |
| 37 | #include <nnapi/hal/1.2/Conversions.h> |
| 38 | #include <nnapi/hal/1.2/Device.h> |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 39 | #include <nnapi/hal/1.2/Utils.h> |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 40 | #include <nnapi/hal/CommonUtils.h> |
| 41 | #include <nnapi/hal/HandleError.h> |
| 42 | #include <nnapi/hal/ProtectCallback.h> |
| 43 | |
| 44 | #include <any> |
| 45 | #include <functional> |
| 46 | #include <memory> |
| 47 | #include <optional> |
| 48 | #include <string> |
| 49 | #include <vector> |
| 50 | |
Michael Butler | 7a655bb | 2020-12-13 23:06:06 -0800 | [diff] [blame] | 51 | // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface |
| 52 | // lifetimes across processes and for protecting asynchronous calls across HIDL. |
| 53 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 54 | namespace android::hardware::neuralnetworks::V1_3::utils { |
| 55 | namespace { |
| 56 | |
| 57 | nn::GeneralResult<hidl_vec<sp<IPreparedModel>>> convert( |
| 58 | const std::vector<nn::SharedPreparedModel>& preparedModels) { |
| 59 | hidl_vec<sp<IPreparedModel>> hidlPreparedModels(preparedModels.size()); |
| 60 | for (size_t i = 0; i < preparedModels.size(); ++i) { |
| 61 | std::any underlyingResource = preparedModels[i]->getUnderlyingResource(); |
| 62 | if (const auto* hidlPreparedModel = |
| 63 | std::any_cast<sp<IPreparedModel>>(&underlyingResource)) { |
| 64 | hidlPreparedModels[i] = *hidlPreparedModel; |
| 65 | } else { |
| 66 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 67 | << "Unable to convert from nn::IPreparedModel to V1_3::IPreparedModel"; |
| 68 | } |
| 69 | } |
| 70 | return hidlPreparedModels; |
| 71 | } |
| 72 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 73 | nn::GeneralResult<nn::Capabilities> capabilitiesCallback(ErrorStatus status, |
| 74 | const Capabilities& capabilities) { |
| 75 | HANDLE_HAL_STATUS(status) << "getting capabilities failed with " << toString(status); |
| 76 | return nn::convert(capabilities); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 79 | nn::GeneralResult<nn::Capabilities> getCapabilitiesFrom(V1_3::IDevice* device) { |
Slava Shklyaev | d594cd0 | 2020-11-30 15:33:17 +0000 | [diff] [blame] | 80 | CHECK(device != nullptr); |
| 81 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 82 | auto cb = hal::utils::CallbackValue(capabilitiesCallback); |
Slava Shklyaev | d594cd0 | 2020-11-30 15:33:17 +0000 | [diff] [blame] | 83 | |
| 84 | const auto ret = device->getCapabilities_1_3(cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 85 | HANDLE_TRANSPORT_FAILURE(ret); |
Slava Shklyaev | d594cd0 | 2020-11-30 15:33:17 +0000 | [diff] [blame] | 86 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 87 | return cb.take(); |
| 88 | } |
| 89 | |
| 90 | nn::GeneralResult<nn::SharedBuffer> allocationCallback(ErrorStatus status, |
| 91 | const sp<IBuffer>& buffer, uint32_t token) { |
| 92 | HANDLE_HAL_STATUS(status) << "IDevice::allocate failed with " << toString(status); |
| 93 | return Buffer::create(buffer, static_cast<nn::Request::MemoryDomainToken>(token)); |
Slava Shklyaev | d594cd0 | 2020-11-30 15:33:17 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 96 | } // namespace |
| 97 | |
| 98 | nn::GeneralResult<std::shared_ptr<const Device>> Device::create(std::string name, |
| 99 | sp<V1_3::IDevice> device) { |
| 100 | if (name.empty()) { |
| 101 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 102 | << "V1_3::utils::Device::create must have non-empty name"; |
| 103 | } |
| 104 | if (device == nullptr) { |
| 105 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 106 | << "V1_3::utils::Device::create must have non-null device"; |
| 107 | } |
| 108 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 109 | auto versionString = NN_TRY(V1_2::utils::getVersionStringFrom(device.get())); |
| 110 | const auto deviceType = NN_TRY(V1_2::utils::getDeviceTypeFrom(device.get())); |
| 111 | auto extensions = NN_TRY(V1_2::utils::getSupportedExtensionsFrom(device.get())); |
| 112 | auto capabilities = NN_TRY(getCapabilitiesFrom(device.get())); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 113 | const auto numberOfCacheFilesNeeded = |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 114 | NN_TRY(V1_2::utils::getNumberOfCacheFilesNeededFrom(device.get())); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 115 | |
| 116 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(device)); |
| 117 | return std::make_shared<const Device>( |
| 118 | PrivateConstructorTag{}, std::move(name), std::move(versionString), deviceType, |
| 119 | std::move(extensions), std::move(capabilities), numberOfCacheFilesNeeded, |
| 120 | std::move(device), std::move(deathHandler)); |
| 121 | } |
| 122 | |
| 123 | Device::Device(PrivateConstructorTag /*tag*/, std::string name, std::string versionString, |
| 124 | nn::DeviceType deviceType, std::vector<nn::Extension> extensions, |
| 125 | nn::Capabilities capabilities, |
| 126 | std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded, sp<V1_3::IDevice> device, |
| 127 | hal::utils::DeathHandler deathHandler) |
| 128 | : kName(std::move(name)), |
| 129 | kVersionString(std::move(versionString)), |
| 130 | kDeviceType(deviceType), |
| 131 | kExtensions(std::move(extensions)), |
| 132 | kCapabilities(std::move(capabilities)), |
| 133 | kNumberOfCacheFilesNeeded(numberOfCacheFilesNeeded), |
| 134 | kDevice(std::move(device)), |
| 135 | kDeathHandler(std::move(deathHandler)) {} |
| 136 | |
| 137 | const std::string& Device::getName() const { |
| 138 | return kName; |
| 139 | } |
| 140 | |
| 141 | const std::string& Device::getVersionString() const { |
| 142 | return kVersionString; |
| 143 | } |
| 144 | |
| 145 | nn::Version Device::getFeatureLevel() const { |
| 146 | return nn::Version::ANDROID_R; |
| 147 | } |
| 148 | |
| 149 | nn::DeviceType Device::getType() const { |
| 150 | return kDeviceType; |
| 151 | } |
| 152 | |
Michael Butler | 080fbf7 | 2021-01-11 19:35:53 -0800 | [diff] [blame] | 153 | bool Device::isUpdatable() const { |
| 154 | return false; |
| 155 | } |
| 156 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 157 | const std::vector<nn::Extension>& Device::getSupportedExtensions() const { |
| 158 | return kExtensions; |
| 159 | } |
| 160 | |
| 161 | const nn::Capabilities& Device::getCapabilities() const { |
| 162 | return kCapabilities; |
| 163 | } |
| 164 | |
| 165 | std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const { |
| 166 | return kNumberOfCacheFilesNeeded; |
| 167 | } |
| 168 | |
| 169 | nn::GeneralResult<void> Device::wait() const { |
| 170 | const auto ret = kDevice->ping(); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 171 | HANDLE_TRANSPORT_FAILURE(ret); |
| 172 | return {}; |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const { |
| 176 | // Ensure that model is ready for IPC. |
| 177 | std::optional<nn::Model> maybeModelInShared; |
| 178 | const nn::Model& modelInShared = |
| 179 | NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared)); |
| 180 | |
| 181 | const auto hidlModel = NN_TRY(convert(modelInShared)); |
| 182 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 183 | auto cb = hal::utils::CallbackValue(supportedOperationsCallback); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 184 | |
| 185 | const auto ret = kDevice->getSupportedOperations_1_3(hidlModel, cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 186 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 187 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 188 | return cb.take(); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel( |
| 192 | const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority, |
Slava Shklyaev | d4290b8 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 193 | nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache, |
| 194 | const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 195 | // Ensure that model is ready for IPC. |
| 196 | std::optional<nn::Model> maybeModelInShared; |
| 197 | const nn::Model& modelInShared = |
| 198 | NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared)); |
| 199 | |
| 200 | const auto hidlModel = NN_TRY(convert(modelInShared)); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 201 | const auto hidlPreference = NN_TRY(convert(preference)); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 202 | const auto hidlPriority = NN_TRY(convert(priority)); |
| 203 | const auto hidlDeadline = NN_TRY(convert(deadline)); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 204 | const auto hidlModelCache = NN_TRY(convert(modelCache)); |
| 205 | const auto hidlDataCache = NN_TRY(convert(dataCache)); |
| 206 | const auto hidlToken = V1_2::utils::CacheToken{token}; |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 207 | |
| 208 | const auto cb = sp<PreparedModelCallback>::make(); |
| 209 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 210 | |
| 211 | const auto ret = |
| 212 | kDevice->prepareModel_1_3(hidlModel, hidlPreference, hidlPriority, hidlDeadline, |
| 213 | hidlModelCache, hidlDataCache, hidlToken, cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 214 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 215 | HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 216 | |
| 217 | return cb->get(); |
| 218 | } |
| 219 | |
| 220 | nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache( |
Slava Shklyaev | d4290b8 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 221 | nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache, |
| 222 | const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 223 | const auto hidlDeadline = NN_TRY(convert(deadline)); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 224 | const auto hidlModelCache = NN_TRY(convert(modelCache)); |
| 225 | const auto hidlDataCache = NN_TRY(convert(dataCache)); |
| 226 | const auto hidlToken = V1_2::utils::CacheToken{token}; |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 227 | |
| 228 | const auto cb = sp<PreparedModelCallback>::make(); |
| 229 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 230 | |
| 231 | const auto ret = kDevice->prepareModelFromCache_1_3(hidlDeadline, hidlModelCache, hidlDataCache, |
| 232 | hidlToken, cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 233 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 234 | HANDLE_HAL_STATUS(status) << "model preparation from cache failed with " << toString(status); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 235 | |
| 236 | return cb->get(); |
| 237 | } |
| 238 | |
| 239 | nn::GeneralResult<nn::SharedBuffer> Device::allocate( |
| 240 | const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels, |
| 241 | const std::vector<nn::BufferRole>& inputRoles, |
| 242 | const std::vector<nn::BufferRole>& outputRoles) const { |
| 243 | const auto hidlDesc = NN_TRY(convert(desc)); |
| 244 | const auto hidlPreparedModels = NN_TRY(convert(preparedModels)); |
| 245 | const auto hidlInputRoles = NN_TRY(convert(inputRoles)); |
| 246 | const auto hidlOutputRoles = NN_TRY(convert(outputRoles)); |
| 247 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 248 | auto cb = hal::utils::CallbackValue(allocationCallback); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 249 | |
| 250 | const auto ret = |
| 251 | kDevice->allocate(hidlDesc, hidlPreparedModels, hidlInputRoles, hidlOutputRoles, cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 252 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 253 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 254 | return cb.take(); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | } // namespace android::hardware::neuralnetworks::V1_3::utils |