blob: 82837bac736ca3429baa3f36bed9a4f34d752392 [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 "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
50namespace android::hardware::neuralnetworks::V1_3::utils {
51namespace {
52
53nn::GeneralResult<hidl_vec<sp<IPreparedModel>>> convert(
54 const std::vector<nn::SharedPreparedModel>& preparedModels) {
55 hidl_vec<sp<IPreparedModel>> hidlPreparedModels(preparedModels.size());
56 for (size_t i = 0; i < preparedModels.size(); ++i) {
57 std::any underlyingResource = preparedModels[i]->getUnderlyingResource();
58 if (const auto* hidlPreparedModel =
59 std::any_cast<sp<IPreparedModel>>(&underlyingResource)) {
60 hidlPreparedModels[i] = *hidlPreparedModel;
61 } else {
62 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
63 << "Unable to convert from nn::IPreparedModel to V1_3::IPreparedModel";
64 }
65 }
66 return hidlPreparedModels;
67}
68
69nn::GeneralResult<nn::SharedBuffer> convert(
70 nn::GeneralResult<std::shared_ptr<const Buffer>> result) {
71 return NN_TRY(std::move(result));
72}
73
Slava Shklyaev77e06d82020-11-30 15:33:17 +000074nn::GeneralResult<nn::Capabilities> initCapabilities(V1_3::IDevice* device) {
75 CHECK(device != nullptr);
76
77 nn::GeneralResult<nn::Capabilities> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
78 << "uninitialized";
79 const auto cb = [&result](ErrorStatus status, const Capabilities& capabilities) {
80 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080081 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Slava Shklyaev77e06d82020-11-30 15:33:17 +000082 result = NN_ERROR(canonical) << "getCapabilities_1_3 failed with " << toString(status);
83 } else {
Michael Butler6547b2a2020-11-22 19:36:30 -080084 result = nn::convert(capabilities);
Slava Shklyaev77e06d82020-11-30 15:33:17 +000085 }
86 };
87
88 const auto ret = device->getCapabilities_1_3(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080089 HANDLE_TRANSPORT_FAILURE(ret);
Slava Shklyaev77e06d82020-11-30 15:33:17 +000090
91 return result;
92}
93
Michael Butler4b276a72020-08-06 23:22:35 -070094} // namespace
95
96nn::GeneralResult<std::shared_ptr<const Device>> Device::create(std::string name,
97 sp<V1_3::IDevice> device) {
98 if (name.empty()) {
99 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
100 << "V1_3::utils::Device::create must have non-empty name";
101 }
102 if (device == nullptr) {
103 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
104 << "V1_3::utils::Device::create must have non-null device";
105 }
106
107 auto versionString = NN_TRY(V1_2::utils::initVersionString(device.get()));
108 const auto deviceType = NN_TRY(V1_2::utils::initDeviceType(device.get()));
109 auto extensions = NN_TRY(V1_2::utils::initExtensions(device.get()));
Slava Shklyaev77e06d82020-11-30 15:33:17 +0000110 auto capabilities = NN_TRY(initCapabilities(device.get()));
Michael Butler4b276a72020-08-06 23:22:35 -0700111 const auto numberOfCacheFilesNeeded =
112 NN_TRY(V1_2::utils::initNumberOfCacheFilesNeeded(device.get()));
113
114 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(device));
115 return std::make_shared<const Device>(
116 PrivateConstructorTag{}, std::move(name), std::move(versionString), deviceType,
117 std::move(extensions), std::move(capabilities), numberOfCacheFilesNeeded,
118 std::move(device), std::move(deathHandler));
119}
120
121Device::Device(PrivateConstructorTag /*tag*/, std::string name, std::string versionString,
122 nn::DeviceType deviceType, std::vector<nn::Extension> extensions,
123 nn::Capabilities capabilities,
124 std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded, sp<V1_3::IDevice> device,
125 hal::utils::DeathHandler deathHandler)
126 : kName(std::move(name)),
127 kVersionString(std::move(versionString)),
128 kDeviceType(deviceType),
129 kExtensions(std::move(extensions)),
130 kCapabilities(std::move(capabilities)),
131 kNumberOfCacheFilesNeeded(numberOfCacheFilesNeeded),
132 kDevice(std::move(device)),
133 kDeathHandler(std::move(deathHandler)) {}
134
135const std::string& Device::getName() const {
136 return kName;
137}
138
139const std::string& Device::getVersionString() const {
140 return kVersionString;
141}
142
143nn::Version Device::getFeatureLevel() const {
144 return nn::Version::ANDROID_R;
145}
146
147nn::DeviceType Device::getType() const {
148 return kDeviceType;
149}
150
151const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
152 return kExtensions;
153}
154
155const nn::Capabilities& Device::getCapabilities() const {
156 return kCapabilities;
157}
158
159std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
160 return kNumberOfCacheFilesNeeded;
161}
162
163nn::GeneralResult<void> Device::wait() const {
164 const auto ret = kDevice->ping();
Michael Butlercca3e202020-11-22 20:25:34 -0800165 HANDLE_TRANSPORT_FAILURE(ret);
166 return {};
Michael Butler4b276a72020-08-06 23:22:35 -0700167}
168
169nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
170 // Ensure that model is ready for IPC.
171 std::optional<nn::Model> maybeModelInShared;
172 const nn::Model& modelInShared =
173 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
174
175 const auto hidlModel = NN_TRY(convert(modelInShared));
176
177 nn::GeneralResult<std::vector<bool>> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
178 << "uninitialized";
179 auto cb = [&result, &model](ErrorStatus status, const hidl_vec<bool>& supportedOperations) {
180 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800181 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700182 result = NN_ERROR(canonical)
183 << "IDevice::getSupportedOperations_1_3 failed with " << toString(status);
184 } else if (supportedOperations.size() != model.main.operations.size()) {
185 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
186 << "IDevice::getSupportedOperations_1_3 returned vector of size "
187 << supportedOperations.size() << " but expected "
188 << model.main.operations.size();
189 } else {
190 result = supportedOperations;
191 }
192 };
193
194 const auto ret = kDevice->getSupportedOperations_1_3(hidlModel, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800195 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700196
197 return result;
198}
199
200nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
201 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000202 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
203 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700204 // Ensure that model is ready for IPC.
205 std::optional<nn::Model> maybeModelInShared;
206 const nn::Model& modelInShared =
207 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
208
209 const auto hidlModel = NN_TRY(convert(modelInShared));
210 const auto hidlPreference = NN_TRY(V1_1::utils::convert(preference));
211 const auto hidlPriority = NN_TRY(convert(priority));
212 const auto hidlDeadline = NN_TRY(convert(deadline));
213 const auto hidlModelCache = NN_TRY(V1_2::utils::convert(modelCache));
214 const auto hidlDataCache = NN_TRY(V1_2::utils::convert(dataCache));
215 const auto hidlToken = token;
216
217 const auto cb = sp<PreparedModelCallback>::make();
218 const auto scoped = kDeathHandler.protectCallback(cb.get());
219
220 const auto ret =
221 kDevice->prepareModel_1_3(hidlModel, hidlPreference, hidlPriority, hidlDeadline,
222 hidlModelCache, hidlDataCache, hidlToken, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800223 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700224 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800225 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700226 return NN_ERROR(canonical) << "prepareModel_1_3 failed with " << toString(status);
227 }
228
229 return cb->get();
230}
231
232nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaev49817a02020-10-27 18:44:01 +0000233 nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
234 const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700235 const auto hidlDeadline = NN_TRY(convert(deadline));
236 const auto hidlModelCache = NN_TRY(V1_2::utils::convert(modelCache));
237 const auto hidlDataCache = NN_TRY(V1_2::utils::convert(dataCache));
238 const auto hidlToken = token;
239
240 const auto cb = sp<PreparedModelCallback>::make();
241 const auto scoped = kDeathHandler.protectCallback(cb.get());
242
243 const auto ret = kDevice->prepareModelFromCache_1_3(hidlDeadline, hidlModelCache, hidlDataCache,
244 hidlToken, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800245 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700246 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800247 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700248 return NN_ERROR(canonical) << "prepareModelFromCache_1_3 failed with " << toString(status);
249 }
250
251 return cb->get();
252}
253
254nn::GeneralResult<nn::SharedBuffer> Device::allocate(
255 const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
256 const std::vector<nn::BufferRole>& inputRoles,
257 const std::vector<nn::BufferRole>& outputRoles) const {
258 const auto hidlDesc = NN_TRY(convert(desc));
259 const auto hidlPreparedModels = NN_TRY(convert(preparedModels));
260 const auto hidlInputRoles = NN_TRY(convert(inputRoles));
261 const auto hidlOutputRoles = NN_TRY(convert(outputRoles));
262
263 nn::GeneralResult<nn::SharedBuffer> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
264 << "uninitialized";
265 auto cb = [&result](ErrorStatus status, const sp<IBuffer>& buffer, uint32_t token) {
266 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800267 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700268 result = NN_ERROR(canonical) << "IDevice::allocate failed with " << toString(status);
269 } else if (buffer == nullptr) {
270 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Returned buffer is nullptr";
271 } else if (token == 0) {
272 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Returned token is invalid (0)";
273 } else {
274 result = convert(
275 Buffer::create(buffer, static_cast<nn::Request::MemoryDomainToken>(token)));
276 }
277 };
278
279 const auto ret =
280 kDevice->allocate(hidlDesc, hidlPreparedModels, hidlInputRoles, hidlOutputRoles, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800281 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700282
283 return result;
284}
285
286} // namespace android::hardware::neuralnetworks::V1_3::utils