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" |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 21 | #include "Execution.h" |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 22 | #include "ExecutionBurstController.h" |
| 23 | #include "ExecutionBurstUtils.h" |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 24 | #include "Utils.h" |
| 25 | |
| 26 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 27 | #include <android/hardware/neuralnetworks/1.1/types.h> |
| 28 | #include <android/hardware/neuralnetworks/1.2/IPreparedModel.h> |
| 29 | #include <android/hardware/neuralnetworks/1.2/types.h> |
| 30 | #include <nnapi/IPreparedModel.h> |
| 31 | #include <nnapi/Result.h> |
| 32 | #include <nnapi/Types.h> |
| 33 | #include <nnapi/hal/1.0/Conversions.h> |
| 34 | #include <nnapi/hal/CommonUtils.h> |
| 35 | #include <nnapi/hal/HandleError.h> |
| 36 | #include <nnapi/hal/ProtectCallback.h> |
| 37 | |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 38 | #include <chrono> |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 39 | #include <memory> |
| 40 | #include <tuple> |
| 41 | #include <utility> |
| 42 | #include <vector> |
| 43 | |
Michael Butler | aad934b | 2020-12-13 23:06:06 -0800 | [diff] [blame] | 44 | // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface |
| 45 | // lifetimes across processes and for protecting asynchronous calls across HIDL. |
| 46 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 47 | namespace android::hardware::neuralnetworks::V1_2::utils { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 48 | |
| 49 | nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create( |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 50 | sp<V1_2::IPreparedModel> preparedModel, bool executeSynchronously) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 51 | if (preparedModel == nullptr) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 52 | return NN_ERROR() << "V1_2::utils::PreparedModel::create must have non-null preparedModel"; |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel)); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 56 | return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously, |
| 57 | std::move(preparedModel), std::move(deathHandler)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 60 | PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously, |
| 61 | sp<V1_2::IPreparedModel> preparedModel, |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 62 | hal::utils::DeathHandler deathHandler) |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 63 | : kExecuteSynchronously(executeSynchronously), |
| 64 | kPreparedModel(std::move(preparedModel)), |
| 65 | kDeathHandler(std::move(deathHandler)) {} |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 66 | |
| 67 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 68 | PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 69 | auto cb = hal::utils::CallbackValue(executionCallback); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 70 | |
| 71 | const auto ret = kPreparedModel->executeSynchronously(request, measure, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 72 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 73 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 74 | return cb.take(); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 78 | PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const { |
| 79 | const auto cb = sp<ExecutionCallback>::make(); |
| 80 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 81 | |
| 82 | const auto ret = kPreparedModel->execute_1_2(request, measure, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 83 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 84 | if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) { |
| 85 | HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return cb->get(); |
| 89 | } |
| 90 | |
| 91 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute( |
| 92 | const nn::Request& request, nn::MeasureTiming measure, |
| 93 | const nn::OptionalTimePoint& /*deadline*/, |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 94 | const nn::OptionalDuration& /*loopTimeoutDuration*/) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 95 | // Ensure that request is ready for IPC. |
| 96 | std::optional<nn::Request> maybeRequestInShared; |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 97 | hal::utils::RequestRelocation relocation; |
| 98 | const nn::Request& requestInShared = |
| 99 | NN_TRY(hal::utils::makeExecutionFailure(hal::utils::convertRequestFromPointerToShared( |
Xusong Wang | e3d0dad | 2021-05-07 14:13:22 -0700 | [diff] [blame^] | 100 | &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding, |
| 101 | &maybeRequestInShared, &relocation))); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 102 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 103 | const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared))); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 104 | const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure))); |
| 105 | |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 106 | return executeInternal(hidlRequest, hidlMeasure, relocation); |
| 107 | } |
| 108 | |
| 109 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 110 | PreparedModel::executeInternal(const V1_0::Request& request, MeasureTiming measure, |
| 111 | const hal::utils::RequestRelocation& relocation) const { |
| 112 | if (relocation.input) { |
| 113 | relocation.input->flush(); |
| 114 | } |
| 115 | |
| 116 | auto result = kExecuteSynchronously ? executeSynchronously(request, measure) |
| 117 | : executeAsynchronously(request, measure); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 118 | auto [outputShapes, timing] = NN_TRY(std::move(result)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 119 | |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 120 | if (relocation.output) { |
| 121 | relocation.output->flush(); |
| 122 | } |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 123 | return std::make_pair(std::move(outputShapes), timing); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 127 | PreparedModel::executeFenced(const nn::Request& /*request*/, |
| 128 | const std::vector<nn::SyncFence>& /*waitFor*/, |
| 129 | nn::MeasureTiming /*measure*/, |
| 130 | const nn::OptionalTimePoint& /*deadline*/, |
| 131 | const nn::OptionalDuration& /*loopTimeoutDuration*/, |
| 132 | const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 133 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 134 | << "IPreparedModel::executeFenced is not supported on 1.2 HAL service"; |
| 135 | } |
| 136 | |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 137 | nn::GeneralResult<nn::SharedExecution> PreparedModel::createReusableExecution( |
| 138 | const nn::Request& request, nn::MeasureTiming measure, |
| 139 | const nn::OptionalDuration& /*loopTimeoutDuration*/) const { |
| 140 | // Ensure that request is ready for IPC. |
| 141 | std::optional<nn::Request> maybeRequestInShared; |
| 142 | hal::utils::RequestRelocation relocation; |
| 143 | const nn::Request& requestInShared = NN_TRY(hal::utils::convertRequestFromPointerToShared( |
Xusong Wang | e3d0dad | 2021-05-07 14:13:22 -0700 | [diff] [blame^] | 144 | &request, nn::kDefaultRequestMemoryAlignment, nn::kMinMemoryPadding, |
| 145 | &maybeRequestInShared, &relocation)); |
Xusong Wang | 5f6bedb | 2021-03-03 16:20:37 -0800 | [diff] [blame] | 146 | |
| 147 | auto hidlRequest = NN_TRY(convert(requestInShared)); |
| 148 | auto hidlMeasure = NN_TRY(convert(measure)); |
| 149 | return Execution::create(shared_from_this(), std::move(hidlRequest), std::move(relocation), |
| 150 | hidlMeasure); |
| 151 | } |
| 152 | |
Michael Butler | 2a8b679 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 153 | nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const { |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 154 | auto self = shared_from_this(); |
Michael Butler | 8414a6e | 2021-03-10 18:41:05 -0800 | [diff] [blame] | 155 | auto fallback = [preparedModel = std::move(self)]( |
| 156 | const nn::Request& request, nn::MeasureTiming measure, |
| 157 | const nn::OptionalTimePoint& deadline, |
| 158 | const nn::OptionalDuration& loopTimeoutDuration) |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 159 | -> nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> { |
Michael Butler | 8414a6e | 2021-03-10 18:41:05 -0800 | [diff] [blame] | 160 | return preparedModel->execute(request, measure, deadline, loopTimeoutDuration); |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 161 | }; |
| 162 | const auto pollingTimeWindow = getBurstControllerPollingTimeWindow(); |
Xusong Wang | b2e8085 | 2021-03-23 15:07:10 -0700 | [diff] [blame] | 163 | return ExecutionBurstController::create(shared_from_this(), kPreparedModel, pollingTimeWindow); |
Michael Butler | 2a8b679 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 166 | std::any PreparedModel::getUnderlyingResource() const { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 167 | sp<V1_2::IPreparedModel> resource = kPreparedModel; |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 168 | return resource; |
| 169 | } |
| 170 | |
| 171 | } // namespace android::hardware::neuralnetworks::V1_2::utils |