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