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/types.h> |
| 24 | #include <android/hardware/neuralnetworks/1.1/types.h> |
| 25 | #include <android/hardware/neuralnetworks/1.2/types.h> |
| 26 | #include <android/hardware/neuralnetworks/1.3/IPreparedModel.h> |
| 27 | #include <android/hardware/neuralnetworks/1.3/types.h> |
| 28 | #include <nnapi/IPreparedModel.h> |
| 29 | #include <nnapi/Result.h> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 30 | #include <nnapi/TypeUtils.h> |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 31 | #include <nnapi/Types.h> |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 32 | #include <nnapi/hal/1.0/Burst.h> |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 33 | #include <nnapi/hal/1.2/Conversions.h> |
| 34 | #include <nnapi/hal/CommonUtils.h> |
| 35 | #include <nnapi/hal/HandleError.h> |
| 36 | #include <nnapi/hal/ProtectCallback.h> |
| 37 | |
| 38 | #include <memory> |
| 39 | #include <tuple> |
| 40 | #include <utility> |
| 41 | #include <vector> |
| 42 | |
Michael Butler | aad934b | 2020-12-13 23:06:06 -0800 | [diff] [blame] | 43 | // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface |
| 44 | // lifetimes across processes and for protecting asynchronous calls across HIDL. |
| 45 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 46 | namespace android::hardware::neuralnetworks::V1_3::utils { |
| 47 | namespace { |
| 48 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 49 | nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> convertFencedExecutionCallbackResults( |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 50 | ErrorStatus status, const V1_2::Timing& timingLaunched, const V1_2::Timing& timingFenced) { |
| 51 | HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status); |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 52 | return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced))); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 55 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> fencedExecutionCallback( |
| 56 | ErrorStatus status, const hidl_handle& syncFence, |
| 57 | const sp<IFencedExecutionCallback>& callback) { |
| 58 | HANDLE_HAL_STATUS(status) << "fenced execution failed with " << toString(status); |
| 59 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 60 | auto resultSyncFence = nn::SyncFence::createAsSignaled(); |
| 61 | if (syncFence.getNativeHandle() != nullptr) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 62 | auto sharedHandle = NN_TRY(nn::convert(syncFence)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 63 | resultSyncFence = NN_TRY(hal::utils::makeGeneralFailure( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 64 | nn::SyncFence::create(std::move(sharedHandle)), nn::ErrorStatus::GENERAL_FAILURE)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (callback == nullptr) { |
| 68 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "callback is null"; |
| 69 | } |
| 70 | |
| 71 | // Create callback which can be used to retrieve the execution error status and timings. |
| 72 | nn::ExecuteFencedInfoCallback resultCallback = |
| 73 | [callback]() -> nn::GeneralResult<std::pair<nn::Timing, nn::Timing>> { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 74 | auto cb = hal::utils::CallbackValue(convertFencedExecutionCallbackResults); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 75 | |
| 76 | const auto ret = callback->getExecutionInfo(cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 77 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 78 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 79 | return cb.take(); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | return std::make_pair(std::move(resultSyncFence), std::move(resultCallback)); |
| 83 | } |
| 84 | |
| 85 | } // namespace |
| 86 | |
| 87 | nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create( |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 88 | sp<V1_3::IPreparedModel> preparedModel, bool executeSynchronously) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 89 | if (preparedModel == nullptr) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 90 | return NN_ERROR() << "V1_3::utils::PreparedModel::create must have non-null preparedModel"; |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel)); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 94 | return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously, |
| 95 | std::move(preparedModel), std::move(deathHandler)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 98 | PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously, |
| 99 | sp<V1_3::IPreparedModel> preparedModel, |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 100 | hal::utils::DeathHandler deathHandler) |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 101 | : kExecuteSynchronously(executeSynchronously), |
| 102 | kPreparedModel(std::move(preparedModel)), |
| 103 | kDeathHandler(std::move(deathHandler)) {} |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 104 | |
| 105 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 106 | PreparedModel::executeSynchronously(const Request& request, V1_2::MeasureTiming measure, |
| 107 | const OptionalTimePoint& deadline, |
| 108 | const OptionalTimeoutDuration& loopTimeoutDuration) const { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 109 | auto cb = hal::utils::CallbackValue(executionCallback); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 110 | |
| 111 | const auto ret = kPreparedModel->executeSynchronously_1_3(request, measure, deadline, |
| 112 | loopTimeoutDuration, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 113 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 114 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 115 | return cb.take(); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 119 | PreparedModel::executeAsynchronously(const Request& request, V1_2::MeasureTiming measure, |
| 120 | const OptionalTimePoint& deadline, |
| 121 | const OptionalTimeoutDuration& loopTimeoutDuration) const { |
| 122 | const auto cb = sp<ExecutionCallback>::make(); |
| 123 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 124 | |
| 125 | const auto ret = |
| 126 | kPreparedModel->execute_1_3(request, measure, deadline, loopTimeoutDuration, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 127 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 128 | if (status != ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) { |
| 129 | HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | return cb->get(); |
| 133 | } |
| 134 | |
| 135 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute( |
| 136 | const nn::Request& request, nn::MeasureTiming measure, |
| 137 | const nn::OptionalTimePoint& deadline, |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 138 | const nn::OptionalDuration& loopTimeoutDuration) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 139 | // Ensure that request is ready for IPC. |
| 140 | std::optional<nn::Request> maybeRequestInShared; |
| 141 | const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure( |
| 142 | hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared))); |
| 143 | |
| 144 | const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared))); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 145 | const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure))); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 146 | const auto hidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline))); |
| 147 | const auto hidlLoopTimeoutDuration = |
| 148 | NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration))); |
| 149 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 150 | auto result = kExecuteSynchronously |
| 151 | ? executeSynchronously(hidlRequest, hidlMeasure, hidlDeadline, |
| 152 | hidlLoopTimeoutDuration) |
| 153 | : executeAsynchronously(hidlRequest, hidlMeasure, hidlDeadline, |
| 154 | hidlLoopTimeoutDuration); |
| 155 | auto [outputShapes, timing] = NN_TRY(std::move(result)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 156 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 157 | NN_TRY(hal::utils::makeExecutionFailure( |
| 158 | hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared))); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 159 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 160 | return std::make_pair(std::move(outputShapes), timing); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
| 164 | PreparedModel::executeFenced(const nn::Request& request, const std::vector<nn::SyncFence>& waitFor, |
| 165 | nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline, |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 166 | const nn::OptionalDuration& loopTimeoutDuration, |
| 167 | const nn::OptionalDuration& timeoutDurationAfterFence) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 168 | // Ensure that request is ready for IPC. |
| 169 | std::optional<nn::Request> maybeRequestInShared; |
| 170 | const nn::Request& requestInShared = |
| 171 | NN_TRY(hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)); |
| 172 | |
| 173 | const auto hidlRequest = NN_TRY(convert(requestInShared)); |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 174 | const auto hidlWaitFor = NN_TRY(hal::utils::convertSyncFences(waitFor)); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 175 | const auto hidlMeasure = NN_TRY(convert(measure)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 176 | const auto hidlDeadline = NN_TRY(convert(deadline)); |
| 177 | const auto hidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration)); |
| 178 | const auto hidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence)); |
| 179 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 180 | auto cb = hal::utils::CallbackValue(fencedExecutionCallback); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 181 | |
| 182 | const auto ret = kPreparedModel->executeFenced(hidlRequest, hidlWaitFor, hidlMeasure, |
| 183 | hidlDeadline, hidlLoopTimeoutDuration, |
| 184 | hidlTimeoutDurationAfterFence, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 185 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 186 | auto [syncFence, callback] = NN_TRY(cb.take()); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 187 | |
| 188 | // If executeFenced required the request memory to be moved into shared memory, block here until |
| 189 | // the fenced execution has completed and flush the memory back. |
| 190 | if (maybeRequestInShared.has_value()) { |
| 191 | const auto state = syncFence.syncWait({}); |
| 192 | if (state != nn::SyncFence::FenceState::SIGNALED) { |
| 193 | return NN_ERROR() << "syncWait failed with " << state; |
| 194 | } |
| 195 | NN_TRY(hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)); |
| 196 | } |
| 197 | |
| 198 | return std::make_pair(std::move(syncFence), std::move(callback)); |
| 199 | } |
| 200 | |
Michael Butler | 2a8b679 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 201 | nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const { |
Michael Butler | 9533151 | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 202 | return V1_0::utils::Burst::create(shared_from_this()); |
Michael Butler | 2a8b679 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 205 | std::any PreparedModel::getUnderlyingResource() const { |
| 206 | sp<V1_3::IPreparedModel> resource = kPreparedModel; |
| 207 | return resource; |
| 208 | } |
| 209 | |
| 210 | } // namespace android::hardware::neuralnetworks::V1_3::utils |