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