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 | |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 19 | #include "Burst.h" |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 20 | #include "Callbacks.h" |
| 21 | #include "Conversions.h" |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 22 | #include "Execution.h" |
Michael Butler | 49d95e0 | 2021-10-15 18:52:52 -0700 | [diff] [blame] | 23 | #include "HandleError.h" |
Michael Butler | e8645c3 | 2021-10-15 18:42:32 -0700 | [diff] [blame] | 24 | #include "ProtectCallback.h" |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 25 | #include "Utils.h" |
| 26 | |
| 27 | #include <android/hardware/neuralnetworks/1.0/IPreparedModel.h> |
| 28 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 29 | #include <nnapi/IPreparedModel.h> |
| 30 | #include <nnapi/Result.h> |
| 31 | #include <nnapi/Types.h> |
| 32 | #include <nnapi/hal/CommonUtils.h> |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 33 | |
| 34 | #include <memory> |
| 35 | #include <tuple> |
| 36 | #include <utility> |
| 37 | #include <vector> |
| 38 | |
Michael Butler | aad934b | 2020-12-13 23:06:06 -0800 | [diff] [blame] | 39 | // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface |
| 40 | // lifetimes across processes and for protecting asynchronous calls across HIDL. |
| 41 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 42 | namespace android::hardware::neuralnetworks::V1_0::utils { |
| 43 | |
| 44 | nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create( |
| 45 | sp<V1_0::IPreparedModel> preparedModel) { |
| 46 | if (preparedModel == nullptr) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 47 | 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] | 48 | } |
| 49 | |
| 50 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel)); |
| 51 | return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel), |
| 52 | std::move(deathHandler)); |
| 53 | } |
| 54 | |
| 55 | PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, sp<V1_0::IPreparedModel> preparedModel, |
| 56 | hal::utils::DeathHandler deathHandler) |
| 57 | : kPreparedModel(std::move(preparedModel)), kDeathHandler(std::move(deathHandler)) {} |
| 58 | |
| 59 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute( |
| 60 | const nn::Request& request, nn::MeasureTiming /*measure*/, |
| 61 | const nn::OptionalTimePoint& /*deadline*/, |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 62 | const nn::OptionalDuration& /*loopTimeoutDuration*/, |
| 63 | const std::vector<nn::TokenValuePair>& /*hints*/, |
| 64 | const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 65 | // Ensure that request is ready for IPC. |
| 66 | std::optional<nn::Request> maybeRequestInShared; |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 67 | hal::utils::RequestRelocation relocation; |
Michael Butler | ff9a5a5 | 2021-10-15 16:23:20 -0700 | [diff] [blame] | 68 | const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared( |
| 69 | &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding, |
| 70 | &maybeRequestInShared, &relocation)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 71 | |
Michael Butler | ff9a5a5 | 2021-10-15 16:23:20 -0700 | [diff] [blame] | 72 | const auto hidlRequest = NN_TRY(convert(requestInShared)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 73 | |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 74 | return executeInternal(hidlRequest, relocation); |
| 75 | } |
| 76 | |
| 77 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 78 | PreparedModel::executeInternal(const V1_0::Request& request, |
| 79 | const hal::utils::RequestRelocation& relocation) const { |
| 80 | if (relocation.input) { |
| 81 | relocation.input->flush(); |
| 82 | } |
| 83 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 84 | const auto cb = sp<ExecutionCallback>::make(); |
| 85 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 86 | |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 87 | const auto ret = kPreparedModel->execute(request, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 88 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 49d95e0 | 2021-10-15 18:52:52 -0700 | [diff] [blame] | 89 | HANDLE_STATUS_HIDL(status) << "execution failed with " << toString(status); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 90 | |
| 91 | auto result = NN_TRY(cb->get()); |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 92 | if (relocation.output) { |
| 93 | relocation.output->flush(); |
| 94 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 95 | return result; |
| 96 | } |
| 97 | |
| 98 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 99 | PreparedModel::executeFenced( |
| 100 | const nn::Request& /*request*/, const std::vector<nn::SyncFence>& /*waitFor*/, |
| 101 | nn::MeasureTiming /*measure*/, const nn::OptionalTimePoint& /*deadline*/, |
| 102 | const nn::OptionalDuration& /*loopTimeoutDuration*/, |
| 103 | const nn::OptionalDuration& /*timeoutDurationAfterFence*/, |
| 104 | const std::vector<nn::TokenValuePair>& /*hints*/, |
| 105 | const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 106 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 107 | << "IPreparedModel::executeFenced is not supported on 1.0 HAL service"; |
| 108 | } |
| 109 | |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 110 | nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution( |
| 111 | const nn::Request& request, nn::MeasureTiming /*measure*/, |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 112 | const nn::OptionalDuration& /*loopTimeoutDuration*/, |
| 113 | const std::vector<nn::TokenValuePair>& /*hints*/, |
| 114 | const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const { |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 115 | // Ensure that request is ready for IPC. |
| 116 | std::optional<nn::Request> maybeRequestInShared; |
| 117 | hal::utils::RequestRelocation relocation; |
| 118 | const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared( |
Xusong Wang | e3d0dad | 2021-05-07 14:13:22 -0700 | [diff] [blame] | 119 | &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding, |
| 120 | &maybeRequestInShared, &relocation)); |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 121 | |
| 122 | auto hidlRequest = NN_TRY(convert(requestInShared)); |
| 123 | return Execution::create(shared_from_this(), std::move(hidlRequest), std::move(relocation)); |
| 124 | } |
| 125 | |
Michael Butler | 2a8b679 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 126 | nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const { |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 127 | return Burst::create(shared_from_this()); |
Michael Butler | 2a8b679 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 130 | std::any PreparedModel::getUnderlyingResource() const { |
| 131 | sp<V1_0::IPreparedModel> resource = kPreparedModel; |
| 132 | return resource; |
| 133 | } |
| 134 | |
| 135 | } // namespace android::hardware::neuralnetworks::V1_0::utils |