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" |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame^] | 21 | #include "ExecutionBurstController.h" |
| 22 | #include "ExecutionBurstUtils.h" |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 23 | #include "Utils.h" |
| 24 | |
| 25 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 26 | #include <android/hardware/neuralnetworks/1.1/types.h> |
| 27 | #include <android/hardware/neuralnetworks/1.2/IPreparedModel.h> |
| 28 | #include <android/hardware/neuralnetworks/1.2/types.h> |
| 29 | #include <nnapi/IPreparedModel.h> |
| 30 | #include <nnapi/Result.h> |
| 31 | #include <nnapi/Types.h> |
| 32 | #include <nnapi/hal/1.0/Conversions.h> |
| 33 | #include <nnapi/hal/CommonUtils.h> |
| 34 | #include <nnapi/hal/HandleError.h> |
| 35 | #include <nnapi/hal/ProtectCallback.h> |
| 36 | |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame^] | 37 | #include <chrono> |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 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_2::utils { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 47 | |
| 48 | nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create( |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 49 | sp<V1_2::IPreparedModel> preparedModel, bool executeSynchronously) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 50 | if (preparedModel == nullptr) { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 51 | 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] | 52 | } |
| 53 | |
| 54 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel)); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 55 | return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously, |
| 56 | std::move(preparedModel), std::move(deathHandler)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 59 | PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously, |
| 60 | sp<V1_2::IPreparedModel> preparedModel, |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 61 | hal::utils::DeathHandler deathHandler) |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 62 | : kExecuteSynchronously(executeSynchronously), |
| 63 | kPreparedModel(std::move(preparedModel)), |
| 64 | kDeathHandler(std::move(deathHandler)) {} |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 65 | |
| 66 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 67 | PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 68 | auto cb = hal::utils::CallbackValue(executionCallback); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 69 | |
| 70 | const auto ret = kPreparedModel->executeSynchronously(request, measure, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 71 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 72 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 73 | return cb.take(); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 77 | PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const { |
| 78 | const auto cb = sp<ExecutionCallback>::make(); |
| 79 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 80 | |
| 81 | const auto ret = kPreparedModel->execute_1_2(request, measure, cb); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 82 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 83 | if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) { |
| 84 | HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return cb->get(); |
| 88 | } |
| 89 | |
| 90 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute( |
| 91 | const nn::Request& request, nn::MeasureTiming measure, |
| 92 | const nn::OptionalTimePoint& /*deadline*/, |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 93 | const nn::OptionalDuration& /*loopTimeoutDuration*/) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 94 | // Ensure that request is ready for IPC. |
| 95 | std::optional<nn::Request> maybeRequestInShared; |
| 96 | const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure( |
| 97 | hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared))); |
| 98 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 99 | const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared))); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 100 | const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure))); |
| 101 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 102 | auto result = kExecuteSynchronously ? executeSynchronously(hidlRequest, hidlMeasure) |
| 103 | : executeAsynchronously(hidlRequest, hidlMeasure); |
| 104 | auto [outputShapes, timing] = NN_TRY(std::move(result)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 105 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 106 | NN_TRY(hal::utils::makeExecutionFailure( |
| 107 | hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared))); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 108 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 109 | return std::make_pair(std::move(outputShapes), timing); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 113 | PreparedModel::executeFenced(const nn::Request& /*request*/, |
| 114 | const std::vector<nn::SyncFence>& /*waitFor*/, |
| 115 | nn::MeasureTiming /*measure*/, |
| 116 | const nn::OptionalTimePoint& /*deadline*/, |
| 117 | const nn::OptionalDuration& /*loopTimeoutDuration*/, |
| 118 | const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 119 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 120 | << "IPreparedModel::executeFenced is not supported on 1.2 HAL service"; |
| 121 | } |
| 122 | |
Michael Butler | 2a8b679 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 123 | nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const { |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame^] | 124 | auto self = shared_from_this(); |
| 125 | auto fallback = [preparedModel = std::move(self)](const nn::Request& request, |
| 126 | nn::MeasureTiming measure) |
| 127 | -> nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> { |
| 128 | return preparedModel->execute(request, measure, {}, {}); |
| 129 | }; |
| 130 | const auto pollingTimeWindow = getBurstControllerPollingTimeWindow(); |
| 131 | return ExecutionBurstController::create(kPreparedModel, std::move(fallback), pollingTimeWindow); |
Michael Butler | 2a8b679 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 134 | std::any PreparedModel::getUnderlyingResource() const { |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 135 | sp<V1_2::IPreparedModel> resource = kPreparedModel; |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 136 | return resource; |
| 137 | } |
| 138 | |
| 139 | } // namespace android::hardware::neuralnetworks::V1_2::utils |