blob: 0061065f0bc02e262c6708c0bcac6f16713e4e08 [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 "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
44namespace android::hardware::neuralnetworks::V1_2::utils {
Slava Shklyaev77e06d82020-11-30 15:33:17 +000045namespace {
46
47nn::GeneralResult<nn::Capabilities> initCapabilities(V1_2::IDevice* device) {
48 CHECK(device != nullptr);
49
50 nn::GeneralResult<nn::Capabilities> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
51 << "uninitialized";
52 const auto cb = [&result](V1_0::ErrorStatus status, const Capabilities& capabilities) {
53 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080054 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Slava Shklyaev77e06d82020-11-30 15:33:17 +000055 result = NN_ERROR(canonical) << "getCapabilities_1_2 failed with " << toString(status);
56 } else {
Michael Butler6547b2a2020-11-22 19:36:30 -080057 result = nn::convert(capabilities);
Slava Shklyaev77e06d82020-11-30 15:33:17 +000058 }
59 };
60
61 const auto ret = device->getCapabilities_1_2(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080062 HANDLE_TRANSPORT_FAILURE(ret);
Slava Shklyaev77e06d82020-11-30 15:33:17 +000063
64 return result;
65}
66
67} // namespace
Michael Butler4b276a72020-08-06 23:22:35 -070068
69nn::GeneralResult<std::string> initVersionString(V1_2::IDevice* device) {
70 CHECK(device != nullptr);
71
72 nn::GeneralResult<std::string> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
73 << "uninitialized";
74 const auto cb = [&result](V1_0::ErrorStatus status, const hidl_string& versionString) {
75 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080076 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070077 result = NN_ERROR(canonical) << "getVersionString failed with " << toString(status);
78 } else {
79 result = versionString;
80 }
81 };
82
83 const auto ret = device->getVersionString(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080084 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070085
86 return result;
87}
88
89nn::GeneralResult<nn::DeviceType> initDeviceType(V1_2::IDevice* device) {
90 CHECK(device != nullptr);
91
92 nn::GeneralResult<nn::DeviceType> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
93 << "uninitialized";
94 const auto cb = [&result](V1_0::ErrorStatus status, DeviceType deviceType) {
95 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080096 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070097 result = NN_ERROR(canonical) << "getDeviceType failed with " << toString(status);
98 } else {
99 result = nn::convert(deviceType);
100 }
101 };
102
103 const auto ret = device->getType(cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800104 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700105
106 return result;
107}
108
109nn::GeneralResult<std::vector<nn::Extension>> initExtensions(V1_2::IDevice* device) {
110 CHECK(device != nullptr);
111
112 nn::GeneralResult<std::vector<nn::Extension>> result =
113 NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "uninitialized";
114 const auto cb = [&result](V1_0::ErrorStatus status, const hidl_vec<Extension>& extensions) {
115 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800116 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700117 result = NN_ERROR(canonical) << "getExtensions failed with " << toString(status);
118 } else {
119 result = nn::convert(extensions);
120 }
121 };
122
123 const auto ret = device->getSupportedExtensions(cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800124 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700125
126 return result;
127}
128
Michael Butler4b276a72020-08-06 23:22:35 -0700129nn::GeneralResult<std::pair<uint32_t, uint32_t>> initNumberOfCacheFilesNeeded(
130 V1_2::IDevice* device) {
131 CHECK(device != nullptr);
132
133 nn::GeneralResult<std::pair<uint32_t, uint32_t>> result =
134 NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "uninitialized";
135 const auto cb = [&result](V1_0::ErrorStatus status, uint32_t numModelCache,
136 uint32_t numDataCache) {
137 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800138 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700139 result = NN_ERROR(canonical)
140 << "getNumberOfCacheFilesNeeded failed with " << toString(status);
141 } else {
142 result = std::make_pair(numModelCache, numDataCache);
143 }
144 };
145
146 const auto ret = device->getNumberOfCacheFilesNeeded(cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800147 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700148
149 return result;
150}
151
152nn::GeneralResult<std::shared_ptr<const Device>> Device::create(std::string name,
153 sp<V1_2::IDevice> device) {
154 if (name.empty()) {
155 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
156 << "V1_2::utils::Device::create must have non-empty name";
157 }
158 if (device == nullptr) {
159 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
160 << "V1_2::utils::Device::create must have non-null device";
161 }
162
163 auto versionString = NN_TRY(initVersionString(device.get()));
164 const auto deviceType = NN_TRY(initDeviceType(device.get()));
165 auto extensions = NN_TRY(initExtensions(device.get()));
166 auto capabilities = NN_TRY(initCapabilities(device.get()));
167 const auto numberOfCacheFilesNeeded = NN_TRY(initNumberOfCacheFilesNeeded(device.get()));
168
169 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(device));
170 return std::make_shared<const Device>(
171 PrivateConstructorTag{}, std::move(name), std::move(versionString), deviceType,
172 std::move(extensions), std::move(capabilities), numberOfCacheFilesNeeded,
173 std::move(device), std::move(deathHandler));
174}
175
176Device::Device(PrivateConstructorTag /*tag*/, std::string name, std::string versionString,
177 nn::DeviceType deviceType, std::vector<nn::Extension> extensions,
178 nn::Capabilities capabilities,
179 std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded, sp<V1_2::IDevice> device,
180 hal::utils::DeathHandler deathHandler)
181 : kName(std::move(name)),
182 kVersionString(std::move(versionString)),
183 kDeviceType(deviceType),
184 kExtensions(std::move(extensions)),
185 kCapabilities(std::move(capabilities)),
186 kNumberOfCacheFilesNeeded(numberOfCacheFilesNeeded),
187 kDevice(std::move(device)),
188 kDeathHandler(std::move(deathHandler)) {}
189
190const std::string& Device::getName() const {
191 return kName;
192}
193
194const std::string& Device::getVersionString() const {
195 return kVersionString;
196}
197
198nn::Version Device::getFeatureLevel() const {
199 return nn::Version::ANDROID_Q;
200}
201
202nn::DeviceType Device::getType() const {
203 return kDeviceType;
204}
205
206const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
207 return kExtensions;
208}
209
210const nn::Capabilities& Device::getCapabilities() const {
211 return kCapabilities;
212}
213
214std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
215 return kNumberOfCacheFilesNeeded;
216}
217
218nn::GeneralResult<void> Device::wait() const {
219 const auto ret = kDevice->ping();
Michael Butlercca3e202020-11-22 20:25:34 -0800220 HANDLE_TRANSPORT_FAILURE(ret);
221 return {};
Michael Butler4b276a72020-08-06 23:22:35 -0700222}
223
224nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
225 // Ensure that model is ready for IPC.
226 std::optional<nn::Model> maybeModelInShared;
227 const nn::Model& modelInShared =
228 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
229
230 const auto hidlModel = NN_TRY(convert(modelInShared));
231
232 nn::GeneralResult<std::vector<bool>> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
233 << "uninitialized";
234 auto cb = [&result, &model](V1_0::ErrorStatus status,
235 const hidl_vec<bool>& supportedOperations) {
236 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800237 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700238 result = NN_ERROR(canonical)
239 << "getSupportedOperations_1_2 failed with " << toString(status);
240 } else if (supportedOperations.size() != model.main.operations.size()) {
241 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
242 << "getSupportedOperations_1_2 returned vector of size "
243 << supportedOperations.size() << " but expected "
244 << model.main.operations.size();
245 } else {
246 result = supportedOperations;
247 }
248 };
249
250 const auto ret = kDevice->getSupportedOperations_1_2(hidlModel, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800251 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700252
253 return result;
254}
255
256nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
257 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority /*priority*/,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000258 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& modelCache,
259 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700260 // Ensure that model is ready for IPC.
261 std::optional<nn::Model> maybeModelInShared;
262 const nn::Model& modelInShared =
263 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
264
265 const auto hidlModel = NN_TRY(convert(modelInShared));
266 const auto hidlPreference = NN_TRY(V1_1::utils::convert(preference));
267 const auto hidlModelCache = NN_TRY(convert(modelCache));
268 const auto hidlDataCache = NN_TRY(convert(dataCache));
269 const auto hidlToken = token;
270
271 const auto cb = sp<PreparedModelCallback>::make();
272 const auto scoped = kDeathHandler.protectCallback(cb.get());
273
274 const auto ret = kDevice->prepareModel_1_2(hidlModel, hidlPreference, hidlModelCache,
275 hidlDataCache, hidlToken, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800276 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700277 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800278 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700279 return NN_ERROR(canonical) << "prepareModel_1_2 failed with " << toString(status);
280 }
281
282 return cb->get();
283}
284
285nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaev49817a02020-10-27 18:44:01 +0000286 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& modelCache,
287 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700288 const auto hidlModelCache = NN_TRY(convert(modelCache));
289 const auto hidlDataCache = NN_TRY(convert(dataCache));
290 const auto hidlToken = token;
291
292 const auto cb = sp<PreparedModelCallback>::make();
293 const auto scoped = kDeathHandler.protectCallback(cb.get());
294
295 const auto ret = kDevice->prepareModelFromCache(hidlModelCache, hidlDataCache, hidlToken, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800296 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700297 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800298 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700299 return NN_ERROR(canonical) << "prepareModelFromCache failed with " << toString(status);
300 }
301
302 return cb->get();
303}
304
305nn::GeneralResult<nn::SharedBuffer> Device::allocate(
306 const nn::BufferDesc& /*desc*/,
307 const std::vector<nn::SharedPreparedModel>& /*preparedModels*/,
308 const std::vector<nn::BufferRole>& /*inputRoles*/,
309 const std::vector<nn::BufferRole>& /*outputRoles*/) const {
310 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
311 << "IDevice::allocate not supported on 1.2 HAL service";
312}
313
314} // namespace android::hardware::neuralnetworks::V1_2::utils