blob: 87c9f32e4d133e847e31f292c1d717c700189e7f [file] [log] [blame]
Michael Butler4b276a72020-08-06 23:22:35 -07001/*
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 Butler7fd03c22020-12-06 21:50:59 -080039#include <nnapi/hal/1.2/Utils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070040#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 Butleraad934b2020-12-13 23:06:06 -080051// 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 Butler4b276a72020-08-06 23:22:35 -070054namespace android::hardware::neuralnetworks::V1_3::utils {
55namespace {
56
57nn::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 Butler7fd03c22020-12-06 21:50:59 -080073nn::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 Butler4b276a72020-08-06 23:22:35 -070077}
78
Michael Butler7fd03c22020-12-06 21:50:59 -080079nn::GeneralResult<nn::Capabilities> getCapabilitiesFrom(V1_3::IDevice* device) {
Slava Shklyaev77e06d82020-11-30 15:33:17 +000080 CHECK(device != nullptr);
81
Michael Butler7fd03c22020-12-06 21:50:59 -080082 auto cb = hal::utils::CallbackValue(capabilitiesCallback);
Slava Shklyaev77e06d82020-11-30 15:33:17 +000083
84 const auto ret = device->getCapabilities_1_3(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080085 HANDLE_TRANSPORT_FAILURE(ret);
Slava Shklyaev77e06d82020-11-30 15:33:17 +000086
Michael Butler7fd03c22020-12-06 21:50:59 -080087 return cb.take();
88}
89
90nn::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 Shklyaev77e06d82020-11-30 15:33:17 +000094}
95
Michael Butler4b276a72020-08-06 23:22:35 -070096} // namespace
97
98nn::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 Butler7fd03c22020-12-06 21:50:59 -0800109 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 Butler4b276a72020-08-06 23:22:35 -0700113 const auto numberOfCacheFilesNeeded =
Michael Butler7fd03c22020-12-06 21:50:59 -0800114 NN_TRY(V1_2::utils::getNumberOfCacheFilesNeededFrom(device.get()));
Michael Butler4b276a72020-08-06 23:22:35 -0700115
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
123Device::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
137const std::string& Device::getName() const {
138 return kName;
139}
140
141const std::string& Device::getVersionString() const {
142 return kVersionString;
143}
144
145nn::Version Device::getFeatureLevel() const {
146 return nn::Version::ANDROID_R;
147}
148
149nn::DeviceType Device::getType() const {
150 return kDeviceType;
151}
152
Michael Butler9adab0c2021-01-11 19:35:53 -0800153bool Device::isUpdatable() const {
154 return false;
155}
156
Michael Butler4b276a72020-08-06 23:22:35 -0700157const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
158 return kExtensions;
159}
160
161const nn::Capabilities& Device::getCapabilities() const {
162 return kCapabilities;
163}
164
165std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
166 return kNumberOfCacheFilesNeeded;
167}
168
169nn::GeneralResult<void> Device::wait() const {
170 const auto ret = kDevice->ping();
Michael Butlercca3e202020-11-22 20:25:34 -0800171 HANDLE_TRANSPORT_FAILURE(ret);
172 return {};
Michael Butler4b276a72020-08-06 23:22:35 -0700173}
174
175nn::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 Butler7fd03c22020-12-06 21:50:59 -0800183 auto cb = hal::utils::CallbackValue(supportedOperationsCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700184
185 const auto ret = kDevice->getSupportedOperations_1_3(hidlModel, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800186 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700187
Michael Butler7fd03c22020-12-06 21:50:59 -0800188 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -0700189}
190
191nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
192 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000193 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
194 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700195 // 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 Butler7fd03c22020-12-06 21:50:59 -0800201 const auto hidlPreference = NN_TRY(convert(preference));
Michael Butler4b276a72020-08-06 23:22:35 -0700202 const auto hidlPriority = NN_TRY(convert(priority));
203 const auto hidlDeadline = NN_TRY(convert(deadline));
Michael Butler7fd03c22020-12-06 21:50:59 -0800204 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 Butler4b276a72020-08-06 23:22:35 -0700207
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 Butlercca3e202020-11-22 20:25:34 -0800214 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800215 HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -0700216
217 return cb->get();
218}
219
220nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaev49817a02020-10-27 18:44:01 +0000221 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
222 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700223 const auto hidlDeadline = NN_TRY(convert(deadline));
Michael Butler7fd03c22020-12-06 21:50:59 -0800224 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 Butler4b276a72020-08-06 23:22:35 -0700227
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 Butlercca3e202020-11-22 20:25:34 -0800233 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -0800234 HANDLE_HAL_STATUS(status) << "model preparation from cache failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -0700235
236 return cb->get();
237}
238
239nn::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 Butler7fd03c22020-12-06 21:50:59 -0800248 auto cb = hal::utils::CallbackValue(allocationCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700249
250 const auto ret =
251 kDevice->allocate(hidlDesc, hidlPreparedModels, hidlInputRoles, hidlOutputRoles, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800252 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700253
Michael Butler7fd03c22020-12-06 21:50:59 -0800254 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -0700255}
256
257} // namespace android::hardware::neuralnetworks::V1_3::utils