blob: d710b8507033c8144e5089bb80e0e04e38ec113f [file] [log] [blame]
Michael Butler3670c382020-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 Butler98ed9ba2020-12-06 21:50:59 -080039#include <nnapi/hal/1.2/Utils.h>
Michael Butler3670c382020-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 Butler7a655bb2020-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 Butler3670c382020-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 Butler98ed9ba2020-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 Butler3670c382020-08-06 23:22:35 -070077}
78
Michael Butler98ed9ba2020-12-06 21:50:59 -080079nn::GeneralResult<nn::Capabilities> getCapabilitiesFrom(V1_3::IDevice* device) {
Slava Shklyaevd594cd02020-11-30 15:33:17 +000080 CHECK(device != nullptr);
81
Michael Butler98ed9ba2020-12-06 21:50:59 -080082 auto cb = hal::utils::CallbackValue(capabilitiesCallback);
Slava Shklyaevd594cd02020-11-30 15:33:17 +000083
84 const auto ret = device->getCapabilities_1_3(cb);
Michael Butler61f508e2020-11-22 20:25:34 -080085 HANDLE_TRANSPORT_FAILURE(ret);
Slava Shklyaevd594cd02020-11-30 15:33:17 +000086
Michael Butler98ed9ba2020-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 Shklyaevd594cd02020-11-30 15:33:17 +000094}
95
Michael Butler3670c382020-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 Butler98ed9ba2020-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 Butler3670c382020-08-06 23:22:35 -0700113 const auto numberOfCacheFilesNeeded =
Michael Butler98ed9ba2020-12-06 21:50:59 -0800114 NN_TRY(V1_2::utils::getNumberOfCacheFilesNeededFrom(device.get()));
Michael Butler3670c382020-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
153const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
154 return kExtensions;
155}
156
157const nn::Capabilities& Device::getCapabilities() const {
158 return kCapabilities;
159}
160
161std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
162 return kNumberOfCacheFilesNeeded;
163}
164
165nn::GeneralResult<void> Device::wait() const {
166 const auto ret = kDevice->ping();
Michael Butler61f508e2020-11-22 20:25:34 -0800167 HANDLE_TRANSPORT_FAILURE(ret);
168 return {};
Michael Butler3670c382020-08-06 23:22:35 -0700169}
170
171nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
172 // Ensure that model is ready for IPC.
173 std::optional<nn::Model> maybeModelInShared;
174 const nn::Model& modelInShared =
175 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
176
177 const auto hidlModel = NN_TRY(convert(modelInShared));
178
Michael Butler98ed9ba2020-12-06 21:50:59 -0800179 auto cb = hal::utils::CallbackValue(supportedOperationsCallback);
Michael Butler3670c382020-08-06 23:22:35 -0700180
181 const auto ret = kDevice->getSupportedOperations_1_3(hidlModel, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800182 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700183
Michael Butler98ed9ba2020-12-06 21:50:59 -0800184 return cb.take();
Michael Butler3670c382020-08-06 23:22:35 -0700185}
186
187nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
188 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000189 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
190 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler3670c382020-08-06 23:22:35 -0700191 // Ensure that model is ready for IPC.
192 std::optional<nn::Model> maybeModelInShared;
193 const nn::Model& modelInShared =
194 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
195
196 const auto hidlModel = NN_TRY(convert(modelInShared));
Michael Butler98ed9ba2020-12-06 21:50:59 -0800197 const auto hidlPreference = NN_TRY(convert(preference));
Michael Butler3670c382020-08-06 23:22:35 -0700198 const auto hidlPriority = NN_TRY(convert(priority));
199 const auto hidlDeadline = NN_TRY(convert(deadline));
Michael Butler98ed9ba2020-12-06 21:50:59 -0800200 const auto hidlModelCache = NN_TRY(convert(modelCache));
201 const auto hidlDataCache = NN_TRY(convert(dataCache));
202 const auto hidlToken = V1_2::utils::CacheToken{token};
Michael Butler3670c382020-08-06 23:22:35 -0700203
204 const auto cb = sp<PreparedModelCallback>::make();
205 const auto scoped = kDeathHandler.protectCallback(cb.get());
206
207 const auto ret =
208 kDevice->prepareModel_1_3(hidlModel, hidlPreference, hidlPriority, hidlDeadline,
209 hidlModelCache, hidlDataCache, hidlToken, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800210 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler98ed9ba2020-12-06 21:50:59 -0800211 HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
Michael Butler3670c382020-08-06 23:22:35 -0700212
213 return cb->get();
214}
215
216nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000217 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
218 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler3670c382020-08-06 23:22:35 -0700219 const auto hidlDeadline = NN_TRY(convert(deadline));
Michael Butler98ed9ba2020-12-06 21:50:59 -0800220 const auto hidlModelCache = NN_TRY(convert(modelCache));
221 const auto hidlDataCache = NN_TRY(convert(dataCache));
222 const auto hidlToken = V1_2::utils::CacheToken{token};
Michael Butler3670c382020-08-06 23:22:35 -0700223
224 const auto cb = sp<PreparedModelCallback>::make();
225 const auto scoped = kDeathHandler.protectCallback(cb.get());
226
227 const auto ret = kDevice->prepareModelFromCache_1_3(hidlDeadline, hidlModelCache, hidlDataCache,
228 hidlToken, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800229 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler98ed9ba2020-12-06 21:50:59 -0800230 HANDLE_HAL_STATUS(status) << "model preparation from cache failed with " << toString(status);
Michael Butler3670c382020-08-06 23:22:35 -0700231
232 return cb->get();
233}
234
235nn::GeneralResult<nn::SharedBuffer> Device::allocate(
236 const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
237 const std::vector<nn::BufferRole>& inputRoles,
238 const std::vector<nn::BufferRole>& outputRoles) const {
239 const auto hidlDesc = NN_TRY(convert(desc));
240 const auto hidlPreparedModels = NN_TRY(convert(preparedModels));
241 const auto hidlInputRoles = NN_TRY(convert(inputRoles));
242 const auto hidlOutputRoles = NN_TRY(convert(outputRoles));
243
Michael Butler98ed9ba2020-12-06 21:50:59 -0800244 auto cb = hal::utils::CallbackValue(allocationCallback);
Michael Butler3670c382020-08-06 23:22:35 -0700245
246 const auto ret =
247 kDevice->allocate(hidlDesc, hidlPreparedModels, hidlInputRoles, hidlOutputRoles, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800248 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700249
Michael Butler98ed9ba2020-12-06 21:50:59 -0800250 return cb.take();
Michael Butler3670c382020-08-06 23:22:35 -0700251}
252
253} // namespace android::hardware::neuralnetworks::V1_3::utils