blob: 60564985de979476363396e0428801a0c8bc4f1a [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>
39#include <nnapi/hal/CommonUtils.h>
40#include <nnapi/hal/HandleError.h>
41#include <nnapi/hal/ProtectCallback.h>
42
43#include <any>
44#include <functional>
45#include <memory>
46#include <optional>
47#include <string>
48#include <vector>
49
Michael Butler7a655bb2020-12-13 23:06:06 -080050// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
51// lifetimes across processes and for protecting asynchronous calls across HIDL.
52
Michael Butler3670c382020-08-06 23:22:35 -070053namespace android::hardware::neuralnetworks::V1_3::utils {
54namespace {
55
56nn::GeneralResult<hidl_vec<sp<IPreparedModel>>> convert(
57 const std::vector<nn::SharedPreparedModel>& preparedModels) {
58 hidl_vec<sp<IPreparedModel>> hidlPreparedModels(preparedModels.size());
59 for (size_t i = 0; i < preparedModels.size(); ++i) {
60 std::any underlyingResource = preparedModels[i]->getUnderlyingResource();
61 if (const auto* hidlPreparedModel =
62 std::any_cast<sp<IPreparedModel>>(&underlyingResource)) {
63 hidlPreparedModels[i] = *hidlPreparedModel;
64 } else {
65 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
66 << "Unable to convert from nn::IPreparedModel to V1_3::IPreparedModel";
67 }
68 }
69 return hidlPreparedModels;
70}
71
72nn::GeneralResult<nn::SharedBuffer> convert(
73 nn::GeneralResult<std::shared_ptr<const Buffer>> result) {
74 return NN_TRY(std::move(result));
75}
76
Slava Shklyaevd594cd02020-11-30 15:33:17 +000077nn::GeneralResult<nn::Capabilities> initCapabilities(V1_3::IDevice* device) {
78 CHECK(device != nullptr);
79
80 nn::GeneralResult<nn::Capabilities> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
81 << "uninitialized";
82 const auto cb = [&result](ErrorStatus status, const Capabilities& capabilities) {
83 if (status != ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -080084 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Slava Shklyaevd594cd02020-11-30 15:33:17 +000085 result = NN_ERROR(canonical) << "getCapabilities_1_3 failed with " << toString(status);
86 } else {
Michael Butler32acc062020-11-22 19:36:30 -080087 result = nn::convert(capabilities);
Slava Shklyaevd594cd02020-11-30 15:33:17 +000088 }
89 };
90
91 const auto ret = device->getCapabilities_1_3(cb);
Michael Butler61f508e2020-11-22 20:25:34 -080092 HANDLE_TRANSPORT_FAILURE(ret);
Slava Shklyaevd594cd02020-11-30 15:33:17 +000093
94 return result;
95}
96
Michael Butler3670c382020-08-06 23:22:35 -070097} // namespace
98
99nn::GeneralResult<std::shared_ptr<const Device>> Device::create(std::string name,
100 sp<V1_3::IDevice> device) {
101 if (name.empty()) {
102 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
103 << "V1_3::utils::Device::create must have non-empty name";
104 }
105 if (device == nullptr) {
106 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
107 << "V1_3::utils::Device::create must have non-null device";
108 }
109
110 auto versionString = NN_TRY(V1_2::utils::initVersionString(device.get()));
111 const auto deviceType = NN_TRY(V1_2::utils::initDeviceType(device.get()));
112 auto extensions = NN_TRY(V1_2::utils::initExtensions(device.get()));
Slava Shklyaevd594cd02020-11-30 15:33:17 +0000113 auto capabilities = NN_TRY(initCapabilities(device.get()));
Michael Butler3670c382020-08-06 23:22:35 -0700114 const auto numberOfCacheFilesNeeded =
115 NN_TRY(V1_2::utils::initNumberOfCacheFilesNeeded(device.get()));
116
117 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(device));
118 return std::make_shared<const Device>(
119 PrivateConstructorTag{}, std::move(name), std::move(versionString), deviceType,
120 std::move(extensions), std::move(capabilities), numberOfCacheFilesNeeded,
121 std::move(device), std::move(deathHandler));
122}
123
124Device::Device(PrivateConstructorTag /*tag*/, std::string name, std::string versionString,
125 nn::DeviceType deviceType, std::vector<nn::Extension> extensions,
126 nn::Capabilities capabilities,
127 std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded, sp<V1_3::IDevice> device,
128 hal::utils::DeathHandler deathHandler)
129 : kName(std::move(name)),
130 kVersionString(std::move(versionString)),
131 kDeviceType(deviceType),
132 kExtensions(std::move(extensions)),
133 kCapabilities(std::move(capabilities)),
134 kNumberOfCacheFilesNeeded(numberOfCacheFilesNeeded),
135 kDevice(std::move(device)),
136 kDeathHandler(std::move(deathHandler)) {}
137
138const std::string& Device::getName() const {
139 return kName;
140}
141
142const std::string& Device::getVersionString() const {
143 return kVersionString;
144}
145
146nn::Version Device::getFeatureLevel() const {
147 return nn::Version::ANDROID_R;
148}
149
150nn::DeviceType Device::getType() const {
151 return kDeviceType;
152}
153
154const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
155 return kExtensions;
156}
157
158const nn::Capabilities& Device::getCapabilities() const {
159 return kCapabilities;
160}
161
162std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
163 return kNumberOfCacheFilesNeeded;
164}
165
166nn::GeneralResult<void> Device::wait() const {
167 const auto ret = kDevice->ping();
Michael Butler61f508e2020-11-22 20:25:34 -0800168 HANDLE_TRANSPORT_FAILURE(ret);
169 return {};
Michael Butler3670c382020-08-06 23:22:35 -0700170}
171
172nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
173 // Ensure that model is ready for IPC.
174 std::optional<nn::Model> maybeModelInShared;
175 const nn::Model& modelInShared =
176 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
177
178 const auto hidlModel = NN_TRY(convert(modelInShared));
179
180 nn::GeneralResult<std::vector<bool>> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
181 << "uninitialized";
182 auto cb = [&result, &model](ErrorStatus status, const hidl_vec<bool>& supportedOperations) {
183 if (status != ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800184 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700185 result = NN_ERROR(canonical)
186 << "IDevice::getSupportedOperations_1_3 failed with " << toString(status);
187 } else if (supportedOperations.size() != model.main.operations.size()) {
188 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
189 << "IDevice::getSupportedOperations_1_3 returned vector of size "
190 << supportedOperations.size() << " but expected "
191 << model.main.operations.size();
192 } else {
193 result = supportedOperations;
194 }
195 };
196
197 const auto ret = kDevice->getSupportedOperations_1_3(hidlModel, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800198 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700199
200 return result;
201}
202
203nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
204 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000205 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
206 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler3670c382020-08-06 23:22:35 -0700207 // Ensure that model is ready for IPC.
208 std::optional<nn::Model> maybeModelInShared;
209 const nn::Model& modelInShared =
210 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
211
212 const auto hidlModel = NN_TRY(convert(modelInShared));
213 const auto hidlPreference = NN_TRY(V1_1::utils::convert(preference));
214 const auto hidlPriority = NN_TRY(convert(priority));
215 const auto hidlDeadline = NN_TRY(convert(deadline));
216 const auto hidlModelCache = NN_TRY(V1_2::utils::convert(modelCache));
217 const auto hidlDataCache = NN_TRY(V1_2::utils::convert(dataCache));
218 const auto hidlToken = token;
219
220 const auto cb = sp<PreparedModelCallback>::make();
221 const auto scoped = kDeathHandler.protectCallback(cb.get());
222
223 const auto ret =
224 kDevice->prepareModel_1_3(hidlModel, hidlPreference, hidlPriority, hidlDeadline,
225 hidlModelCache, hidlDataCache, hidlToken, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800226 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700227 if (status != ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800228 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700229 return NN_ERROR(canonical) << "prepareModel_1_3 failed with " << toString(status);
230 }
231
232 return cb->get();
233}
234
235nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000236 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
237 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler3670c382020-08-06 23:22:35 -0700238 const auto hidlDeadline = NN_TRY(convert(deadline));
239 const auto hidlModelCache = NN_TRY(V1_2::utils::convert(modelCache));
240 const auto hidlDataCache = NN_TRY(V1_2::utils::convert(dataCache));
241 const auto hidlToken = token;
242
243 const auto cb = sp<PreparedModelCallback>::make();
244 const auto scoped = kDeathHandler.protectCallback(cb.get());
245
246 const auto ret = kDevice->prepareModelFromCache_1_3(hidlDeadline, hidlModelCache, hidlDataCache,
247 hidlToken, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800248 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700249 if (status != ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800250 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700251 return NN_ERROR(canonical) << "prepareModelFromCache_1_3 failed with " << toString(status);
252 }
253
254 return cb->get();
255}
256
257nn::GeneralResult<nn::SharedBuffer> Device::allocate(
258 const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
259 const std::vector<nn::BufferRole>& inputRoles,
260 const std::vector<nn::BufferRole>& outputRoles) const {
261 const auto hidlDesc = NN_TRY(convert(desc));
262 const auto hidlPreparedModels = NN_TRY(convert(preparedModels));
263 const auto hidlInputRoles = NN_TRY(convert(inputRoles));
264 const auto hidlOutputRoles = NN_TRY(convert(outputRoles));
265
266 nn::GeneralResult<nn::SharedBuffer> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
267 << "uninitialized";
268 auto cb = [&result](ErrorStatus status, const sp<IBuffer>& buffer, uint32_t token) {
269 if (status != ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800270 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700271 result = NN_ERROR(canonical) << "IDevice::allocate failed with " << toString(status);
272 } else if (buffer == nullptr) {
273 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Returned buffer is nullptr";
274 } else if (token == 0) {
275 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Returned token is invalid (0)";
276 } else {
277 result = convert(
278 Buffer::create(buffer, static_cast<nn::Request::MemoryDomainToken>(token)));
279 }
280 };
281
282 const auto ret =
283 kDevice->allocate(hidlDesc, hidlPreparedModels, hidlInputRoles, hidlOutputRoles, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800284 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700285
286 return result;
287}
288
289} // namespace android::hardware::neuralnetworks::V1_3::utils