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> |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 30 | #include <nnapi/hal/1.0/Burst.h> |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 31 | #include <nnapi/hal/1.0/Conversions.h> |
| 32 | #include <nnapi/hal/CommonUtils.h> |
| 33 | #include <nnapi/hal/HandleError.h> |
| 34 | #include <nnapi/hal/ProtectCallback.h> |
| 35 | |
| 36 | #include <memory> |
| 37 | #include <tuple> |
| 38 | #include <utility> |
| 39 | #include <vector> |
| 40 | |
Michael Butler | 7a655bb | 2020-12-13 23:06:06 -0800 | [diff] [blame] | 41 | // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface |
| 42 | // lifetimes across processes and for protecting asynchronous calls across HIDL. |
| 43 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 44 | namespace android::hardware::neuralnetworks::V1_2::utils { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 45 | |
| 46 | nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create( |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 47 | sp<V1_2::IPreparedModel> preparedModel, bool executeSynchronously) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 48 | if (preparedModel == nullptr) { |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 49 | return NN_ERROR() << "V1_2::utils::PreparedModel::create must have non-null preparedModel"; |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel)); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 53 | return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, executeSynchronously, |
| 54 | std::move(preparedModel), std::move(deathHandler)); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 57 | PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, bool executeSynchronously, |
| 58 | sp<V1_2::IPreparedModel> preparedModel, |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 59 | hal::utils::DeathHandler deathHandler) |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 60 | : kExecuteSynchronously(executeSynchronously), |
| 61 | kPreparedModel(std::move(preparedModel)), |
| 62 | kDeathHandler(std::move(deathHandler)) {} |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 63 | |
| 64 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 65 | PreparedModel::executeSynchronously(const V1_0::Request& request, MeasureTiming measure) const { |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 66 | auto cb = hal::utils::CallbackValue(executionCallback); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 67 | |
| 68 | const auto ret = kPreparedModel->executeSynchronously(request, measure, cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 69 | HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 70 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 71 | return cb.take(); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> |
| 75 | PreparedModel::executeAsynchronously(const V1_0::Request& request, MeasureTiming measure) const { |
| 76 | const auto cb = sp<ExecutionCallback>::make(); |
| 77 | const auto scoped = kDeathHandler.protectCallback(cb.get()); |
| 78 | |
| 79 | const auto ret = kPreparedModel->execute_1_2(request, measure, cb); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 80 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 81 | if (status != V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) { |
| 82 | HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return cb->get(); |
| 86 | } |
| 87 | |
| 88 | nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute( |
| 89 | const nn::Request& request, nn::MeasureTiming measure, |
| 90 | const nn::OptionalTimePoint& /*deadline*/, |
Michael Butler | ca11420 | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 91 | const nn::OptionalDuration& /*loopTimeoutDuration*/) const { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 92 | // Ensure that request is ready for IPC. |
| 93 | std::optional<nn::Request> maybeRequestInShared; |
| 94 | const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure( |
| 95 | hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared))); |
| 96 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 97 | const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared))); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 98 | const auto hidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure))); |
| 99 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 100 | auto result = kExecuteSynchronously ? executeSynchronously(hidlRequest, hidlMeasure) |
| 101 | : executeAsynchronously(hidlRequest, hidlMeasure); |
| 102 | auto [outputShapes, timing] = NN_TRY(std::move(result)); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 103 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 104 | NN_TRY(hal::utils::makeExecutionFailure( |
| 105 | hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared))); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 106 | |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 107 | return std::make_pair(std::move(outputShapes), timing); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> |
Michael Butler | ca11420 | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 111 | PreparedModel::executeFenced(const nn::Request& /*request*/, |
| 112 | const std::vector<nn::SyncFence>& /*waitFor*/, |
| 113 | nn::MeasureTiming /*measure*/, |
| 114 | const nn::OptionalTimePoint& /*deadline*/, |
| 115 | const nn::OptionalDuration& /*loopTimeoutDuration*/, |
| 116 | const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 117 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 118 | << "IPreparedModel::executeFenced is not supported on 1.2 HAL service"; |
| 119 | } |
| 120 | |
Michael Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 121 | nn::GeneralResult<nn::SharedBurst> PreparedModel::configureExecutionBurst() const { |
Michael Butler | 44f324f | 2020-12-18 20:53:55 -0800 | [diff] [blame] | 122 | return V1_0::utils::Burst::create(shared_from_this()); |
Michael Butler | b6a7ed5 | 2020-12-18 20:31:14 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 125 | std::any PreparedModel::getUnderlyingResource() const { |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 126 | sp<V1_2::IPreparedModel> resource = kPreparedModel; |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 127 | return resource; |
| 128 | } |
| 129 | |
| 130 | } // namespace android::hardware::neuralnetworks::V1_2::utils |