blob: 967a252c88c60d116b5cc8beac4ef520b9f1509b [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);
62 NN_TRY(hal::utils::handleTransportError(ret));
63
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);
84 NN_TRY(hal::utils::handleTransportError(ret));
85
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);
104 NN_TRY(hal::utils::handleTransportError(ret));
105
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);
124 NN_TRY(hal::utils::handleTransportError(ret));
125
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);
147 NN_TRY(hal::utils::handleTransportError(ret));
148
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();
220 return hal::utils::handleTransportError(ret);
221}
222
223nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
224 // Ensure that model is ready for IPC.
225 std::optional<nn::Model> maybeModelInShared;
226 const nn::Model& modelInShared =
227 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
228
229 const auto hidlModel = NN_TRY(convert(modelInShared));
230
231 nn::GeneralResult<std::vector<bool>> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
232 << "uninitialized";
233 auto cb = [&result, &model](V1_0::ErrorStatus status,
234 const hidl_vec<bool>& supportedOperations) {
235 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800236 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700237 result = NN_ERROR(canonical)
238 << "getSupportedOperations_1_2 failed with " << toString(status);
239 } else if (supportedOperations.size() != model.main.operations.size()) {
240 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
241 << "getSupportedOperations_1_2 returned vector of size "
242 << supportedOperations.size() << " but expected "
243 << model.main.operations.size();
244 } else {
245 result = supportedOperations;
246 }
247 };
248
249 const auto ret = kDevice->getSupportedOperations_1_2(hidlModel, cb);
250 NN_TRY(hal::utils::handleTransportError(ret));
251
252 return result;
253}
254
255nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
256 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority /*priority*/,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000257 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& modelCache,
258 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700259 // Ensure that model is ready for IPC.
260 std::optional<nn::Model> maybeModelInShared;
261 const nn::Model& modelInShared =
262 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
263
264 const auto hidlModel = NN_TRY(convert(modelInShared));
265 const auto hidlPreference = NN_TRY(V1_1::utils::convert(preference));
266 const auto hidlModelCache = NN_TRY(convert(modelCache));
267 const auto hidlDataCache = NN_TRY(convert(dataCache));
268 const auto hidlToken = token;
269
270 const auto cb = sp<PreparedModelCallback>::make();
271 const auto scoped = kDeathHandler.protectCallback(cb.get());
272
273 const auto ret = kDevice->prepareModel_1_2(hidlModel, hidlPreference, hidlModelCache,
274 hidlDataCache, hidlToken, cb);
275 const auto status = NN_TRY(hal::utils::handleTransportError(ret));
276 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800277 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700278 return NN_ERROR(canonical) << "prepareModel_1_2 failed with " << toString(status);
279 }
280
281 return cb->get();
282}
283
284nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaev49817a02020-10-27 18:44:01 +0000285 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& modelCache,
286 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700287 const auto hidlModelCache = NN_TRY(convert(modelCache));
288 const auto hidlDataCache = NN_TRY(convert(dataCache));
289 const auto hidlToken = token;
290
291 const auto cb = sp<PreparedModelCallback>::make();
292 const auto scoped = kDeathHandler.protectCallback(cb.get());
293
294 const auto ret = kDevice->prepareModelFromCache(hidlModelCache, hidlDataCache, hidlToken, cb);
295 const auto status = NN_TRY(hal::utils::handleTransportError(ret));
296 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800297 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700298 return NN_ERROR(canonical) << "prepareModelFromCache failed with " << toString(status);
299 }
300
301 return cb->get();
302}
303
304nn::GeneralResult<nn::SharedBuffer> Device::allocate(
305 const nn::BufferDesc& /*desc*/,
306 const std::vector<nn::SharedPreparedModel>& /*preparedModels*/,
307 const std::vector<nn::BufferRole>& /*inputRoles*/,
308 const std::vector<nn::BufferRole>& /*outputRoles*/) const {
309 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
310 << "IDevice::allocate not supported on 1.2 HAL service";
311}
312
313} // namespace android::hardware::neuralnetworks::V1_2::utils