Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 1 | /* |
| 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 "PreparedModel.h" |
| 18 | |
| 19 | #include "Callbacks.h" |
| 20 | #include "Conversions.h" |
| 21 | #include "Utils.h" |
| 22 | |
| 23 | #include <android/hardware/neuralnetworks/1.0/IPreparedModel.h> |
| 24 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 25 | #include <nnapi/IPreparedModel.h> |
| 26 | #include <nnapi/Result.h> |
| 27 | #include <nnapi/Types.h> |
| 28 | #include <nnapi/hal/CommonUtils.h> |
| 29 | #include <nnapi/hal/HandleError.h> |
| 30 | #include <nnapi/hal/ProtectCallback.h> |
| 31 | |
| 32 | #include <memory> |
| 33 | #include <tuple> |
| 34 | #include <utility> |
| 35 | #include <vector> |
| 36 | |
Michael Butler | aad934b | 2020-12-13 23:06:06 -0800 | [diff] [blame] | 37 | // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface |
| 38 | // lifetimes across processes and for protecting asynchronous calls across HIDL. |
| 39 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 40 | namespace android::hardware::neuralnetworks::V1_0::utils { |
| 41 | |
| 42 | nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create( |
| 43 | sp<V1_0::IPreparedModel> preparedModel) { |
| 44 | if (preparedModel == nullptr) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 45 | return NN_ERROR() << "V1_0::utils::PreparedModel::create must have non-null preparedModel"; |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel)); |
| 49 | return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel), |
| 50 | std::move(deathHandler)); |
| 51 | } |
| 52 | |
| 53 | PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, sp<V1_0::IPreparedModel> preparedModel, |
| 54 | hal::utils::DeathHandler deathHandler) |
| 55 | : kPreparedModel(std::move(preparedModel)), kDeathHandler(std::move(deathHandler)) {} |
| 56 | |
| 57 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute( |
| 58 | const nn::Request& request, nn::MeasureTiming /*measure*/, |
| 59 | const nn::OptionalTimePoint& /*deadline*/, |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 60 | const nn::OptionalDuration& /*loopTimeoutDuration*/) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 61 | // Ensure that request is ready for IPC. |
| 62 | std::optional<nn::Request> maybeRequestInShared; |
| 63 | const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure( |
| 64 | hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared))); |
| 65 | |
| 66 | const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared))); |
| 67 | |
| 68 | const auto cb = sp<ExecutionCallback>::make(); |
| 69 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 70 | |
| 71 | const auto ret = kPreparedModel->execute(hidlRequest, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 72 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame^] | 73 | HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 74 | |
| 75 | auto result = NN_TRY(cb->get()); |
| 76 | NN_TRY(hal::utils::makeExecutionFailure( |
| 77 | hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared))); |
| 78 | |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 83 | PreparedModel::executeFenced(const nn::Request& /*request*/, |
| 84 | const std::vector<nn::SyncFence>& /*waitFor*/, |
| 85 | nn::MeasureTiming /*measure*/, |
| 86 | const nn::OptionalTimePoint& /*deadline*/, |
| 87 | const nn::OptionalDuration& /*loopTimeoutDuration*/, |
| 88 | const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 89 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 90 | << "IPreparedModel::executeFenced is not supported on 1.0 HAL service"; |
| 91 | } |
| 92 | |
| 93 | std::any PreparedModel::getUnderlyingResource() const { |
| 94 | sp<V1_0::IPreparedModel> resource = kPreparedModel; |
| 95 | return resource; |
| 96 | } |
| 97 | |
| 98 | } // namespace android::hardware::neuralnetworks::V1_0::utils |