Michael Butler | 3670c38 | 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/IPreparedModel.h> |
| 26 | #include <android/hardware/neuralnetworks/1.2/types.h> |
| 27 | #include <nnapi/IPreparedModel.h> |
| 28 | #include <nnapi/Result.h> |
| 29 | #include <nnapi/Types.h> |
| 30 | #include <nnapi/hal/1.0/Conversions.h> |
| 31 | #include <nnapi/hal/CommonUtils.h> |
| 32 | #include <nnapi/hal/HandleError.h> |
| 33 | #include <nnapi/hal/ProtectCallback.h> |
| 34 | |
| 35 | #include <memory> |
| 36 | #include <tuple> |
| 37 | #include <utility> |
| 38 | #include <vector> |
| 39 | |
| 40 | namespace android::hardware::neuralnetworks::V1_2::utils { |
| 41 | namespace { |
| 42 | |
| 43 | nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 44 | convertExecutionResultsHelper(const hidl_vec<OutputShape>& outputShapes, const Timing& timing) { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 45 | return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing))); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> convertExecutionResults( |
| 49 | const hidl_vec<OutputShape>& outputShapes, const Timing& timing) { |
| 50 | return hal::utils::makeExecutionFailure(convertExecutionResultsHelper(outputShapes, timing)); |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create( |
| 56 | sp<V1_2::IPreparedModel> preparedModel) { |
| 57 | if (preparedModel == nullptr) { |
| 58 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 59 | << "V1_2::utils::PreparedModel::create must have non-null preparedModel"; |
| 60 | } |
| 61 | |
| 62 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel)); |
| 63 | return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel), |
| 64 | std::move(deathHandler)); |
| 65 | } |
| 66 | |
| 67 | PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, sp<V1_2::IPreparedModel> preparedModel, |
| 68 | hal::utils::DeathHandler deathHandler) |
| 69 | : kPreparedModel(std::move(preparedModel)), kDeathHandler(std::move(deathHandler)) {} |
| 70 | |
| 71 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 72 | PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const { |
| 73 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> result = |
| 74 | NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "uninitialized"; |
| 75 | const auto cb = [&result](V1_0::ErrorStatus status, const hidl_vec<OutputShape>& outputShapes, |
| 76 | const Timing& timing) { |
| 77 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 78 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 79 | result = NN_ERROR(canonical) << "executeSynchronously failed with " << toString(status); |
| 80 | } else { |
| 81 | result = convertExecutionResults(outputShapes, timing); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | const auto ret = kPreparedModel->executeSynchronously(request, measure, cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame^] | 86 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 87 | |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 92 | PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const { |
| 93 | const auto cb = sp<ExecutionCallback>::make(); |
| 94 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 95 | |
| 96 | const auto ret = kPreparedModel->execute_1_2(request, measure, cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame^] | 97 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 98 | if (status != V1_0::ErrorStatus::NONE) { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 99 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 100 | return NN_ERROR(canonical) << "execute failed with " << toString(status); |
| 101 | } |
| 102 | |
| 103 | return cb->get(); |
| 104 | } |
| 105 | |
| 106 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute( |
| 107 | const nn::Request& request, nn::MeasureTiming measure, |
| 108 | const nn::OptionalTimePoint& /*deadline*/, |
| 109 | const nn::OptionalTimeoutDuration& /*loopTimeoutDuration*/) const { |
| 110 | // Ensure that request is ready for IPC. |
| 111 | std::optional<nn::Request> maybeRequestInShared; |
| 112 | const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure( |
| 113 | hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared))); |
| 114 | |
| 115 | const auto hidlRequest = |
| 116 | NN_TRY(hal::utils::makeExecutionFailure(V1_0::utils::convert(requestInShared))); |
| 117 | const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure))); |
| 118 | |
| 119 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> result = |
| 120 | NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "uninitialized"; |
| 121 | const bool preferSynchronous = true; |
| 122 | |
| 123 | // Execute synchronously if allowed. |
| 124 | if (preferSynchronous) { |
| 125 | result = executeSynchronously(hidlRequest, hidlMeasure); |
| 126 | } |
| 127 | |
| 128 | // Run asymchronous execution if execution has not already completed. |
| 129 | if (!result.has_value()) { |
| 130 | result = executeAsynchronously(hidlRequest, hidlMeasure); |
| 131 | } |
| 132 | |
| 133 | // Flush output buffers if suxcessful execution. |
| 134 | if (result.has_value()) { |
| 135 | NN_TRY(hal::utils::makeExecutionFailure( |
| 136 | hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared))); |
| 137 | } |
| 138 | |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
| 143 | PreparedModel::executeFenced( |
| 144 | const nn::Request& /*request*/, const std::vector<nn::SyncFence>& /*waitFor*/, |
| 145 | nn::MeasureTiming /*measure*/, const nn::OptionalTimePoint& /*deadline*/, |
| 146 | const nn::OptionalTimeoutDuration& /*loopTimeoutDuration*/, |
| 147 | const nn::OptionalTimeoutDuration& /*timeoutDurationAfterFence*/) const { |
| 148 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 149 | << "IPreparedModel::executeFenced is not supported on 1.2 HAL service"; |
| 150 | } |
| 151 | |
| 152 | std::any PreparedModel::getUnderlyingResource() const { |
| 153 | sp<V1_0::IPreparedModel> resource = kPreparedModel; |
| 154 | return resource; |
| 155 | } |
| 156 | |
| 157 | } // namespace android::hardware::neuralnetworks::V1_2::utils |