blob: 6cca841aba50ce6758491303ee80a3929b700812 [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 "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
Michael Butler7a655bb2020-12-13 23:06:06 -080044// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
45// lifetimes across processes and for protecting asynchronous calls across HIDL.
46
Michael Butler3670c382020-08-06 23:22:35 -070047namespace android::hardware::neuralnetworks::V1_2::utils {
Slava Shklyaevd594cd02020-11-30 15:33:17 +000048namespace {
49
50nn::GeneralResult<nn::Capabilities> initCapabilities(V1_2::IDevice* device) {
51 CHECK(device != nullptr);
52
53 nn::GeneralResult<nn::Capabilities> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
54 << "uninitialized";
55 const auto cb = [&result](V1_0::ErrorStatus status, const Capabilities& capabilities) {
56 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -080057 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Slava Shklyaevd594cd02020-11-30 15:33:17 +000058 result = NN_ERROR(canonical) << "getCapabilities_1_2 failed with " << toString(status);
59 } else {
Michael Butler32acc062020-11-22 19:36:30 -080060 result = nn::convert(capabilities);
Slava Shklyaevd594cd02020-11-30 15:33:17 +000061 }
62 };
63
64 const auto ret = device->getCapabilities_1_2(cb);
Michael Butler61f508e2020-11-22 20:25:34 -080065 HANDLE_TRANSPORT_FAILURE(ret);
Slava Shklyaevd594cd02020-11-30 15:33:17 +000066
67 return result;
68}
69
70} // namespace
Michael Butler3670c382020-08-06 23:22:35 -070071
72nn::GeneralResult<std::string> initVersionString(V1_2::IDevice* device) {
73 CHECK(device != nullptr);
74
75 nn::GeneralResult<std::string> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
76 << "uninitialized";
77 const auto cb = [&result](V1_0::ErrorStatus status, const hidl_string& versionString) {
78 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -080079 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -070080 result = NN_ERROR(canonical) << "getVersionString failed with " << toString(status);
81 } else {
82 result = versionString;
83 }
84 };
85
86 const auto ret = device->getVersionString(cb);
Michael Butler61f508e2020-11-22 20:25:34 -080087 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -070088
89 return result;
90}
91
92nn::GeneralResult<nn::DeviceType> initDeviceType(V1_2::IDevice* device) {
93 CHECK(device != nullptr);
94
95 nn::GeneralResult<nn::DeviceType> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
96 << "uninitialized";
97 const auto cb = [&result](V1_0::ErrorStatus status, DeviceType deviceType) {
98 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -080099 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700100 result = NN_ERROR(canonical) << "getDeviceType failed with " << toString(status);
101 } else {
102 result = nn::convert(deviceType);
103 }
104 };
105
106 const auto ret = device->getType(cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800107 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700108
109 return result;
110}
111
112nn::GeneralResult<std::vector<nn::Extension>> initExtensions(V1_2::IDevice* device) {
113 CHECK(device != nullptr);
114
115 nn::GeneralResult<std::vector<nn::Extension>> result =
116 NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "uninitialized";
117 const auto cb = [&result](V1_0::ErrorStatus status, const hidl_vec<Extension>& extensions) {
118 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800119 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700120 result = NN_ERROR(canonical) << "getExtensions failed with " << toString(status);
121 } else {
122 result = nn::convert(extensions);
123 }
124 };
125
126 const auto ret = device->getSupportedExtensions(cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800127 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700128
129 return result;
130}
131
Michael Butler3670c382020-08-06 23:22:35 -0700132nn::GeneralResult<std::pair<uint32_t, uint32_t>> initNumberOfCacheFilesNeeded(
133 V1_2::IDevice* device) {
134 CHECK(device != nullptr);
135
136 nn::GeneralResult<std::pair<uint32_t, uint32_t>> result =
137 NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "uninitialized";
138 const auto cb = [&result](V1_0::ErrorStatus status, uint32_t numModelCache,
139 uint32_t numDataCache) {
140 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800141 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700142 result = NN_ERROR(canonical)
143 << "getNumberOfCacheFilesNeeded failed with " << toString(status);
144 } else {
145 result = std::make_pair(numModelCache, numDataCache);
146 }
147 };
148
149 const auto ret = device->getNumberOfCacheFilesNeeded(cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800150 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700151
152 return result;
153}
154
155nn::GeneralResult<std::shared_ptr<const Device>> Device::create(std::string name,
156 sp<V1_2::IDevice> device) {
157 if (name.empty()) {
158 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
159 << "V1_2::utils::Device::create must have non-empty name";
160 }
161 if (device == nullptr) {
162 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
163 << "V1_2::utils::Device::create must have non-null device";
164 }
165
166 auto versionString = NN_TRY(initVersionString(device.get()));
167 const auto deviceType = NN_TRY(initDeviceType(device.get()));
168 auto extensions = NN_TRY(initExtensions(device.get()));
169 auto capabilities = NN_TRY(initCapabilities(device.get()));
170 const auto numberOfCacheFilesNeeded = NN_TRY(initNumberOfCacheFilesNeeded(device.get()));
171
172 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(device));
173 return std::make_shared<const Device>(
174 PrivateConstructorTag{}, std::move(name), std::move(versionString), deviceType,
175 std::move(extensions), std::move(capabilities), numberOfCacheFilesNeeded,
176 std::move(device), std::move(deathHandler));
177}
178
179Device::Device(PrivateConstructorTag /*tag*/, std::string name, std::string versionString,
180 nn::DeviceType deviceType, std::vector<nn::Extension> extensions,
181 nn::Capabilities capabilities,
182 std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded, sp<V1_2::IDevice> device,
183 hal::utils::DeathHandler deathHandler)
184 : kName(std::move(name)),
185 kVersionString(std::move(versionString)),
186 kDeviceType(deviceType),
187 kExtensions(std::move(extensions)),
188 kCapabilities(std::move(capabilities)),
189 kNumberOfCacheFilesNeeded(numberOfCacheFilesNeeded),
190 kDevice(std::move(device)),
191 kDeathHandler(std::move(deathHandler)) {}
192
193const std::string& Device::getName() const {
194 return kName;
195}
196
197const std::string& Device::getVersionString() const {
198 return kVersionString;
199}
200
201nn::Version Device::getFeatureLevel() const {
202 return nn::Version::ANDROID_Q;
203}
204
205nn::DeviceType Device::getType() const {
206 return kDeviceType;
207}
208
209const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
210 return kExtensions;
211}
212
213const nn::Capabilities& Device::getCapabilities() const {
214 return kCapabilities;
215}
216
217std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
218 return kNumberOfCacheFilesNeeded;
219}
220
221nn::GeneralResult<void> Device::wait() const {
222 const auto ret = kDevice->ping();
Michael Butler61f508e2020-11-22 20:25:34 -0800223 HANDLE_TRANSPORT_FAILURE(ret);
224 return {};
Michael Butler3670c382020-08-06 23:22:35 -0700225}
226
227nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
228 // Ensure that model is ready for IPC.
229 std::optional<nn::Model> maybeModelInShared;
230 const nn::Model& modelInShared =
231 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
232
233 const auto hidlModel = NN_TRY(convert(modelInShared));
234
235 nn::GeneralResult<std::vector<bool>> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
236 << "uninitialized";
237 auto cb = [&result, &model](V1_0::ErrorStatus status,
238 const hidl_vec<bool>& supportedOperations) {
239 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800240 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700241 result = NN_ERROR(canonical)
242 << "getSupportedOperations_1_2 failed with " << toString(status);
243 } else if (supportedOperations.size() != model.main.operations.size()) {
244 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
245 << "getSupportedOperations_1_2 returned vector of size "
246 << supportedOperations.size() << " but expected "
247 << model.main.operations.size();
248 } else {
249 result = supportedOperations;
250 }
251 };
252
253 const auto ret = kDevice->getSupportedOperations_1_2(hidlModel, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800254 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700255
256 return result;
257}
258
259nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
260 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority /*priority*/,
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000261 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& modelCache,
262 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler3670c382020-08-06 23:22:35 -0700263 // Ensure that model is ready for IPC.
264 std::optional<nn::Model> maybeModelInShared;
265 const nn::Model& modelInShared =
266 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
267
268 const auto hidlModel = NN_TRY(convert(modelInShared));
269 const auto hidlPreference = NN_TRY(V1_1::utils::convert(preference));
270 const auto hidlModelCache = NN_TRY(convert(modelCache));
271 const auto hidlDataCache = NN_TRY(convert(dataCache));
272 const auto hidlToken = token;
273
274 const auto cb = sp<PreparedModelCallback>::make();
275 const auto scoped = kDeathHandler.protectCallback(cb.get());
276
277 const auto ret = kDevice->prepareModel_1_2(hidlModel, hidlPreference, hidlModelCache,
278 hidlDataCache, hidlToken, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800279 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700280 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800281 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700282 return NN_ERROR(canonical) << "prepareModel_1_2 failed with " << toString(status);
283 }
284
285 return cb->get();
286}
287
288nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000289 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& modelCache,
290 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler3670c382020-08-06 23:22:35 -0700291 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);
Michael Butler61f508e2020-11-22 20:25:34 -0800299 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700300 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800301 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700302 return NN_ERROR(canonical) << "prepareModelFromCache failed with " << toString(status);
303 }
304
305 return cb->get();
306}
307
308nn::GeneralResult<nn::SharedBuffer> Device::allocate(
309 const nn::BufferDesc& /*desc*/,
310 const std::vector<nn::SharedPreparedModel>& /*preparedModels*/,
311 const std::vector<nn::BufferRole>& /*inputRoles*/,
312 const std::vector<nn::BufferRole>& /*outputRoles*/) const {
313 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
314 << "IDevice::allocate not supported on 1.2 HAL service";
315}
316
317} // namespace android::hardware::neuralnetworks::V1_2::utils